Wallet¶
The implementation of Hierarchical Deterministic (HD) wallets generator for Bitcoin blockchain.
-
class
swap.providers.bitcoin.wallet.
Wallet
(network: str = 'mainnet')¶ Bitcoin Wallet class.
- Parameters
network (str) – Bitcoin network, defaults to mainnet.
- Returns
Wallet – Bitcoin wallet instance.
Note
Bitcoin has only two networks,
mainnet
andmainnet
.-
from_entropy
(entropy: str, passphrase: str = None, language: str = 'english') → swap.providers.bitcoin.wallet.Wallet¶ Initialize wallet from entropy.
- Parameters
entropy (str) – Bitcoin wallet entropy.
passphrase (str) – Bitcoin wallet passphrase, default to None.
language (str) – Bitcoin wallet language, default to english.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_mnemonic
(mnemonic: str, passphrase: str = None, language: str = None) → swap.providers.bitcoin.wallet.Wallet¶ Initialize wallet from mnemonic.
- Parameters
mnemonic (str) – Bitcoin wallet mnemonic.
passphrase (str) – Bitcoin wallet passphrase, default to None.
language (str) – Bitcoin wallet language, default to english.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_mnemonic("extend length miss suit broken rescue around harbor vehicle vicious jelly quality") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_seed
(seed: str) → swap.providers.bitcoin.wallet.Wallet¶ Initialize wallet from seed.
- Parameters
seed (str) – Bitcoin wallet seed.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_seed("51a0f6fb9abd5e5aa27f42dd375d8e4fc6944c704c859454e557fc419d3979e5a50273743c93e5035244adb09e9a37914abc583fdfae0da1ae2bedaa373f050e") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_root_xprivate_key
(root_xprivate_key: str) → swap.providers.bitcoin.wallet.Wallet¶ Initialize wallet from root xprivate key.
- Parameters
root_xprivate_key (str) – Bitcoin wallet root xprivate key.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_root_xprivate_key("xprv9s21ZrQH143K4QXLfi9Ht3fz7CciYxE2MuTdNxvDs8kRQRyPByvJLRSvfNBa3kh6twMksiJtZuyT2Cor7aLAAag3f7TPpnnjBADARWmrJmx") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_xprivate_key
(xprivate_key: str) → swap.providers.bitcoin.wallet.Wallet¶ Initialize wallet from xprivate key.
- Parameters
xprivate_key (str) – Bitcoin wallet xprivate key.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_xprivate_key("xprvA2WJkML27XMaNmtrsuVcvCCXTR5LiBKAwkV6LapBFmw7eGFkHHzVa57gPRjBToTnvr2PpkN4s1reiDW6Ay9yXxi8WaVskDXRwzZLxiPBQfL") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_wif
(wif: str) → swap.providers.bitcoin.wallet.Wallet¶ Initialize wallet from wallet important format (WIF).
- Parameters
wif (str) – Bitcoin wallet important format.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_wif("L4p5duRK9PZVP22rPLTZ8Zar77JQ1Pc6dz3Js5drL89wPRH1kz6R") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_private_key
(private_key) → swap.providers.bitcoin.wallet.Wallet¶ Initialize wallet from private key.
- Parameters
private_key (str) – Bitcoin wallet private key.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_private_key("e28afe15f98501502fac7da75939d41a0c8d074aeb76d0131f5a5c5ce3132a79") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_path
(path: str) → swap.providers.bitcoin.wallet.Wallet¶ Drive Bitcoin wallet from path.
- Parameters
path (str) – Bitcoin wallet path.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
from_index
(index: int, harden: bool = False) → swap.providers.bitcoin.wallet.Wallet¶ Drive Bitcoin wallet from index.
- Parameters
index (int) – Bitcoin wallet index.
harden (bool) – Use harden, default to False.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_index(44, harden=True) >>> wallet.from_index(0, harden=True) >>> wallet.from_index(0, harden=True) >>> wallet.from_index(0) >>> wallet.from_index(0) <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268>
-
clean_derivation
() → swap.providers.bitcoin.wallet.Wallet¶ Clean derivation Bitcoin wallet.
- Returns
Wallet – Bitcoin wallet instance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.path() "m/44'/0'/0'/0/0" >>> wallet.clean_derivation() <swap.providers.bitcoin.wallet.Wallet object at 0x040DA268> >>> wallet.path() None
-
entropy
() → Optional[str]¶ Get Bitcoin wallet entropy.
- Returns
str – Bitcoin wallet entropy.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.entropy() "50f002376c81c96e430b48f1fe71df57"
-
mnemonic
() → Optional[str]¶ Get Bitcoin wallet mnemonic.
- Returns
str – Bitcoin wallet mnemonic.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.mnemonic() "extend length miss suit broken rescue around harbor vehicle vicious jelly quality"
-
passphrase
() → Optional[str]¶ Get Bitcoin wallet passphrase.
- Returns
str – Bitcoin wallet passphrase.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57", passphrase="meherett") >>> wallet.passphrase() "meherett"
-
language
() → Optional[str]¶ Get Bitcoin wallet language.
- Returns
str – Bitcoin wallet language.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.language() "english"
-
seed
() → Optional[str]¶ Get Bitcoin wallet seed.
- Returns
str – Bitcoin wallet seed.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.seed() "51a0f6fb9abd5e5aa27f42dd375d8e4fc6944c704c859454e557fc419d3979e5a50273743c93e5035244adb09e9a37914abc583fdfae0da1ae2bedaa373f050e"
-
root_xprivate_key
(encoded: bool = True) → Optional[str]¶ Get Bitcoin wallet root xprivate key.
- Parameters
encoded (bool) – Encoded root xprivate key, default to True.
- Returns
str – Bitcoin wallet root xprivate key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.root_xprivate_key() "xprv9s21ZrQH143K4QXLfi9Ht3fz7CciYxE2MuTdNxvDs8kRQRyPByvJLRSvfNBa3kh6twMksiJtZuyT2Cor7aLAAag3f7TPpnnjBADARWmrJmx"
-
root_xpublic_key
(encoded: bool = True) → Optional[str]¶ Get Bitcoin wallet root xpublic key.
- Parameters
encoded (bool) – Encoded root xprivate key, default to True.
- Returns
str – Bitcoin wallet root xpublic key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.root_xpublic_key() "xpub661MyMwAqRbcGtbomjgJFBcifETCxQwsj8PEBMKqRUHQHEJXjXEYtDmQWdoFYwDCpQWXhUt7Ce6D34r9gq7osUQR5RpUWSHjTWMCEHYZQ48"
-
xprivate_key
(encoded=True) → Optional[str]¶ Get Bitcoin wallet xprivate key.
- Parameters
encoded (bool) – Encoded xprivate key, default to True.
- Returns
str – Bitcoin wallet xprivate key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.xprivate_key() "xprvA2WJkML27XMaNmtrsuVcvCCXTR5LiBKAwkV6LapBFmw7eGFkHHzVa57gPRjBToTnvr2PpkN4s1reiDW6Ay9yXxi8WaVskDXRwzZLxiPBQfL"
-
xpublic_key
(encoded: bool = True) → Optional[str]¶ Get Bitcoin wallet xpublic key.
- Parameters
encoded (bool) – Encoded xprivate key, default to True.
- Returns
str – Bitcoin wallet xpublic key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.xpublic_key() "xpub6FVf9rruwtusbFyKyw2dHL9G1Suq7e32JyQh8yDnp7U6X4atpqJk7sSAEgfR45VFNs64tsRF67XnGFjuHER3SbTrzWEhzwkuAhKcHbpxb44"
-
uncompressed
() → str¶ Get Bitcoin wallet uncompressed public key.
- Returns
str – Bitcoin wallet uncompressed public key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.uncompressed() "d5fb6799738d146d7558ac0b14c74cc66f879bd846231b64296fb7cc7c9d974fc6e73408a18811a99906fd10f6a8c1ec401f034ae49ca4a6e8c09275c5ae44b6"
-
compressed
() → str¶ Get Bitcoin wallet compressed public key.
- Returns
str – Bitcoin wallet compressed public key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.compressed() "02d5fb6799738d146d7558ac0b14c74cc66f879bd846231b64296fb7cc7c9d974f"
-
chain_code
() → str¶ Get Bitcoin wallet chain code.
- Returns
str – Bitcoin wallet chain code.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.chain_code() "baa85729cc8400fd0321ec6df70e7f976a601b133c1aae91a5ec2638fa748017"
-
private_key
() → str¶ Get Bitcoin wallet private key.
- Returns
str – Bitcoin wallet private key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.private_key() "e28afe15f98501502fac7da75939d41a0c8d074aeb76d0131f5a5c5ce3132a79"
-
public_key
(private_key: str = None) → str¶ Get Bitcoin wallet public key.
- Returns
str – Bitcoin wallet public key.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.public_key() "02d5fb6799738d146d7558ac0b14c74cc66f879bd846231b64296fb7cc7c9d974f"
-
path
() → Optional[str]¶ Get Bitcoin wallet path.
- Returns
str – Bitcoin wallet path.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.path() "m/44'/0'/0'/0/0"
-
address
() → str¶ Get Bitcoin wallet address.
- Returns
str – Bitcoin wallet address.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.address() "18Ac1AiZuNU7ywC1qP6Ref3hGbdRM74Rxv"
-
wif
() → str¶ Get Bitcoin wallet important format (WIF).
- Returns
str – Bitcoin wallet important format.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.wif() "L4p5duRK9PZVP22rPLTZ8Zar77JQ1Pc6dz3Js5drL89wPRH1kz6R"
-
hash
() → str¶ Get Bitcoin wallet public key/address hash.
- Returns
str – Bitcoin wallet public key/address hash.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.hash() "4e99d7d43ebb41a620426aa836dbd4c4fa85667e"
-
p2pkh
() → str¶ Get Bitcoin wallet public key/address p2pkh.
- Returns
str – Bitcoin wallet public key/address p2pkh.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.p2pkh() "76a9144e99d7d43ebb41a620426aa836dbd4c4fa85667e88ac"
-
balance
() → int¶ Get Bitcoin wallet balance.
- Returns
int – Bitcoin wallet balance.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.balance() 1000000
-
utxos
(limit: int = 15) → list¶ Get Bitcoin wallet unspent transaction output (UTXO’s).
- Parameters
limit (int) – Bitcoin balance, default is 15.
- Returns
list – Bitcoin unspent transaction outputs.
>>> from swap.providers.bitcoin.wallet import Wallet >>> wallet = Wallet(network="mainnet") >>> wallet.from_entropy("50f002376c81c96e430b48f1fe71df57") >>> wallet.from_path("m/44'/0'/0'/0/0") >>> wallet.utxos() [{'index': 0, 'hash': 'be346626628199608926792d775381e54d8632c14b3ce702f90639481722392c', 'output_index': 1, 'amount': 12340, 'script': '76a9146bce65e58a50b97989930e9a4ff1ac1a77515ef188ac'}]