From 0b00d214c4c7b43ce1e8ffdc07b5c75626deccb2 Mon Sep 17 00:00:00 2001 From: Nicolas Pouillard Date: Fri, 16 Jan 2015 16:57:26 +0100 Subject: [PATCH] bip39-seed: Decode from hex if needed. More tests. --- Utils.hs | 5 ++- hx.hs | 6 ++-- tests/bip39-seed-from-hex.t/TESTRECIPE | 42 ++++++++++++++++++++++++++ tests/bip39-seed-from-hex.t/stdin | 1 + tests/bip39-seed-from-hex.t/stdout | 1 + tests/bip39-seed.t/TESTRECIPE | 42 ++++++++++++++++++++++++++ tests/bip39-seed.t/stdin | 1 + tests/bip39-seed.t/stdout | 1 + 8 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 tests/bip39-seed-from-hex.t/TESTRECIPE create mode 100644 tests/bip39-seed-from-hex.t/stdin create mode 100644 tests/bip39-seed-from-hex.t/stdout create mode 100644 tests/bip39-seed.t/TESTRECIPE create mode 100644 tests/bip39-seed.t/stdin create mode 100644 tests/bip39-seed.t/stdout diff --git a/Utils.hs b/Utils.hs index 527f351..8723e75 100644 --- a/Utils.hs +++ b/Utils.hs @@ -3,7 +3,7 @@ module Utils where import Control.Applicative import Data.Binary -import Data.Char (isSpace,isDigit,toLower) +import Data.Char (isSpace,isDigit,toLower,isHexDigit) import Data.Maybe import Data.Monoid import Data.String @@ -43,6 +43,9 @@ instance Hex LBS.ByteString where decodeHex msg = decodeHex msg . toStrictBS encodeHex = toLazyBS . encodeHex +isHex :: String -> Bool +isHex = all (\ c -> isHexDigit c || isSpace c) + putLn :: (IsString s, Monoid s) => s -> s putLn = (<> "\n") diff --git a/hx.hs b/hx.hs index a3aba1f..d33d1ef 100644 --- a/hx.hs +++ b/hx.hs @@ -295,13 +295,15 @@ hx_hd_decode :: BS -> BS hx_hd_decode = xKeyDetails . xKeyImportE hx_bip39_mnemonic :: Hex s => s -> BS -hx_bip39_mnemonic = either error B8.pack . toMnemonic . decodeHex "seed" +hx_bip39_mnemonic = either error B8.pack . toMnemonic . decodeHex "mnemonic-as-hex" hx_bip39_hex :: Hex s => BS -> s hx_bip39_hex = encodeHex . either error id . fromMnemonic . B8.unpack hx_bip39_seed :: Hex s => {-passphrase-}BS -> {-mnemonic-}BS -> s -hx_bip39_seed pf = encodeHex . either error id . mnemonicToSeed (B8.unpack pf) . B8.unpack +hx_bip39_seed pf = encodeHex . either error id . mnemonicToSeed (B8.unpack pf) . f . B8.unpack + where f s | isHex s = either (const s) id (toMnemonic (decodeHex "seed" s)) + | otherwise = s hx_btc, hx_satoshi :: BS -> BS hx_btc = B8.pack . formatScientific Fixed (Just 8) . (/ one_btc_in_satoshi) . readBS diff --git a/tests/bip39-seed-from-hex.t/TESTRECIPE b/tests/bip39-seed-from-hex.t/TESTRECIPE new file mode 100644 index 0000000..55e4b5f --- /dev/null +++ b/tests/bip39-seed-from-hex.t/TESTRECIPE @@ -0,0 +1,42 @@ +#!/bin/bash + +testname=bip39-seed-from-hex.t +command=hx +args=( bip39-seed ) +exit_code=0 +stdin_file=stdin +stdout_file=stdout +stderr_file=/dev/null +sources=( ) +products=( ) + +# Environment variables: +env_vars=( ) + +setup(){ + : Perform here actions to be run before the tested program +} + +munge(){ + : Munge here the results of the tested program to ease the check +} + +check(){ + check_exit_code && + check_stderr && + check_stdout && + check_products && + : Perform here extra checks on the tested program +} + +explain(){ + explain_exit_code + explain_stdout + explain_stderr + explain_products + : Explain here more potential differences +} + +teardown(){ + : Undo here the actions of setup +} diff --git a/tests/bip39-seed-from-hex.t/stdin b/tests/bip39-seed-from-hex.t/stdin new file mode 100644 index 0000000..abd8fc7 --- /dev/null +++ b/tests/bip39-seed-from-hex.t/stdin @@ -0,0 +1 @@ +636f727265637420686f727365206261747465727920737461706c65 diff --git a/tests/bip39-seed-from-hex.t/stdout b/tests/bip39-seed-from-hex.t/stdout new file mode 100644 index 0000000..71b7f7e --- /dev/null +++ b/tests/bip39-seed-from-hex.t/stdout @@ -0,0 +1 @@ +1929625a9cb3ff0fc1a2bc5288807730469969278dc9629926a862cf3932bd888cb0d621f818ee7bee359281e9aa08f35deaa0dd559be5041bbc4c64ff57d7c6 diff --git a/tests/bip39-seed.t/TESTRECIPE b/tests/bip39-seed.t/TESTRECIPE new file mode 100644 index 0000000..39a94bb --- /dev/null +++ b/tests/bip39-seed.t/TESTRECIPE @@ -0,0 +1,42 @@ +#!/bin/bash + +testname=bip39-seed.t +command=hx +args=( bip39-seed ) +exit_code=0 +stdin_file=stdin +stdout_file=stdout +stderr_file=/dev/null +sources=( ) +products=( ) + +# Environment variables: +env_vars=( ) + +setup(){ + : Perform here actions to be run before the tested program +} + +munge(){ + : Munge here the results of the tested program to ease the check +} + +check(){ + check_exit_code && + check_stderr && + check_stdout && + check_products && + : Perform here extra checks on the tested program +} + +explain(){ + explain_exit_code + explain_stdout + explain_stderr + explain_products + : Explain here more potential differences +} + +teardown(){ + : Undo here the actions of setup +} diff --git a/tests/bip39-seed.t/stdin b/tests/bip39-seed.t/stdin new file mode 100644 index 0000000..1e2cff1 --- /dev/null +++ b/tests/bip39-seed.t/stdin @@ -0,0 +1 @@ +gloom knee orient skull dance awake payment unusual inflict cinnamon blush seed elder crawl size catch inflict perfect foster sugar fox diff --git a/tests/bip39-seed.t/stdout b/tests/bip39-seed.t/stdout new file mode 100644 index 0000000..71b7f7e --- /dev/null +++ b/tests/bip39-seed.t/stdout @@ -0,0 +1 @@ +1929625a9cb3ff0fc1a2bc5288807730469969278dc9629926a862cf3932bd888cb0d621f818ee7bee359281e9aa08f35deaa0dd559be5041bbc4c64ff57d7c6