Skip to content

Commit

Permalink
Define standard key paths in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Apr 7, 2024
1 parent 70d35db commit 8595801
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
17 changes: 11 additions & 6 deletions bitcoinlib/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
ENCODING_BECH32_PREFIXES = ['bc', 'tb', 'ltc', 'tltc', 'blt']
DEFAULT_WITNESS_TYPE = 'segwit'
BECH32M_CONST = 0x2bc830a3
KEY_PATH_LEGACY = ["m", "purpose'", "coin_type'", "account'", "change", "address_index"]
KEY_PATH_P2SH = ["m", "purpose'", "cosigner_index", "change", "address_index"]
KEY_PATH_P2WSH = ["m", "purpose'", "coin_type'", "account'", "script_type'", "change", "address_index"]
KEY_PATH_P2WPKH = ["m", "purpose'", "coin_type'", "account'", "change", "address_index"]
KEY_PATH_BITCOINCORE = ['m', "account'", "change'", "address_index'"]

# Wallets
WALLET_KEY_STRUCTURES = [
Expand All @@ -176,7 +181,7 @@
'multisig': False,
'encoding': 'base58',
'description': 'Legacy wallet using pay-to-public-key-hash scripts',
'key_path': ["m", "purpose'", "coin_type'", "account'", "change", "address_index"]
'key_path': KEY_PATH_LEGACY
},
{
'purpose': 45,
Expand All @@ -185,7 +190,7 @@
'multisig': True,
'encoding': 'base58',
'description': 'Legacy multisig wallet using pay-to-script-hash scripts',
'key_path': ["m", "purpose'", "cosigner_index", "change", "address_index"]
'key_path': KEY_PATH_P2SH
},
{
'purpose': 48,
Expand All @@ -194,7 +199,7 @@
'multisig': True,
'encoding': 'base58',
'description': 'Segwit multisig wallet using pay-to-wallet-script-hash scripts nested in p2sh scripts',
'key_path': ["m", "purpose'", "coin_type'", "account'", "script_type'", "change", "address_index"]
'key_path': KEY_PATH_P2WSH
},
{
'purpose': 48,
Expand All @@ -203,7 +208,7 @@
'multisig': True,
'encoding': 'bech32',
'description': 'Segwit multisig wallet using native segwit pay-to-wallet-script-hash scripts',
'key_path': ["m", "purpose'", "coin_type'", "account'", "script_type'", "change", "address_index"]
'key_path': KEY_PATH_P2WSH
},
{
'purpose': 49,
Expand All @@ -212,7 +217,7 @@
'multisig': False,
'encoding': 'base58',
'description': 'Segwit wallet using pay-to-wallet-public-key-hash scripts nested in p2sh scripts',
'key_path': ["m", "purpose'", "coin_type'", "account'", "change", "address_index"]
'key_path': KEY_PATH_P2WPKH
},
{
'purpose': 84,
Expand All @@ -221,7 +226,7 @@
'multisig': False,
'encoding': 'bech32',
'description': 'Segwit multisig wallet using native segwit pay-to-wallet-public-key-hash scripts',
'key_path': ["m", "purpose'", "coin_type'", "account'", "change", "address_index"]
'key_path': KEY_PATH_P2WPKH
},
# {
# 'purpose': 86,
Expand Down
5 changes: 0 additions & 5 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2335,11 +2335,6 @@ def keys_for_path(self, path, level_offset=None, name=None, account_id=None, cos
change_pos = [self.key_path.index(chg) for chg in ["change", "change'"] if chg in self.key_path]
change = None if not change_pos or change_pos[0] >= len(fullpath) else (
int(fullpath[change_pos[0]].strip("'")))
# if "change" not in self.key_path or self.key_path.index("change") >= len(fullpath) \
# else int(fullpath[self.key_path.index("change")])
# if change is None:
# change = None if "change'" not in self.key_path or self.key_path.index("change'") >= len(fullpath) \
# else int(fullpath[self.key_path.index("change'")].strip("'"))
if name and len(fullpath) == len(newpath.split('/')):
key_name = name
else:
Expand Down
37 changes: 37 additions & 0 deletions examples/wallet_bitcoind.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
#
# BitcoinLib - Python Cryptocurrency Library
#
# EXAMPLES - Using Bitcoin Core wallets with Bitcoinlib
#
# © 2024 April - 1200 Web Development <http://1200wd.com/>
#

import os
from bitcoinlib.wallets import wallet_create_or_open
from bitcoinlib.services.bitcoind import BitcoindClient

#
# Settings and Initialization
#

# Generate your own private key with: HDKey(network='testnet').wif_private()
pkwif = 'vprv9DMUxX4ShgxMKxHYfZ7Z35RxRLC9Av59MyJaMmCFRqfvUdUZdBB1awTDvTfDzZbtsPzZVCcpCunaELcuPsnLeLMg634hsxSSvwpTdfgCYMX'
# Put connection string with format http://bitcoinlib:password@localhost:18332)
# to Bitcoin Core node in the following file:
bitcoind_url = open(os.path.join(os.path.expanduser('~'), ".bitcoinlib/.bitcoind_connection_string")).read()
bcc = BitcoindClient(base_url=bitcoind_url)
lastblock = bcc.proxy.getblockcount()
print("Connected to bitcoind, last block: " + str(lastblock))

#
# Create Wallets
#




#
# Using wallets
#

0 comments on commit 8595801

Please sign in to comment.