rocklabs-io / ic-py

Python Agent Library for the DFINITY Internet Computer
MIT License
128 stars 27 forks source link

how to transfer identity.pem to hex? #5

Closed mingming-tang closed 2 years ago

mingming-tang commented 2 years ago

I want to use ic.identity.Identity, but I have identity.pem only

mircial commented 2 years ago

not supported pem yet, but I am going to add it as soon as possible

mingming-tang commented 2 years ago

hwo to user ic.identity.Identity.from_pem, I get an error: ValueError: ('Could not deserialize key data. The data may be in an incorrect format, it may be encrypted with an unsupported algorithm, or it may be an unsupported key type (e.g. EC curves with explicit parameters).', [_OpenSSLErrorWithText(code=218529960, lib=13, reason=168, reason_text=b'error:0D0680A8:asn1 encoding routines:asn1_check_tlen:wrong tag'), _OpenSSLErrorWithText(code=218640442, lib=13, reason=58, reason_text=b'error:0D08303A:asn1 encoding routines:asn1_template_noexp_d2i:nested asn1 error'), _OpenSSLErrorWithText(code=151498765, lib=9, reason=13, reason_text=b'error:0907B00D:PEM routines:PEM_read_bio_PrivateKey:ASN1 lib')])

my code:

import ic

private_key_1 = """-----BEGIN PRIVATE KEY----- MFMCAQEwBQYDK2VwBCIEIGQqNAZlORmn1k4QrYz1FvO4fOQowS3GXQMqRKDzmx9P oSMDIQCrO5iGM5hnLWrHavywoXekAoXPpYRuB0Dr6DjZF6FZkg== -----END PRIVATE KEY-----"""

private_key_2 = """-----BEGIN PRIVATE KEY----- MFMCAQEwBQYDK2VwBCIEIGQqNAZlORmn1k4QrYz1FvO4fOQowS3GXQMqRKDzmx9P -----END PRIVATE KEY-----"""

i1 = ic.identity.Identity.from_pem(private_key_1) i2 = ic.identity.Identity.from_pem(private_key_2)

ccyanxyz commented 2 years ago

We'll look into this. BTW, are the two keys in your code just for testing, or are they the ones you are actually using? Be careful of private key leakage.

bodily11 commented 2 years ago

I also would like to be able to create an identity from a PEM file. This would allow me to use other identities in the contexts of my scripts, which would be incredibly helpful.

Do you have a rough timeline on when you are hoping to support this functionality? Thank you!

ccyanxyz commented 2 years ago

Should be fixed in the next week.

bodily11 commented 2 years ago

Amazing! Can't wait. Thank you.

ccyanxyz commented 2 years ago

Fixed in #12

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)