Skip to content

Commit

Permalink
update reading passphrase from bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Sep 23, 2022
1 parent 59d8959 commit 486f0eb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -70,6 +70,20 @@ Please enter a [9, 12, 15, 18, 21, 24] word mnemonic:
exercise club noble adult miracle awkward problem olympic puppy private goddess piano fatal fashion vacuum
Please enter a 9–12 word second factor:
swing payment diagram happy chimney mammal flip become lyrics
root_xsk1jqx0xpke7de69ceyk20tdl9rq7nsava7cfnyeu42yqum8usnpppwmsxn2qsfj0nn2ur2kuq0kmrll67ryvkdhd6pgpsls6s6qx7hlyv6uqt0907t73eflkpw3xz45lcg5fsh6dunfk56j08jslh6x6rttspfny8c

cardano-address key from-recovery-phrase Shelley --passphrase from-mnemonic --sensitive
Please enter a [9, 12, 15, 18, 21, 24] word mnemonic:
**********************************************************************************************************
Please enter a 9–12 word second factor:
*************************************************************
root_xsk1jqx0xpke7de69ceyk20tdl9rq7nsava7cfnyeu42yqum8usnpppwmsxn2qsfj0nn2ur2kuq0kmrll67ryvkdhd6pgpsls6s6qx7hlyv6uqt0907t73eflkpw3xz45lcg5fsh6dunfk56j08jslh6x6rttspfny8c

$ cardano-address key from-recovery-phrase Shelley --passphrase from-mnemonic --silent
Please enter a [9, 12, 15, 18, 21, 24] word mnemonic:

Please enter a 9–12 word second factor:

root_xsk1jqx0xpke7de69ceyk20tdl9rq7nsava7cfnyeu42yqum8usnpppwmsxn2qsfj0nn2ur2kuq0kmrll67ryvkdhd6pgpsls6s6qx7hlyv6uqt0907t73eflkpw3xz45lcg5fsh6dunfk56j08jslh6x6rttspfny8c

$ cardano-address key from-recovery-phrase Shelley --passphrase from-hex
Expand Down
26 changes: 15 additions & 11 deletions command-line/lib/Command/Key/FromRecoveryPhrase.hs
Expand Up @@ -45,12 +45,11 @@ import Options.Applicative.Style
import System.IO
( stderr, stdin, stdout )
import System.IO.Extra
( hGetPassphraseBytes
( hGetPassphraseBytesInteractively
, hGetPassphraseMnemonicInteractively
, hGetSomeMnemonic
, hGetSomeMnemonicInteractively
, hPutBytes
, hPutString
, progName
)

Expand Down Expand Up @@ -98,23 +97,28 @@ run FromRecoveryPhrase{style,passphraseInfo, passphraseInputMode} = do
handlePassphraseInfo = \case
Mnemonic -> do
let prompt = "Please enter a 9–12 word second factor:"
p <- hGetPassphraseMnemonicInteractively (stdin, stderr) passphraseInputMode prompt
p <- hGetPassphraseMnemonicInteractively (stdin, stderr)
passphraseInputMode prompt
pure $ FromMnemonic p
Hex -> do
hPutString stderr "Please enter hex-encoded passphrase:"
p <- hGetPassphraseBytes stdin Hex
let prompt = "Please enter hex-encoded passphrase:"
p <- hGetPassphraseBytesInteractively (stdin, stderr)
passphraseInputMode prompt Hex
pure $ FromEncoded p
Base64 -> do
hPutString stderr "Please enter base64-encoded passphrase:"
p <- hGetPassphraseBytes stdin Base64
let prompt = "Please enter base64-encoded passphrase:"
p <- hGetPassphraseBytesInteractively (stdin, stderr)
passphraseInputMode prompt Base64
pure $ FromEncoded p
Utf8 -> do
hPutString stderr "Please enter utf8-encoded passphrase:"
p <- hGetPassphraseBytes stdin Utf8
let prompt = "Please enter utf8-encoded passphrase:"
p <- hGetPassphraseBytesInteractively (stdin, stderr)
passphraseInputMode prompt Utf8
pure $ FromEncoded p
Octets -> do
hPutString stderr "Please enter passphrase in the form of octet array:"
p <- hGetPassphraseBytes stdin Octets
let prompt = "Please enter passphrase in the form of octet array:"
p <- hGetPassphraseBytesInteractively (stdin, stderr)
passphraseInputMode prompt Octets
pure $ FromEncoded p

styleHrp :: Style -> HumanReadablePart
Expand Down
19 changes: 10 additions & 9 deletions command-line/lib/System/IO/Extra.hs
Expand Up @@ -17,7 +17,7 @@ module System.IO.Extra
, hGetSomeMnemonic
, hGetSomeMnemonicInteractively
, hGetPassphraseMnemonicInteractively
, hGetPassphraseBytes
, hGetPassphraseBytesInteractively

-- ** Write
, hPutBytes
Expand Down Expand Up @@ -94,7 +94,6 @@ import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as TIO

--
-- I/O Read
Expand Down Expand Up @@ -260,22 +259,24 @@ hGetPassphraseMnemonicInteractively (hstdin, hstderr) mode prompt = do
Right mw -> pure mw

-- | Read some bytes from the console, and decode them accoring to passphrase info.
hGetPassphraseBytes
:: Handle
hGetPassphraseBytesInteractively
:: (Handle, Handle)
-> PassphraseInputMode
-> String
-> PassphraseInfo
-> IO ByteString
hGetPassphraseBytes h = \case
hGetPassphraseBytesInteractively (hstdin, hstderr) mode prompt = \case
Hex -> do
raw <- B8.filter noNewline <$> B8.hGetLine h
raw <- B8.filter noNewline . T.encodeUtf8 <$> hGetSensitiveLine (hstdin, hstderr) mode prompt
decodeBytes fromBase16 raw
Base64 -> do
raw <- B8.filter noNewline <$> B8.hGetLine h
raw <- B8.filter noNewline . T.encodeUtf8 <$> hGetSensitiveLine (hstdin, hstderr) mode prompt
decodeBytes fromBase64 raw
Utf8 -> do
txt <- TIO.hGetLine h
txt <- hGetSensitiveLine (hstdin, hstderr) mode prompt
pure $ T.encodeUtf8 txt
Octets -> do
txt <- TIO.hGetLine h
txt <- hGetSensitiveLine (hstdin, hstderr) mode prompt
let bytes = read @[Word8] (T.unpack txt)
pure $ BS.pack bytes
_ -> fail
Expand Down

0 comments on commit 486f0eb

Please sign in to comment.