Skip to content

Commit

Permalink
Implement xPrvFromBytes such that input can either be 96 or 128 bytes…
Browse files Browse the repository at this point in the history
… long
  • Loading branch information
intricate authored and Jimbo4350 committed Mar 16, 2023
1 parent 9342afd commit 92b4616
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cardano-api/src/Cardano/Api/Crypto/Ed25519Bip32.hs
Expand Up @@ -170,7 +170,7 @@ xPrvToBytes xPrv = privateKeyBytes <> chainCodeBytes
chainCodeBytes :: ByteString
chainCodeBytes = BS.drop 96 (CC.unXPrv xPrv)

-- | Deserialise an 'CC.XPrv' from a 'ByteString' (96 bytes).
-- | Deserialise an 'CC.XPrv' from a 'ByteString' (96 or 128 bytes).
--
-- In @cardano-crypto@, an 'CC.XPrv' was originally deserialised using the
-- following 128-byte binary format:
Expand All @@ -179,20 +179,21 @@ xPrvToBytes xPrv = privateKeyBytes <> chainCodeBytes
-- | Extended Private Key (64 bytes) | Public Key (32 bytes) | Chain Code (32 bytes) |
-- +---------------------------------+-----------------------+-----------------------+
--
-- However, this function deserialises an 'CC.XPrv' using a more compact
-- 96-byte binary format:
-- However, this function also supports deserialising an 'CC.XPrv' using a
-- more compact 96-byte binary format:
--
-- +---------------------------------+-----------------------+
-- | Extended Private Key (64 bytes) | Chain Code (32 bytes) |
-- +---------------------------------+-----------------------+
--
xPrvFromBytes :: ByteString -> Maybe CC.XPrv
xPrvFromBytes bytes
| BS.length bytes /= 96 = Nothing
| otherwise = do
| BS.length bytes == 96 = do
let (prv, cc) = BS.splitAt 64 bytes
pub <- ed25519ScalarMult (BS.take 32 prv)
eitherToMaybe $ CC.xprv $ prv <> pub <> cc
| BS.length bytes == 128 = eitherToMaybe (CC.xprv bytes)
| otherwise = Nothing
where
eitherToMaybe :: Either a b -> Maybe b
eitherToMaybe = either (const Nothing) Just
Expand Down

0 comments on commit 92b4616

Please sign in to comment.