Question about signing module within pyhmy

Hi! Noob here, wasn’t sure where the signing module went on pyhmy.

Trying to sign and send transactions over the harmony blockchain through python, pyhmy seemed like the easiest way to do this although the signing module seems to be missing.

I’m sure there is a different way to send transactions over pyhmy now, just not sure where to find that information.

Any help would be great, thanks :slight_smile:

After we merge this PR: Resolve Bounty 7 (improve Harmony Python SDK) by MaxMustermann2 · Pull Request #20 · harmony-one/pyhmy · GitHub signing should be available in pyhmy. meanwhile, you could use web3.py as we are fully ethereum tooling compatible.

1 Like

I can add that you would essentially follow this: web3.eth API — Web3.py 5.19.0 documentation

With one caviat that you need to add two new fields shardID and toShardID to the tx dict.

Also you initiate class with:

from web3 import Web3
my_provider = Web3.HTTPProvider('https://api.s0.t.hmny.io')

# Then you are ready to initialize your Web3 instance, like so:

w3 = Web3(my_provider)

I didn’t have time to test this yet so let me know how it goes :slight_smile:

2 Likes

you don’t need to explicitly add the shardID and toShardID. Those are not needed while using the web3 libraries. Based on the endpoint you are using the shardIDs are automatically recognized. If you want to send cross-shard, then this won’t work and you must use pyhmy.

from web3 import Web3
w3 = Web3.HTTPProvider('https://api.s0.b.hmny.io')
tx = {
    'chainId': w3.eth.chainId,
    'nonce': w3.eth.getTransactionCount(sender_address),
    'to': receiver_address,
    'value': w3.toWei(2, "ether"),
    'gas': 21000,
    'gasPrice': w3.toWei('1', 'gwei')
}
send(address, tx, p_key)

def send(address, tx, p_key):
  signed_tx = w3.eth.account.signTransaction(tx, p_key)
  t_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
  print(w3.eth.get_transaction(t_hash))
2 Likes

Thanks @ganesha for clarifying that. As I said this was not tested yet and I was following API docs which say:

Transaction uses RLP, but adds two fields, one is shardID and the other is toShardID.

Maybe it would be worth to clarify in docs that these would be pre-populated automatically based on RPC endpoint ?

1 Like

thanks @adamwojt. can you point me to the doc that says above? will fix.

1 Like

@ganesha API - Harmony

2 Likes

Hey, I am new to harmony but using your code I get the following error:

ValueError: {‘code’: -32000, ‘message’: ‘shard do not match, txShard: 2628267298, nodeShard: 0’}

When I try to include an additional field (shardID) in the transaction, I get:

TypeError: Transaction must not include unrecognized fields: {‘shardID’}

Any ideas what’s going wrong? Thanks

Well this is weird…
I was using:

‘chainId’: 0

and changed it to:

‘chainId’: w3.eth.chainId

and now everything seems to be working…

when using web3.py you cannot use the harmony default chain ids like 0 for mainnet and 1 for testnet. Those only work for harmony specific libraries. For all ethereum libraries, best to query the chainId like w3.eth.chainId which fetches the ethereum compatible chain ids like 1666600000 for mainnet and 1666700000 for testnet.

1 Like

That makes sense, thanks for the clarification