From f22ae0a827f74ddad0e9c5ce39b64e9f93453f9f Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Sat, 31 Mar 2018 10:47:16 +0300 Subject: [PATCH] Compute pubkey from privkey, instead of keeping together, initPrivateKey (#13) * Compute pubkey from privkey, instead of keeping together * Added initPrivateKey --- src/datatypes.nim | 2 -- src/datatypes_interface.nim | 7 ++++++- src/eth_keys.nim | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/datatypes.nim b/src/datatypes.nim index 9b722f7..050e4a5 100644 --- a/src/datatypes.nim +++ b/src/datatypes.nim @@ -17,7 +17,6 @@ type PrivateKey* = object Fraw_key: array[32, byte] - Fpublic_key: PublicKey # This is exported publicly through public_key type Scalar256 = distinct array[32, byte] @@ -53,7 +52,6 @@ template genAccessors(name: untyped, fieldType, objType: typedesc): untyped = genAccessors(raw_key, array[64, byte], PublicKey) genAccessors(raw_key, array[32, byte], PrivateKey) -genAccessors(public_key, PublicKey, PrivateKey) ## If we hide the fields we need to provide a custom `==` proc diff --git a/src/datatypes_interface.nim b/src/datatypes_interface.nim index d2392ec..103cad8 100644 --- a/src/datatypes_interface.nim +++ b/src/datatypes_interface.nim @@ -30,9 +30,11 @@ else: # ################################ # Initialization +proc initPrivateKey*(data: array[32, byte]): PrivateKey {.noInit, inline.} = + result.raw_key = data + proc initPrivateKey*(hexString: string): PrivateKey {.noInit.} = hexToByteArrayBE(hexString, result.raw_key) - result.public_key = private_key_to_public_key(result) proc initPublicKey*(hexString: string): PublicKey {.noInit.} = var b: array[65, byte] @@ -60,6 +62,9 @@ proc verify_msg*(key: PublicKey, message: string, sig: Signature): bool {.inline # # ################################ # # Private key interface +proc public_key*(key: PrivateKey): PublicKey {.inline.} = + private_key_to_public_key(key) + proc sign_msg*(key: PrivateKey, message: openarray[byte]): Signature {.inline.} = let message_hash = keccak256.digest(message) ecdsa_sign(key, message_hash) diff --git a/src/eth_keys.nim b/src/eth_keys.nim index 3571ed7..afb23dc 100644 --- a/src/eth_keys.nim +++ b/src/eth_keys.nim @@ -8,11 +8,12 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. import ./datatypes -export PublicKey, PrivateKey, Signature, public_key, raw_key, `==` import ./datatypes_interface export datatypes_interface +export PublicKey, PrivateKey, Signature, public_key, raw_key, `==` + when defined(backend_native): import ttmath export ttmath