Make SkKeyPair a wrapper around secp256k1_keypair
This commit is contained in:
parent
2ed110f198
commit
858d6ca3d6
|
@ -80,8 +80,7 @@ type
|
||||||
|
|
||||||
SkKeyPair* = object
|
SkKeyPair* = object
|
||||||
## Representation of private/public keys pair.
|
## Representation of private/public keys pair.
|
||||||
seckey*: SkSecretKey
|
data*: secp256k1_keypair
|
||||||
pubkey*: SkPublicKey
|
|
||||||
|
|
||||||
SkSignature* {.requiresInit.} = object
|
SkSignature* {.requiresInit.} = object
|
||||||
## Representation of non-recoverable signature.
|
## Representation of non-recoverable signature.
|
||||||
|
@ -399,20 +398,40 @@ func toRaw*(sig: SkRecoverableSignature): array[SkRawRecoverableSignatureSize, b
|
||||||
func toHex*(sig: SkRecoverableSignature): string =
|
func toHex*(sig: SkRecoverableSignature): string =
|
||||||
toHex(toRaw(sig))
|
toHex(toRaw(sig))
|
||||||
|
|
||||||
|
func pubkey*(kp: SkKeyPair): SkPublicKey =
|
||||||
|
var key {.noinit.}: secp256k1_pubkey
|
||||||
|
let res = secp256k1_keypair_pub(secp256k1_context_no_precomp, addr key, addr kp.data)
|
||||||
|
doAssert res == 1, "Can't fail, per documentation"
|
||||||
|
SkPublicKey(data: key)
|
||||||
|
|
||||||
|
func seckey*(kp: SkKeyPair): SkSecretKey =
|
||||||
|
var key {.noinit.}: array[SkRawSecretKeySize, byte]
|
||||||
|
let res = secp256k1_keypair_sec(secp256k1_context_no_precomp, key.baseAddr, addr kp.data)
|
||||||
|
doAssert res == 1, "Can't fail, per documentation"
|
||||||
|
SkSecretKey(data: key)
|
||||||
|
|
||||||
proc random*(T: type SkKeyPair, rng: Rng): SkResult[T] =
|
proc random*(T: type SkKeyPair, rng: Rng): SkResult[T] =
|
||||||
## Generates new random key pair.
|
## Generates new random key pair.
|
||||||
let seckey = ? SkSecretKey.random(rng)
|
let seckey = ? SkSecretKey.random(rng)
|
||||||
|
|
||||||
|
var keypair {.noinit.}: secp256k1_keypair
|
||||||
|
let res = secp256k1_keypair_create(getContext(), addr keypair, addr seckey.data[0])
|
||||||
|
doAssert res == 1, "Can't fail, only fails if secret key is invalid but it was freshly generated."
|
||||||
|
|
||||||
ok(T(
|
ok(T(
|
||||||
seckey: seckey,
|
data: keypair
|
||||||
pubkey: seckey.toPublicKey()
|
|
||||||
))
|
))
|
||||||
|
|
||||||
proc random*(T: type SkKeyPair, rng: FoolproofRng): T =
|
proc random*(T: type SkKeyPair, rng: FoolproofRng): T =
|
||||||
## Generates new random key pair.
|
## Generates new random key pair.
|
||||||
let seckey = SkSecretKey.random(rng)
|
let seckey = SkSecretKey.random(rng)
|
||||||
|
|
||||||
|
var keypair {.noinit.}: secp256k1_keypair
|
||||||
|
let res = secp256k1_keypair_create(getContext(), addr keypair, addr seckey.data[0])
|
||||||
|
doAssert res == 1, "Can't fail, only fails if secret key is invalid but it was freshly generated."
|
||||||
|
|
||||||
T(
|
T(
|
||||||
seckey: seckey,
|
data: keypair
|
||||||
pubkey: seckey.toPublicKey()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func `==`*(lhs, rhs: SkPublicKey): bool =
|
func `==`*(lhs, rhs: SkPublicKey): bool =
|
||||||
|
|
|
@ -57,3 +57,7 @@ suite "secp256k1":
|
||||||
SkMessage.fromBytes([]).isErr()
|
SkMessage.fromBytes([]).isErr()
|
||||||
SkMessage.fromBytes([0'u8]).isErr()
|
SkMessage.fromBytes([0'u8]).isErr()
|
||||||
SkMessage.fromBytes(array[32, byte](msg0)).isOk()
|
SkMessage.fromBytes(array[32, byte](msg0)).isOk()
|
||||||
|
|
||||||
|
test "Keypairs":
|
||||||
|
check:
|
||||||
|
SkKeyPair.random(workingRng).isOk()
|
||||||
|
|
Loading…
Reference in New Issue