polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
240 stars 114 forks source link

Add support for Initiating a keypair object with an Ethereum style public key or wallet address #217

Closed hyd628 closed 2 years ago

hyd628 commented 2 years ago

Currently, the keypair object only supports using SS58 public key and wallet for construction. It supports ECDSA private keys and mnemonics, but not public keys.

https://polkascan.github.io/py-substrate-interface/#substrateinterface.Keypair

This makes it impossible if someone wants to use an Ethereum style public key or wallet to initialize a keypair. It would be good if keypairs can be created using an Ethereum style wallet address or public key, to enable certain scenarios like offline signing.

hyd628 commented 2 years ago

I think I might have found the correct syntax for what I'm trying to do:

keypair = Keypair(public_key="8a2250aafb31638b19a83caa49d1ee61089dcb4b", crypto_type=KeypairType.ECDSA)

where the public key is the Ethereum wallet address.

Just confirming this is correct? If so, I or someone else can close the issue.

arjanz commented 2 years ago

It is not very explicitly mentioned, but this is indeed the correct syntax.

I tested this successfully:

keypair = Keypair.create_from_uri("/m/44'/60/0'/0", crypto_type=KeypairType.ECDSA)

keypair_public = Keypair(public_key='0x5e20a619338338772e97aa444e001043da96a43b', crypto_type=KeypairType.ECDSA)

signature = keypair.sign('test')

result = keypair_public.verify('test', signature)
# True

I will add this use case in the docs, so it can be found more easily

arjanz commented 2 years ago

@hyd628 Wondering, is this working for you?

hyd628 commented 2 years ago

Yes. keypair = Keypair(public_key="8a2250aafb31638b19a83caa49d1ee61089dcb4b", crypto_type=KeypairType.ECDSA) did what I was trying to instantiate.

Adding the syntax into the documentation should be enough, thanks.