mirror of
https://github.com/status-im/nim-eth-keys.git
synced 2025-02-12 17:26:27 +00:00
Tests for Signature serialization (#11)
This commit is contained in:
parent
09c42764e3
commit
0c4bc995d2
@ -153,3 +153,8 @@ proc ecdsa_recover*(msg_hash: Hash[256], sig: Signature): PublicKey =
|
||||
raise newException(ValueError, "Failed to recover public key. Is the signature correct?")
|
||||
|
||||
proc `$`*(key: PublicKey): string {.inline.} = key.toString()
|
||||
|
||||
proc `$`*(s: Signature): string =
|
||||
var data: array[65, byte]
|
||||
s.serialize(data)
|
||||
data.toHex()
|
||||
|
@ -36,6 +36,7 @@ type
|
||||
privkey*: string
|
||||
pubkey*: string
|
||||
raw_sig*: tuple[v: int, r, s: string]
|
||||
serialized_sig*: string
|
||||
|
||||
let
|
||||
MSG* = "message"
|
||||
@ -49,7 +50,8 @@ let
|
||||
v: 1,
|
||||
r: "80536744857756143861726945576089915884233437828013729338039544043241440681784",
|
||||
s: "1902566422691403459035240420865094128779958320521066670269403689808757640701"
|
||||
)
|
||||
),
|
||||
serialized_sig: "b20e2ea5d3cbaa83c1e0372f110cf12535648613b479b64c1a8c1a20c5021f380434d07ec5795e3f789794351658e80b7faf47a46328f41e019d7b853745cdfd01"
|
||||
)
|
||||
|
||||
bob* = testKeySig(
|
||||
@ -59,8 +61,9 @@ let
|
||||
v: 1,
|
||||
r: "41741612198399299636429810387160790514780876799439767175315078161978521003886",
|
||||
s: "47545396818609319588074484786899049290652725314938191835667190243225814114102"
|
||||
),
|
||||
serialized_sig: "5c48ea4f0f2257fa23bd25e6fcb0b75bbe2ff9bbda0167118dab2bb6e31ba76e691dbdaf2a231fc9958cd8edd99507121f8184042e075cf10f98ba88abff1f3601"
|
||||
)
|
||||
)
|
||||
|
||||
eve* = testKeySig(
|
||||
privkey: "876be0999ed9b7fc26f1b270903ef7b0c35291f89407903270fea611c85f515c",
|
||||
@ -69,5 +72,6 @@ let
|
||||
v: 0,
|
||||
r: "84467545608142925331782333363288012579669270632210954476013542647119929595395",
|
||||
s: "43529886636775750164425297556346136250671451061152161143648812009114516499167"
|
||||
),
|
||||
serialized_sig: "babeefc5082d3ca2e0bc80532ab38f9cfb196fb9977401b2f6a98061f15ed603603d0af084bf906b2cdf6cdde8b2e1c3e51a41af5e9adec7f3643b3f1aa2aadf00"
|
||||
)
|
||||
)
|
||||
|
@ -19,9 +19,10 @@ suite "Test key and signature data structure":
|
||||
pk = initPrivateKey(person.privkey)
|
||||
signature = pk.sign_msg(MSG)
|
||||
|
||||
check: signature.v == person.raw_sig.v
|
||||
check: signature.r == person.raw_sig.r.u256
|
||||
check: signature.s == person.raw_sig.s.u256
|
||||
check:
|
||||
signature.v == person.raw_sig.v
|
||||
signature.r == person.raw_sig.r.u256
|
||||
signature.s == person.raw_sig.s.u256
|
||||
|
||||
test "Signing from private key object (ported from official eth-keys)":
|
||||
for person in [alice, bob, eve]:
|
||||
@ -32,15 +33,15 @@ suite "Test key and signature data structure":
|
||||
check: verify_msg(pk.public_key, MSG, signature)
|
||||
|
||||
test "Hash signing from private key object":
|
||||
|
||||
for person in [alice, bob, eve]:
|
||||
let
|
||||
pk = initPrivateKey(person.privkey)
|
||||
signature = pk.sign_msg(MSG)
|
||||
|
||||
check: signature.v == person.raw_sig.v
|
||||
check: signature.r == person.raw_sig.r.u256
|
||||
check: signature.s == person.raw_sig.s.u256
|
||||
check:
|
||||
signature.v == person.raw_sig.v
|
||||
signature.r == person.raw_sig.r.u256
|
||||
signature.s == person.raw_sig.s.u256
|
||||
|
||||
test "Hash signing from private key object (ported from official eth-keys)":
|
||||
for person in [alice, bob, eve]:
|
||||
@ -69,3 +70,18 @@ suite "Test key and signature data structure":
|
||||
recovered_pubkey = recover_pubkey_from_msg(MSGHASH, signature)
|
||||
|
||||
check: pk.public_key == recovered_pubkey
|
||||
|
||||
test "Signature serialization and deserialization":
|
||||
for person in [alice, bob, eve]:
|
||||
let
|
||||
pk = initPrivateKey(person.privkey)
|
||||
signature = pk.sign_msg(MSG)
|
||||
deserializedSignature = parseSignature(hexToSeqByteBE(person.serialized_sig))
|
||||
|
||||
var serialized_sig: array[65, byte]
|
||||
signature.serialize(serialized_sig)
|
||||
|
||||
check:
|
||||
signature == deserializedSignature
|
||||
serialized_sig.toHex() == person.serialized_sig
|
||||
$signature == person.serialized_sig
|
||||
|
Loading…
x
Reference in New Issue
Block a user