Skip to content

Commit

Permalink
use normaliseDelegationAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Oct 28, 2021
1 parent 03ce121 commit e8ee5bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
let txCbor = getFromResponse #transaction (HTTP.status202, Right $ ApiSerialisedTransaction signedTx)
let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor)
let withdrawalWith ownership wdrls = case wdrls of
wdrl:[] ->
[wdrl] ->
wdrl ^. #amount == Quantity withdrawalAmt &&
wdrl ^. #context == ownership
_ -> False
Expand Down Expand Up @@ -480,7 +480,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
let expectedTxOutTarget = WalletOutput $ ApiWalletOutput
{ address = addrDest
, amount = Quantity 0
, assets = ApiT $ TokenMap.empty
, assets = ApiT TokenMap.empty
, derivationPath = NE.fromList
[ ApiT (DerivationIndex 2147485500)
, ApiT (DerivationIndex 2147485463)
Expand All @@ -489,7 +489,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
, ApiT (DerivationIndex $ fromIntegral addrIx)
]
, amountIncoming = Quantity amt
, assetsIncoming = ApiT $ TokenMap.empty
, assetsIncoming = ApiT TokenMap.empty
}
let isOurTxOut :: ApiTxOutputGeneral n -> [ApiTxOutputGeneral n] -> Bool
isOurTxOut expectedTxOut = (expectedTxOut `elem`)
Expand Down Expand Up @@ -548,7 +548,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
let expectedTxOutTarget' = WalletOutput $ ApiWalletOutput
{ address = addrDest
, amount = Quantity amt -- now we have this
, assets = ApiT $ TokenMap.empty
, assets = ApiT TokenMap.empty
, derivationPath = NE.fromList
[ ApiT (DerivationIndex 2147485500)
, ApiT (DerivationIndex 2147485463)
Expand All @@ -557,7 +557,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
, ApiT (DerivationIndex $ fromIntegral addrIx)
]
, amountIncoming = Quantity amt
, assetsIncoming = ApiT $ TokenMap.empty
, assetsIncoming = ApiT TokenMap.empty
}
addrsSourceAll <- listAddresses @n ctx wa
--we expect change address here with x=0 as this wallet does not participated in outcoming tx before this one
Expand All @@ -574,10 +574,10 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
let expectedTxOutSource = WalletOutput $ ApiWalletOutput
{ address = addrSrc
, amount = Quantity $ initialAmt - (amt + fromIntegral expectedFee)
, assets = ApiT $ TokenMap.empty
, assets = ApiT TokenMap.empty
, derivationPath = derPath
, amountIncoming = Quantity $ initialAmt - (amt + fromIntegral expectedFee)
, assetsIncoming = ApiT $ TokenMap.empty
, assetsIncoming = ApiT TokenMap.empty
}
let txCbor' = getFromResponse #transaction (HTTP.status202, Right $ ApiSerialisedTransaction signedTx)
let decodePayload' = Json (toJSON $ ApiSerialisedTransaction txCbor')
Expand Down
41 changes: 17 additions & 24 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,25 +1159,27 @@ lookupTxIns
( HasDBLayer IO s k ctx
, KnownAddresses s
, IsOurs s Address
, WalletKey k
, DelegationAddress n k
, s ~ SeqState n k
)
=> ctx
-> WalletId
-> [TxIn]
-> XPub
-> ExceptT ErrDecodeTx IO [(TxIn, Maybe (TxOut, NonEmpty DerivationIndex))]
lookupTxIns ctx wid txins xpub = db & \DBLayer{..} -> do
lookupTxIns ctx wid txins = db & \DBLayer{..} -> do
cp <- mapExceptT atomically
$ withExceptT ErrDecodeTxNoSuchWallet
$ withNoSuchWallet wid
$ readCheckpoint wid
-- NOTE it is working properly for base address.
let st = getState cp
let extendAddr addr = case normalizeDelegationAddress @s @k @n st addr of
Just addr' -> addr'
Nothing -> error "knownAddresses should have valid fingerprints"
let f (addr, addrState, ix) =
( extAddr @k @n addr xpub
( extendAddr addr
, addrState
, ix )
let walletAddrs = map f $ knownAddresses (getState cp)
let walletAddrs = map f $ knownAddresses st
pure $ map (tryGetTxOutPath cp walletAddrs) txins
where
db = ctx ^. dbLayer @IO @s @k
Expand All @@ -1188,31 +1190,20 @@ lookupTxIns ctx wid txins xpub = db & \DBLayer{..} -> do
let path = map (\(_, _,p) -> p) $
filter (\(addr', _,_) -> addr' == addr) walletAddrs
in case path of
[] -> (txin, Nothing) -- will probably use error here
[] -> (txin, Nothing)
path':_ -> (txin, Just (txout, path'))

extAddr
:: forall k (n :: NetworkDiscriminant).
( WalletKey k
, DelegationAddress n k
)
=> Address
-> XPub
-> Address
extAddr addr xpub =
liftDelegationAddress @n @k ((\(Right finger) -> finger) $
paymentKeyFingerprint @k addr) (liftRawKey @k xpub)

lookupTxOuts
:: forall ctx s k (n :: NetworkDiscriminant).
( HasDBLayer IO s k ctx
, KnownAddresses s
, IsOurs s Address
, WalletKey k
, DelegationAddress n k
, GetPurpose k
, WalletKey k
, GetAccount s k
, SoftDerivation k
, s ~ SeqState n k
)
=> ctx
-> WalletId
Expand All @@ -1225,7 +1216,7 @@ lookupTxOuts ctx wid txouts xpub = db & \DBLayer{..} -> do
$ withNoSuchWallet wid
$ readCheckpoint wid
let f (addr, addrState, ix) =
( extAddr @k @n addr xpub
( extendAddr cp addr
, addrState
, ix )
let walletAddrs = map f $ knownAddresses (getState cp)
Expand All @@ -1241,15 +1232,17 @@ lookupTxOuts ctx wid txouts xpub = db & \DBLayer{..} -> do
pure $ map (tryGetTxOutPath cp walletAddrs') txouts
where
db = ctx ^. dbLayer @IO @s @k
extendAddr cp addr = case normalizeDelegationAddress @s @k @n (getState cp) addr of
Just addr' -> addr'
Nothing -> error "knownAddresses should have valid fingerprints"
tryGetTxOutPath cp walletAddrs txout@(TxOut addr _) =
let allTxOuts = Map.elems $ unUTxO $ totalUTxO mempty cp
in case map (\(_, _,p) -> p) (filter (\(addr', _,_) -> addr' == addr) walletAddrs) of
[] -> (txout, Nothing)
path:_ ->
let bundles =
foldr (<>) TokenBundle.empty $
map tokens $
filter (\(TxOut addr' _) -> addr == extAddr @k @n addr' xpub) allTxOuts
foldr ((<>) . tokens) TokenBundle.empty
(filter (\(TxOut addr' _) -> addr == extendAddr cp addr') allTxOuts)
in (txout, Just (TxOut addr bundles, path))
getChainType (_ :| [_,_,DerivationIndex i,_]) = i
getChainType _ = error "expect 5 segment derivation path"
Expand Down
7 changes: 4 additions & 3 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,7 @@ decodeTransaction
, SoftDerivation k
, GetAccount s k
, GetPurpose k
, s ~ SeqState n k
)
=> ctx
-> ApiT WalletId
Expand All @@ -2447,13 +2448,13 @@ decodeTransaction ctx (ApiT wid) (ApiSerialisedTransaction (ApiT sealed)) = do
(txinsOutsPaths, collsOutsPaths, outsPath, acct) <-
withWorkerCtx ctx wid liftE liftE $ \wrk -> do
(acct, xpub, _) <- liftHandler $ W.readRewardAccount @_ @s @k @n wrk wid
txinsOutsPaths <- liftHandler $ W.lookupTxIns @_ @s @k @n wrk wid (fst <$> inps) xpub
collsOutsPaths <- liftHandler $ W.lookupTxIns @_ @s @k @n wrk wid (fst <$> colls) xpub
txinsOutsPaths <- liftHandler $ W.lookupTxIns @_ @s @k @n wrk wid (fst <$> inps)
collsOutsPaths <- liftHandler $ W.lookupTxIns @_ @s @k @n wrk wid (fst <$> colls)
outsPath <- liftHandler $ W.lookupTxOuts @_ @s @k @n wrk wid outs xpub
pure (txinsOutsPaths, collsOutsPaths, outsPath, acct)
pure $ ApiDecodedTransaction
{ id = ApiT txid
, fee = fromMaybe (Quantity 0) (Quantity . fromIntegral . unCoin <$> feeM)
, fee = maybe (Quantity 0) (Quantity . fromIntegral . unCoin) feeM
, inputs = map toInp txinsOutsPaths
, outputs = map toOut outsPath
, collateral = map toInp collsOutsPaths
Expand Down

0 comments on commit e8ee5bf

Please sign in to comment.