icon-project / icon-sdk-python

ICON SDK for Python
45 stars 45 forks source link

Could not load wallet from private key #53

Closed sangduongvan closed 4 years ago

sangduongvan commented 4 years ago

when I try to load wallet from private key like that KeyWallet.load(b'1dceb0ef559ac6372a258f6118b44596428c78bea7844fad085af432c48c2315').

It keeps should me error: raise ValueError('Secret scalar must be greater than 0 and less than {}.'.format(GROUP_ORDER_INT)) ValueError: Secret scalar must be greater than 0 and less than 115792089237316195423570985008687907852837564279074904382605163141518161494337

I not sure what it's

Spl3en commented 4 years ago

You probably want to do that instead :

KeyWallet.load(b'\x1d\xce\xb0\xef\x55\x9a\xc6\x37\x2a\x25\x8f\x61\x18\xb4\x45\x96\x42\x8c\x78\xbe\xa7\x84\x4f\xad\x08\x5a\xf4\x32\xc4\x8c\x23\x15')
sangduongvan commented 4 years ago

You probably want to do that instead :

KeyWallet.load(b'\x1d\xce\xb0\xef\x55\x9a\xc6\x37\x2a\x25\x8f\x61\x18\xb4\x45\x96\x42\x8c\x78\xbe\xa7\x84\x4f\xad\x08\x5a\xf4\x32\xc4\x8c\x23\x15')

Sorry, but why need to add \x to my private key string?

boyeon555 commented 4 years ago

You loaded your wallet by your own private key in hexadecimal. But it is the wrong way. To load a wallet by a private key on Python-SDK, the param of 'private_key' on the 'load' API method should be bytes.

def load(private_key: bytes) -> 'KeyWallet':

You need to load a wallet with the private key as below.

# Private key in bytes
PRIVATE_KEY_FOR_TEST = bytes.fromhex("592eb276d534e2c41a2d9356c0ab262dc233d87e4dd71ce705ec130a8d27ff0c")

# Loads a wallet from a key store file
wallet1 = KeyWallet.load(PRIVATE_KEY_FOR_TEST)
print("[wallet1] address: ", wallet1.get_address(), " private key: ", wallet1.get_private_key())
sangduongvan commented 4 years ago

You loaded your wallet by your own private key in hexadecimal. But it is the wrong way. To load a wallet by a private key on Python-SDK, the param of 'private_key' on the 'load' API method should be bytes.

def load(private_key: bytes) -> 'KeyWallet':

You need to load a wallet with the private key as below.

# Private key in bytes
PRIVATE_KEY_FOR_TEST = bytes.fromhex("592eb276d534e2c41a2d9356c0ab262dc233d87e4dd71ce705ec130a8d27ff0c")

# Loads a wallet from a key store file
wallet1 = KeyWallet.load(PRIVATE_KEY_FOR_TEST)
print("[wallet1] address: ", wallet1.get_address(), " private key: ", wallet1.get_private_key())

Great, it works now thanks, but I do believe sdk should add method load private key directly from hex, it's more natural you know.