本文整理汇总了Python中pycoin.tx.Tx.as_hex方法的典型用法代码示例。如果您正苦于以下问题:Python Tx.as_hex方法的具体用法?Python Tx.as_hex怎么用?Python Tx.as_hex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycoin.tx.Tx
的用法示例。
在下文中一共展示了Tx.as_hex方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_opreturn
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
def write_opreturn(bitcoin_address, bitcoin_private_key, raw_message, fee=5000, push=False):
message = hexlify(raw_message.encode()).decode('utf8')
spendables = spendables_for_address(bitcoin_address)
spendables = [s for s in spendables]
bitcoin_sum = sum([spendable.coin_value for spendable in spendables])
inputs = [spendable.tx_in() for spendable in spendables]
outputs = []
if (bitcoin_sum > fee):
change_output_script = standard_tx_out_script(bitcoin_address)
print change_output_script
outputs.append(TxOut(bitcoin_sum - fee, change_output_script))
## Build the OP_RETURN output with our message
op_return_output_script = script.tools.compile("OP_RETURN %s" % message)
outputs.append(TxOut(0, op_return_output_script))
## Create the transaction and sign it with the private key
tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
tx.set_unspents(spendables)
sign_tx(tx, wifs=[bitcoin_private_key])
print tx.as_hex()
if not push:
return tx.as_hex()
else:
pushtx(tx.as_hex())
else:
print "INADEQUATE FUNDS"
示例2: write_transfer
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
def write_transfer(sender, sender_priv, recipient, message, fee=m.default_fee, avoid_inputs=[]):
message = hexlify(message.encode()).decode('utf8')
spendables = spendables_for_address(sender)
spendables = [s for s in spendables if not spendable_to_legible(s.tx_in()) in avoid_inputs]
bitcoin_sum = sum([spendable.coin_value for spendable in spendables])
inputs = [spendable.tx_in() for spendable in spendables]
outputs = []
if bitcoin_sum > fee + m.dust*2:
remaining = bitcoin_sum - fee - m.dust*2
dest_output_script = standard_tx_out_script(recipient)
change_output_script = standard_tx_out_script(sender)
btc_change_output_script = standard_tx_out_script(sender)
op_return_output_script = script.tools.compile("OP_RETURN %s" % message)
outputs.append(TxOut(m.dust, dest_output_script))
outputs.append(TxOut(m.dust, change_output_script))
outputs.append(TxOut(remaining, btc_change_output_script))
outputs.append(TxOut(0, op_return_output_script))
tx = Tx(version=1, txs_in=inputs, txs_out=outputs)
tx.set_unspents(spendables)
sign_tx(tx, wifs=[sender_priv])
print tx.as_hex()
return tx.as_hex()
else:
print "INADEQUATE FUNDS"
示例3: process_tx_initial
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
def process_tx_initial(tx_obj: Tx):
found_relevant_address = False
for out in tx_obj.txs_out:
address = out.bitcoin_address()
if address in all_addresses:
found_relevant_address = True
break
if not found_relevant_address:
logging.info('Found irrelevant tx %s' % hash_to_hex(tx_obj.hash()))
return
tx_hash = tx_obj.hash()
txid = hash_to_hex(tx_hash).decode()
if tx_hash in known_txs:
return
known_txs.add(tx_hash)
txs[tx_hash] = tx_obj.as_hex()
for out in tx_obj.txs_out:
address = out.bitcoin_address()
if address in all_addresses and address is not None:
unprocessed_txs.add(tx_hash)
uid = addr_to_uid[address]
account = Account(uid)
account.txs.add(tx_hash)
account.unconf_minutes.incr(calc_node_minutes(satoshi_amount=out.coin_value, exchange_rate=exchange_rate.get()))
account.add_msg('Found tx for %.08f, %s' % (out.coin_value / COIN, txid))
nodes_recently_updated.append(account.uid)
示例4: main
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
def main():
if len(sys.argv) != 2:
print("usage: %s address" % sys.argv[0])
sys.exit(-1)
# validate the address
address = sys.argv[1]
assert is_address_valid(address)
print("creating coinbase transaction to %s" % address)
tx_in = TxIn.coinbase_tx_in(script=b'')
tx_out = TxOut(50*1e8, standard_tx_out_script(address))
tx = Tx(1, [tx_in], [tx_out])
print("Here is the tx as hex:\n%s" % tx.as_hex())
示例5: Spendable
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
amount = convention.btc_to_satoshi(first_nondust['amount'])
dustbtc = convention.btc_to_satoshi(DUST)
feebtc = convention.btc_to_satoshi(FEE)
unspent = Spendable( amount, standard_tx_out_script(first_address), \
serialize.h2b_rev(first_nondust['tx']), first_nondust['n'] )
txout = TxOut( dustbtc, standard_tx_out_script(bob_notif.address()) )
change = TxOut( amount - (dustbtc + feebtc), standard_tx_out_script(change_addresses.pop()) )
op_return_script = pay_to.ScriptNulldata(alice_masked_pcode_nosuffix)
op_return_txout = TxOut(0, op_return_script.script())
notif_tx = Tx( 1, [unspent.tx_in()], [txout, change, op_return_txout], unspents=[unspent] )
tx_utils.sign_tx( notif_tx, [first_node.wif()] )
print "Signed Notification TX as hex:\n", notif_tx.as_hex()
data = urlencode(dict( hex=notif_tx.as_hex() ))
response = json.load( urlopen(url="http://tbtc.blockr.io/api/v1/tx/decode", data=data) )
print "Decoded:\n", pretty_json(response), '\n'
alice_notif_exp = alice_notif.secret_exponent()
bob_pcode_nodes = get_nodes(bob_wallet, GAP_LIMIT, pcode=True)
bob_ephem_key = None
for bob_pcode_node in bob_pcode_nodes.values():
pcode_pp = bob_pcode_node.public_pair()
pcode_point = ellipticcurve.Point(curve, pcode_pp[0], pcode_pp[1])
secret_point = alice_notif_exp * pcode_point
示例6: main
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
#.........这里部分代码省略.........
version = DEFAULT_VERSION
if args.remove_tx_in:
s = set(args.remove_tx_in)
txs_in = [tx_in for idx, tx_in in enumerate(txs_in) if idx not in s]
if args.remove_tx_out:
s = set(args.remove_tx_out)
txs_out = [tx_out for idx, tx_out in enumerate(txs_out) if idx not in s]
tx = Tx(txs_in=txs_in, txs_out=txs_out, lock_time=lock_time, version=version, unspents=unspents)
fee = args.fee
try:
distribute_from_split_pool(tx, fee)
except ValueError as ex:
print("warning: %s" % ex.args[0], file=sys.stderr)
unsigned_before = tx.bad_signature_count()
if unsigned_before > 0 and key_iters:
def wif_iter(iters):
while len(iters) > 0:
for idx, iter in enumerate(iters):
try:
wif = next(iter)
yield wif
except StopIteration:
iters = iters[:idx] + iters[idx+1:]
break
print("signing...", file=sys.stderr)
sign_tx(tx, wif_iter(key_iters))
unsigned_after = tx.bad_signature_count()
if unsigned_after > 0 and key_iters:
print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr)
if len(tx.txs_in) == 0:
print("warning: transaction has no inputs", file=sys.stderr)
if len(tx.txs_out) == 0:
print("warning: transaction has no outputs", file=sys.stderr)
include_unspents = (unsigned_after > 0)
tx_as_hex = tx.as_hex(include_unspents=include_unspents)
if args.output_file:
f = args.output_file
if f.name.endswith(".hex"):
f.write(tx_as_hex.encode("utf8"))
else:
tx.stream(f)
if include_unspents:
tx.stream_unspents(f)
f.close()
elif args.show_unspents:
for spendable in tx.tx_outs_as_spendable():
print(spendable.as_text())
else:
if not tx.missing_unspents():
check_fees(tx)
dump_tx(tx, args.network)
if include_unspents:
print("including unspents in hex dump since transaction not fully signed")
print(tx_as_hex)
if args.cache:
if tx_db is None:
warning_tx_cache = message_about_tx_cache_env()
warning_get_tx = message_about_get_tx_env()
tx_db = get_tx_db()
tx_db.put(tx)
if args.bitcoind_url:
if tx_db is None:
warning_tx_cache = message_about_tx_cache_env()
warning_get_tx = message_about_get_tx_env()
tx_db = get_tx_db()
validate_bitcoind(tx, tx_db, args.bitcoind_url)
if tx.missing_unspents():
print("\n** can't validate transaction as source transactions missing", file=sys.stderr)
else:
try:
if tx_db is None:
warning_tx_cache = message_about_tx_cache_env()
warning_get_tx = message_about_get_tx_env()
tx_db = get_tx_db()
tx.validate_unspents(tx_db)
print('all incoming transaction values validated')
except BadSpendableError as ex:
print("\n**** ERROR: FEES INCORRECTLY STATED: %s" % ex.args[0], file=sys.stderr)
except Exception as ex:
print("\n*** can't validate source transactions as untampered: %s" %
ex.args[0], file=sys.stderr)
# print warnings
for m in [warning_tx_cache, warning_get_tx, warning_spendables]:
if m:
print("warning: %s" % m, file=sys.stderr)
示例7: main
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
def main():
parser = create_parser()
args = parser.parse_args()
(txs, spendables, payables, key_iters, p2sh_lookup, tx_db,
warning_tx_cache, warning_tx_for_tx_hash, warning_spendables) = parse_context(args, parser)
txs_in = []
txs_out = []
unspents = []
# we use a clever trick here to keep each tx_in corresponding with its tx_out
for tx in txs:
smaller = min(len(tx.txs_in), len(tx.txs_out))
txs_in.extend(tx.txs_in[:smaller])
txs_out.extend(tx.txs_out[:smaller])
unspents.extend(tx.unspents[:smaller])
for tx in txs:
smaller = min(len(tx.txs_in), len(tx.txs_out))
txs_in.extend(tx.txs_in[smaller:])
txs_out.extend(tx.txs_out[smaller:])
unspents.extend(tx.unspents[smaller:])
for spendable in spendables:
txs_in.append(spendable.tx_in())
unspents.append(spendable)
for address, coin_value in payables:
script = standard_tx_out_script(address)
txs_out.append(TxOut(coin_value, script))
lock_time = args.lock_time
version = args.transaction_version
# if no lock_time is explicitly set, inherit from the first tx or use default
if lock_time is None:
if txs:
lock_time = txs[0].lock_time
else:
lock_time = DEFAULT_LOCK_TIME
# if no version is explicitly set, inherit from the first tx or use default
if version is None:
if txs:
version = txs[0].version
else:
version = DEFAULT_VERSION
if args.remove_tx_in:
s = set(args.remove_tx_in)
txs_in = [tx_in for idx, tx_in in enumerate(txs_in) if idx not in s]
if args.remove_tx_out:
s = set(args.remove_tx_out)
txs_out = [tx_out for idx, tx_out in enumerate(txs_out) if idx not in s]
tx = Tx(txs_in=txs_in, txs_out=txs_out, lock_time=lock_time, version=version, unspents=unspents)
fee = args.fee
try:
distribute_from_split_pool(tx, fee)
except ValueError as ex:
print("warning: %s" % ex.args[0], file=sys.stderr)
unsigned_before = tx.bad_signature_count()
unsigned_after = unsigned_before
if unsigned_before > 0 and key_iters:
def wif_iter(iters):
while len(iters) > 0:
for idx, iter in enumerate(iters):
try:
wif = next(iter)
yield wif
except StopIteration:
iters = iters[:idx] + iters[idx+1:]
break
print("signing...", file=sys.stderr)
sign_tx(tx, wif_iter(key_iters), p2sh_lookup=p2sh_lookup)
unsigned_after = tx.bad_signature_count()
if unsigned_after > 0:
print("warning: %d TxIn items still unsigned" % unsigned_after, file=sys.stderr)
if len(tx.txs_in) == 0:
print("warning: transaction has no inputs", file=sys.stderr)
if len(tx.txs_out) == 0:
print("warning: transaction has no outputs", file=sys.stderr)
include_unspents = (unsigned_after > 0)
tx_as_hex = tx.as_hex(include_unspents=include_unspents)
if args.output_file:
f = args.output_file
if f.name.endswith(".hex"):
f.write(tx_as_hex.encode("utf8"))
else:
tx.stream(f)
if include_unspents:
tx.stream_unspents(f)
f.close()
#.........这里部分代码省略.........
示例8: print
# 需要导入模块: from pycoin.tx import Tx [as 别名]
# 或者: from pycoin.tx.Tx import as_hex [as 别名]
print(address)
# Filling up the new created address with the fake coinbase transaction. No signature rquired.
# Very important part. When you move funds to a p2sh address you write a special scriptPubKey:
# Instead of: OP_DUP OP_HASH160 <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG
# your p2sh scriptPubKey will be:
# OP_HASH160 <hash(redeemScript)> OP_EQUAL
# standard_tx_out_script(address) gives the scriptPubKey for a given multisig address
script = standard_tx_out_script(address)
# Fake coinbase transaction to fill our p2sh address
# It it is a coinbase transaction we put in a newly constructed block.
tx_in = TxIn.coinbase_tx_in(script=b'')
print("TxIn: %s" % tx_in.__str__())
tx_out = TxOut(1000000, script)
print("TxOut: %s" % tx_out.__str__())
tx1 = Tx(version=1, txs_in=[tx_in], txs_out=[tx_out])
tx1.as_hex()
# we now have an UTXO redeemable by supplying the script and the required sigs.
# tx_utils.create_tx() allows to spend all the UTXO from the preavious tx to an arbitrary address.
tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [keys[-1].address()])
# to split the input in each of the generated addresses
# tx2 = tx_utils.create_tx(tx1.tx_outs_as_spendable(), [keys[i].address() for i in range(len(keys))])
print("unsigned transaction:")
print("bad signatures: %s" % tx2.bad_signature_count())
print(tx2.as_hex())
for i in range(1, N+1):
print("signining with key number: %s" % i)
hash160_lookup = build_hash160_lookup([keys[i].secret_exponent()])
p2sh_lookup = build_p2sh_lookup([underlying_script])
tx2.sign(hash160_lookup=hash160_lookup,p2sh_lookup=p2sh_lookup)
print(tx2.as_hex())
print("This transactions have now : %s signature of %t necessary" % (i, N))