本文整理汇总了Python中pycoin.tx.pay_to.build_hash160_lookup函数的典型用法代码示例。如果您正苦于以下问题:Python build_hash160_lookup函数的具体用法?Python build_hash160_lookup怎么用?Python build_hash160_lookup使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了build_hash160_lookup函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_build_spends
def test_build_spends(self):
# first, here is the tx database
TX_DB = {}
# create a coinbase Tx where we know the public & private key
exponent = wif_to_secret_exponent("5JMys7YfK72cRVTrbwkq5paxU7vgkMypB55KyXEtN5uSnjV7K8Y")
compressed = False
public_key_sec = public_pair_to_sec(
ecdsa.public_pair_for_secret_exponent(ecdsa.generator_secp256k1, exponent), compressed=compressed
)
the_coinbase_tx = Tx.coinbase_tx(public_key_sec, int(50 * 1e8), COINBASE_BYTES_FROM_80971)
TX_DB[the_coinbase_tx.hash()] = the_coinbase_tx
# now create a Tx that spends the coinbase
compressed = False
exponent_2 = int("137f3276686959c82b454eea6eefc9ab1b9e45bd4636fb9320262e114e321da1", 16)
bitcoin_address_2 = public_pair_to_bitcoin_address(
ecdsa.public_pair_for_secret_exponent(ecdsa.generator_secp256k1, exponent_2), compressed=compressed
)
self.assertEqual("12WivmEn8AUth6x6U8HuJuXHaJzDw3gHNZ", bitcoin_address_2)
coins_from = [(the_coinbase_tx.hash(), 0, the_coinbase_tx.txs_out[0])]
coins_to = [(int(50 * 1e8), bitcoin_address_2)]
unsigned_coinbase_spend_tx = standard_tx(coins_from, coins_to)
solver = build_hash160_lookup([exponent])
coinbase_spend_tx = unsigned_coinbase_spend_tx.sign(solver)
# now check that it validates
self.assertEqual(coinbase_spend_tx.bad_signature_count(), 0)
TX_DB[coinbase_spend_tx.hash()] = coinbase_spend_tx
## now try to respend from priv_key_2 to priv_key_3
compressed = True
exponent_3 = int("f8d39b8ecd0e1b6fee5a340519f239097569d7a403a50bb14fb2f04eff8db0ff", 16)
bitcoin_address_3 = public_pair_to_bitcoin_address(
ecdsa.public_pair_for_secret_exponent(ecdsa.generator_secp256k1, exponent_3), compressed=compressed
)
self.assertEqual("13zzEHPCH2WUZJzANymow3ZrxcZ8iFBrY5", bitcoin_address_3)
coins_from = [(coinbase_spend_tx.hash(), 0, coinbase_spend_tx.txs_out[0])]
unsigned_spend_tx = standard_tx(coins_from, [(int(50 * 1e8), bitcoin_address_3)])
solver.update(build_hash160_lookup([exponent_2]))
spend_tx = unsigned_spend_tx.sign(solver)
# now check that it validates
self.assertEqual(spend_tx.bad_signature_count(), 0)
示例2: test_sign
def test_sign(self):
sv = 33143560198659167577410026742586567991638126035902913554051654024377193788946
tx_out_script = b'v\xa9\x14\x91\xb2K\xf9\xf5(\x852\x96\n\xc6\x87\xab\xb05\x12{\x1d(\xa5\x88\xac'
st = script_obj_from_script(tx_out_script)
hl = build_hash160_lookup([1])
solution = st.solve(hash160_lookup=hl, sign_value=sv, signature_type=SIGHASH_ALL)
self.assertEqual(solution, b'G0D\x02 ^=\xf5\xb5[\[email protected]\x04,"\x0b\x1f\xdf\x10\\\xc8Q\x13\xafV*!\\\x1f\xc5\xb5\xc5"\xd1\xb3\xd3\x02 8\xc9YK\x15o\xae\xd7\xf3|0\x07z\xff\xbfj\xcfB\xbf\x17\xb1\xe69\xa1\xfc\xc6\xc5\x1ag\xab\xa2\x16\x01A\x04y\xbef~\xf9\xdc\xbb\xacU\xa0b\x95\xce\x87\x0b\x07\x02\x9b\xfc\xdb-\xce(\xd9Y\xf2\x81[\x16\xf8\x17\x98H:\xdaw&\xa3\xc4e]\xa4\xfb\xfc\x0e\x11\x08\xa8\xfd\x17\xb4H\xa6\x85T\x19\x9cG\xd0\x8f\xfb\x10\xd4\xb8')
示例3: sign_tx
def sign_tx(certificate_metadata, last_input, allowable_wif_prefixes=None):
"""sign the transaction with private key"""
with open(certificate_metadata.unsigned_tx_file_name, 'rb') as in_file:
hextx = str(in_file.read(), 'utf-8')
logging.info(
'Signing tx with private key for recipient id: %s ...', certificate_metadata.uid)
tx = Tx.from_hex(hextx)
if allowable_wif_prefixes:
wif = wif_to_secret_exponent(
helpers.import_key(), allowable_wif_prefixes)
else:
wif = wif_to_secret_exponent(helpers.import_key())
lookup = build_hash160_lookup([wif])
tx.set_unspents(
[TxOut(coin_value=last_input.amount, script=last_input.script_pub_key)])
signed_tx = tx.sign(lookup)
signed_hextx = signed_tx.as_hex()
with open(certificate_metadata.unsent_tx_file_name, 'wb') as out_file:
out_file.write(bytes(signed_hextx, 'utf-8'))
logging.info('Finished signing transaction for certificate uid=%s',
certificate_metadata.uid)
示例4: sign_transaction
def sign_transaction(self, wif, transaction_to_sign):
secret_exponent = wif_to_secret_exponent(wif, self.allowable_wif_prefixes)
lookup = build_hash160_lookup([secret_exponent])
signed_transaction = transaction_to_sign.sign(lookup)
# Because signing failures silently continue, first check that the inputs are signed
for input in signed_transaction.txs_in:
if len(input.script) == 0:
logging.error('Unable to sign transaction. hextx=%s', signed_transaction.as_hex())
raise UnableToSignTxError('Unable to sign transaction')
return signed_transaction
示例5: sign_tx
def sign_tx(service, testnet, tx, keys):
netcode = 'XTN' if testnet else 'BTC'
secretexponents = list(map(lambda key: key.secret_exponent(), keys))
lookup = build_hash160_lookup(secretexponents)
for txin_idx in range(len(tx.txs_in)):
txin = tx.txs_in[txin_idx]
utxo_tx = service.get_tx(txin.previous_hash)
script = utxo_tx.txs_out[txin.previous_index].script
tx.sign_tx_in(lookup, txin_idx, script, SIGHASH_ALL, netcode=netcode)
return tx
示例6: get
def get(self, v):
if v in self.secret_exponent_db_cache:
return self.secret_exponent_db_cache[v]
for wif in self.wif_iterable:
secret_exponent = wif_to_secret_exponent(wif, self.wif_prefix)
d = build_hash160_lookup([secret_exponent])
self.secret_exponent_db_cache.update(d)
if v in self.secret_exponent_db_cache:
return self.secret_exponent_db_cache[v]
self.wif_iterable = []
return None
示例7: test_sign
def test_sign(self):
sv = 33143560198659167577410026742586567991638126035902913554051654024377193788946
tx_out_script = b'v\xa9\x14\x91\xb2K\xf9\xf5(\x852\x96\n\xc6\x87\xab\xb05\x12{\x1d(\xa5\x88\xac'
st = script_obj_from_script(tx_out_script)
hl = build_hash160_lookup([1])
solution = st.solve(hash160_lookup=hl, signature_for_hash_type_f=const_f(sv), signature_type=SIGHASH_ALL)
self.assertEqual(
solution, h2b(
"47304402205e3df5b55be62140042c220b1fdf105cc85113af562a215c1fc5b5c522d1"
"b3d3022038c9594b156faed7f37c30077affbf6acf42bf17b1e639a1fcc6c51a67aba2"
"1601410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817"
"98483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"))
示例8: multisig_N_of_M
def multisig_N_of_M(self, N, M, unsigned_id, signed_id):
keys = [Key(secret_exponent=i) for i in range(1, M+2)]
tx_in = TxIn.coinbase_tx_in(script=b'')
script = ScriptMultisig(n=N, sec_keys=[key.sec() for key in keys[:M]]).script()
tx_out = TxOut(1000000, script)
tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [keys[-1].address()])
self.assertEqual(tx2.id(), unsigned_id)
self.assertEqual(tx2.bad_signature_count(), 1)
hash160_lookup = build_hash160_lookup(key.secret_exponent() for key in keys)
tx2.sign(hash160_lookup=hash160_lookup)
self.assertEqual(tx2.id(), signed_id)
self.assertEqual(tx2.bad_signature_count(), 0)
示例9: multisig_M_of_N_individually
def multisig_M_of_N_individually(self, M, N):
keys = [Key(secret_exponent=i) for i in range(1, N+2)]
tx_in = TxIn.coinbase_tx_in(script=b'')
script = ScriptMultisig(n=M, sec_keys=[key.sec() for key in keys[:N]]).script()
tx_out = TxOut(1000000, script)
tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
for partial_key_list in itertools.permutations(keys[:N], M):
tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [keys[-1].address()])
for key in partial_key_list:
self.assertEqual(tx2.bad_signature_count(), 1)
hash160_lookup = build_hash160_lookup([key.secret_exponent()])
tx2.sign(hash160_lookup=hash160_lookup)
self.assertEqual(tx2.bad_signature_count(), 0)
示例10: test_solve_pay_to_public_pair
def test_solve_pay_to_public_pair(self):
for se in range(1, 10):
key = Key(secret_exponent=se)
for b in [True, False]:
addr = key.address(use_uncompressed=b)
st = ScriptPayToPublicKey.from_key(key, use_uncompressed=b)
self.assertEqual(st.address(), addr)
hl = build_hash160_lookup([se])
sv = 100
solution = st.solve(hash160_lookup=hl, sign_value=sv, signature_type=SIGHASH_ALL)
sc = st.script()
st = script_obj_from_script(sc)
self.assertEqual(st.address(), addr)
示例11: test_solve_pay_to_address
def test_solve_pay_to_address(self):
for se in range(1, 10):
key = Key(secret_exponent=se)
for b in [True, False]:
addr = key.address(use_uncompressed=b)
st = script_obj_from_address(addr)
self.assertEqual(st.address(), addr)
hl = build_hash160_lookup([se])
sv = 100
solution = st.solve(hash160_lookup=hl, signature_for_hash_type_f=const_f(sv), signature_type=SIGHASH_ALL)
sc = st.script()
st = script_obj_from_script(sc)
self.assertEqual(st.address(), addr)
示例12: test_sign_pay_to_script_multisig
def test_sign_pay_to_script_multisig(self):
N, M = 3, 3
keys = [Key(secret_exponent=i) for i in range(1, M+2)]
tx_in = TxIn.coinbase_tx_in(script=b'')
underlying_script = ScriptMultisig(n=N, sec_keys=[key.sec() for key in keys[:M]]).script()
address = address_for_pay_to_script(underlying_script)
self.assertEqual(address, "39qEwuwyb2cAX38MFtrNzvq3KV9hSNov3q")
script = standard_tx_out_script(address)
tx_out = TxOut(1000000, script)
tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [address])
hash160_lookup = build_hash160_lookup(key.secret_exponent() for key in keys[:M])
p2sh_lookup = build_p2sh_lookup([underlying_script])
tx2.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup)
self.assertEqual(tx2.bad_signature_count(), 0)
示例13: sign_tx
def sign_tx(hex_tx, tx_input):
"""
Sign the transaction with private key
:param hex_tx:
:param tx_input:
:return:
"""
logging.info('Signing tx with private key')
transaction = Tx.from_hex(hex_tx)
wif = wif_to_secret_exponent(helpers.import_key(), ALLOWABLE_WIF_PREFIXES)
lookup = build_hash160_lookup([wif])
transaction.set_unspents([TxOut(coin_value=tx_input.coin_value, script=tx_input.script)])
signed_tx = transaction.sign(lookup)
logging.info('Finished signing transaction')
return signed_tx
示例14: test_sign_bitcoind_partially_signed_2_of_2
def test_sign_bitcoind_partially_signed_2_of_2(self):
# Finish signing a 2 of 2 transaction, that already has one signature signed by bitcoind
# This tx can be found on testnet3 blockchain, txid: 9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2
raw_script = "522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452ae"
p2sh_lookup = build_p2sh_lookup([h2b(raw_script)])
partially_signed_raw_tx = "010000000196238f11a5fd3ceef4efd5a186a7e6b9217d900418e72aca917cd6a6e634e74100000000910047304402201b41b471d9dd93cf97eed7cfc39a5767a546f6bfbf3e0c91ff9ad23ab9770f1f02205ce565666271d055be1f25a7e52e34cbf659f6c70770ff59bd783a6fcd1be3dd0147522103e33b41f5ed67a77d4c4c54b3e946bd30d15b8f66e42cb29fde059c16885116552102b92cb20a9fb1eb9656a74eeb7387636cf64cdf502ff50511830328c1b479986452aeffffffff01a0bb0d00000000001976a9143b3beefd6f7802fa8706983a76a51467bfa36f8b88ac00000000"
tx = Tx.from_hex(partially_signed_raw_tx)
tx_out = TxOut(1000000, h2b("a914a10dfa21ee8c33b028b92562f6fe04e60563d3c087"))
tx.set_unspents([tx_out])
key = Key.from_text("cThRBRu2jAeshWL3sH3qbqdq9f4jDiDbd1SVz4qjTZD2xL1pdbsx")
hash160_lookup = build_hash160_lookup([key.secret_exponent()])
self.assertEqual(tx.bad_signature_count(), 1)
tx.sign(hash160_lookup=hash160_lookup, p2sh_lookup=p2sh_lookup)
self.assertEqual(tx.bad_signature_count(), 0)
self.assertEqual(tx.id(), "9618820d7037d2f32db798c92665231cd4599326f5bd99cb59d0b723be2a13a2")
示例15: sign_tx
def sign_tx(tx, utxo_list, is_test):
secret_exponents = [utxo.address_rec.rawPrivKey
for utxo in utxo_list if utxo.address_rec]
hash160_lookup = build_hash160_lookup(secret_exponents)
txins = tx.txs_in[:]
for txin_idx in xrange(len(txins)):
blank_txin = txins[txin_idx]
utxo = None
for utxo_candidate in utxo_list:
if utxo_candidate.get_txhash() == blank_txin.previous_hash \
and utxo_candidate.outindex == blank_txin.previous_index:
utxo = utxo_candidate
break
if not (utxo and utxo.address_rec):
continue
txout_script = utxo.script.decode('hex')
tx.sign_tx_in(hash160_lookup, txin_idx, txout_script, SIGHASH_ALL)