mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-10-12 02:22:45 +00:00
simplify wallet tooling (#1904)
This commit is contained in:
@@ -13,12 +13,6 @@ module Simplex.Messaging.Crypto.BIP32
|
||||
renderPath,
|
||||
hardened,
|
||||
isHardened,
|
||||
WalletMaster,
|
||||
masterEntropy,
|
||||
walletMasterKey,
|
||||
mkWalletMaster,
|
||||
parseWalletMaster,
|
||||
masterBytes,
|
||||
)
|
||||
where
|
||||
|
||||
@@ -35,7 +29,6 @@ import Data.ByteString (ByteString)
|
||||
import qualified Data.ByteString.Char8 as BC
|
||||
import Data.List (intercalate)
|
||||
import Data.Word (Word32)
|
||||
import Simplex.Messaging.Crypto.BIP39 (WalletEntropy, entropySeed, mkEntropy)
|
||||
import qualified Simplex.Messaging.Crypto.Secp256k1 as S
|
||||
import Simplex.Messaging.Encoding (smpEncode)
|
||||
|
||||
@@ -106,24 +99,3 @@ renderPath is = BC.pack $ intercalate "/" ("m" : map component is)
|
||||
|
||||
hmacSHA512 :: ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes
|
||||
hmacSHA512 key msg = BA.convert (HMAC.hmac key msg :: HMAC.HMAC H.SHA512)
|
||||
|
||||
-- | Entropy with the master key it derives.
|
||||
data WalletMaster = WalletMaster WalletEntropy ExtendedKey
|
||||
|
||||
masterEntropy :: WalletMaster -> WalletEntropy
|
||||
masterEntropy (WalletMaster ent _) = ent
|
||||
|
||||
walletMasterKey :: WalletMaster -> ExtendedKey
|
||||
walletMasterKey (WalletMaster _ k) = k
|
||||
|
||||
mkWalletMaster :: WalletEntropy -> Either String WalletMaster
|
||||
mkWalletMaster ent = WalletMaster ent <$> masterKey (entropySeed ent "")
|
||||
|
||||
-- | From storage: the master bytes must be the ones the entropy derives.
|
||||
parseWalletMaster :: ScrubbedBytes -> ScrubbedBytes -> Either String WalletMaster
|
||||
parseWalletMaster entBytes mBytes = do
|
||||
m <- mkWalletMaster =<< mkEntropy entBytes
|
||||
if masterBytes m == mBytes then Right m else Left "wallet master: does not match the entropy"
|
||||
|
||||
masterBytes :: WalletMaster -> ScrubbedBytes
|
||||
masterBytes (WalletMaster _ ExtendedKey {xkKey, xkChainCode}) = S.unPrivateKey xkKey <> xkChainCode
|
||||
|
||||
@@ -235,17 +235,9 @@ bip32Tests g = do
|
||||
isLeft (B32.masterKey (BA.replicate 65 1)) `shouldBe` True
|
||||
it "renders a path" $
|
||||
B32.renderPath [B32.hardened 44, B32.hardened 60, B32.hardened 0, 0, 0] `shouldBe` "m/44'/60'/0'/0/0"
|
||||
it "parses a wallet master back from its storage form" $
|
||||
(xkHex . B32.walletMasterKey <$> B32.parseWalletMaster (B39.unEntropy canonicalEntropy) (B32.masterBytes canonicalMaster))
|
||||
`shouldBe` Right (xkHex $ B32.walletMasterKey canonicalMaster)
|
||||
it "rejects a stored master the entropy does not derive" $ do
|
||||
let stored = B32.masterBytes canonicalMaster
|
||||
isLeft (B32.parseWalletMaster (B39.unEntropy canonicalEntropy) (BA.xor stored (BA.replicate 64 1 :: BA.ScrubbedBytes))) `shouldBe` True
|
||||
isLeft (B32.parseWalletMaster (B39.unEntropy canonicalEntropy) (BA.take 63 stored)) `shouldBe` True
|
||||
isLeft (B32.parseWalletMaster (BA.replicate 17 0) stored) `shouldBe` True
|
||||
|
||||
canonicalMaster :: B32.WalletMaster
|
||||
canonicalMaster = right $ B32.mkWalletMaster canonicalEntropy
|
||||
canonicalMaster :: B32.ExtendedKey
|
||||
canonicalMaster = right $ B32.masterKey (B39.entropySeed canonicalEntropy "")
|
||||
|
||||
derivationTests :: TVar ChaChaDRG -> Spec
|
||||
derivationTests g = do
|
||||
@@ -259,8 +251,8 @@ derivationTests g = do
|
||||
mapM addrAt [0 .. 4] >>= (`shouldSatisfy` \as -> length as == length (nub as))
|
||||
it "derives an account key together with its address" $ do
|
||||
let path = bip44Path Ethereum $ account 0
|
||||
(xk, a) <- right <$> deriveAddress g (B32.walletMasterKey canonicalMaster) path
|
||||
xk' <- right <$> B32.derivePath g (B32.walletMasterKey canonicalMaster) path
|
||||
(xk, a) <- right <$> deriveAddress g canonicalMaster path
|
||||
xk' <- right <$> B32.derivePath g canonicalMaster path
|
||||
xkHex xk `shouldBe` xkHex xk'
|
||||
addressFromPrivateKey g (B32.xkKey xk) `shouldReturn` a
|
||||
it "renders the Ethereum path of an account" $
|
||||
@@ -271,7 +263,7 @@ derivationTests g = do
|
||||
where
|
||||
seed = B39.entropySeed canonicalEntropy ""
|
||||
account = right . mkAccountIndex
|
||||
addrAt i = snd . right <$> deriveAddress g (B32.walletMasterKey canonicalMaster) (bip44Path Ethereum $ account i)
|
||||
addrAt i = snd . right <$> deriveAddress g canonicalMaster (bip44Path Ethereum $ account i)
|
||||
|
||||
eip55Tests :: Spec
|
||||
eip55Tests = do
|
||||
|
||||
Reference in New Issue
Block a user