mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-02 13:43:06 +00:00
Verify signatures
This commit is contained in:
parent
d8bb98768a
commit
48e27a9bd1
@ -34,6 +34,9 @@ proc recover*(signature: Signature, state: State): ?EthAddress =
|
||||
let hash = hashMessage(hashState(state))
|
||||
recover(signature, hash)?.toAddress
|
||||
|
||||
proc verify*(signature: Signature, state: State, signer: EthAddress): bool =
|
||||
recover(signature, state) == signer.some
|
||||
|
||||
proc `$`*(signature: Signature): string =
|
||||
var bytes = signature.toRaw()
|
||||
bytes[64] += 27
|
||||
|
||||
@ -18,12 +18,21 @@ suite "signature":
|
||||
check recover(signature, SkMessage(hash)).tryGet() == publicKey
|
||||
|
||||
test "recovers ethereum address from signature":
|
||||
let state1, state2 = State.example
|
||||
let state = State.example
|
||||
let key = PrivateKey.random()
|
||||
let address = key.toPublicKey.toAddress
|
||||
let signature = key.sign(state1)
|
||||
check recover(signature, state1) == address.some
|
||||
check recover(signature, state2) != address.some
|
||||
let signature = key.sign(state)
|
||||
check recover(signature, state) == address.some
|
||||
check recover(signature, State.example) != address.some
|
||||
|
||||
test "verifies signatures":
|
||||
let state = State.example
|
||||
let key = PrivateKey.random()
|
||||
let address = key.toPublicKey.toAddress
|
||||
let signature = key.sign(state)
|
||||
check verify(signature, state, address)
|
||||
check not verify(signature, state, EthAddress.example)
|
||||
check not verify(signature, State.example, address)
|
||||
|
||||
test "produces the same signatures as the javascript implementation":
|
||||
let state =State(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user