Skip to content

Commit 864d333

Browse files
committed
Cleaning
1 parent 454af25 commit 864d333

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

src/Commands/ListKeys.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ listKeysCommand efc mInd mDeriv= do
4343
(Right (HDRoot seed mpass), Just ChainWeaver) -> printChainWeaverKeys seed mpass mInd
4444
(Right (HDRoot seed _), Just KIP) -> printKipKeys seed mInd
4545
(Right (HDRoot seed mpass), Nothing) -> printChainWeaverKeys seed mpass mInd >> putStrLn "" >> printKipKeys seed mInd
46-
(Right (PlainKeyPair _ pub), _) -> printPlainKey pub
46+
(Right (SingleKeyPair _ pub), _) -> printPlainKey pub

src/Commands/Sign.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ signYamlFile kkey mindex enc msgFile = do
8686
signingKeys = S.fromList $ map _s_pubKey $ unSignatureList sigs
8787
case kkey of
8888
HDRoot seed mpass -> tryHdIndex msgFile csd seed mpass mindex
89-
PlainKeyPair sec pub -> do
89+
SingleKeyPair sec pub -> do
9090
let pubHex = PublicKeyHex $ pubKeyToText pub
9191
if S.member pubHex signingKeys
9292
then do
@@ -160,7 +160,7 @@ signOther msgFile kkey kind enc = do
160160
HDRoot seed mpass ->
161161
let (esec, pub) = generateCryptoPairFromRoot (seedToRoot seed mpass) mpass kind
162162
in (pub, sign esec mpass msg)
163-
PlainKeyPair sec pub -> (pub, sign sec Nothing msg)
163+
SingleKeyPair sec pub -> (pub, sign sec Nothing msg)
164164
lift $ T.putStrLn $ pubKeyToText pubKey <> ": " <> coerce sig
165165
case res of
166166
Left e -> die e

src/Keys.hs

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ import Utils
4848
import Data.Base16.Types (extractBase16)
4949
------------------------------------------------------------------------------
5050

51+
newtype MnemonicPhrase = MnemonicPhrase [ Text ]
52+
deriving (Show, Eq)
53+
54+
data SecretKey = CardanoSecretKey Crypto.XPrv
55+
| PlainSecretKey ED25519.SecretKey
56+
57+
data PublicKey = CardanoPublicKey ByteString
58+
| PlainPublicKey ED25519.PublicKey
59+
deriving (Eq, Show)
60+
61+
62+
newtype Signature = Signature Text
63+
deriving (Eq, Ord, Show)
64+
65+
newtype ParsedSignature = ParsedSignature ByteString
66+
deriving (Eq, Ord, Show)
67+
68+
data KadenaKey
69+
= HDRoot ByteString (Maybe Text) --Seed + Maybe Chaibnweaver password
70+
| SingleKeyPair SecretKey PublicKey
71+
72+
5173
mnemonicToRoot :: MnemonicPhrase -> Crypto.XPrv
5274
mnemonicToRoot phrase = seedToRoot (phraseToSeed phrase) Nothing -- TODO: Empty passowrd
5375

@@ -56,14 +78,20 @@ genMnemonic12 = liftIO $ bimap tshow Crypto.entropyToWords . Crypto.toEntropy @1
5678
-- This size must be a 1/8th the size of the 'toEntropy' size: 128 / 8 = 16
5779
<$> Crypto.Random.Entropy.getEntropy @ByteString 16
5880

81+
slip10modifier :: ByteString
82+
slip10modifier = "ed25519 seed"
83+
84+
kadenaChainCode :: Word32
85+
kadenaChainCode = 626
86+
5987
-- KIP-0026 / SLIP-10 derivation
6088
kipDerivSecretKey :: ByteString -> KeyIndex -> ED25519.SecretKey
6189
kipDerivSecretKey seed ki = onCryptoFailure (error . show) id $ ED25519.secretKey pkey3
6290
where
6391
(pkey3, _) = doDeriv pkey2 code2 (fromKeyIndex ki)
64-
(pkey2, code2) = doDeriv pkey1 code1 626
92+
(pkey2, code2) = doDeriv pkey1 code1 kadenaChainCode
6593
(pkey1, code1) = doDeriv pkey0 code0 44
66-
(pkey0, code0) = doHmac "ed25519 seed" seed
94+
(pkey0, code0) = doHmac slip10modifier seed
6795

6896
doDeriv:: ByteString -> ByteString -> Word32 -> (ByteString, ByteString)
6997
doDeriv pkey code idx = doHmac code $ LBS.toStrict $ runPut $ putWord8 0 >> putByteString pkey >> putWord32be (0x80000000 .|. idx)
@@ -76,11 +104,13 @@ generateKipCryptoPairFromSeed :: ByteString -> KeyIndex -> (SecretKey, PublicKey
76104
generateKipCryptoPairFromSeed seed ki = let skey = kipDerivSecretKey seed ki
77105
in (PlainSecretKey skey , PlainPublicKey $ ED25519.toPublic skey)
78106

107+
encodePass :: Maybe Text -> ByteString
108+
encodePass = T.encodeUtf8 . fromMaybe ""
79109

80110
generateCryptoPairFromRoot :: Crypto.XPrv -> Maybe Text -> KeyIndex -> (SecretKey, PublicKey)
81111
generateCryptoPairFromRoot root pass i =
82112
let hardenedIdx = 0x80000000 .|. (fromKeyIndex i)
83-
xprv = Crypto.deriveXPrv scheme (T.encodeUtf8 $ fromMaybe "" pass) root hardenedIdx
113+
xprv = Crypto.deriveXPrv scheme (encodePass pass) root hardenedIdx
84114
in (CardanoSecretKey xprv, CardanoPublicKey $ Crypto.xpubPublicKey $ Crypto.toXPub xprv)
85115
where
86116
scheme = Crypto.DerivationScheme2
@@ -93,10 +123,6 @@ mkPhraseMapFromMnemonic
93123
mkPhraseMapFromMnemonic = wordsToPhraseMap . T.words . baToText
94124
. Crypto.mnemonicSentenceToString @mw Crypto.english
95125

96-
newtype MnemonicPhrase = MnemonicPhrase [ Text ]
97-
deriving (Show, Eq)
98-
99-
-- TODO Allow 24-word phrases
100126
mkMnemonicPhrase :: [Text] -> Maybe MnemonicPhrase
101127
mkMnemonicPhrase lst
102128
| length lst == 12 = Just $ MnemonicPhrase lst
@@ -150,7 +176,7 @@ sentenceToSeed s = Crypto.sentenceToSeed s Crypto.english ""
150176
-- unlocked with the password
151177
-- TODO: enter password 2x, to confirm
152178
seedToRoot :: ByteArrayAccess ba => ba -> Maybe Text -> Crypto.XPrv
153-
seedToRoot seed password = Crypto.generate seed $ T.encodeUtf8 $ fromMaybe "" password
179+
seedToRoot seed password = Crypto.generate seed $ encodePass password
154180

155181
-- | Convenience function for unpacking byte array things into 'Text'
156182
newtype WordKey = WordKey { _unWordKey :: Int }
@@ -159,10 +185,6 @@ newtype WordKey = WordKey { _unWordKey :: Int }
159185
wordsToPhraseMap :: [Text] -> Map.Map WordKey Text
160186
wordsToPhraseMap = Map.fromList . zip [WordKey 1 ..]
161187

162-
data KadenaKey
163-
= HDRoot ByteString (Maybe Text) --Seed + Maybe Chaibnweaver password
164-
| PlainKeyPair SecretKey PublicKey
165-
166188
data KeyPairYaml = KeyPairYaml
167189
{ kpyPublic :: Text
168190
, kpySecret :: Text
@@ -188,7 +210,7 @@ readKadenaKey h = do
188210
let mres = do
189211
pub <- maybeCryptoError . ED25519.publicKey =<< hush (fromB16 $ kpyPublic kpy)
190212
sec <- maybeCryptoError . ED25519.secretKey =<< hush (fromB16 $ kpySecret kpy)
191-
pure $ PlainKeyPair (PlainSecretKey sec) (PlainPublicKey pub)
213+
pure $ SingleKeyPair (PlainSecretKey sec) (PlainPublicKey pub)
192214
pure $ note "not a valid ED25519 key pair" mres
193215
Right _ -> pure $ Left "Invalid JSON type for key material"
194216
Left _ -> pure $ Left "Could not parse key material"
@@ -221,21 +243,6 @@ genPairFromPhrase :: MnemonicPhrase -> KeyIndex -> (SecretKey, PublicKey)
221243
genPairFromPhrase phrase idx =
222244
generateCryptoPairFromRoot (mnemonicToRoot phrase) Nothing idx
223245

224-
225-
data SecretKey = CardanoSecretKey Crypto.XPrv
226-
| PlainSecretKey ED25519.SecretKey
227-
228-
data PublicKey = CardanoPublicKey ByteString
229-
| PlainPublicKey ED25519.PublicKey
230-
deriving (Eq, Show)
231-
232-
233-
newtype Signature = Signature Text
234-
deriving (Eq, Ord, Show)
235-
236-
newtype ParsedSignature = ParsedSignature ByteString
237-
deriving (Eq, Ord, Show)
238-
239246
parseSignature :: Text -> Either Text ParsedSignature
240247
parseSignature x = do
241248
bs <- fromB16 x
@@ -256,7 +263,7 @@ toPubKey txt = do
256263

257264

258265
sign :: SecretKey -> Maybe Text -> ByteString -> Signature
259-
sign (CardanoSecretKey xprv) mpass = Signature . toB16 . Crypto.unXSignature . Crypto.sign @ByteString (T.encodeUtf8 (fromMaybe "" mpass)) xprv
266+
sign (CardanoSecretKey xprv) mpass = Signature . toB16 . Crypto.unXSignature . Crypto.sign @ByteString (encodePass mpass) xprv
260267
sign (PlainSecretKey xprv) _ = Signature . toB16 . BA.convert . ED25519.sign xprv (ED25519.toPublic xprv)
261268

262269
verify :: PublicKey -> ParsedSignature -> ByteString -> Bool

0 commit comments

Comments
 (0)