parent
b99fd88deb
commit
6331b04cb4
|
@ -163,7 +163,7 @@ proc getKey*(key: PrivateKey): CryptoResult[PublicKey] =
|
||||||
let eckey = ? key.eckey.getKey().orError(KeyError)
|
let eckey = ? key.eckey.getKey().orError(KeyError)
|
||||||
ok(PublicKey(scheme: ECDSA, eckey: eckey))
|
ok(PublicKey(scheme: ECDSA, eckey: eckey))
|
||||||
of Secp256k1:
|
of Secp256k1:
|
||||||
let skkey = ? key.skkey.getKey().orError(KeyError)
|
let skkey = key.skkey.getKey()
|
||||||
ok(PublicKey(scheme: Secp256k1, skkey: skkey))
|
ok(PublicKey(scheme: Secp256k1, skkey: skkey))
|
||||||
else:
|
else:
|
||||||
err(KeyError)
|
err(KeyError)
|
||||||
|
@ -516,7 +516,7 @@ proc sign*(key: PrivateKey, data: openarray[byte]): CryptoResult[Signature] {.gc
|
||||||
res.data = ? sig.getBytes().orError(SigError)
|
res.data = ? sig.getBytes().orError(SigError)
|
||||||
ok(res)
|
ok(res)
|
||||||
elif key.scheme == Secp256k1:
|
elif key.scheme == Secp256k1:
|
||||||
let sig = ? key.skkey.sign(data).orError(SigError)
|
let sig = key.skkey.sign(data)
|
||||||
res.data = sig.getBytes()
|
res.data = sig.getBytes()
|
||||||
ok(res)
|
ok(res)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -131,9 +131,9 @@ proc init*(t: typedesc[SkSignature], data: string): SkResult[SkSignature] =
|
||||||
var sig: SkSignature
|
var sig: SkSignature
|
||||||
sig.init(data) and ok(sig)
|
sig.init(data) and ok(sig)
|
||||||
|
|
||||||
proc getKey*(key: SkPrivateKey): SkResult[SkPublicKey] =
|
proc getKey*(key: SkPrivateKey): SkPublicKey =
|
||||||
## Calculate and return Secp256k1 `public key` from `private key` ``key``.
|
## Calculate and return Secp256k1 `public key` from `private key` ``key``.
|
||||||
ok(SkPublicKey(? SkSecretKey(key).toPublicKey()))
|
SkPublicKey(SkSecretKey(key).toPublicKey())
|
||||||
|
|
||||||
proc toBytes*(key: SkPrivateKey, data: var openarray[byte]): SkResult[int] =
|
proc toBytes*(key: SkPrivateKey, data: var openarray[byte]): SkResult[int] =
|
||||||
## Serialize Secp256k1 `private key` ``key`` to raw binary form and store it
|
## Serialize Secp256k1 `private key` ``key`` to raw binary form and store it
|
||||||
|
@ -181,22 +181,17 @@ proc getBytes*(sig: SkSignature): seq[byte] {.inline.} =
|
||||||
let length = toBytes(sig, result)
|
let length = toBytes(sig, result)
|
||||||
result.setLen(length)
|
result.setLen(length)
|
||||||
|
|
||||||
proc sign*[T: byte|char](key: SkPrivateKey, msg: openarray[T]): SkResult[SkSignature] =
|
proc sign*[T: byte|char](key: SkPrivateKey, msg: openarray[T]): SkSignature =
|
||||||
## Sign message `msg` using private key `key` and return signature object.
|
## Sign message `msg` using private key `key` and return signature object.
|
||||||
let h = sha256.digest(msg)
|
let h = sha256.digest(msg)
|
||||||
ok(SkSignature(? sign(SkSecretKey(key), h)))
|
SkSignature(sign(SkSecretKey(key), h))
|
||||||
|
|
||||||
proc verify*[T: byte|char](sig: SkSignature, msg: openarray[T],
|
proc verify*[T: byte|char](sig: SkSignature, msg: openarray[T],
|
||||||
key: SkPublicKey): bool =
|
key: SkPublicKey): bool =
|
||||||
let h = sha256.digest(msg)
|
let h = sha256.digest(msg)
|
||||||
verify(secp256k1.SkSignature(sig), h, secp256k1.SkPublicKey(key))
|
verify(secp256k1.SkSignature(sig), h, secp256k1.SkPublicKey(key))
|
||||||
|
|
||||||
proc clear*(key: var SkPrivateKey) {.borrow.}
|
func clear*(key: var SkPrivateKey) {.borrow.}
|
||||||
proc clear*(key: var SkPublicKey) {.borrow.}
|
|
||||||
proc clear*(key: var SkSignature) {.borrow.}
|
|
||||||
proc clear*(key: var SkKeyPair) {.borrow.}
|
|
||||||
|
|
||||||
proc verify*(key: SkPrivateKey): bool {.borrow.}
|
|
||||||
|
|
||||||
proc `$`*(key: SkPrivateKey): string {.borrow.}
|
proc `$`*(key: SkPrivateKey): string {.borrow.}
|
||||||
proc `$`*(key: SkPublicKey): string {.borrow.}
|
proc `$`*(key: SkPublicKey): string {.borrow.}
|
||||||
|
|
|
@ -32,11 +32,6 @@ suite "Secp256k1 testing suite":
|
||||||
rkey2 == key
|
rkey2 == key
|
||||||
rkey3 == key
|
rkey3 == key
|
||||||
rkey4 == key
|
rkey4 == key
|
||||||
rkey1.clear()
|
|
||||||
rkey2.clear()
|
|
||||||
check:
|
|
||||||
not rkey1.verify()
|
|
||||||
not rkey2.verify()
|
|
||||||
test "Public key serialize/deserialize test":
|
test "Public key serialize/deserialize test":
|
||||||
for i in 0..<TestsCount:
|
for i in 0..<TestsCount:
|
||||||
var rkey1, rkey2: SkPublicKey
|
var rkey1, rkey2: SkPublicKey
|
||||||
|
@ -54,13 +49,11 @@ suite "Secp256k1 testing suite":
|
||||||
rkey2 == pair.pubkey
|
rkey2 == pair.pubkey
|
||||||
rkey3 == pair.pubkey
|
rkey3 == pair.pubkey
|
||||||
rkey4 == pair.pubkey
|
rkey4 == pair.pubkey
|
||||||
rkey1.clear()
|
|
||||||
rkey2.clear()
|
|
||||||
test "Generate/Sign/Serialize/Deserialize/Verify test":
|
test "Generate/Sign/Serialize/Deserialize/Verify test":
|
||||||
var message = "message to sign"
|
var message = "message to sign"
|
||||||
for i in 0..<TestsCount:
|
for i in 0..<TestsCount:
|
||||||
var kp = SkKeyPair.random().expect("random key pair")
|
var kp = SkKeyPair.random().expect("random key pair")
|
||||||
var sig = kp.seckey.sign(message).expect("signature")
|
var sig = kp.seckey.sign(message)
|
||||||
var sersk = kp.seckey.getBytes()
|
var sersk = kp.seckey.getBytes()
|
||||||
var serpk = kp.pubkey.getBytes()
|
var serpk = kp.pubkey.getBytes()
|
||||||
var sersig = sig.getBytes()
|
var sersig = sig.getBytes()
|
||||||
|
|
Loading…
Reference in New Issue