Skip to content

Commit

Permalink
fix random address import to not mark every imported address as 'Used'
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Aug 13, 2020
1 parent 61bbd7b commit eed460c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module Cardano.Wallet
, normalizeDelegationAddress
, ErrCreateRandomAddress(..)
, ErrImportRandomAddress(..)
, ErrImportAddress(..)

-- ** Payment
, selectCoinsExternal
Expand Down Expand Up @@ -236,7 +237,7 @@ import Cardano.Wallet.Primitive.AddressDiscovery
, KnownAddresses (..)
)
import Cardano.Wallet.Primitive.AddressDiscovery.Random
( RndState )
( ErrImportAddress (..), RndState )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( SeqState
, defaultAddressPoolGap
Expand Down Expand Up @@ -1143,12 +1144,13 @@ importRandomAddresses
importRandomAddresses ctx wid addrs = db & \DBLayer{..} -> mapExceptT atomically $ do
cp <- withExceptT ErrImportAddrNoSuchWallet
$ withNoSuchWallet wid (readCheckpoint (PrimaryKey wid))
let s = getState cp
ours = scanl' (\(_, t) addr -> isOurs addr t) (True, s) addrs
s' = snd (last ours)
if (not . any fst) ours
then throwE ErrImportAddrDoesNotBelong
else withExceptT ErrImportAddrNoSuchWallet $
let s0 = getState cp
ours = scanl' (\s addr -> s >>= Rnd.importAddress addr) (Right s0) addrs
case last ours of
Left err ->
throwE $ ErrImportAddr err
Right s' ->
withExceptT ErrImportAddrNoSuchWallet $
putCheckpoint (PrimaryKey wid) (updateState s' cp)
where
db = ctx ^. dbLayer @s @k
Expand Down Expand Up @@ -2287,7 +2289,7 @@ data ErrCreateRandomAddress

data ErrImportRandomAddress
= ErrImportAddrNoSuchWallet ErrNoSuchWallet
| ErrImportAddrDoesNotBelong
| ErrImportAddr ErrImportAddress
| ErrImportAddressNotAByronWallet
deriving (Generic, Eq, Show)

Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ import Cardano.Wallet
, ErrDecodeSignedTx (..)
, ErrFetchRewards (..)
, ErrGetTransaction (..)
, ErrImportAddress (..)
, ErrImportRandomAddress (..)
, ErrJoinStakePool (..)
, ErrListTransactions (..)
Expand Down Expand Up @@ -2445,7 +2446,7 @@ instance LiftHandler ErrImportRandomAddress where
[ "I cannot derive new address for this wallet type."
, " Make sure to use Byron random wallet id."
]
ErrImportAddrDoesNotBelong ->
ErrImportAddr ErrAddrDoesNotBelong{} ->
apiError err403 KeyNotFoundForAddress $ mconcat
[ "I couldn't identify this address as one of mine. It likely "
, "belongs to another wallet and I will therefore not import it."
Expand Down
21 changes: 21 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/AddressDiscovery/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module Cardano.Wallet.Primitive.AddressDiscovery.Random
, DerivationPath

-- ** Low-level API
, importAddress
, ErrImportAddress(..)
, addDiscoveredAddress
, deriveRndStateAddress
, findUnusedPath
Expand Down Expand Up @@ -156,6 +158,25 @@ mkRndState key seed = RndState
, gen = mkStdGen seed
}

newtype ErrImportAddress
= ErrAddrDoesNotBelong Address
deriving (Generic, Eq, Show)

-- | Import an address into the state. This fails if the address does not belong
-- to the wallet. Import an address that is already known is a no-op.
importAddress
:: Address
-> RndState n
-> Either ErrImportAddress (RndState n)
importAddress addr s = do
case addressToPath addr (hdPassphrase s) of
Nothing ->
Left (ErrAddrDoesNotBelong addr)
Just path | Map.member path (addresses s) ->
Right s
Just path ->
Right (addDiscoveredAddress addr Unused path s)

-- | Updates a 'RndState' by adding an address and its derivation path to the
-- set of discovered addresses. If the address was in the 'pendingAddresses' set
-- (i.e. it was a newly generated change address), then it is removed from
Expand Down

0 comments on commit eed460c

Please sign in to comment.