rocklabs-io / ic-py

Python Agent Library for the DFINITY Internet Computer
MIT License
127 stars 26 forks source link

Example for Pem Identity import? #13

Closed bodily11 closed 2 years ago

bodily11 commented 2 years ago

Do you have an example for how to import a pem identity? I have exported a dfx identity from Plug Wallet, which downloaded as identity.pem. Now I am trying to import private key. Here is the error I get:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/yg/mzpfyl291vx30knxqlx43d2r0000gn/T/ipykernel_62451/1169095691.py in <module>
      2     private_key_1 = f.read()
      3 from ic.identity import Identity
----> 4 i1 = Identity.from_pem(private_key_1)

/opt/anaconda3/lib/python3.9/site-packages/ic/identity.py in from_pem(pem)
     34     def from_pem(pem: str):
     35         key = load_pem_private_key(pem.encode(), password=None)
---> 36         privkey = key.private_bytes(encoding=Encoding.Raw, format=PrivateFormat.Raw, encryption_algorithm=NoEncryption()).hex()
     37         return Identity(privkey=privkey, type='ed25519')
     38 

/opt/anaconda3/lib/python3.9/site-packages/cryptography/hazmat/backends/openssl/ec.py in private_bytes(self, encoding, format, encryption_algorithm)
    230         encryption_algorithm: serialization.KeySerializationEncryption,
    231     ) -> bytes:
--> 232         return self._backend._private_key_bytes(
    233             encoding,
    234             format,

/opt/anaconda3/lib/python3.9/site-packages/cryptography/hazmat/backends/openssl/backend.py in _private_key_bytes(self, encoding, format, encryption_algorithm, key, evp_pkey, cdata)
   1471         # Anything that key-specific code was supposed to handle earlier,
   1472         # like Raw.
-> 1473         raise ValueError("format is invalid with this key")
   1474 
   1475     def _private_key_bytes_via_bio(self, write_bio, evp_pkey, password):

ValueError: format is invalid with this key

And then here is the code I am running.

from ic.identity import Identity

with open('/Users/bob/Downloads/identity.pem','r') as f:
    private_key_1 = f.read()

i1 = Identity.from_pem(private_key_1)

I commented this on the old issue thinking it would reopen it, but it didn't open it, so I'm commenting again here. Thanks!

Myse1f commented 2 years ago

First, I notice that you are still using cryptography. Please reinstall ic-py as I alter some crypto package.

Second, it seems that plug wallet use secp256k1 algorithm to generate key. dfx does not support to import pem file exported by plug. Please refer to https://github.com/Psychedelic/plug/issues/263#issue-1031995153

It would be better if you use dfx new identity to generate identity and pem file. Though ic-py support for loading pem file from plug wallet now, I don't know whether it will cause some problem further.

bodily11 commented 2 years ago

Excellent! Thank you! Got it working and made my first NFT transfer using the Python agent. 🎉 Pretty slick.