本文整理汇总了Python中pycoin.tx.Tx.Tx.from_hex方法的典型用法代码示例。如果您正苦于以下问题:Python Tx.from_hex方法的具体用法?Python Tx.from_hex怎么用?Python Tx.from_hex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycoin.tx.Tx.Tx
的用法示例。
在下文中一共展示了Tx.from_hex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_bip143_tx
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def check_bip143_tx(self, tx_u_hex, tx_s_hex, txs_out_value_scripthex_pair, tx_in_count, tx_out_count, version, lock_time):
tx_u = Tx.from_hex(tx_u_hex)
tx_s = Tx.from_hex(tx_s_hex)
txs_out = [
TxOut(int(coin_value * 1e8), h2b(script_hex)) for coin_value, script_hex in txs_out_value_scripthex_pair
]
for tx in (tx_u, tx_s):
self.assertEqual(len(tx.txs_in), tx_in_count)
self.assertEqual(len(tx.txs_out), tx_out_count)
self.assertEqual(tx.version, version)
self.assertEqual(tx.lock_time, lock_time)
tx.set_unspents(txs_out)
self.check_unsigned(tx_u)
self.check_signed(tx_s)
tx_hex = tx_u.as_hex()
self.assertEqual(tx_hex, tx_u_hex)
tx_hex = tx_s.as_hex()
self.assertEqual(tx_hex, tx_s_hex)
tx_u_prime = self.unsigned_copy(tx_s)
tx_hex = tx_u_prime.as_hex()
self.assertEqual(tx_hex, tx_u_hex)
self.assertEqual(b2h_rev(double_sha256(h2b(tx_s_hex))), tx_s.w_id())
self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.w_id())
self.assertEqual(b2h_rev(double_sha256(h2b(tx_u_hex))), tx_u.id())
return tx_u, tx_s
示例2: test_pay_to_script_file
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def test_pay_to_script_file(self):
the_dir = self.set_cache_dir()
p2sh_file = tempfile.NamedTemporaryFile()
p2sh_file.write(
"52210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102c60"
"47f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258"
"c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae\n".encode("utf8"))
p2sh_file.write(
"53210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102c60"
"47f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258"
"c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae\n".encode("utf8"))
p2sh_file.flush()
tx_source_hex = (
"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0"
"0ffffffff0200f902950000000017a91415fc0754e73eb85d1cbce08786fadb7320ecb8dc8700f90295"
"0000000017a914594f349df0bac3084ffea8a477bba5f03dcd45078700000000")
self.launch_tool("tx -C %s" % tx_source_hex)
tx_to_sign = (
"01000000020a316ea8980ef9ba02f4e6637c88229bf059f39b06238d48d06a8e"
"f672aea2bb0000000000ffffffff0a316ea8980ef9ba02f4e6637c88229bf059"
"f39b06238d48d06a8ef672aea2bb0100000000ffffffff01f0ca052a01000000"
"1976a914751e76e8199196d454941c45d1b3a323f1433bd688ac0000000000f9"
"02950000000017a91415fc0754e73eb85d1cbce08786fadb7320ecb8dc8700f9"
"02950000000017a914594f349df0bac3084ffea8a477bba5f03dcd450787")
wifs = ' '.join(Key(_).wif() for _ in (1, 2, 3))
signed = tempfile.mktemp(suffix=".hex")
self.launch_tool("tx -a -P %s --db %s %s %s -o %s" % (
p2sh_file.name, tx_source_hex, tx_to_sign, wifs, signed), env=dict(PYCOIN_CACHE_DIR=the_dir))
tx = Tx.from_hex(open(signed).read())
self.assertEqual(tx.id(), "9d991ddccf77e33cb4584e4fc061a36da0da43589232b2e78a1aa0748ac3254b")
示例3: main
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def main():
if len(sys.argv) != 4:
print("usage: %s tx-hex-file-path wif-file-path p2sh-file-path" % sys.argv[0])
sys.exit(-1)
# get the tx
with open(sys.argv[1], "r") as f:
tx_hex = f.readline().strip()
tx = Tx.from_hex(tx_hex)
# get the WIF
with open(sys.argv[2], "r") as f:
wif = f.readline().strip()
assert is_wif_valid(wif)
# create the p2sh_lookup
with open(sys.argv[3], "r") as f:
p2sh_script_hex = f.readline().strip()
p2sh_script = h2b(p2sh_script_hex)
# build a dictionary of script hashes to scripts
p2sh_lookup = build_p2sh_lookup([p2sh_script])
# sign the transaction with the given WIF
sign_tx(tx, wifs=[wif], p2sh_lookup=p2sh_lookup)
bad_signature_count = tx.bad_signature_count()
print("tx %s now has %d bad signature(s)" % (tx.id(), bad_signature_count))
include_unspents = (bad_signature_count > 0)
print("Here is the tx as hex:\n%s" % tx.as_hex(include_unspents=include_unspents))
示例4: parse_tx
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def parse_tx(arg, parser, tx_db, network):
# hex transaction id
tx = None
if TX_ID_RE.match(arg):
if tx_db is None:
tx_db = create_tx_db(network)
tx = tx_db.get(h2b_rev(arg))
if not tx:
parser.error("can't find Tx with id %s" % arg)
return tx, tx_db
# hex transaction data
try:
return Tx.from_hex(arg), tx_db
except Exception:
pass
if os.path.exists(arg):
try:
with open(arg, "rb") as f:
if f.name.endswith("hex"):
f = io.BytesIO(codecs.getreader("hex_codec")(f).read())
tx = Tx.parse(f)
tx.parse_unspents(f)
except Exception:
pass
return tx, tx_db
示例5: test_cache_tx
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def test_cache_tx(self):
the_dir = self.set_cache_dir()
tx = Tx.from_hex(
"01000000010000000000000000000000000000000000000000000000000000000000000000"
"ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a"
"2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781"
"e62294721166bf621e73a82cbf2342c858eeac00000000")
self.launch_tool("tx -C %s --db %s" % (tx.id(), tx.as_hex()), env=dict(PYCOIN_CACHE_DIR=the_dir))
self.assertTrue(os.path.exists(os.path.join(the_dir, "txs", "%s_tx.bin" % tx.id())))
示例6: tx_to_b64
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def tx_to_b64(tx_hex):
# use this to dump raw transactions in the data above
import io
tx = Tx.from_hex(tx_hex)
f = io.BytesIO()
tx.stream(f)
d = f.getvalue()
for idx in range(0, len(d), 45):
print('"%s"' % binascii.b2a_base64(d[idx:idx+45]).decode("utf8")[:-1])
示例7: load_tx
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def load_tx(self, txid):
p = subprocess.Popen(['electrum', 'gettransaction', txid], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
raw_data = p.communicate()[0]
data = json.loads(raw_data)
txhex = data['hex']
tx = Tx.from_hex(txhex)
return tx
示例8: tx_for_tx_hash
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def tx_for_tx_hash(self, tx_hash):
"""
Get a Tx by its hash.
"""
url = "%s/rawtx/%s" % (self.url, b2h_rev(tx_hash))
d = urlopen(url).read()
j = json.loads(d.decode("utf8"))
tx = Tx.from_hex(j.get("rawtx", ""))
if tx.hash() == tx_hash:
return tx
示例9: test_compare_cost
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def test_compare_cost(self):
"""
Compare our size estimation with a known transaction. The transaction contains 1 input, 6 outputs, and 1
OP_RETURN
Note that the estimation may be off +/- the number of inputs, which is why the estimate was off by 1 in this
case.
:return:
"""
tx = Tx.from_hex(
'0100000001ae17c5db3174b46ae2bdc911c25df6bc3ce88092256b6f6e564989693ecf67fc030000006b483045022100b0cfd576dd30bbdf6fd11e0d6118c59b6c6f8e7bf6513d323c7f9f5f8296bef102200174a28e28c792425b71155df99ea6110cdb67d3567792f1696e61424c1f67400121037175dfbeecd8b5a54eb5ad9a696f15b7b39da2ea7d67b4cd7a3299bb95e28884ffffffff07be0a0000000000001976a91481c706f7e6b2d9546169c1e76f50a3ee18e1e1d788acbe0a0000000000001976a914c2b9a62457e35bef48ef350a00622b1e63394d4588acbe0a0000000000001976a91481c706f7e6b2d9546169c1e76f50a3ee18e1e1d788acbe0a0000000000001976a914c2b9a62457e35bef48ef350a00622b1e63394d4588acbe0a0000000000001976a914cc0a909c4c83068be8b45d69b60a6f09c2be0fda88ac5627cb1d000000001976a9144103222e7c72b869c5e47bfe86702684531f2c9088ac0000000000000000226a206f308c70afcfcb0311ad0de989b80904fb54d9131fd3ab2187b89ca9601adab000000000')
s = io.BytesIO()
tx.stream(s)
tx_byte_count = len(s.getvalue())
estimated_byte_count = tx_utils.calculate_raw_tx_size_with_op_return(num_inputs=1, num_outputs=6)
self.assertEquals(estimated_byte_count, tx_byte_count + 1)
示例10: test_blanked_hash
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def test_blanked_hash(self):
tx = Tx.from_hex(TX_E1A18B843FC420734DEEB68FF6DF041A2585E1A0D7DBF3B82AAB98291A6D9952_HEX)
self.assertEqual(tx.id(), "e1a18b843fc420734deeb68ff6df041a2585e1a0d7dbf3b82aab98291a6d9952")
self.assertEqual(
b2h(tx.blanked_hash()), "909579526c4c2c441687c7478d3f96249724d2ff071d2272b44500d6cf70d5d6")
tx.txs_in[0].script = b"foo"
self.assertEqual(
b2h(tx.blanked_hash()), "909579526c4c2c441687c7478d3f96249724d2ff071d2272b44500d6cf70d5d6")
tx.txs_out[0].coin_value += 1
self.assertEqual(
b2h(tx.blanked_hash()), "10d4e87f7bf35f2949e7693e7a4a84189aad8631f0b2b0999e88f7261066cbe5")
tx.txs_in[0].script = b"bar"
self.assertEqual(
b2h(tx.blanked_hash()), "10d4e87f7bf35f2949e7693e7a4a84189aad8631f0b2b0999e88f7261066cbe5")
tx.txs_in[0].script = b""
self.assertEqual(b2h(tx.hash()), "10d4e87f7bf35f2949e7693e7a4a84189aad8631f0b2b0999e88f7261066cbe5")
tx.txs_in[0].script = b"foo"
self.assertEqual(b2h(tx.hash()), "c91910058722f1c0f52fc5c734939053c9b87882a9c72b609f21632e0bd13751")
示例11: txs_from_json
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def txs_from_json(path):
"""
Read tests from ./data/tx_??valid.json
Format is an array of arrays
Inner arrays are either [ "comment" ]
or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...], serializedTransaction, verifyFlags]
... where all scripts are stringified scripts.
verifyFlags is a comma separated list of script verification flags to apply, or "NONE"
"""
comments = None
with open(path, 'r') as f:
for tvec in json.load(f):
if len(tvec) == 1:
comments = tvec[0]
continue
assert len(tvec) == 3
prevouts = tvec[0]
for prevout in prevouts:
assert len(prevout) in (3, 4)
tx_hex = tvec[1]
flag_mask = parse_flags(tvec[2])
try:
tx = Tx.from_hex(tx_hex)
except:
print("Cannot parse tx_hex: %s" % tx_hex)
raise
spendable_db = {}
blank_spendable = Spendable(0, b'', b'\0' * 32, 0)
for prevout in prevouts:
coin_value = 1000000
if len(prevout) == 4:
coin_value = prevout[3]
spendable = Spendable(coin_value=coin_value,
script=compile(prevout[2]),
tx_hash=h2b_rev(prevout[0]), tx_out_index=prevout[1])
spendable_db[(spendable.tx_hash, spendable.tx_out_index)] = spendable
unspents = [
spendable_db.get((tx_in.previous_hash, tx_in.previous_index), blank_spendable) for tx_in in tx.txs_in]
tx.set_unspents(unspents)
yield (tx, flag_mask, comments)
示例12: test_tx_with_gpg
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def test_tx_with_gpg(self):
# binary data with GPG-encrypted WIF KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn for secret exponent 1
WIF_1_GPG = h2b(
"8c0d040303026c3030b7518a94eb60c950bc87ab26f0604a37f247f74f88deda10b180bb807"
"2879b728b8f056808baea0c8e511e7cf2eba77cce937d2f69a67a79e163bf70b57113d27cb6"
"a1c2390a1e8069b447c34a7c9b5ba268c2beedd85b50")
gpg_wif = tempfile.NamedTemporaryFile(suffix=".gpg")
gpg_wif.write(WIF_1_GPG)
gpg_wif.flush()
output_file = tempfile.NamedTemporaryFile(suffix=".hex")
self.launch_tool(
args=["tx",
"5564224b6c01dbc2bfad89bfd8038bc2f4ca6c55eb660511d7151d71e4b94b6d/0/"
"210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac/5000000000",
"1KissFDVu2wAYWPRm4UGh5ZCDU9sE9an8T", "-f", gpg_wif.name, "-g", "--batch --passphrase=foo",
"-o", output_file.name])
d = open(output_file.name).read()
tx = Tx.from_hex(d)
self.assertEqual(tx.id(), "c52b0c66cff6147b99acb29389343f6eae68c29faf2186fa8c1613d615b217e8")
示例13: test_is_invalid
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def test_is_invalid(self):
for (prevouts, tx_hex, flags) in txs_from_json(TX_INVALID_JSON):
try:
tx = Tx.from_hex(tx_hex)
if not check_transaction(tx):
continue
unspents = [Spendable(coin_value=1000000,
script=compile_script(prevout[2]),
tx_hash=prevout[0], tx_out_index=prevout[1])
for prevout in prevouts]
tx.set_unspents(unspents)
bs = tx.bad_signature_count()
self.assertEqual(bs, 0)
except:
continue
self.fail("Invalid transaction: " + tx.id() +
" appears to be valid.")
示例14: test_is_valid
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def test_is_valid(self):
for (prevouts, tx_hex, flags) in txs_from_json(TX_VALID_JSON):
try:
tx = Tx.from_hex(tx_hex)
except:
self.fail("Cannot parse tx_hex: " + tx_hex)
if not check_transaction(tx):
self.fail("check_transaction(tx) = False for valid tx: " +
tx_hex)
unspents = [Spendable(coin_value=1000000,
script=compile_script(prevout[2]),
tx_hash=prevout[0], tx_out_index=prevout[1])
for prevout in prevouts]
tx.set_unspents(unspents)
bs = tx.bad_signature_count()
if bs > 0:
msg = str(tx_hex) + " bad_signature_count() = " + str(bs)
self.fail(msg)
示例15: main
# 需要导入模块: from pycoin.tx.Tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx.Tx import from_hex [as 别名]
def main():
if len(sys.argv) != 4:
print("usage: %s incoming_tx_hex_filename tx_out_index new_address" % sys.argv[0])
sys.exit(-1)
with open(sys.argv[1], "r") as f:
tx_hex = f.readline().strip()
# get the spendable from the prior transaction
tx = Tx.from_hex(tx_hex)
tx_out_index = int(sys.argv[2])
spendable = tx.tx_outs_as_spendable()[tx_out_index]
# make sure the address is valid
payable = sys.argv[3]
assert is_address_valid(payable)
# create the unsigned transaction
tx = create_tx([spendable], [payable])
print("here is the transaction: %s" % tx.as_hex(include_unspents=True))