def getutxos(self, addresslist, utxos_per_address=2): """ Dummy method to retreive UTXO's. This method creates a new UTXO for each address provided out of the testnet void, which can be used to create test transactions for the bitcoinlib testnet. :param addresslist: List of addresses :type addresslist: list :param utxos_per_address: Number of UTXO's to be created per address :type utxos_per_address: int :return list: The created UTXO set """ utxos = [] for n in range(utxos_per_address): for address in addresslist: pkh = str(n).encode() + addr_to_pubkeyhash(address)[1:] utxos.append( { 'address': address, 'tx_hash': hashlib.sha256(pkh).hexdigest(), 'confirmations': 10, 'output_n': 0, 'index': 0, 'value': 1 * self.units, 'script': '', } ) return utxos
def getutxos(self, address, after_txid='', max_txs=10, utxos_per_address=2): """ Dummy method to retreive UTXO's. This method creates a new UTXO for each address provided out of the testnet void, which can be used to create test transactions for the bitcoinlib testnet. :param address: Address string :type address: str :param after_txid: Transaction ID of last known transaction. Only check for utxos after given tx id. Default: Leave empty to return all utxos. If used only provide a single address :type after_txid: str :param max_txs: Maximum number of utxo's to return :type max_txs: int :return list: The created UTXO set """ utxos = [] for n in range(utxos_per_address): try: pkh = str(n).encode() + addr_to_pubkeyhash(address)[1:] except: pkh = str(n).encode() + addr_bech32_to_pubkeyhash(address)[1:] utxos.append( { 'address': address, 'tx_hash': hashlib.sha256(pkh).hexdigest(), 'confirmations': 10, 'output_n': 0, 'index': 0, 'value': 1 * self.units, 'script': '', } ) return utxos
def getutxos(self, addresslist, utxos_per_address=2): utxos = [] for n in range(utxos_per_address): for address in addresslist: pkh = str(n).encode() + addr_to_pubkeyhash(address)[1:] utxos.append({ 'address': address, 'tx_hash': hashlib.sha256(pkh).hexdigest(), 'confirmations': 10, 'output_n': 0, 'index': 0, 'value': 1 * self.units, 'script': '', }) return utxos
def getutxos(self, addresslist): utxos = [] for address in addresslist: utxos.append({ 'address': address, 'tx_hash': hashlib.sha256( addr_to_pubkeyhash( '21DBmFUMQMP7A6KeENXgZQ4wJdSCeGc2zFo')).hexdigest(), 'confirmations': 10, 'output_n': 0, 'index': 0, 'value': 1 * self.units, 'script': '', }) return utxos
def _get_tx_hash(self, address, n): try: pkh = str(n).encode() + addr_to_pubkeyhash(address)[1:] except Exception: pkh = str(n).encode() + addr_bech32_to_pubkeyhash(address)[1:] return hashlib.sha256(pkh).hexdigest()
def test_address_to_pkh_bech32(self): addr = 'bc1qy8qmc6262m68ny0ftlexs4h9paud8sgce3sf84' self.assertEqual(addr_to_pubkeyhash(addr), b'!\xc1\xbciZV\xf4y\x91\xe9_\xf2hV\xe5\x0fx\xd3\xc1\x18') self.assertEqual(addr_to_pubkeyhash(addr, as_hex=True), '21c1bc695a56f47991e95ff26856e50f78d3c118')
def test_address_to_pkh_conversion_3(self): self.assertEqual(b'\xFF' * 20, addr_to_pubkeyhash('1QLbz7JHiBTspS962RLKV8GndWFwi5j6Qr', False))
def test_address_to_pkh_conversion_2(self): self.assertEqual('00' * 20, addr_to_pubkeyhash('1111111111111111111114oLvT2', True))
def test_address_to_pkh_conversion_1(self): self.assertEqual('13d215d212cd5188ae02c5635faabdc4d7d4ec91', addr_to_pubkeyhash('12ooWd8Xag7hsgP9PBPnmyGe36VeUrpMSH', True))
] print("\n=== Change base: convert from base N to base M ===") for example in examples: print("\n>>> change_base%s # Change from base%d to base%d" % (example, example[1], example[2])) print("%s" % change_base(*example)) # # Address and Script conversion examples # print("\n=== Conversion of Bitcoin Addresses to Public Key Hashes ===") addrs = ['12ooWd8Xag7hsgP9PBPnmyGe36VeUrpMSH', '1111111111111111111114oLvT2', '1QLbz7JHiBTspS962RLKV8GndWFwi5j6Qr'] for addr in addrs: print("Public Key Hash of address '%s' is '%s'" % (addr, addr_to_pubkeyhash(addr, True))) print("\n=== From Public Key Hashes to address ===") print(pubkeyhash_to_addr('13d215d212cd5188ae02c5635faabdc4d7d4ec91')) print(pubkeyhash_to_addr('00' * 20)) print("\n=== Create PKH from redeemscript ===") redeemscript = '5221023dd6aeaa2acb92cbea35820361e5fd07af10f4b01c985adec30848b424756a6c210381cd2bb2a38d939fa677a5dcc' \ '981ee0630b32b956b2e6dc3e1c028e6d09db5a72103d2c6d31cabe4025c25879010465f501194b352823c553660d303adfa' \ '9a26ad3c53ae' print(to_hexstring(hash160(to_bytes(redeemscript)))) # # Other type conversions and normalizations #