本文整理汇总了Python中ethereum.transactions.Transaction.sign方法的典型用法代码示例。如果您正苦于以下问题:Python Transaction.sign方法的具体用法?Python Transaction.sign怎么用?Python Transaction.sign使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ethereum.transactions.Transaction
的用法示例。
在下文中一共展示了Transaction.sign方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sendout
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def sendout(self):
log.debug("Sendout ping")
if not self.__awaiting:
return
payments = self.__awaiting # FIXME: Should this list be synchronized?
self.__awaiting = []
addr = keys.privtoaddr(self.__privkey) # TODO: Should be done once?
nonce = self.__client.get_transaction_count(addr.encode('hex'))
p, value = _encode_payments(payments)
data = bank_contract.encode('transfer', [p])
gas = 21000 + len(p) * 30000
tx = Transaction(nonce, self.GAS_PRICE, gas, to=self.BANK_ADDR,
value=value, data=data)
tx.sign(self.__privkey)
h = tx.hash
log.info("Batch payments: {}".format(h.encode('hex')))
# Firstly write transaction hash to database. We need the hash to be
# remembered before sending the transaction to the Ethereum node in
# case communication with the node is interrupted and it will be not
# known if the transaction has been sent or not.
with Payment._meta.database.transaction():
for payment in payments:
assert payment.status == PaymentStatus.awaiting
payment.status = PaymentStatus.sent
payment.details['tx'] = h.encode('hex')
payment.save()
tx_hash = self.__client.send(tx)
assert tx_hash[2:].decode('hex') == h # FIXME: Improve Client.
self.__inprogress[h] = payments
示例2: send_transaction
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def send_transaction(self, sender, to, value=0, data='', startgas=0, gasprice=10 * denoms.szabo):
"can send a locally signed transaction if privkey is given"
assert self.privkey or sender
if self.privkey:
_sender = sender
sender = privtoaddr(self.privkey)
assert sender == _sender
assert sender
# fetch nonce
nonce = self.nonce(sender)
if not startgas:
startgas = quantity_decoder(self.call('eth_gasLimit')) - 1
# create transaction
tx = Transaction(nonce, gasprice, startgas, to=to, value=value, data=data)
if self.privkey:
tx.sign(self.privkey)
tx_dict = tx.to_dict()
tx_dict.pop('hash')
for k, v in dict(gasprice='gasPrice', startgas='gas').items():
tx_dict[v] = tx_dict.pop(k)
tx_dict['sender'] = sender
res = self.eth_sendTransaction(**tx_dict)
assert len(res) in (20, 32)
return res.encode('hex')
示例3: send_transaction
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def send_transaction(
self,
sender: address,
to: address,
value: int = 0,
data: bytes = b'',
startgas: int = 0,
gasprice: int = GAS_PRICE,
nonce: Optional[int] = None):
""" Helper to send signed messages.
This method will use the `privkey` provided in the constructor to
locally sign the transaction. This requires an extended server
implementation that accepts the variables v, r, and s.
"""
if not self.privkey and not sender:
raise ValueError('Either privkey or sender needs to be supplied.')
if self.privkey:
privkey_address = privatekey_to_address(self.privkey)
sender = sender or privkey_address
if sender != privkey_address:
raise ValueError('sender for a different privkey.')
if nonce is None:
nonce = self.nonce(sender)
else:
if nonce is None:
nonce = 0
if not startgas:
startgas = self.gaslimit() - 1
tx = Transaction(nonce, gasprice, startgas, to=to, value=value, data=data)
if self.privkey:
tx.sign(self.privkey)
result = self.call(
'eth_sendRawTransaction',
data_encoder(rlp.encode(tx)),
)
return result[2 if result.startswith('0x') else 0:]
else:
# rename the fields to match the eth_sendTransaction signature
tx_dict = tx.to_dict()
tx_dict.pop('hash')
tx_dict['sender'] = sender
tx_dict['gasPrice'] = tx_dict.pop('gasprice')
tx_dict['gas'] = tx_dict.pop('startgas')
res = self.eth_sendTransaction(**tx_dict)
assert len(res) in (20, 32)
return hexlify(res)
示例4: test_eth_sendRawTransaction
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def test_eth_sendRawTransaction(accounts, rpc_client):
tx = Transaction(0, tester.gas_price, tester.gas_limit, accounts[1], 1234, '')
tx.sign(tester.keys[0])
raw_tx = rlp.encode(tx)
raw_tx_hex = encode_data(raw_tx)
result = rpc_client('eth_sendRawTransaction', params=[raw_tx_hex])
assert len(result) == 66
示例5: direct
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def direct(o, recipient, value):
nonce = o.eth.get_transaction_count(o.me.address.encode('hex'))
print "NONCE", nonce
print "VALUE", value
tx = Transaction(nonce, 1, 21000, to=recipient, value=value,
data='')
tx.sign(o.me.priv)
print o.eth.send(tx)
gevent.sleep(1) # FIXME: Wait for confirmed transaction receipt.
示例6: faucet_send
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def faucet_send(o, to, value):
value = int(value * denoms.ether)
nonce = o.eth.get_transaction_count(Faucet.ADDR.encode('hex'))
to = normalize_address(to)
tx = Transaction(nonce, 1, 21000, to, value, '')
tx.sign(Faucet.PRIVKEY)
r = o.eth.send(tx)
print "Transaction sent:", r
gevent.sleep(10)
示例7: faucet
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def faucet(o):
nonce = o.eth.get_transaction_count(Faucet.ADDR.encode('hex'))
print "NONCE", nonce
if nonce == 0: # Deploy Bank of Deposit contract
tx = Transaction(nonce, 1, 3141592, to='', value=0,
data=BankOfDeposit.INIT_HEX.decode('hex'))
tx.sign(Faucet.PRIVKEY)
o.eth.send(tx)
addr = tx.creates
assert addr == "cfdc7367e9ece2588afe4f530a9adaa69d5eaedb".decode('hex')
print "ADDR", addr.encode('hex')
示例8: gimme_money
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def gimme_money(ethnode, addr, value):
nonce = ethnode.get_transaction_count(Faucet.ADDR.encode('hex'))
addr = normalize_address(addr)
tx = Transaction(nonce, 1, 21000, addr, value, '')
tx.sign(Faucet.PRIVKEY)
h = ethnode.send(tx)
log.info("Faucet --({} ETH)--> {} ({})".format(float(value) / 10**18,
addr.encode('hex'), h))
h = h[2:].decode('hex')
assert h == tx.hash
return h
示例9: send_transaction
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def send_transaction(sender, to, value=0, data='', startgas=3141592,
gasprice=GAS_PRICE, nonce=None):
"""Custom implementation for `pyethapp.rpc_client.JSONRPCClient.send_transaction`.
This is necessary to support other remotes that don't support pyethapp's extended specs.
@see https://github.com/ethereum/pyethapp/blob/develop/pyethapp/rpc_client.py#L359
"""
nonce = int(client.call('eth_getTransactionCount', encode_hex(sender), 'pending'), 16) + nonce_offset
tx = Transaction(nonce, gasprice, startgas, to, value, data)
assert hasattr(client, 'privkey') and client.privkey
tx.sign(client.privkey)
result = client.call('eth_sendRawTransaction', rlp.encode(tx).encode('hex'))
return result[2 if result.startswith('0x') else 0:]
示例10: test_eth_sendRawTransaction
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def test_eth_sendRawTransaction(hex_accounts, client):
tx = Transaction(0, tester.gas_price, tester.gas_limit, tester.accounts[1], 1234, '')
tx.sign(tester.keys[0])
raw_tx = rlp.encode(tx)
raw_tx_hex = encode_data(raw_tx)
tx_hash = client.send_raw_transaction(raw_tx_hex)
assert tx_hash
tx_data = client.get_transaction_by_hash(tx_hash)
assert tx_data['hash'] == tx_hash
assert tx_data['from'] == hex_accounts[0]
assert tx_data['to'] == hex_accounts[1]
示例11: deliver
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def deliver(self, enc_num, to):
# nonce = number of transactions already sent by that account
head = self.app.services.chain.chain.head
nonce = head.get_nonce(self.my_addr)
# Took from buterin example:
# https://blog.ethereum.org/2014/04/10/pyethereum-and-serpent-programming-guide/
gasprice = 10**12
# Took from buterin example:
# https://blog.ethereum.org/2014/04/10/pyethereum-and-serpent-programming-guide/
startgas = 10000
value = 0 # It's just a message, don't need to send any value (TODO: confirm that info)
# data is a json formatted message but has to be 'binary'
unix_now = int(round(time()))
payload = {}
payload['when'] = unix_now
payload['number'] = enc_num
payload['publish_on'] = unix_now + 86400 # in 24 hours
payload['published_at'] = 'http://www.example.com/foo'
data = json.dumps(payload)
deliver_tx = Transaction(nonce, gasprice, startgas, to, value, data)
signed_deliver_tx = deliver_tx.sign(self.privkey_hex)
success, output = apply_transaction(head, signed_deliver_tx)
示例12: test_create_gnt
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def test_create_gnt(chain):
owner_addr, receiver_addr, gnt, gntb, cdep = mysetup(chain)
faucet, _ = chain.provider.get_or_deploy_contract('Faucet',
deploy_args=[gnt.address])
assert gnt.call().balanceOf(faucet.address) == 0
chain.wait.for_receipt(gnt.transact({'from': encode_hex(ethereum.tester.a0)}).transfer(
faucet.address, 1000 * utils.denoms.ether ))
assert gnt.call().balanceOf(faucet.address) == 1000 * utils.denoms.ether
key = sha3(to_string(11))
account = privtoaddr(key)
ethereum.tester.accounts.append(account)
ethereum.tester.keys.append(key)
assert chain.web3.eth.getBalance(encode_hex(account)) == 0
previousA0 = chain.web3.eth.getBalance(encode_hex(ethereum.tester.a0))
assert previousA0 > utils.denoms.ether
tx = Transaction(
nonce=chain.web3.eth.getTransactionCount(ethereum.tester.a0),
gasprice=chain.web3.eth.gasPrice,
startgas=100000,
to=encode_hex(account),
value=utils.denoms.ether,
data=b'',
)
tx.sign(ethereum.tester.k0)
raw_tx = rlp.encode(tx)
raw_tx_hex = chain.web3.toHex(raw_tx)
chain.web3.eth.sendRawTransaction(raw_tx_hex)
assert gnt.call().balanceOf(faucet.address) == 1000 * utils.denoms.ether
assert chain.web3.eth.getBalance(encode_hex(account)) == utils.denoms.ether
assert gnt.call().decimals() == 18
assert gnt.call().balanceOf(encode_hex(account)) == 0
tx = chain.wait.for_receipt(
faucet.transact({'from': encode_hex(account)}).create())
assert gnt.call().balanceOf(encode_hex(account)) == 1000 * utils.denoms.ether
assert gnt.call().balanceOf(faucet.address) == 0
示例13: signed_tx_example
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def signed_tx_example():
from ethereum.transactions import Transaction
from pyethapp.accounts import mk_privkey, privtoaddr
secret_seed = "wow"
privkey = mk_privkey(secret_seed)
sender = privtoaddr(privkey)
# fetch nonce
nonce = quantity_decoder(JSONRPCClient().call("eth_getTransactionCount", address_encoder(sender), "pending"))
# create transaction
tx = Transaction(nonce, default_gasprice, default_startgas, to=z_address, value=100, data="")
tx.sign(privkey)
tx_dict = tx.to_dict()
tx_dict.pop("hash")
res = JSONRPCClient().eth_sendTransaction(**tx_dict)
if len(res) == 20:
print "contract created @", res.encode("hex")
else:
assert len(res) == 32
print "tx hash", res.encode("hex")
示例14: multi
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def multi(o, payments):
print "multi payment"
data = ''
encp = []
value = 0
for p in payments:
p = p.split(':')
print "->", p[0], p[1]
encp.append(encode_payment(p[0], p[1]))
value += long(p[1])
nonce = o.eth.get_transaction_count(o.me.address.encode('hex'))
translator = abi.ContractTranslator(BankOfDeposit.ABI)
data = translator.encode('transfer', [encp])
print "DATA: ", data.encode('hex')
gas = 21000 + len(encp) * 30000
tx = Transaction(nonce, 1, gas, to=BANK_ADDR, value=value, data=data)
tx.sign(o.me.priv)
print o.eth.send(tx)
示例15: signed_tx_example
# 需要导入模块: from ethereum.transactions import Transaction [as 别名]
# 或者: from ethereum.transactions.Transaction import sign [as 别名]
def signed_tx_example(to=z_address, value=100):
from ethereum.transactions import Transaction
from pyethapp.accounts import mk_privkey, privtoaddr
secret_seed = 'wow'
privkey = mk_privkey(secret_seed)
sender = privtoaddr(privkey)
# fetch nonce
nonce = quantity_decoder(
JSONRPCClient().call('eth_getTransactionCount', address_encoder(sender), 'pending'))
# create transaction
tx = Transaction(nonce, default_gasprice, default_startgas, to=z_address, value=value, data='')
tx.sign(privkey)
tx_dict = tx.to_dict()
tx_dict.pop('hash')
res = JSONRPCClient().eth_sendTransaction(**tx_dict)
if len(res) == 20:
print 'contract created @', res.encode('hex')
else:
assert len(res) == 32
print 'tx hash', res.encode('hex')