diff --git a/src/Simplex/Chat/Library/Commands.hs b/src/Simplex/Chat/Library/Commands.hs index cb74015b90..68e65dccc8 100644 --- a/src/Simplex/Chat/Library/Commands.hs +++ b/src/Simplex/Chat/Library/Commands.hs @@ -1510,9 +1510,8 @@ processChatCommand cxt nm = \case (entropy, nextAccount) <- case mnemonic_ of Nothing -> (,Just 1) <$> (liftIO . newEntropy =<< asks random) Just phrase -> (,Nothing) <$> liftWallet (entropyFromMnemonic phrase) - -- derive the first account before storing, so stored entropy derives keys - firstAccount <- liftEitherWith (ChatError . CEInternalError) $ mkAccountIndex $ fromMaybe 0 nextAccount - void $ walletAccount entropy firstAccount + -- derive the first account before storing, so stored entropy is valid for derivation + void $ walletAccount entropy =<< liftDerivation (pure $ mkAccountIndex $ fromMaybe 0 nextAccount) created <- withFastStore' $ \db -> createWallet db entropy nextAccount unless created $ throwWalletError WEMasterExists pure $ CRWallet user (Just $ WalletInfo [] nextAccount) @@ -6720,7 +6719,7 @@ chatCommandP = char_ = optional . A.char accountIndexP = do i <- A.decimal :: Parser Integer - if i < 2147483648 then either fail pure $ mkAccountIndex (fromInteger i) else fail "account index too large" + if i <= toInteger (maxBound :: Word32) then either fail pure $ mkAccountIndex (fromInteger i) else fail "account index too large" displayNameP :: Parser Text displayNameP = safeDecodeUtf8 <$> displayNameP_ diff --git a/src/Simplex/Chat/Store/Wallets.hs b/src/Simplex/Chat/Store/Wallets.hs index 46e5294020..79979de505 100644 --- a/src/Simplex/Chat/Store/Wallets.hs +++ b/src/Simplex/Chat/Store/Wallets.hs @@ -6,8 +6,7 @@ {-# LANGUAGE TupleSections #-} module Simplex.Chat.Store.Wallets - ( SeedId, - Wallet (..), + ( Wallet (..), getWallet, createWallet, deleteWallet, @@ -77,13 +76,10 @@ deleteWallet db = rowReturned $ DB.query_ db "DELETE FROM wallet_seeds RETURNING wallet_seed_id" resolveAccount :: DB.Connection -> Maybe AccountIndex -> ExceptT StoreError IO (Either WalletError (WalletEntropy, AccountIndex)) -resolveAccount db accountIdx_ = fmap (first entropy) <$> resolveAccount_ db accountIdx_ - -resolveAccount_ :: DB.Connection -> Maybe AccountIndex -> ExceptT StoreError IO (Either WalletError (Wallet, AccountIndex)) -resolveAccount_ db accountIdx_ = +resolveAccount db accountIdx_ = getWallet db >>= \case Nothing -> pure $ Left WENoMaster - Just w@Wallet {nextAccountIndex} -> pure $ (w,) <$> maybe next Right accountIdx_ + Just Wallet {entropy, nextAccountIndex} -> pure $ (entropy,) <$> maybe next Right accountIdx_ where next = maybe (Left WECounterUnknown) (first (const WEAccountsExhausted) . mkAccountIndex) nextAccountIndex @@ -121,7 +117,6 @@ bindAccount db userId accountIdx_ = Nothing -> True <$ insertAccount db userId walletId n if held then Right (entropy, n) <$ raiseNextAccount db walletId n else pure $ Left WEAccountBound --- | Moves the counter in one statement, so concurrent binds get different accounts. takeNextAccount :: DB.Connection -> SeedId -> IO (Maybe AccountIndex) takeNextAccount db sId = maybeFirstRow fromOnly $ diff --git a/src/Simplex/Chat/Wallet.hs b/src/Simplex/Chat/Wallet.hs index af30575c57..4d449f7e21 100644 --- a/src/Simplex/Chat/Wallet.hs +++ b/src/Simplex/Chat/Wallet.hs @@ -55,11 +55,8 @@ data WalletError | WEAccountsExhausted deriving (Eq, Show) -masterStrength :: B39.EntropyStrength -masterStrength = B39.ES256 - newEntropy :: TVar ChaChaDRG -> IO B39.WalletEntropy -newEntropy = atomically . B39.randomEntropy masterStrength +newEntropy = atomically . B39.randomEntropy B39.ES256 entropyFromMnemonic :: Text -> Either WalletError B39.WalletEntropy entropyFromMnemonic = first (const WEBadMnemonic) . B39.parsePhrase