Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Ethereum Keypairs #141

Closed
hyd628 opened this issue Oct 13, 2021 · 9 comments
Closed

Add support for Ethereum Keypairs #141

hyd628 opened this issue Oct 13, 2021 · 9 comments

Comments

@hyd628
Copy link

hyd628 commented Oct 13, 2021

We would like to use this library with EVM compatible parachains such as Moonbeam, but need support for Ethereum Keypairs to be included.

In this PR to Substrate: paritytech/substrate#8615, the parachain's types will be included in the Substrate metadata, so the types can be obtained at runtime. Thus the only additional change for py-substrate-interface to work with EVM compatible parachains is to add support for Ethereum keypairs (ECDSA) here.

This would be a good feature to have given the popularity of Pallet EVM and Frontier and the number of parachains that have some compatibility with EVM and Ethereum keypairs.

@arjanz
Copy link
Member

arjanz commented Oct 22, 2021

I have looked into adding ECDSA keypairs to the library before, but it was a bit more work than I thought so set it aside for the time being. With the arriving of EVM compatible parachains it is indeed more relevant, it is on the todo list. Of course a proof of concept PR would also be appreciated! :)

arjanz added a commit that referenced this issue Nov 5, 2021
Generate ECDSA Keypairs and sign extrinsics on for example Moonriver
arjanz added a commit that referenced this issue Nov 8, 2021
* Added support for ECDSA keypairs #141

Generate ECDSA Keypairs and sign extrinsics on for example Moonriver

* Fixed incorrect scalecodec version

* Added missing pycryptodome dependency

* Fixed incorrect signature format

* Raise ValueError when empty string in ss58_decode #151

* Updated unit test
@arjanz
Copy link
Member

arjanz commented Nov 8, 2021

Support for ECDSA keypairs added in https://github.com/polkascan/py-substrate-interface/releases/tag/v1.1

Feedback would be appreciated..

@hyd628
Copy link
Author

hyd628 commented Nov 8, 2021

Awesome. We will try a couple of things with it and let you know.

@arjanz
Copy link
Member

arjanz commented Nov 9, 2021

I received some comments that there was some trouble submitting transactions to Moonbase Alpha testnet. I will look for a way to achieve this a bit more elegant, but for now you can config the lib correctly with:

substrate = SubstrateInterface(
    url=MOONBASE_ALPHA_WS_URL
)
substrate.init_runtime()
substrate.runtime_config.update_type_registry({
    "types": {
        "Address": "H160",
        "LookupSource": "H160",
        "AccountId": "H160",
        "ExtrinsicSignature": "EcdsaSignature",
    }
})

@arjanz
Copy link
Member

arjanz commented Nov 26, 2021

@hyd628 Did you ran into any issues? Otherwise I'll close the ticket..

@hyd628
Copy link
Author

hyd628 commented Nov 27, 2021

Thanks, the changes look good, but I did run into a few issues:

  1. I can't get Keypair.create_from_private_key(privatekey, crypto_type=KeypairType.ED25519) to work, it's throwing this error:

Traceback (most recent call last):
File "/Users/henryduong/Documents/workspace/pysubstrateinterface/sendtransaction.py", line 29, in
keypair = Keypair.create_from_private_key(privatekey, crypto_type=KeypairType.ED25519)
File "/usr/local/lib/python3.9/site-packages/substrateinterface/base.py", line 302, in create_from_private_key
return cls(
File "/usr/local/lib/python3.9/site-packages/substrateinterface/base.py", line 100, in init
raise ValueError('No SS58 formatted address or public key provided')
ValueError: No SS58 formatted address or public key provided

create_from_mnemonic works.

  1. .create_signed_extrinsic() currently doesn't work with Ethereum style signed transactions, right? getting this error:

File "/Users/henryduong/Documents/workspace/pysubstrateinterface/sendtransaction.py", line 45, in
extrinsic = moonbeamAPIProvider.create_signed_extrinsic(call=call, keypair=keypair)
File "/usr/local/lib/python3.9/site-packages/substrateinterface/base.py", line 1535, in create_signed_extrinsic
nonce = self.get_account_nonce(keypair.ss58_address) or 0
File "/usr/local/lib/python3.9/site-packages/substrateinterface/base.py", line 1455, in get_account_nonce
response = self.rpc_request("system_accountNextIndex", [account_address])
File "/usr/local/lib/python3.9/site-packages/substrateinterface/base.py", line 553, in rpc_request
raise SubstrateRequestException(message['error'])
substrateinterface.exceptions.SubstrateRequestException: {'code': -32602, 'message': 'Invalid params: 0x prefix is missing.'}

Here is the test script I used:

https://github.com/hyd628/pysubstrateinterface/blob/main/sendtransaction.py

@arjanz
Copy link
Member

arjanz commented Nov 27, 2021

@hyd628 I think you made a typo when chosing the crypto_type (should beKeypairType.ECDSA) .. I replaced it with:

keypair = Keypair.create_from_private_key(privatekey, crypto_type=KeypairType.ECDSA)

And it seems to work with the test script you provided. Could you confirm this?

The feedback you got was confusing though, I'll improve this so this will be easier to debug.

@arjanz
Copy link
Member

arjanz commented Nov 27, 2021

2. .create_signed_extrinsic() currently doesn't work with Ethereum style signed transactions, right?

This should also work with Ethereum (Ecdsa) signatures

@hyd628
Copy link
Author

hyd628 commented Nov 28, 2021

Everything seems to be working now, thanks this is great!

@arjanz arjanz closed this as completed Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants