Copyright | No rights reserved |
---|---|
License | MIT |
Maintainer | jprupp@protonmail.ch |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
BIP-32 extended keys.
Synopsis
- data XPubKey = XPubKey {
- xPubDepth :: !Word8
- xPubParent :: !Fingerprint
- xPubIndex :: !KeyIndex
- xPubChain :: !ChainCode
- xPubKey :: !PubKey
- data XPrvKey = XPrvKey {
- xPrvDepth :: !Word8
- xPrvParent :: !Fingerprint
- xPrvIndex :: !KeyIndex
- xPrvChain :: !ChainCode
- xPrvKey :: !SecKey
- type ChainCode = Hash256
- type KeyIndex = Word32
- type Fingerprint = Word32
- newtype DerivationException = DerivationException String
- makeXPrvKey :: ByteString -> XPrvKey
- deriveXPubKey :: XPrvKey -> XPubKey
- prvSubKey :: XPrvKey -> KeyIndex -> XPrvKey
- pubSubKey :: XPubKey -> KeyIndex -> XPubKey
- hardSubKey :: XPrvKey -> KeyIndex -> XPrvKey
- xPrvIsHard :: XPrvKey -> Bool
- xPubIsHard :: XPubKey -> Bool
- xPrvChild :: XPrvKey -> KeyIndex
- xPubChild :: XPubKey -> KeyIndex
- xPubID :: XPubKey -> Hash160
- xPrvID :: XPrvKey -> Hash160
- xPubFP :: XPubKey -> Fingerprint
- xPrvFP :: XPrvKey -> Fingerprint
- xPubAddr :: XPubKey -> Address
- xPubWitnessAddr :: XPubKey -> Address
- xPubCompatWitnessAddr :: XPubKey -> Address
- xPubExport :: Network -> XPubKey -> Base58
- xPubToJSON :: Network -> XPubKey -> Value
- xPubToEncoding :: Network -> XPubKey -> Encoding
- xPubFromJSON :: Network -> Value -> Parser XPubKey
- xPrvExport :: Network -> XPrvKey -> Base58
- xPrvToJSON :: Network -> XPrvKey -> Value
- xPrvToEncoding :: Network -> XPrvKey -> Encoding
- xPrvFromJSON :: Network -> Value -> Parser XPrvKey
- xPubImport :: Network -> Base58 -> Maybe XPubKey
- xPrvImport :: Network -> Base58 -> Maybe XPrvKey
- xPrvWif :: Network -> XPrvKey -> Base58
- putXPrvKey :: MonadPut m => Network -> XPrvKey -> m ()
- putXPubKey :: MonadPut m => Network -> XPubKey -> m ()
- getXPrvKey :: MonadGet m => Network -> m XPrvKey
- getXPubKey :: MonadGet m => Network -> m XPubKey
- prvSubKeys :: XPrvKey -> KeyIndex -> [(XPrvKey, KeyIndex)]
- pubSubKeys :: XPubKey -> KeyIndex -> [(XPubKey, KeyIndex)]
- hardSubKeys :: XPrvKey -> KeyIndex -> [(XPrvKey, KeyIndex)]
- deriveAddr :: XPubKey -> KeyIndex -> (Address, PubKey)
- deriveWitnessAddr :: XPubKey -> KeyIndex -> (Address, PubKey)
- deriveCompatWitnessAddr :: XPubKey -> KeyIndex -> (Address, PubKey)
- deriveAddrs :: XPubKey -> KeyIndex -> [(Address, PubKey, KeyIndex)]
- deriveWitnessAddrs :: XPubKey -> KeyIndex -> [(Address, PubKey, KeyIndex)]
- deriveCompatWitnessAddrs :: XPubKey -> KeyIndex -> [(Address, PubKey, KeyIndex)]
- deriveMSAddr :: [XPubKey] -> Int -> KeyIndex -> (Address, RedeemScript)
- deriveMSAddrs :: [XPubKey] -> Int -> KeyIndex -> [(Address, RedeemScript, KeyIndex)]
- cycleIndex :: KeyIndex -> [KeyIndex]
- data DerivPathI t where
- (:|) :: HardOrAny t => !(DerivPathI t) -> !KeyIndex -> DerivPathI t
- (:/) :: AnyOrSoft t => !(DerivPathI t) -> !KeyIndex -> DerivPathI t
- Deriv :: DerivPathI t
- data AnyDeriv
- data HardDeriv
- data SoftDeriv
- class HardOrAny a
- class AnyOrSoft a
- type DerivPath = DerivPathI AnyDeriv
- type HardPath = DerivPathI HardDeriv
- type SoftPath = DerivPathI SoftDeriv
- data Bip32PathIndex
- derivePath :: DerivPathI t -> XPrvKey -> XPrvKey
- derivePubPath :: SoftPath -> XPubKey -> XPubKey
- toHard :: DerivPathI t -> Maybe HardPath
- toSoft :: DerivPathI t -> Maybe SoftPath
- toGeneric :: DerivPathI t -> DerivPath
- (++/) :: DerivPathI t1 -> DerivPathI t2 -> DerivPath
- pathToStr :: DerivPathI t -> String
- listToPath :: [KeyIndex] -> DerivPath
- pathToList :: DerivPathI t -> [KeyIndex]
- data XKey
- = XPrv {
- getXKeyPrv :: !XPrvKey
- getXKeyNet :: !Network
- | XPub {
- getXKeyPub :: !XPubKey
- getXKeyNet :: !Network
- = XPrv {
- data ParsedPath
- = ParsedPrv { }
- | ParsedPub { }
- | ParsedEmpty { }
- parsePath :: String -> Maybe ParsedPath
- parseHard :: String -> Maybe HardPath
- parseSoft :: String -> Maybe SoftPath
- applyPath :: ParsedPath -> XKey -> Either String XKey
- derivePathAddr :: XPubKey -> SoftPath -> KeyIndex -> (Address, PubKey)
- derivePathAddrs :: XPubKey -> SoftPath -> KeyIndex -> [(Address, PubKey, KeyIndex)]
- derivePathMSAddr :: [XPubKey] -> SoftPath -> Int -> KeyIndex -> (Address, RedeemScript)
- derivePathMSAddrs :: [XPubKey] -> SoftPath -> Int -> KeyIndex -> [(Address, RedeemScript, KeyIndex)]
- concatBip32Segments :: [Bip32PathIndex] -> DerivPath
Extended Keys
Data type representing an extended BIP32 public key.
XPubKey | |
|
Instances
Data type representing an extended BIP32 private key. An extended key is a node in a tree of key derivations. It has a depth in the tree, a parent node and an index to differentiate it from other siblings.
XPrvKey | |
|
Instances
type Fingerprint = Word32 Source #
Fingerprint of parent
newtype DerivationException Source #
A derivation exception is thrown in the very unlikely event that a derivation is invalid.
Instances
makeXPrvKey :: ByteString -> XPrvKey Source #
Build a BIP32 compatible extended private key from a bytestring. This will
produce a root node (depth=0
and parent=0
).
deriveXPubKey :: XPrvKey -> XPubKey Source #
Derive an extended public key from an extended private key. This function will preserve the depth, parent, index and chaincode fields of the extended private keys.
:: XPrvKey | extended parent private key |
-> KeyIndex | child derivation index |
-> XPrvKey | extended child private key |
Compute a private, soft child key derivation. A private soft derivation will allow the equivalent extended public key to derive the public key for this child. Given a parent key m and a derivation index i, this function will compute m/i.
Soft derivations allow for more flexibility such as read-only wallets. However, care must be taken not the leak both the parent extended public key and one of the extended child private keys as this would compromise the extended parent private key.
:: XPubKey | extended parent public key |
-> KeyIndex | child derivation index |
-> XPubKey | extended child public key |
Compute a public, soft child key derivation. Given a parent key M and a derivation index i, this function will compute M/i.
:: XPrvKey | extended parent private key |
-> KeyIndex | child derivation index |
-> XPrvKey | extended child private key |
Compute a hard child key derivation. Hard derivations can only be computed for private keys. Hard derivations do not allow the parent public key to derive the child public keys. However, they are safer as a breach of the parent public key and child private keys does not lead to a breach of the parent private key. Given a parent key m and a derivation index i, this function will compute m/i'.
xPrvIsHard :: XPrvKey -> Bool Source #
Returns true if the extended private key was derived through a hard derivation.
xPubIsHard :: XPubKey -> Bool Source #
Returns true if the extended public key was derived through a hard derivation.
xPrvChild :: XPrvKey -> KeyIndex Source #
Returns the derivation index of this extended private key without the hard bit set.
xPubChild :: XPubKey -> KeyIndex Source #
Returns the derivation index of this extended public key without the hard bit set.
xPubFP :: XPubKey -> Fingerprint Source #
Computes the key fingerprint of an extended public key.
xPrvFP :: XPrvKey -> Fingerprint Source #
Computes the key fingerprint of an extended private key.
xPubWitnessAddr :: XPubKey -> Address Source #
Compute a SegWit P2WPKH address for an extended public key.
xPubCompatWitnessAddr :: XPubKey -> Address Source #
Compute a backwards-compatible SegWit P2SH-P2WPKH address for an extended public key.
xPubExport :: Network -> XPubKey -> Base58 Source #
Exports an extended public key to the BIP32 key export format (Base58
).
xPubFromJSON :: Network -> Value -> Parser XPubKey Source #
Decode an extended public key from a JSON string
xPrvExport :: Network -> XPrvKey -> Base58 Source #
Exports an extended private key to the BIP32 key export format (Base58
).
xPrvFromJSON :: Network -> Value -> Parser XPrvKey Source #
Decode an extended private key from a JSON string
xPubImport :: Network -> Base58 -> Maybe XPubKey Source #
Decodes a BIP32 encoded extended public key. This function will fail if invalid base 58 characters are detected or if the checksum fails.
xPrvImport :: Network -> Base58 -> Maybe XPrvKey Source #
Decodes a BIP32 encoded extended private key. This function will fail if invalid base 58 characters are detected or if the checksum fails.
xPrvWif :: Network -> XPrvKey -> Base58 Source #
Export an extended private key to WIF (Wallet Import Format).
Helper Functions
prvSubKeys :: XPrvKey -> KeyIndex -> [(XPrvKey, KeyIndex)] Source #
Cyclic list of all private soft child key derivations of a parent key starting from an offset index.
pubSubKeys :: XPubKey -> KeyIndex -> [(XPubKey, KeyIndex)] Source #
Cyclic list of all public soft child key derivations of a parent key starting from an offset index.
hardSubKeys :: XPrvKey -> KeyIndex -> [(XPrvKey, KeyIndex)] Source #
Cyclic list of all hard child key derivations of a parent key starting from an offset index.
deriveAddr :: XPubKey -> KeyIndex -> (Address, PubKey) Source #
Derive a standard address from an extended public key and an index.
deriveWitnessAddr :: XPubKey -> KeyIndex -> (Address, PubKey) Source #
Derive a SegWit P2WPKH address from an extended public key and an index.
deriveCompatWitnessAddr :: XPubKey -> KeyIndex -> (Address, PubKey) Source #
Derive a backwards-compatible SegWit P2SH-P2WPKH address from an extended public key and an index.
deriveAddrs :: XPubKey -> KeyIndex -> [(Address, PubKey, KeyIndex)] Source #
Cyclic list of all addresses derived from a public key starting from an offset index.
deriveWitnessAddrs :: XPubKey -> KeyIndex -> [(Address, PubKey, KeyIndex)] Source #
Cyclic list of all SegWit P2WPKH addresses derived from a public key starting from an offset index.
deriveCompatWitnessAddrs :: XPubKey -> KeyIndex -> [(Address, PubKey, KeyIndex)] Source #
Cyclic list of all backwards-compatible SegWit P2SH-P2WPKH addresses derived from a public key starting from an offset index.
deriveMSAddr :: [XPubKey] -> Int -> KeyIndex -> (Address, RedeemScript) Source #
Derive a multisig address from a list of public keys, the number of required signatures m and a derivation index. The derivation type is a public, soft derivation.
deriveMSAddrs :: [XPubKey] -> Int -> KeyIndex -> [(Address, RedeemScript, KeyIndex)] Source #
Cyclic list of all multisig addresses derived from a list of public keys, a number of required signatures m and starting from an offset index. The derivation type is a public, soft derivation.
cycleIndex :: KeyIndex -> [KeyIndex] Source #
Helper function to go through derivation indices.
Derivation Paths
data DerivPathI t where Source #
Data type representing a derivation path. Two constructors are provided
for specifying soft or hard derivations. The path /0/1'/2 for example can be
expressed as
. The Deriv
:/ 0 :| 1 :/ 2HardOrAny
and AnyOrSoft
type
classes are used to constrain the valid values for the phantom type t. If
you mix hard (:|)
and soft (:\/)
paths, the only valid type for t is AnyDeriv
.
Otherwise, t can be HardDeriv
if you only have hard derivation or SoftDeriv
if you only have soft derivations.
Using this type is as easy as writing the required derivation like in these example:
Deriv :/ 0 :/ 1 :/ 2 :: SoftPath Deriv :| 0 :| 1 :| 2 :: HardPath Deriv :| 0 :/ 1 :/ 2 :: DerivPath
(:|) :: HardOrAny t => !(DerivPathI t) -> !KeyIndex -> DerivPathI t | |
(:/) :: AnyOrSoft t => !(DerivPathI t) -> !KeyIndex -> DerivPathI t | |
Deriv :: DerivPathI t |
Instances
Phantom type signaling no knowledge about derivation path: can be hardened or not.
Instances
Read DerivPath Source # | |
Show DerivPath Source # | |
IsString DerivPath Source # | |
Defined in Haskoin.Keys.Extended fromString :: String -> DerivPath # | |
Generic AnyDeriv Source # | |
FromJSON DerivPath Source # | |
Binary DerivPath Source # | |
Serial DerivPath Source # | |
Defined in Haskoin.Keys.Extended | |
Serialize DerivPath Source # | |
NFData AnyDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
AnyOrSoft AnyDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
HardOrAny AnyDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
type Rep AnyDeriv Source # | |
Phantom type signaling a hardened derivation path that can only be computed from private extended key.
Instances
Read HardPath Source # | |
Show HardPath Source # | |
IsString HardPath Source # | |
Defined in Haskoin.Keys.Extended fromString :: String -> HardPath # | |
Generic HardDeriv Source # | |
FromJSON HardPath Source # | |
Binary HardPath Source # | |
Serial HardPath Source # | |
Defined in Haskoin.Keys.Extended | |
Serialize HardPath Source # | |
NFData HardDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
HardOrAny HardDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
type Rep HardDeriv Source # | |
Phantom type signaling derivation path including only non-hardened paths that can be computed from an extended public key.
Instances
Read SoftPath Source # | |
Show SoftPath Source # | |
IsString SoftPath Source # | |
Defined in Haskoin.Keys.Extended fromString :: String -> SoftPath # | |
Generic SoftDeriv Source # | |
FromJSON SoftPath Source # | |
Binary SoftPath Source # | |
Serial SoftPath Source # | |
Defined in Haskoin.Keys.Extended | |
Serialize SoftPath Source # | |
NFData SoftDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
AnyOrSoft SoftDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
type Rep SoftDeriv Source # | |
Helper class to perform validations on a hardened derivation path.
Instances
HardOrAny AnyDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
HardOrAny HardDeriv Source # | |
Defined in Haskoin.Keys.Extended |
Helper class to perform validations on a non-hardened derivation path.
Instances
AnyOrSoft SoftDeriv Source # | |
Defined in Haskoin.Keys.Extended | |
AnyOrSoft AnyDeriv Source # | |
Defined in Haskoin.Keys.Extended |
type DerivPath = DerivPathI AnyDeriv Source #
Any derivation path.
type HardPath = DerivPathI HardDeriv Source #
Hardened derivation path. Can be computed from extended private key only.
type SoftPath = DerivPathI SoftDeriv Source #
Non-hardened derivation path can be computed from extended public key.
data Bip32PathIndex Source #
Type for BIP32 path index element.
Instances
derivePath :: DerivPathI t -> XPrvKey -> XPrvKey Source #
Derive a private key from a derivation path
derivePubPath :: SoftPath -> XPubKey -> XPubKey Source #
Derive a public key from a soft derivation path
toHard :: DerivPathI t -> Maybe HardPath Source #
Turn a derivation path into a hard derivation path. Will fail if the path contains soft derivations.
toSoft :: DerivPathI t -> Maybe SoftPath Source #
Turn a derivation path into a soft derivation path. Will fail if the path has hard derivations.
toGeneric :: DerivPathI t -> DerivPath Source #
Make a derivation path generic.
(++/) :: DerivPathI t1 -> DerivPathI t2 -> DerivPath Source #
Append two derivation paths together. The result will be a mixed derivation path.
pathToStr :: DerivPathI t -> String Source #
Convert a derivation path to a human-readable string.
listToPath :: [KeyIndex] -> DerivPath Source #
Convert a list of derivation indices to a derivation path.
pathToList :: DerivPathI t -> [KeyIndex] Source #
Get a list of derivation indices from a derivation path.
Derivation Path Parser
Data type representing a private or public key with its respective network.
XPrv | |
| |
XPub | |
|
Instances
Eq XKey Source # | |
Show XKey Source # | |
Generic XKey Source # | |
NFData XKey Source # | |
Defined in Haskoin.Keys.Extended | |
type Rep XKey Source # | |
Defined in Haskoin.Keys.Extended type Rep XKey = D1 ('MetaData "XKey" "Haskoin.Keys.Extended" "haskoin-core-0.20.0-inplace" 'False) (C1 ('MetaCons "XPrv" 'PrefixI 'True) (S1 ('MetaSel ('Just "getXKeyPrv") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 XPrvKey) :*: S1 ('MetaSel ('Just "getXKeyNet") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Network)) :+: C1 ('MetaCons "XPub" 'PrefixI 'True) (S1 ('MetaSel ('Just "getXKeyPub") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 XPubKey) :*: S1 ('MetaSel ('Just "getXKeyNet") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Network))) |
data ParsedPath Source #
Type for parsing derivation paths of the form m/1/2'/3 or M/1/2'/3.
Instances
parsePath :: String -> Maybe ParsedPath Source #
Parse derivation path string for extended key. Forms: m/0'/2, M/2/3/4.
applyPath :: ParsedPath -> XKey -> Either String XKey Source #
Apply a parsed path to an extended key to derive the new key defined in the path. If the path starts with m, a private key will be returned and if the path starts with M, a public key will be returned. Private derivations on a public key, and public derivations with a hard segment, return an error value.
derivePathAddr :: XPubKey -> SoftPath -> KeyIndex -> (Address, PubKey) Source #
Derive an address from a given parent path.
derivePathAddrs :: XPubKey -> SoftPath -> KeyIndex -> [(Address, PubKey, KeyIndex)] Source #
Cyclic list of all addresses derived from a given parent path and starting from the given offset index.
derivePathMSAddr :: [XPubKey] -> SoftPath -> Int -> KeyIndex -> (Address, RedeemScript) Source #
Derive a multisig address from a given parent path. The number of required signatures (m in m of n) is also needed.
derivePathMSAddrs :: [XPubKey] -> SoftPath -> Int -> KeyIndex -> [(Address, RedeemScript, KeyIndex)] Source #
Cyclic list of all multisig addresses derived from a given parent path and starting from the given offset index. The number of required signatures (m in m of n) is also needed.
concatBip32Segments :: [Bip32PathIndex] -> DerivPath Source #
Concatenate derivation path indices into a derivation path.