本文整理汇总了Python中transaction.Transaction.as_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Transaction.as_dict方法的具体用法?Python Transaction.as_dict怎么用?Python Transaction.as_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transaction.Transaction
的用法示例。
在下文中一共展示了Transaction.as_dict方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: signtransaction
# 需要导入模块: from transaction import Transaction [as 别名]
# 或者: from transaction.Transaction import as_dict [as 别名]
def signtransaction(self, tx, privkey=None):
"""Sign a transaction. The wallet keys will be used unless a private key is provided."""
t = Transaction(tx)
if privkey:
pubkey = lbrycrd.public_key_from_private_key(privkey)
t.sign({pubkey:privkey})
else:
self.wallet.sign_transaction(t, self._password)
return t.as_dict()
示例2: gettransaction
# 需要导入模块: from transaction import Transaction [as 别名]
# 或者: from transaction.Transaction import as_dict [as 别名]
def gettransaction(self, txid):
"""Retrieve a transaction. """
tx = self.wallet.transactions.get(txid) if self.wallet else None
if tx is None and self.network:
raw = self.network.synchronous_get(('blockchain.transaction.get', [txid]))
if raw:
tx = Transaction(raw)
else:
raise BaseException("Unknown transaction")
return tx.as_dict()
示例3: signtransaction
# 需要导入模块: from transaction import Transaction [as 别名]
# 或者: from transaction.Transaction import as_dict [as 别名]
def signtransaction(self, tx, privkey=None):
"""Sign a transaction. The wallet keys will be used unless a private key is provided."""
tx = Transaction(tx)
if privkey:
pubkey = bitcoin.public_key_from_private_key(privkey)
h160 = bitcoin.hash_160(pubkey.decode('hex'))
x_pubkey = 'fd' + (chr(0) + h160).encode('hex')
tx.sign({x_pubkey:privkey})
else:
self.wallet.sign_transaction(tx, self._password)
return tx.as_dict()
示例4: main
# 需要导入模块: from transaction import Transaction [as 别名]
# 或者: from transaction.Transaction import as_dict [as 别名]
#.........这里部分代码省略.........
print('...input {} is {} BTC from {}'.format(n, Bitcoin.format_money(unspent['value']), bitcoin_input_address))
tx.addInput(tx_input)
# setup the outputs. a trigger address isn't really needed, since the encryption
# key can actually be used as the trigger (only those interested will be able to find
# the message, anyway)
# cost of the transaction is (targets + pieces/3 + sacrifice) * SPECIAL_SATOSHI
# peices/3 because we include 3 pieces per output
outputs_count = (len(bitcoin_delivery_addresses) + math.ceil(len(bitcoin_message_pieces) / 3))
approx_tx_cost = MINIMUM_SACRIFICE + outputs_count * SPECIAL_SATOSHI
if approx_tx_cost > total_input_amount:
raise Exception("not enough inputs provided")
tx_change_output = None
tx_change_output_n = None
if total_input_amount > approx_tx_cost:
print('...output (change) to {}'.format(bitcoin_change_address))
tx_change_output = TransactionOutput(bitcoin_change_address, amount=total_input_amount - approx_tx_cost)
tx_change_output_n = tx.addOutput(tx_change_output)
# The recipient will know how to handle this if they see their key...
for i, bitcoin_delivery_address in enumerate(bitcoin_delivery_addresses):
print('...output (target) to {}'.format(bitcoin_delivery_address))
tx_output = TransactionOutput(bitcoin_delivery_address, amount=SPECIAL_SATOSHI)
tx.addOutput(tx_output)
for i in range(0, len(bitcoin_message_pieces), 3):
pieces = bitcoin_message_pieces[i:i+3]
d = b''.join([p[1:] for p in pieces])
header = None
if i == 0:
header = d[:5]
d = d[5:]
if (i + 3) >= len(bitcoin_message_pieces):
if padding > 0:
d = d[:-padding]
print('...output (message) to multisig 1-of-{}{}'.format(len(pieces), ' (header={})'.format(header) if header is not None else ''))
tx_output = TransactionOutput(amount=SPECIAL_SATOSHI)
tx_output.setMultisig(pieces, 1)
tx.addOutput(tx_output)
# we should now be able to figure out how much in fees is required now that the tx is built
recommended_fee = max(MINIMUM_SACRIFICE * SPECIAL_SATOSHI, tx.getRecommendedTransactionFee(per_kb=SACRIFICE_PER_KB))
recommended_tx_cost = recommended_fee + outputs_count * SPECIAL_SATOSHI
if recommended_tx_cost > total_input_amount:
raise Exception("not enough inputs provided ({} BTC required)".format(Bitcoin.format_money(recommended_tx_cost)))
if tx_change_output is not None and recommended_tx_cost == total_input_amount:
# We can remove the output
tx.removeOutput(tx_change_output_n)
tx_change_output = None
if recommended_tx_cost < total_input_amount:
if tx_change_output is None:
print('...output (change) to {}'.format(bitcoin_change_address))
tx_change_output = TransactionOutput(bitcoin_change_address)
tx_change_output_n = tx.addOutput(tx_change_output)
tx_change_output.amount = total_input_amount - recommended_tx_cost
print('...the fee for this transaction is {} BTC'.format(Bitcoin.format_money(tx.totalInput() - tx.totalOutput())))
print('...the total sent is {} BTC (change = {})'.format(Bitcoin.format_money(tx.totalInput()), Bitcoin.format_money(0 if tx_change_output is None else tx.outputs[tx_change_output_n].amount)))
# sign all inputs
tx.sign()
print('...the transaction is {} bytes.'.format(tx.size()))
# Finally, do something with the transaction
print('\n*** Step 6. The transaction is built. What would you like to do with it?')
while True:
print('...1. Show JSON')
print('...2. Show HEX')
print('...3. Push (via blockchain.info/pushtx)')
print('...4. Quit')
try:
command = int(input('? ')) - 1
assert command >= 0 and command < 4
if command == 0:
print(json.dumps(tx.as_dict()))
elif command == 1:
print(Bitcoin.bytes_to_hexstring(tx.serialize(), reverse=False))
elif command == 2:
err = push_transaction(tx.serialize())
if isinstance(err, bool):
print("...pushed {}".format(Bitcoin.bytes_to_hexstring(tx.hash())))
else:
print("...error pushing:", err)
elif command == 3:
break
except EOFError:
break
except:
print('Try again.')
pass
print("...exiting")