Created
March 31, 2020 13:55
-
-
Save andelf/79b1d6a004ca09e98bc20cbc43ddd6d3 to your computer and use it in GitHub Desktop.
Send trx and sign offline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time as unix_time | |
from pprint import pprint | |
import requests | |
import hashlib | |
import base58 | |
import ecdsa | |
from eth_hash.auto import keccak as keccak_256 | |
# Config | |
# private key of my account | |
raw_priv_key = bytes.fromhex('d705fc17c82942f...............d12ad881bdc0e1327031976') | |
TO_ADDR = base58.b58decode_check('TEiMQZpHs4N4HuTKP3xcCKZ68XSQSfEbMW').hex() | |
AMOUNT = 1000 | |
# get head block as ref block | |
resp = requests.post('https://api.shasta.trongrid.io/wallet/getblockbylatestnum', json={"num": 1}) | |
block = resp.json()['block'][0] | |
# pprint(block) | |
ref_block_num = block['block_header']['raw_data']['number'] | |
ref_block_hash = bytes.fromhex(block['blockID']) | |
ref_block_num = int.to_bytes(ref_block_num, 8, 'big')[6:8].hex() | |
ref_block_hash = ref_block_hash[8:16].hex() | |
print('ref_block:', ref_block_num, ref_block_hash) | |
priv_key = ecdsa.SigningKey.from_string(raw_priv_key, curve=ecdsa.SECP256k1) | |
pub_key = priv_key.get_verifying_key().to_string() | |
print('Pub Key:', pub_key.hex()) | |
primitive_addr = b'\x41' + keccak_256(pub_key)[-20:] | |
my_addr = base58.b58encode_check(primitive_addr).decode() | |
print('Addr:', my_addr) | |
transaction = { | |
"owner_address": primitive_addr.hex(), | |
"to_address": TO_ADDR, | |
"amount": AMOUNT, | |
} | |
print('=> createtransaction') | |
resp = requests.post('https://api.shasta.trongrid.io/wallet/createtransaction', json=transaction) | |
payload = resp.json() | |
pprint(payload) | |
def signencode(r, s, order): | |
# order is stable for curves | |
print((r, s)) | |
return r.to_bytes(32, 'big') + s.to_bytes(32, 'big') | |
raw_data = bytes.fromhex(payload['raw_data_hex']) | |
signature = priv_key.sign_deterministic(raw_data, hashfunc=hashlib.sha256, sigencode=signencode) | |
print(signature) | |
# calculate v?, the rec_id | |
def verifying_key_to_addr(key): | |
pub_key = key.to_string() | |
primitive_addr = b'\x41' + keccak_256(pub_key)[-20:] | |
addr = base58.b58encode_check(primitive_addr).decode() | |
return addr | |
pub_keys = ecdsa.VerifyingKey.from_public_key_recovery( | |
signature[:64], raw_data, curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256 | |
) | |
for v, pk in enumerate(pub_keys): | |
if verifying_key_to_addr(pk) == my_addr: | |
break | |
signature += bytes([v]) | |
print('signature =', signature.hex()) | |
payload['signature'] = [signature.hex()] | |
# pprint(payload) | |
print('=> broadcasttransaction') | |
resp = requests.post('https://api.shasta.trongrid.io/wallet/broadcasttransaction', json=payload) | |
result = resp.json() | |
pprint(result) | |
if 'message' in result: | |
print('MSG:', bytes.fromhex(result['message'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment