def sign(data: bytes, private_key_seed_ascii: str, hash_function=sha256_msg): """Sign data using Ethereum private key. :param private_key_seed_ascii: Private key seed as ASCII string """ priv_key = PrivateKey(Web3.sha3(text=private_key_seed_ascii)) msghash = hash_function(data) signature = priv_key.sign_msg_hash(msghash) v, r, s = signature.vrs # assuming chainID is 1 i.e the main net # TODO: take in chainID as a param, so that v is set appropriately # currently there's no good way to determine chainID v = to_eth_v(v) r_bytes = to_bytes(r) s_bytes = to_bytes(s) # Make sure we use bytes data and zero padding stays # good across different systems r_hex = binascii.hexlify(r_bytes).decode("ascii") s_hex = binascii.hexlify(s_bytes).decode("ascii") # Convert to Etheruem address format pub_key = priv_key.public_key addr = pub_key.to_checksum_address() pub = pub_key.to_bytes() # # Return various bits about signing so it's easier to debug return { "signature": signature, "v": v, "r": r, "s": s, "r_bytes": r_bytes, "s_bytes": s_bytes, "r_hex": "0x" + r_hex, "s_hex": "0x" + s_hex, "address_bitcoin": addr, "address_ethereum": get_ethereum_address_from_private_key(private_key_seed_ascii), "public_key": pub, "hash": msghash, "payload": binascii.hexlify(bytes([v] + list(r_bytes) + list(s_bytes, ))) }
def sign(self, key_bytes): key = PrivateKey(key_bytes) content = self.build_message(key.public_key) result = { 'author': key.public_key.to_checksum_address(), 'raw_transaction': content, 'signature': key.sign_msg_hash(Web3.keccak(text=content)), } # very confused where this data goes, but trying this for now result.update(self.content) return result
def sign_block_header(header: BlockHeaderAPI, private_key: PrivateKey) -> BlockHeaderAPI: signature_hash = get_signature_hash(header) signature = private_key.sign_msg_hash(signature_hash) signers = get_signers_at_checkpoint(header) signed_extra_data = b''.join(( header.extra_data[:VANITY_LENGTH], b''.join(signers), signature.to_bytes(), )) return header.copy(extra_data=signed_extra_data)
def test_sha3(): commitLastBlock = unhexlify('%010x' % 100) # 和uint40对应 print(commitLastBlock.hex()) commit = keccak(unhexlify('%064x' % 99)) print('0x' + commit.hex()) privateKey = unhexlify( 'dbbad2a5682517e4ff095f948f721563231282ca4179ae0dfea1c76143ba9607') pk = PrivateKey(privateKey, CoinCurveECCBackend) msg = commitLastBlock + commit # 00000000640b42b6393c1f53060fe3ddbfcd7aadcca894465a5a438f69c87d790b2299b9b2 print('msg: {} '.format(msg.hex())) sh = keccak(commitLastBlock + commit) print('sh: {}'.format(sh.hex())) sig = pk.sign_msg_hash(message_hash=sh) print('sig: {}'.format(sig.to_bytes().hex())) # right: 0x6d9d45f732dbf3db243496c5b854e4cd3faaeace4da533cc07b723ddf046ad33 pass
def new_sign(privkey, msg_hash): pk = PrivateKey(privkey, CoinCurveECCBackend) sig = pk.sign_msg_hash(msg_hash) return sig
def test_placeBet(conftest_args, abi): assert len(hrc20_contract_address) > 0 contract_address = Address(hrc20_contract_address[0]) htdfrpc = HtdfRPC(chaid_id=conftest_args['CHAINID'], rpc_host=conftest_args['RPC_HOST'], rpc_port=conftest_args['RPC_PORT']) hc = HtdfContract(rpc=htdfrpc, address=contract_address, abi=abi) new_to_addr = HtdfPrivateKey('').address from_addr = Address(conftest_args['ADDRESS']) private_key = HtdfPrivateKey(conftest_args['PRIVATE_KEY']) from_acc = htdfrpc.get_account_info(address=from_addr.address) #################### blk = htdfrpc.get_latest_block() # last_block_number = int(blk['block_meta']['header']['height']) last_block_number = 100 reveal = 99 #int(os.urandom(32).hex(), 16) commitLastBlock = unhexlify('%010x' % last_block_number) # 和uint40对应 commit = keccak(unhexlify('%064x' % reveal)) print('0x' + commit.hex()) privateKey = unhexlify( 'dbbad2a5682517e4ff095f948f721563231282ca4179ae0dfea1c76143ba9607') pk = PrivateKey(privateKey, CoinCurveECCBackend) sh = keccak(commitLastBlock + commit) print('sh ==========> {}'.format(sh.hex())) sig = pk.sign_msg_hash(message_hash=sh) print('"0x' + sig.to_bytes()[:32].hex() + '"') print('"0x' + sig.to_bytes()[32:-1].hex() + '"') print(sig.to_bytes()[-1]) r = sig.to_bytes()[:32] s = sig.to_bytes()[32:-1] v = sig.to_bytes()[-1] + 27 ###################### placeBetTx = hc.functions.placeBet( betMask=1, modulo=2, commitLastBlock=last_block_number, commit=int(commit.hex(), 16), r=r, s=s, # v = v, ).buildTransaction_htdf() data = remove_0x_prefix(placeBetTx['data']) print('========> data{}'.format(remove_0x_prefix(placeBetTx['data']))) # data = '227ada370000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000640b42b6393c1f53060fe3ddbfcd7aadcca894465a5a438f69c87d790b2299b9b22e07166e8a445db2a2a805af052fbbed92ad61d3cb1f81ebc1eaf927c6474f507996175577577bfe8782299a3153327e56cc28e108f4c9105b646e46d5179557000000000000000000000000000000000000000000000000000000000000001c' signed_tx = HtdfTxBuilder( from_address=from_addr, to_address=contract_address, amount_satoshi=2 * 10**8, sequence=from_acc.sequence, account_number=from_acc.account_number, chain_id=htdfrpc.chain_id, gas_price=100, gas_wanted=5000000, data=data, memo='test_hrc20_transfer').build_and_sign(private_key=private_key) tx_hash = htdfrpc.broadcast_tx(tx_hex=signed_tx) print('tx_hash: {}'.format(tx_hash)) tx = htdfrpc.get_tranaction_until_timeout(transaction_hash=tx_hash) pprint(tx) pass
def sign_message(message: str, private_key: PrivateKey): return private_key.sign_msg_hash(Transaction.hash(message))