pyhdwallet package

Submodules

pyhdwallet.ecpair module

Elliptic Curve Cryptography module

class pyhdwallet.ecpair.ECPair(privkey, pubkey_buffer=None, compressed=True, network=<pyhdwallet.networks.Network object>)

Bases: object

Elliptic Curve Cryptography key pair

property compressed

Returns whether or not the public key is in compressed format.

Returns

True for compressed; False otherwise

classmethod from_wif(wif)

Imports the private key from WIF format.

Parameters

wif – private key as WIF (Wallet Import Format)

Returns

New object containing the imported private key

get_address()

Converts the public key to a bitcoin address (P2PKH address)

Returns

Address as string (P2PKH address)

property network

Returns the network associated with this key pair.

Returns

Network object

property privkey

Returns the private key as a 256 bit integer.

Returns

256 bit int secret

property privkey_buffer

Returns the private key as bytes.

Returns

bytes object representing the private key

property pubkey_buffer

Returns the public key (33 or 65 bytes)

Returns

bytes object representing the public key

sign(hash_buffer)

Sign a 32 byte hash and returns a signature

Parameters

buffer – 32 byte buffer (as bytes)

Returns

ECSignature object

to_wif()

Exports the private key as WIF (Wallet Import Format)

Returns

string corresponding to the private Key as WIF

verify(buffer, ec_signature)

Verify signature of a 32 byte buffer (as bytes)

Parameters
  • buffer – 32 byte buffer (as bytes)

  • ec_signature – ECSignature object

Returns

True if this signature is valid

pyhdwallet.ecutils module

Low level Elliptic Curve Functions

class pyhdwallet.ecutils.ECSignature(r, s)

Bases: object

EC Signature

classmethod sign(secret, hash_buffer)

Sign a message (hash) with the provided private key and returns the signature.

Parameters
  • secret – private key as 32-byte int

  • hash_buffer – Hash of the message as bytes

Returns

ECSignature object

verify(pubkey_buffer, hash_buffer)

Verify a digital signature.

Parameters
  • pubkey_buffer – Public key as bytes

  • hash_buffer – hash of the message

Returns

True if this signature is valid

pyhdwallet.ecutils.combine_pubkeys(secret, pubkey_buffer)

Combines the public keys.

Parameters
  • secret – private key (32-bytes int)

  • pubkey_buffer – public key compressed

Returns

bytes of compressed public key

pyhdwallet.ecutils.get_pubkey_from_privkey(secret, compressed=True)

Returns a compressed public key from a private key.

Parameters
  • secret – private key (32-bytes int)

  • compressed – get a compressed public key if true

Returns

public key as bytes

pyhdwallet.ecutils.is_compressed_key(pubkey_buffer)

Checks whether or not a public key is compressed.

Parameters

pubkey_buffer – public keys as bytes obj.

Returns

true if public key is compressed; false otherwise

pyhdwallet.hashutils module

Hash functions

pyhdwallet.hashutils.hash160(data)

RIPEMD-160 after SHA-256.

Parameters

data

Returns

ripemd160(sha256(data))

pyhdwallet.hashutils.hmac_sha512(key, msg)

HMAC using SHA-512.

Parameters
  • key

  • msg

Returns

pyhdwallet.hashutils.ripemd160(data)

RIPEMD-160 hash function.

Parameters

data

Returns

pyhdwallet.hashutils.sha256(data)

SHA-256 hash function.

Parameters

data

Returns

pyhdwallet.hdnode module

Module to deal with Hierarchical Deterministic (HD) tree according to BIP32 specification (https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)

class pyhdwallet.hdnode.HDNode(keypair, chaincode, depth=0, index=0, parent_fingerprint=0)

Bases: object

A node from Hierarchical Deterministic (HD) tree.

Each node has extended keys allowing derivation of children nodes

derive(index)

Child Extended Key Derivation. Given the parent extended key and an index, computes the corresponding child extended key.

Parameters

index – index for derivation

Returns

HDNode child

derive_hardened(index)

Child Extended Key Derivation. (hardened version) Given the parent extended key and an index, computes the corresponding child extended key.

Parameters

index – index for derivation

Returns

HDNode child

derive_path(path)

Child Extended Key Derivation. Given the derivation path in the format m/x/x’ (e.g. m/0/1’/0) computes the corresponding child extended key.

Parameters

index – derivation path as string (e.g. m/0/1’/0)

Returns

HDNode child

classmethod from_base58(encoded)

Creates a new HDNode from a extended key (xpub/xpriv)

Parameters

encoded – a base58check string

Returns

a new HDNode object

classmethod from_seed(seed_bytes, network=<pyhdwallet.networks.Network object>)

Creates a new HDNode from a bip39 seed

Parameters
  • seed_bytes – binary bip39 seed

  • network – Network object

Returns

new HDNode object

get_address()

Returns a P2PKH address

Returns

Address as string (P2PKH address)

get_fingerprint()

Returns the fingerprint. The first 32 bits of the identifier are called the key fingerprint.

Returns

the fingerprint

get_identifier()

Returns the identifier. Extended keys can be identified by the Hash160 (RIPEMD160 after SHA256) of the serialized ECDSA public key K.

Returns

identifier

get_keypair()

Returns the keypair

is_neutered()

Returns true if this object is neutered.

Returns

true if this object is neutered; false otherwise

neutered()

Returns a new node without the private key. (Removes the privkey)

Returns

a neutered HDNode

to_base58()

Returns the extended key (xpriv or xpub) as a Base58Check string. (xpub if neutered; xpriv otherwise)

Returns

Extended key as Base58Check string

pyhdwallet.networks module

Cryptocurrency network definitions

class pyhdwallet.networks.Network(description, version_priv, version_pub, pub_key_hash, wif)

Bases: object

Represents a cryptocurrency network (e.g. Bitcoin Mainnet)

classmethod get_supported_networks()

Returns the list of supported networks

Returns

list of supported networks

classmethod set_supported_networks(network_list)

Sets up the list of supported networks

Parameters

network_list – New list of supported networks

Module contents

Hierarchical Deterministic Wallets (BIP32) in python