Skip to content

Commit

Permalink
Add example connect to bitcoin core node
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Apr 7, 2024
1 parent 8595801 commit 482e9bc
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 38 deletions.
1 change: 1 addition & 0 deletions bitcoinlib/services/baseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, network, provider, base_url, denominator, api_key='', provide
if not isinstance(network, Network):
self.network = Network(network)
self.provider = provider

self.base_url = base_url
self.resp = None
self.units = denominator
Expand Down
2 changes: 1 addition & 1 deletion bitcoinlib/services/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def gettransactions(self, address, after_txid='', limit=MAX_TRANSACTIONS):
if len(txs_list) >= MAX_WALLET_TRANSACTIONS:
raise ClientError("Bitcoind wallet contains too many transactions %d, use other service provider for this "
"wallet" % MAX_WALLET_TRANSACTIONS)
txids = list(set([(tx['txid'], tx['blockheight']) for tx in txs_list if tx['address'] == address]))
txids = list(set([(tx['txid'], tx.get('blockheight')) for tx in txs_list if tx['address'] == address]))
for (txid, blockheight) in txids:
tx_raw = self.proxy.getrawtransaction(txid, 1)
t = self._parse_transaction(tx_raw, blockheight)
Expand Down
37 changes: 0 additions & 37 deletions examples/wallet_bitcoind.py

This file was deleted.

66 changes: 66 additions & 0 deletions examples/wallet_bitcoind_connected_wallets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
#
# BitcoinLib - Python Cryptocurrency Library
#
# EXAMPLES - Using Bitcoin Core wallets with Bitcoinlib
#
# Method 1 - Create wallet in Bitcoin Core and use the same wallet in Bitcoinlib using the bitcoin node to
# receive and send bitcoin transactions.
#
# © 2024 April - 1200 Web Development <http://1200wd.com/>
#

from bitcoinlib.wallets import *
from bitcoinlib.services.bitcoind import BitcoindClient

#
# Settings and Initialization
#

# Call bitcoin-cli dumpwallet and look for extended private masterkey at top off the export. Then copy the
# bitcoin core node masterseed here:
pkwif = 'tprv8ZgxMBicQKsPe2iVrERVdAgjcqHhxvZcWeS2Va6nvgddpDH1r33A4aTtdYYkoFDY6CCf5fogwLYmAdQQNxkk7W3ygwFd6hquJVLmmpbJRp2'
enable_verify_wallet = False

# 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 a copy of the Bitcoin Core Wallet in Bitcoinlib
#
w = wallet_create_or_open('wallet_bitcoincore', pkwif, network='testnet', witness_type='segwit',
key_path=KEY_PATH_BITCOINCORE)
addr = bcc.proxy.getnewaddress()
addrinfo = bcc.proxy.getaddressinfo(addr)
bcl_addr = w.key_for_path(addrinfo['hdkeypath']).address

# Verify if we are using the same wallet
if enable_verify_wallet and addr == bcl_addr:
print("Address %s with path %s, is identical in Bitcoin core and Bitcoinlib" % (addr, addrinfo['hdkeypath']))
elif not addr == bcl_addr:
print ("Address %s with path %s, is NOT identical in Bitcoin core and Bitcoinlib" % (addr, addrinfo['hdkeypath']))
raise ValueError("Wallets not identical in Bitcoin core and Bitcoinlib")

#
# Using wallets
#

# Now pick an address from your wallet and send some testnet coins to it, for example by using another wallet or a
# testnet faucet.
w.providers = ['bitcoind']
w.scan()
# w.info()

if not w.balance():
print("No testnet coins available")
else:
print("Found testnet coins. Wallet balance: %d" % w.balance())
# Send some coins to our own wallet
t = w.send_to(w.get_key().address, 1000, fee=200, offline=False)
t.info()

# If you now run bitcoin-cli listunspent 0, you should see the 1 or 2 new utxo's for this transaction.
27 changes: 27 additions & 0 deletions examples/wallet_bitcoind_connected_wallets2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
#
# BitcoinLib - Python Cryptocurrency Library
#
# EXAMPLES - Using Bitcoin Core wallets with Bitcoinlib
#
# Method 2 - ...
#
# © 2024 April - 1200 Web Development <http://1200wd.com/>
#

from bitcoinlib.wallets import *
from bitcoinlib.services.bitcoind import BitcoindClient

#
# Settings and Initialization
#

pkwif = 'cTAyLb37Sr4XQPzWCiwihJxdFpkLKeJBFeSnd5hwNiW8aqrbsZCd'

w = wallet_create_or_open("wallet_bitcoincore2", keys=pkwif, network='testnet', witness_type='segwit',
key_path=KEY_PATH_BITCOINCORE)
w.providers=['bitcoind']
w.get_key()
w.scan(scan_gap_limit=1)
w.info()

0 comments on commit 482e9bc

Please sign in to comment.