diff --git a/libp2p/crypto/secp.nim b/libp2p/crypto/secp.nim index 7116403..5e9137f 100644 --- a/libp2p/crypto/secp.nim +++ b/libp2p/crypto/secp.nim @@ -12,9 +12,7 @@ import secp256k1, stew/byteutils, nimcrypto/hash, nimcrypto/sha2 export sha2 import stew/results - export results -export secp256k1 const SkRawPrivateKeySize* = 256 div 8 @@ -24,40 +22,56 @@ const SkRawPublicKeySize* = SkRawPrivateKeySize + 1 ## Size of public key in octets (bytes) +# This is extremely confusing but it's to avoid.. confusion between Eth standard and Secp standard type - SkPrivateKey* = SkSecretKey + SkPrivateKey* = distinct secp256k1.SkSecretKey + SkPublicKey* = distinct secp256k1.SkPublicKey + SkSignature* = distinct secp256k1.SkSignature + SkKeyPair* = distinct secp256k1.SkKeyPair -template pubkey*(v: SkKeyPair): SkPublicKey = SkPublicKey(SkKeyPair(v).pubkey) -template seckey*(v: SkKeyPair): SkPrivateKey = SkPrivateKey(SkKeyPair(v).seckey) +template pubkey*(v: SkKeyPair): SkPublicKey = SkPublicKey(secp256k1.SkKeyPair(v).pubkey) +template seckey*(v: SkKeyPair): SkPrivateKey = SkPrivateKey(secp256k1.SkKeyPair(v).seckey) + +proc random*(t: typedesc[SkPrivateKey]): SkResult[SkPrivateKey] = + ok(SkPrivateKey(? SkSecretKey.random())) + +proc random*(t: typedesc[SkKeyPair]): SkResult[SkKeyPair] = + ok(SkKeyPair(? secp256k1.SkKeyPair.random())) + +template seckey*(v: SkKeyPair): SkPrivateKey = + SkPrivateKey(secp256k1.SkKeyPair(v).seckey) + +template pubkey*(v: SkKeyPair): SkPublicKey = + SkPublicKey(secp256k1.SkKeyPair(v).pubkey) proc init*(key: var SkPrivateKey, data: openarray[byte]): SkResult[void] = ## Initialize Secp256k1 `private key` ``key`` from raw binary ## representation ``data``. - key = ? SkSecretKey.fromRaw(data) + key = SkPrivateKey(? secp256k1.SkSecretKey.fromRaw(data)) ok() proc init*(key: var SkPrivateKey, data: string): SkResult[void] = ## Initialize Secp256k1 `private key` ``key`` from hexadecimal string ## representation ``data``. - key = ? SkSecretKey.fromHex(data) + key = SkPrivateKey(? secp256k1.SkSecretKey.fromHex(data)) ok() proc init*(key: var SkPublicKey, data: openarray[byte]): SkResult[void] = ## Initialize Secp256k1 `public key` ``key`` from raw binary ## representation ``data``. - key = ? SkPublicKey.fromRaw(data) + key = SkPublicKey(? secp256k1.SkPublicKey.fromRaw(data)) ok() proc init*(key: var SkPublicKey, data: string): SkResult[void] = ## Initialize Secp256k1 `public key` ``key`` from hexadecimal string ## representation ``data``. - key = ? SkPublicKey.fromHex(data) + key = SkPublicKey(? secp256k1.SkPublicKey.fromHex(data)) ok() proc init*(sig: var SkSignature, data: openarray[byte]): SkResult[void] = ## Initialize Secp256k1 `signature` ``sig`` from raw binary ## representation ``data``. - sig = ? SkSignature.fromDer(data) + sig = SkSignature(? secp256k1.SkSignature.fromDer(data)) ok() proc init*(sig: var SkSignature, data: string): SkResult[void] = @@ -76,14 +90,14 @@ proc init*(t: typedesc[SkPrivateKey], data: openarray[byte]): SkResult[SkPrivate ## representation ``data``. ## ## Procedure returns `private key` on success. - SkSecretKey.fromRaw(data) + ok(SkPrivateKey(? SkSecretKey.fromRaw(data))) proc init*(t: typedesc[SkPrivateKey], data: string): SkResult[SkPrivateKey] = ## Initialize Secp256k1 `private key` from hexadecimal string ## representation ``data``. ## ## Procedure returns `private key` on success. - SkSecretKey.fromHex(data) + ok(SkPrivateKey(? SkSecretKey.fromHex(data))) proc init*(t: typedesc[SkPublicKey], data: openarray[byte]): SkResult[SkPublicKey] = ## Initialize Secp256k1 `public key` from raw binary @@ -119,7 +133,7 @@ proc init*(t: typedesc[SkSignature], data: string): SkResult[SkSignature] = proc getKey*(key: SkPrivateKey): SkResult[SkPublicKey] = ## Calculate and return Secp256k1 `public key` from `private key` ``key``. - key.toPublicKey() + ok(SkPublicKey(? SkSecretKey(key).toPublicKey())) proc toBytes*(key: SkPrivateKey, data: var openarray[byte]): SkResult[int] = ## Serialize Secp256k1 `private key` ``key`` to raw binary form and store it @@ -140,7 +154,7 @@ proc toBytes*(key: SkPublicKey, data: var openarray[byte]): SkResult[int] = ## Procedure returns number of bytes (octets) needed to store ## Secp256k1 public key. if len(data) >= SkRawPublicKeySize: - data[0..