def main(): # We need a wallet, but don't need a network. Set peer_goal to 0 spv = pyspv.pyspv('pyspv-simple-wallet', logging_level=pyspv.INFO, peer_goal=0, listen=None) tx, _ = pyspv.transaction.Transaction.unserialize( pyspv.hexstring_to_bytes(sys.argv[1], reverse=False), spv.coin) input_count = len(tx.inputs) total_added = 0 for spend_hash in sys.argv[2:]: spend_hash = pyspv.hexstring_to_bytes(spend_hash) spend = spv.wallet.spends[spend_hash]['spend'] for input_creator in spend.create_input_creators( spv, pyspv.transaction.Transaction.SIGHASH_ALL | pyspv.transaction.Transaction.SIGHASH_ANYONECANPAY): tx.inputs.append( pyspv.transaction.UnsignedTransactionInput(input_creator)) total_added += spend.amount for i, unsigned_input in enumerate(tx.inputs): if isinstance(unsigned_input, pyspv.transaction.UnsignedTransactionInput): tx.inputs[i] = unsigned_input.sign(tx, i) print(pyspv.bytes_to_hexstring(tx.serialize(), reverse=False)) print("total added as inputs: {}".format( spv.coin.format_money(total_added))) spv.shutdown() # Async shutdown spv.join() # Wait for shutdown to complete
def main(): # We need a wallet, but don't need a network. Set peer_goal to 0 spv = pyspv.pyspv('pyspv-simple-wallet', logging_level=pyspv.INFO, peer_goal=0, listen=None) tx, _ = pyspv.transaction.Transaction.unserialize(pyspv.hexstring_to_bytes(sys.argv[1], reverse=False), spv.coin) input_count = len(tx.inputs) total_added = 0 for spend_hash in sys.argv[2:]: spend_hash = pyspv.hexstring_to_bytes(spend_hash) spend = spv.wallet.spends[spend_hash]['spend'] for input_creator in spend.create_input_creators(spv, pyspv.transaction.Transaction.SIGHASH_ALL | pyspv.transaction.Transaction.SIGHASH_ANYONECANPAY): tx.inputs.append(pyspv.transaction.UnsignedTransactionInput(input_creator)) total_added += spend.amount for i, unsigned_input in enumerate(tx.inputs): if isinstance(unsigned_input, pyspv.transaction.UnsignedTransactionInput): tx.inputs[i] = unsigned_input.sign(tx, i) print(pyspv.bytes_to_hexstring(tx.serialize(), reverse=False)) print("total added as inputs: {}".format(spv.coin.format_money(total_added))) spv.shutdown() # Async shutdown spv.join() # Wait for shutdown to complete
class BitcoinSHA256(unittest.TestCase): vectors = [ (b'abc', hexstring_to_bytes( '4f8b42c22dd3729b519ba6f68d2da7cc5b2d606d05daed5ad5128cc03e6c6358', False)), (b'crazy horse battery staple', hexstring_to_bytes( '83e8d1654ddd49726cebed16b32cbfd7ac807f66d2fcad8dc61c774e38812ae2', False)), ] def testsha256(self): for src, result in BitcoinSHA256.vectors: self.assertEqual(Bitcoin.hash(src), result)
def test1(self): hasher = hashlib.sha256() hasher.update(b'test case') data = hasher.digest() self.assertEqual(bytes_to_hexstring(data, reverse=False), '{:064x}'.format(int.from_bytes(data, 'big'))) self.assertEqual( hexstring_to_bytes(bytes_to_hexstring(data, reverse=False), reverse=False), data)
class AddressTestBitcoinKeys(unittest.TestCase): test_vectors = [ (hexstring_to_bytes( '0000000000000000000000000000000000000000000000000000000000000001', reverse=False), '5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf', False, '1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm'), (hexstring_to_bytes( '0000000000000000000000000000000000000000000000000000000000000001', reverse=False), 'KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn', True, '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH'), # TODO : more... ] def test_private_keys(self): for pk_bytes, wif, is_compressed, bitcoin_address in AddressTestBitcoinKeys.test_vectors: private_key = keys.PrivateKey(pk_bytes) self.assertEqual(private_key.as_wif(Bitcoin, is_compressed), wif) public_key = private_key.get_public_key(is_compressed) self.assertEqual(public_key.as_address(Bitcoin), bitcoin_address)
def sendspendtoaddress(spend_hash, address, amount, memo=''): spend_hash = pyspv.hexstring_to_bytes(spend_hash) transaction_builder = spv.new_transaction_builder(memo=memo) transaction_builder.include_spend(spend_hash) transaction_builder.process_change(pyspv.PubKeyChange) transaction_builder.process(get_output_producer(spv, address, spv.coin.parse_money(amount))) tx = transaction_builder.finish(shuffle_inputs=True, shuffle_outputs=True) if not tx.verify_scripts(): raise Exception("internal error building transaction") spv.broadcast_transaction(tx) return { 'tx': pyspv.bytes_to_hexstring(tx.serialize(), reverse=False), 'hash': pyspv.bytes_to_hexstring(tx.hash()), }
def sendspendtoaddress(spend_hash, address, amount, memo=''): spend_hash = pyspv.hexstring_to_bytes(spend_hash) transaction_builder = spv.new_transaction_builder(memo=memo) transaction_builder.include_spend(spend_hash) transaction_builder.process_change(pyspv.PubKeyChange) transaction_builder.process( get_output_producer(spv, address, spv.coin.parse_money(amount))) tx = transaction_builder.finish(shuffle_inputs=True, shuffle_outputs=True) if not tx.verify_scripts(): raise Exception("internal error building transaction") spv.broadcast_transaction(tx) return { 'tx': pyspv.bytes_to_hexstring(tx.serialize(), reverse=False), 'hash': pyspv.bytes_to_hexstring(tx.hash()), }
def test1(self): hasher = hashlib.sha256() hasher.update(b'test case') data = hasher.digest() self.assertEqual(bytes_to_hexstring(data, reverse=False), '{:064x}'.format(int.from_bytes(data, 'big'))) self.assertEqual(hexstring_to_bytes(bytes_to_hexstring(data, reverse=False), reverse=False), data)
def sendrawtransaction(tx_bytes): tx_bytes = pyspv.hexstring_to_bytes(tx_bytes, reverse=False) tx, _ = pyspv.transaction.Transaction.unserialize(tx_bytes, spv.coin) spv.broadcast_transaction(tx) return pyspv.bytes_to_hexstring(tx.hash())