Safe Haskell | None |
---|
This package provides functions for generating hierarchical deterministic keys (BIP32). It also provides functions for building and signing both simple transactions and multisignature transactions. This package also provides a command line application called hw (haskoin wallet). It is a lightweight bitcoin wallet featuring BIP32 key management, deterministic signatures (RFC-6979) and first order support for multisignature transactions. A library API for hw is also exposed.
- type ChainCode = Hash256
- data XPrvKey = XPrvKey {}
- makeXPrvKey :: ByteString -> Maybe XPrvKey
- xPrvIsPrime :: XPrvKey -> Bool
- xPrvChild :: XPrvKey -> Word32
- xPrvID :: XPrvKey -> Hash160
- xPrvFP :: XPrvKey -> Word32
- xPrvExport :: XPrvKey -> String
- xPrvImport :: String -> Maybe XPrvKey
- xPrvWIF :: XPrvKey -> String
- data XPubKey = XPubKey {}
- deriveXPubKey :: XPrvKey -> XPubKey
- xPubIsPrime :: XPubKey -> Bool
- xPubChild :: XPubKey -> Word32
- xPubID :: XPubKey -> Hash160
- xPubFP :: XPubKey -> Word32
- xPubAddr :: XPubKey -> Address
- xPubExport :: XPubKey -> String
- xPubImport :: String -> Maybe XPubKey
- prvSubKey :: XPrvKey -> Word32 -> Maybe XPrvKey
- pubSubKey :: XPubKey -> Word32 -> Maybe XPubKey
- primeSubKey :: XPrvKey -> Word32 -> Maybe XPrvKey
- prvSubKeys :: XPrvKey -> Word32 -> [(XPrvKey, Word32)]
- pubSubKeys :: XPubKey -> Word32 -> [(XPubKey, Word32)]
- primeSubKeys :: XPrvKey -> Word32 -> [(XPrvKey, Word32)]
- mulSigSubKey :: [XPubKey] -> Word32 -> Maybe [XPubKey]
- mulSigSubKeys :: [XPubKey] -> Word32 -> [([XPubKey], Word32)]
- type KeyIndex = Word32
- newtype MasterKey = MasterKey {}
- makeMasterKey :: ByteString -> Maybe MasterKey
- loadMasterKey :: XPrvKey -> Maybe MasterKey
- newtype AccPrvKey = AccPrvKey {}
- newtype AccPubKey = AccPubKey {}
- loadPrvAcc :: XPrvKey -> Maybe AccPrvKey
- loadPubAcc :: XPubKey -> Maybe AccPubKey
- accPrvKey :: MasterKey -> KeyIndex -> Maybe AccPrvKey
- accPubKey :: MasterKey -> KeyIndex -> Maybe AccPubKey
- accPrvKeys :: MasterKey -> KeyIndex -> [(AccPrvKey, KeyIndex)]
- accPubKeys :: MasterKey -> KeyIndex -> [(AccPubKey, KeyIndex)]
- newtype AddrPrvKey = AddrPrvKey {}
- newtype AddrPubKey = AddrPubKey {}
- addr :: AddrPubKey -> Address
- extPrvKey :: AccPrvKey -> KeyIndex -> Maybe AddrPrvKey
- extPubKey :: AccPubKey -> KeyIndex -> Maybe AddrPubKey
- intPrvKey :: AccPrvKey -> KeyIndex -> Maybe AddrPrvKey
- intPubKey :: AccPubKey -> KeyIndex -> Maybe AddrPubKey
- extPrvKeys :: AccPrvKey -> KeyIndex -> [(AddrPrvKey, KeyIndex)]
- extPubKeys :: AccPubKey -> KeyIndex -> [(AddrPubKey, KeyIndex)]
- intPrvKeys :: AccPrvKey -> KeyIndex -> [(AddrPrvKey, KeyIndex)]
- intPubKeys :: AccPubKey -> KeyIndex -> [(AddrPubKey, KeyIndex)]
- extAddr :: AccPubKey -> KeyIndex -> Maybe String
- intAddr :: AccPubKey -> KeyIndex -> Maybe String
- extAddrs :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]
- intAddrs :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]
- extAddrs' :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]
- intAddrs' :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]
- extMulSigKey :: AccPubKey -> [XPubKey] -> KeyIndex -> Maybe [AddrPubKey]
- intMulSigKey :: AccPubKey -> [XPubKey] -> KeyIndex -> Maybe [AddrPubKey]
- extMulSigKeys :: AccPubKey -> [XPubKey] -> KeyIndex -> [([AddrPubKey], KeyIndex)]
- intMulSigKeys :: AccPubKey -> [XPubKey] -> KeyIndex -> [([AddrPubKey], KeyIndex)]
- extMulSigAddr :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> Maybe String
- intMulSigAddr :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> Maybe String
- extMulSigAddrs :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> [(String, KeyIndex)]
- intMulSigAddrs :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> [(String, KeyIndex)]
- buildTx :: [OutPoint] -> [(ScriptOutput, Word64)] -> Either String Tx
- buildAddrTx :: [OutPoint] -> [(String, Word64)] -> Either String Tx
- data SigInput
- signTx :: Monad m => Tx -> [SigInput] -> [PrvKey] -> SecretT (BuildT m) Tx
- detSignTx :: Tx -> [SigInput] -> [PrvKey] -> Build Tx
- isTxComplete :: Tx -> Bool
- data Coin = Coin {}
- chooseCoins :: Word64 -> Word64 -> [Coin] -> Either String ([Coin], Word64)
- chooseMSCoins :: Word64 -> Word64 -> (Int, Int) -> [Coin] -> Either String ([Coin], Word64)
- guessTxSize :: Int -> [(Int, Int)] -> Int -> Int -> Int
Extended Keys
Extended Private Keys
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.
makeXPrvKey :: ByteString -> Maybe XPrvKeySource
Build a BIP32 compatible extended private key from a bytestring. This will produce a root node (depth=0 and parent=0).
xPrvIsPrime :: XPrvKey -> BoolSource
Returns True if the extended private key was derived through a prime derivation.
xPrvChild :: XPrvKey -> Word32Source
Returns the derivation index of this extended private key without the prime bit set.
xPrvExport :: XPrvKey -> StringSource
Exports an extended private key to the BIP32 key export format (base 58).
xPrvImport :: String -> Maybe XPrvKeySource
Decodes a BIP32 encoded extended private key. This function will fail if invalid base 58 characters are detected or if the checksum fails.
Extended Public Keys
Data type representing an extended BIP32 public key.
deriveXPubKey :: XPrvKey -> XPubKeySource
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.
xPubIsPrime :: XPubKey -> BoolSource
Returns True if the extended public key was derived through a prime derivation.
xPubChild :: XPubKey -> Word32Source
Returns the derivation index of this extended public key without the prime bit set.
xPubExport :: XPubKey -> StringSource
Exports an extended public key to the BIP32 key export format (base 58).
xPubImport :: String -> Maybe XPubKeySource
Decodes a BIP32 encoded extended public key. This function will fail if invalid base 58 characters are detected or if the checksum fails.
Child key derivations
:: XPrvKey | Extended parent private key |
-> Word32 | Child derivation index |
-> Maybe XPrvKey | Extended child private key |
Compute a private, non-prime child key derivation. A private non-prime 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/.
Non-prime 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 |
-> Word32 | Child derivation index |
-> Maybe XPubKey | Extended child public key |
Compute a public, non-prime child key derivation. Given a parent key M and a derivation index i, this function will compute M/i/.
:: XPrvKey | Extended Parent private key |
-> Word32 | Child derivation index |
-> Maybe XPrvKey | Extended child private key |
Compute a prime child key derivation. Prime derivations can only be computed for private keys. Prime 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'/.
prvSubKeys :: XPrvKey -> Word32 -> [(XPrvKey, Word32)]Source
Cyclic list of all private non-prime child key derivations of a parent key starting from an offset index.
pubSubKeys :: XPubKey -> Word32 -> [(XPubKey, Word32)]Source
Cyclic list of all public non-prime child key derivations of a parent key starting from an offset index.
primeSubKeys :: XPrvKey -> Word32 -> [(XPrvKey, Word32)]Source
Cyclic list of all prime child key derivations of a parent key starting from an offset index.
Multisig derivations
:: [XPubKey] | List of extended parent public keys |
-> Word32 | Child key derivation index |
-> Maybe [XPubKey] | List of extended child public keys |
Compute a public, non-prime subkey derivation for all of the parent public keys in the input. This function will succeed only if the child key derivations for all the parent keys are valid.
This function is intended to be used in the context of multisignature accounts. Parties exchanging their master public keys to create a multisignature account can then individually generate all the receiving multisignature addresses without further communication.
mulSigSubKeys :: [XPubKey] -> Word32 -> [([XPubKey], Word32)]Source
Cyclic list of all public, non-prime multisig key derivations of a list of parent keys starting from an offset index.
Derivation tree interoperability
To improve BIP32 wallet interoperability, a standard derivation tree is used. All accounts are generated through prime derivations from the master key. This ensures that the master key is not compromised if an account is compromised. Every account will generate receiving addresses from the non-prime subtree index 0 and internal change addresses from the non-prime subtree index 1. MasterKey, AccountKey and AddressKey types are defined to conform to the wallet interoperability format.
Master keys
Data type representing an extended private key at the root of the derivation tree. Master keys have depth 0 and no parents. They are represented as m/ in BIP32 notation.
makeMasterKey :: ByteString -> Maybe MasterKeySource
Create a MasterKey
from a seed.
Account keys
Data type representing a public account key. It is computed through
derivation from an AccPrvKey
. It can not be derived from the MasterKey
directly (property of prime derivation). It is represented as M/i'/ in
BIP32 notation. AccPubKey
is used for generating receiving payment
addresses without the knowledge of the AccPrvKey
.
Address keys
newtype AddrPrvKey Source
Data type representing a private address key. Private address keys are
generated through a non-prime derivation from an AccPrvKey
. Non-prime
derivation is used so that the public account key can generate the receiving
payment addresses without knowledge of the private account key. AccPrvKey
is represented as m/i'/0/j/ in BIP32 notation if it is a regular
receiving address. Internal (change) addresses are represented as
m/i'/1/j/. Non-prime subtree 0 is used for regular receiving addresses
and non-prime subtree 1 for internal (change) addresses.
newtype AddrPubKey Source
Data type representing a public address key. They are generated through
non-prime derivation from an AccPubKey
. This is a useful feature for
read-only wallets. They are represented as M/i'/0/j in BIP32 notation
for regular receiving addresses and by M/i'/1/j for internal (change)
addresses.
addr :: AddrPubKey -> AddressSource
Computes an Address
from an AddrPubKey
.
extPrvKey :: AccPrvKey -> KeyIndex -> Maybe AddrPrvKeySource
Computes an external AddrPrvKey
from an AccPrvKey
and a derivation
index.
extPubKey :: AccPubKey -> KeyIndex -> Maybe AddrPubKeySource
Computes an external AddrPubKey
from an AccPubKey
and a derivation
index.
intPrvKey :: AccPrvKey -> KeyIndex -> Maybe AddrPrvKeySource
Computes an internal AddrPrvKey
from an AccPrvKey
and a derivation
index.
intPubKey :: AccPubKey -> KeyIndex -> Maybe AddrPubKeySource
Computes an internal AddrPubKey
from an AccPubKey
and a derivation
index.
extPrvKeys :: AccPrvKey -> KeyIndex -> [(AddrPrvKey, KeyIndex)]Source
Cyclic list of all valid external AddrPrvKey
derived from a AccPrvKey
and starting from an offset index.
extPubKeys :: AccPubKey -> KeyIndex -> [(AddrPubKey, KeyIndex)]Source
Cyclic list of all valid external AddrPubKey
derived from a AccPubKey
and starting from an offset index.
intPrvKeys :: AccPrvKey -> KeyIndex -> [(AddrPrvKey, KeyIndex)]Source
Cyclic list of all internal AddrPrvKey
derived from a AccPrvKey
and
starting from an offset index.
intPubKeys :: AccPubKey -> KeyIndex -> [(AddrPubKey, KeyIndex)]Source
Cyclic list of all internal AddrPubKey
derived from a AccPubKey
and
starting from an offset index.
extAddr :: AccPubKey -> KeyIndex -> Maybe StringSource
Computes an external base58 address from an AccPubKey
and a
derivation index.
intAddr :: AccPubKey -> KeyIndex -> Maybe StringSource
Computes an internal base58 addres from an AccPubKey
and a
derivation index.
extAddrs :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]Source
Cyclic list of all external base58 addresses derived from a AccPubKey
and starting from an offset index.
intAddrs :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]Source
Cyclic list of all internal base58 addresses derived from a AccPubKey
and starting from an offset index.
extAddrs' :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]Source
Same as extAddrs
with the list reversed.
intAddrs' :: AccPubKey -> KeyIndex -> [(String, KeyIndex)]Source
Same as intAddrs
with the list reversed.
Multisig address keys
extMulSigKey :: AccPubKey -> [XPubKey] -> KeyIndex -> Maybe [AddrPubKey]Source
Computes a list of external AddrPubKey
from an AccPubKey
, a list
of thirdparty multisig keys and a derivation index. This is useful for
computing the public keys associated with a derivation index for
multisig accounts.
intMulSigKey :: AccPubKey -> [XPubKey] -> KeyIndex -> Maybe [AddrPubKey]Source
Computes a list of internal AddrPubKey
from an AccPubKey
, a list
of thirdparty multisig keys and a derivation index. This is useful for
computing the public keys associated with a derivation index for
multisig accounts.
extMulSigKeys :: AccPubKey -> [XPubKey] -> KeyIndex -> [([AddrPubKey], KeyIndex)]Source
Cyclic list of all external multisignature AddrPubKey
derivations
starting from an offset index.
intMulSigKeys :: AccPubKey -> [XPubKey] -> KeyIndex -> [([AddrPubKey], KeyIndex)]Source
Cyclic list of all internal multisignature AddrPubKey
derivations
starting from an offset index.
extMulSigAddr :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> Maybe StringSource
Computes an external base58 multisig address from an AccPubKey
, a
list of thirdparty multisig keys and a derivation index.
intMulSigAddr :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> Maybe StringSource
Computes an internal base58 multisig address from an AccPubKey
, a
list of thirdparty multisig keys and a derivation index.
extMulSigAddrs :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> [(String, KeyIndex)]Source
Cyclic list of all external base58 multisig addresses derived from
an AccPubKey
and a list of thirdparty multisig keys. The list starts
at an offset index.
intMulSigAddrs :: AccPubKey -> [XPubKey] -> Int -> KeyIndex -> [(String, KeyIndex)]Source
Cyclic list of all internal base58 multisig addresses derived from
an AccPubKey
and a list of thirdparty multisig keys. The list starts
at an offset index.
Build Transactions
buildTx :: [OutPoint] -> [(ScriptOutput, Word64)] -> Either String TxSource
Build a transaction by providing a list of outpoints as inputs
and a list of ScriptOutput
and amounts as outputs.
buildAddrTx :: [OutPoint] -> [(String, Word64)] -> Either String TxSource
Build a transaction by providing a list of outpoints as inputs and a list of recipients addresses and amounts as outputs.
Transaction signing
Data type used to specify the signing parameters of a transaction input. To sign an input, the previous output script, outpoint and sighash are required. When signing a pay to script hash output, an additional redeem script is required.
SigInput | Parameters for signing a pay to public key hash output. |
SigInputSH | Parameters for signing a pay to script hash output. |
isTxComplete :: Tx -> BoolSource
Returns True if all the inputs of a transactions are non-empty and if all multisignature inputs are fully signed.
Coin selection
A Coin is something that can be spent by a transaction and is represented by a transaction output, an outpoint and optionally a redeem script.
Coin | |
|
:: Word64 | Target price to pay. |
-> Word64 | Fee price per 1000 bytes. |
-> [Coin] | List of coins to choose from. |
-> Either String ([Coin], Word64) | Coin selection result and change amount. |
Coin selection algorithm for normal (non-multisig) transactions. This function returns the selected coins together with the amount of change to send back to yourself, taking the fee into account.
:: Word64 | Target price to pay. |
-> Word64 | Fee price per 1000 bytes. |
-> (Int, Int) | Multisig parameters m of n (m,n). |
-> [Coin] | List of coins to choose from. |
-> Either String ([Coin], Word64) | Coin selection result and change amount. |
Coin selection algorithm for multisignature transactions. This function returns the selected coins together with the amount of change to send back to yourself, taking the fee into account. This function assumes all the coins are script hash outputs that send funds to a multisignature address.
:: Int | Number of regular transaction inputs. |
-> [(Int, Int)] | For every multisig input in the transaction, provide the multisig parameters m of n (m,n) for that input. |
-> Int | Number of pay to public key hash outputs. |
-> Int | Number of pay to script hash outputs. |
-> Int | Upper bound on the transaction size. |
Computes an upper bound on the size of a transaction based on some known properties of the transaction.