ppad-bip32-0.1.1: BIP32 hierarchical deterministic wallets.
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellNone
LanguageHaskell2010

Crypto.HDKey.BIP32

Description

BIP32 hierarchical deterministic wallets and extended keys, with support for serialization and parsing.

Synopsis

Hierarchical deterministic keys

data HDKey Source #

A BIP32 hierarchical deterministic key.

This differs from lower-level "extended" keys in that it carries all information required for serialization.

Constructors

HDKey 

Fields

Instances

Instances details
Show HDKey Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

showsPrec :: Int -> HDKey -> ShowS #

show :: HDKey -> String #

showList :: [HDKey] -> ShowS #

Eq HDKey Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

(==) :: HDKey -> HDKey -> Bool #

(/=) :: HDKey -> HDKey -> Bool #

Extended HDKey Source # 
Instance details

Defined in Crypto.HDKey.BIP32

master :: ByteString -> Maybe HDKey Source #

Derive a master HDKey from a master seed.

Fails with Nothing if the provided seed has an invalid length.

Extended keys

class Extended k where Source #

Key types supporting identifier/fingerprint calculation.

>>> let Just hd = master "my very secret entropy"
>>> let Right my_xprv = hd_key hd
>>> let my_xpub = n k
>>> -- all have the same fingerprint
>>> fingerprint hd
"G\157\&8\146"
>>> fingerprint my_xprv
"G\157\&8\146"
>>> fingerprint my_xpub
"G\157\&8\146"

Minimal complete definition

identifier

Methods

identifier :: k -> ByteString Source #

Calculate the identifier for an extended key.

fingerprint :: k -> ByteString Source #

Calculate the fingerprint of an extended key.

Instances

Instances details
Extended HDKey Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Extended XPrv Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Extended XPub Source # 
Instance details

Defined in Crypto.HDKey.BIP32

newtype XPub Source #

An extended public key.

Constructors

XPub (X Projective) 

Instances

Instances details
Show XPub Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

showsPrec :: Int -> XPub -> ShowS #

show :: XPub -> String #

showList :: [XPub] -> ShowS #

Eq XPub Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

(==) :: XPub -> XPub -> Bool #

(/=) :: XPub -> XPub -> Bool #

Extended XPub Source # 
Instance details

Defined in Crypto.HDKey.BIP32

newtype XPrv Source #

An extended private key.

Constructors

XPrv (X Integer) 

Instances

Instances details
Show XPrv Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

showsPrec :: Int -> XPrv -> ShowS #

show :: XPrv -> String #

showList :: [XPrv] -> ShowS #

Eq XPrv Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

(==) :: XPrv -> XPrv -> Bool #

(/=) :: XPrv -> XPrv -> Bool #

Extended XPrv Source # 
Instance details

Defined in Crypto.HDKey.BIP32

data X a Source #

A public or private key, extended with a chain code.

Constructors

X !a !ByteString 

Instances

Instances details
Show a => Show (X a) Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

showsPrec :: Int -> X a -> ShowS #

show :: X a -> String #

showList :: [X a] -> ShowS #

Eq a => Eq (X a) Source # 
Instance details

Defined in Crypto.HDKey.BIP32

Methods

(==) :: X a -> X a -> Bool #

(/=) :: X a -> X a -> Bool #

Child derivation via path

derive Source #

Arguments

:: HDKey 
-> ByteString

derivation path

-> Maybe HDKey 

Derive a child node via the provided derivation path.

Fails with Nothing if derivation is impossible, or if the provided path is invalid.

>>> let Just hd = master "my very secret master seed"
>>> let Just child = derive hd "m/44'/0'/0'/0/0"
>>> xpub child
"xpub6FvaeGNFmCkLky6jwefrUfyH7gCGSAUckRBANT6wLQkm4eWZApsf4LqAadtbM8EBFfuKGFgzhgta4ByP6xnBodk2EV7BiwxCPLgu13oYWGp"

derive_partial :: HDKey -> ByteString -> HDKey Source #

Derive a child node via the provided derivation path.

Fails with error if derivation is impossible, or if the provided path is invalid.

>>> let Just other_child = derive hd "m/44'/0'/0'/0/1"
>>> xpub other_child
"xpub6FvaeGNFmCkLpkT3uahJnGPTfEX62PtH7uZAyjtru8S2FvPuYTQKn8ct6CNQAwHMXaGN6EYuwi1Tz2VD7msftH8VTAtzgNra9CForA9FBP4"

Serialization

xpub :: HDKey -> ByteString Source #

Serialize a mainnet extended public key in base58check format.

>>> let Just hd = master "my very secret entropy"
>>> xpub hd
"xpub661MyMwAqRbcGTJPtZRqZyrvjxHCfhqXeiqb5GVU3EGuFBy4QxT3yt8iiHwZTiCzZFyuyNiqXB3eqzqFZ8z4L6HCrPSkDVFNuW59LXYvMjs"

xprv :: HDKey -> ByteString Source #

Serialize a mainnet extended private key in base58check format.

>>> xprv hd
"xprv9s21ZrQH143K3yDvnXtqCqvCBvSiGF7gHVuzGt5rUtjvNPdusR8oS5pErywDM1jDDTcLpNNCbg9a9NuidBczRzSUp7seDeu8am64h6nfdrg"

tpub :: HDKey -> ByteString Source #

Serialize a testnet extended public key in base58check format.

>>> tpub hd
"tpubD6NzVbkrYhZ4YFVFLkQvmuCJ55Nrf6LbCMRtRpYcP92nzUdmVBJ98KoYxL2LzDAEMAWpaxEi4GshYBKrwzqJDXjVuzC3u1ucVTfZ6ZD415x"

tprv :: HDKey -> ByteString Source #

Serialize a testnet extended private key in base58check format.

>>> tprv hd
"tprv8ZgxMBicQKsPenTTT6kLNVYBW3rvVm9gd3q79JWJxsEQ9zNzrnUYwqBgnA6sMP7Xau97pTyxm2jNcETTkPxwF3i5Lm5wt1dBVrqV8kKi5v5"

Parsing

parse :: ByteString -> Maybe HDKey Source #

Parse a base58check-encoded ByteString into a HDKey.

Fails with Nothing if the provided key is invalid.

>>> let Just hd = master "my very secret entropy"
>>> let Just my_xprv = parse (xprv hd)
>>> my_xprv == hd
True

Child key derivation functions

derive_child_pub :: HDKey -> Word32 -> Maybe HDKey Source #

Derive a public child node at the provided index.

Fails with Nothing if derivation is impossible.

>>> :set -XNumericUnderscores
>>> let Just child_pub = derive_child_pub child_prv 0x8000_0000
>>> xpub child_pub
"xpub6B6LoU83Cpyx1UVMwuoQdQvY2BuGbPd2xsEVxCnj85UGgDN9bRz82hQhe9UFmyo4Pokuhjc8M1Cfc8ufLxcL6FkCF7Zc2eajEfWfZwMFF6X"

derive_child_priv :: HDKey -> Word32 -> Maybe HDKey Source #

Derive a private child node at the provided index.

Fails with Nothing if derivation is impossible.

>>> let Just child_prv = derive_child_priv hd 0
>>> xpub child_prv
"xpub68R2ZbtFeJTFJApdEdPqW5cy3d5wF96tTfJErhu3mTi2Ttaqvc88BMPrgS3hQSrHj91kRbzVLM9pue9f8219szRKZuTAx1LWbdLDLFDm6Ly"