2019-02-05 10:32:22 +00:00
|
|
|
# Nim Ethereum Keys (nim-eth-keys)
|
2020-04-04 09:40:47 +00:00
|
|
|
# Copyright (c) 2020 Status Research & Development GmbH
|
2019-02-05 10:32:22 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# - Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# - MIT license (LICENSE-MIT)
|
|
|
|
#
|
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
# This module contains adaptations of the general secp interface to help make
|
|
|
|
# working with keys and signatures as they appear in Ethereum in particular:
|
|
|
|
#
|
|
|
|
# * Public keys as serialized in uncompressed format without the initial byte
|
2022-11-16 16:44:00 +00:00
|
|
|
# * Shared secrets are serialized in raw format without the initial byte
|
2020-04-04 09:40:47 +00:00
|
|
|
# * distinct types are used to avoid confusion with the "standard" secp types
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
2020-04-02 12:40:29 +00:00
|
|
|
|
|
|
|
import
|
2021-04-06 11:33:24 +00:00
|
|
|
std/strformat,
|
2022-06-17 20:45:37 +00:00
|
|
|
secp256k1, bearssl/hash as bhash, bearssl/rand,
|
|
|
|
stew/[byteutils, objects, results],
|
2022-09-02 14:57:52 +00:00
|
|
|
./common/eth_hash
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-04-04 09:40:47 +00:00
|
|
|
from nimcrypto/utils import burnMem
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
export secp256k1, results, rand
|
2020-04-02 12:40:29 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
KeyLength* = SkEcdhRawSecretSize - 1
|
|
|
|
## Shared secret key length without format marker
|
|
|
|
RawPublicKeySize* = SkRawPublicKeySize - 1
|
|
|
|
## Size of uncompressed public key without format marker (0x04)
|
|
|
|
RawSignatureSize* = SkRawRecoverableSignatureSize
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-04-04 16:44:01 +00:00
|
|
|
RawSignatureNRSize* = SkRawSignatureSize
|
|
|
|
|
2019-02-05 10:32:22 +00:00
|
|
|
type
|
2020-04-02 12:40:29 +00:00
|
|
|
PrivateKey* = distinct SkSecretKey
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
PublicKey* = distinct SkPublicKey
|
|
|
|
## Public key that's serialized to raw format without 0x04 marker
|
|
|
|
Signature* = distinct SkRecoverableSignature
|
|
|
|
## Ethereum uses recoverable signatures allowing some space savings
|
|
|
|
SignatureNR* = distinct SkSignature
|
|
|
|
## ...but ENR uses non-recoverable signatures!
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
SharedSecretFull* = SkEcdhRawSecret
|
|
|
|
SharedSecret* = object
|
|
|
|
data*: array[KeyLength, byte]
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
KeyPair* = distinct SkKeyPair
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
template pubkey*(v: KeyPair): PublicKey = PublicKey(SkKeyPair(v).pubkey)
|
|
|
|
template seckey*(v: KeyPair): PrivateKey = PrivateKey(SkKeyPair(v).seckey)
|
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
proc newRng*(): ref HmacDrbgContext =
|
2020-07-07 08:56:26 +00:00
|
|
|
# You should only create one instance of the RNG per application / library
|
|
|
|
# Ref is used so that it can be shared between components
|
2022-06-17 20:45:37 +00:00
|
|
|
HmacDrbgContext.new()
|
|
|
|
|
|
|
|
proc random*(T: type PrivateKey, rng: var HmacDrbgContext): T =
|
2020-07-07 08:56:26 +00:00
|
|
|
let rngPtr = unsafeAddr rng # doesn't escape
|
|
|
|
proc callRng(data: var openArray[byte]) =
|
2022-06-17 20:45:37 +00:00
|
|
|
generate(rngPtr[], data)
|
2020-07-07 08:56:26 +00:00
|
|
|
|
|
|
|
T(SkSecretKey.random(callRng))
|
2020-04-04 09:40:47 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func fromRaw*(T: type PrivateKey, data: openArray[byte]): SkResult[T] =
|
2020-04-04 09:40:47 +00:00
|
|
|
SkSecretKey.fromRaw(data).mapConvert(T)
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func fromHex*(T: type PrivateKey, data: string): SkResult[T] =
|
2020-04-04 09:40:47 +00:00
|
|
|
SkSecretKey.fromHex(data).mapConvert(T)
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toRaw*(seckey: PrivateKey): array[SkRawSecretKeySize, byte] =
|
|
|
|
SkSecretKey(seckey).toRaw()
|
2020-04-04 09:40:47 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toPublicKey*(seckey: PrivateKey): PublicKey {.borrow.}
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func fromRaw*(T: type PublicKey, data: openArray[byte]): SkResult[T] =
|
2020-04-17 11:13:08 +00:00
|
|
|
if data.len() == SkRawCompressedPublicKeySize:
|
2020-06-22 16:07:48 +00:00
|
|
|
return SkPublicKey.fromRaw(data).mapConvert(T)
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
if len(data) < SkRawPublicKeySize - 1:
|
2020-04-04 09:40:47 +00:00
|
|
|
return err(static(
|
|
|
|
&"keys: raw eth public key should be {SkRawPublicKeySize - 1} bytes"))
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
var d: array[SkRawPublicKeySize, byte]
|
|
|
|
d[0] = 0x04'u8
|
|
|
|
copyMem(addr d[1], unsafeAddr data[0], 64)
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
SkPublicKey.fromRaw(d).mapConvert(T)
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func fromHex*(T: type PublicKey, data: string): SkResult[T] =
|
2020-04-04 09:40:47 +00:00
|
|
|
T.fromRaw(? seq[byte].fromHex(data))
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toRaw*(pubkey: PublicKey): array[RawPublicKeySize, byte] =
|
2020-04-04 09:40:47 +00:00
|
|
|
let tmp = SkPublicKey(pubkey).toRaw()
|
|
|
|
copyMem(addr result[0], unsafeAddr tmp[1], 64)
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toRawCompressed*(pubkey: PublicKey): array[33, byte] {.borrow.}
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2022-06-17 20:45:37 +00:00
|
|
|
proc random*(T: type KeyPair, rng: var HmacDrbgContext): T =
|
2020-07-07 08:56:26 +00:00
|
|
|
let seckey = SkSecretKey(PrivateKey.random(rng))
|
|
|
|
KeyPair(SkKeyPair(
|
|
|
|
seckey: seckey,
|
|
|
|
pubkey: seckey.toPublicKey()
|
|
|
|
))
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toKeyPair*(seckey: PrivateKey): KeyPair =
|
2020-07-07 08:56:26 +00:00
|
|
|
KeyPair(SkKeyPair(
|
|
|
|
seckey: SkSecretKey(seckey), pubkey: SkSecretKey(seckey).toPublicKey()))
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func fromRaw*(T: type Signature, data: openArray[byte]): SkResult[T] =
|
|
|
|
SkRecoverableSignature.fromRaw(data).mapConvert(T)
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func fromHex*(T: type Signature, data: string): SkResult[T] =
|
2020-04-04 09:40:47 +00:00
|
|
|
T.fromRaw(? seq[byte].fromHex(data))
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toRaw*(sig: Signature): array[RawSignatureSize, byte] {.borrow.}
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func fromRaw*(T: type SignatureNR, data: openArray[byte]): SkResult[T] =
|
|
|
|
SkSignature.fromRaw(data).mapConvert(T)
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toRaw*(sig: SignatureNR): array[RawSignatureNRSize, byte] {.borrow.}
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toAddress*(pubkey: PublicKey, with0x = true): string =
|
2019-02-05 10:32:22 +00:00
|
|
|
## Convert public key to hexadecimal string address.
|
2022-09-02 14:57:52 +00:00
|
|
|
var hash = keccakHash(pubkey.toRaw())
|
2019-02-05 10:32:22 +00:00
|
|
|
result = if with0x: "0x" else: ""
|
2020-04-02 12:40:29 +00:00
|
|
|
result.add(toHex(toOpenArray(hash.data, 12, len(hash.data) - 1)))
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func toChecksumAddress*(pubkey: PublicKey, with0x = true): string =
|
2019-02-05 10:32:22 +00:00
|
|
|
## Convert public key to checksumable mixed-case address (EIP-55).
|
|
|
|
result = if with0x: "0x" else: ""
|
2022-09-02 14:57:52 +00:00
|
|
|
var hash1 = keccakHash(pubkey.toRaw())
|
2020-04-02 12:40:29 +00:00
|
|
|
var hhash1 = toHex(toOpenArray(hash1.data, 12, len(hash1.data) - 1))
|
2022-09-02 14:57:52 +00:00
|
|
|
var hash2 = keccakHash(hhash1)
|
2020-04-02 12:40:29 +00:00
|
|
|
var hhash2 = toHex(hash2.data)
|
2019-02-05 10:32:22 +00:00
|
|
|
for i in 0..<len(hhash1):
|
|
|
|
if hhash2[i] >= '0' and hhash2[i] <= '7':
|
|
|
|
result.add(hhash1[i])
|
|
|
|
else:
|
|
|
|
if hhash1[i] >= '0' and hhash1[i] <= '9':
|
|
|
|
result.add(hhash1[i])
|
|
|
|
else:
|
|
|
|
let ch = chr(ord(hhash1[i]) - ord('a') + ord('A'))
|
|
|
|
result.add(ch)
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func validateChecksumAddress*(a: string): bool =
|
2019-02-05 10:32:22 +00:00
|
|
|
## Validate checksumable mixed-case address (EIP-55).
|
|
|
|
var address = ""
|
|
|
|
var check = "0x"
|
|
|
|
if len(a) != 42:
|
|
|
|
return false
|
|
|
|
if a[0] != '0' and a[1] != 'x':
|
|
|
|
return false
|
|
|
|
for i in 2..41:
|
|
|
|
let ch = a[i]
|
|
|
|
if ch in {'0'..'9'} or ch in {'a'..'f'}:
|
|
|
|
address &= ch
|
|
|
|
elif ch in {'A'..'F'}:
|
|
|
|
address &= chr(ord(ch) - ord('A') + ord('a'))
|
|
|
|
else:
|
|
|
|
return false
|
2022-09-02 14:57:52 +00:00
|
|
|
var hash = keccakHash(address)
|
2020-04-02 12:40:29 +00:00
|
|
|
var hexhash = toHex(hash.data)
|
2019-02-05 10:32:22 +00:00
|
|
|
for i in 0..<len(address):
|
|
|
|
if hexhash[i] >= '0' and hexhash[i] <= '7':
|
|
|
|
check.add(address[i])
|
|
|
|
else:
|
|
|
|
if address[i] >= '0' and address[i] <= '9':
|
|
|
|
check.add(address[i])
|
|
|
|
else:
|
|
|
|
let ch = chr(ord(address[i]) - ord('a') + ord('A'))
|
|
|
|
check.add(ch)
|
|
|
|
result = (check == a)
|
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
func toCanonicalAddress*(pubkey: PublicKey): array[20, byte] =
|
2019-02-05 10:32:22 +00:00
|
|
|
## Convert public key to canonical address.
|
2022-09-02 14:57:52 +00:00
|
|
|
var hash = keccakHash(pubkey.toRaw())
|
2019-02-05 10:32:22 +00:00
|
|
|
copyMem(addr result[0], addr hash.data[12], 20)
|
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
func `$`*(pubkey: PublicKey): string =
|
2019-02-05 10:32:22 +00:00
|
|
|
## Convert public key to hexadecimal string representation.
|
2020-04-02 12:40:29 +00:00
|
|
|
toHex(pubkey.toRaw())
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
func `$`*(sig: Signature): string =
|
2019-02-05 10:32:22 +00:00
|
|
|
## Convert signature to hexadecimal string representation.
|
2020-04-04 09:40:47 +00:00
|
|
|
toHex(sig.toRaw())
|
2019-02-05 10:32:22 +00:00
|
|
|
|
2020-04-02 12:40:29 +00:00
|
|
|
func `$`*(seckey: PrivateKey): string =
|
2019-02-05 10:32:22 +00:00
|
|
|
## Convert private key to hexadecimal string representation
|
2020-04-04 09:40:47 +00:00
|
|
|
toHex(seckey.toRaw())
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func `==`*(lhs, rhs: PublicKey): bool {.borrow.}
|
|
|
|
func `==`*(lhs, rhs: Signature): bool {.borrow.}
|
|
|
|
func `==`*(lhs, rhs: SignatureNR): bool {.borrow.}
|
2020-04-04 09:40:47 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func clear*(v: var PrivateKey) {.borrow.}
|
|
|
|
func clear*(v: var KeyPair) =
|
2020-04-04 09:40:47 +00:00
|
|
|
v.seckey.clear()
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func clear*(v: var SharedSecret) = burnMem(v.data)
|
|
|
|
func clear*(v: var SharedSecretFull) = burnMem(v.data)
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func sign*(seckey: PrivateKey, msg: SkMessage): Signature =
|
|
|
|
Signature(signRecoverable(SkSecretKey(seckey), msg))
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func sign*(seckey: PrivateKey, msg: openArray[byte]): Signature =
|
2022-09-02 14:57:52 +00:00
|
|
|
let hash = keccakHash(msg)
|
2020-07-07 08:56:26 +00:00
|
|
|
sign(seckey, SkMessage(hash.data))
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func signNR*(seckey: PrivateKey, msg: SkMessage): SignatureNR =
|
|
|
|
SignatureNR(sign(SkSecretKey(seckey), msg))
|
2020-04-04 09:40:47 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func signNR*(seckey: PrivateKey, msg: openArray[byte]): SignatureNR =
|
2022-09-02 14:57:52 +00:00
|
|
|
let hash = keccakHash(msg)
|
2020-07-07 08:56:26 +00:00
|
|
|
signNR(seckey, SkMessage(hash.data))
|
2020-04-02 12:40:29 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func recover*(sig: Signature, msg: SkMessage): SkResult[PublicKey] =
|
2020-04-04 16:44:01 +00:00
|
|
|
recover(SkRecoverableSignature(sig), msg).mapConvert(PublicKey)
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func recover*(sig: Signature, msg: openArray[byte]): SkResult[PublicKey] =
|
2022-09-02 14:57:52 +00:00
|
|
|
let hash = keccakHash(msg)
|
2020-07-07 08:56:26 +00:00
|
|
|
recover(sig, SkMessage(hash.data))
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func verify*(sig: SignatureNR, msg: SkMessage, key: PublicKey): bool =
|
2020-04-04 16:44:01 +00:00
|
|
|
verify(SkSignature(sig), msg, SkPublicKey(key))
|
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func verify*(sig: SignatureNR, msg: openArray[byte], key: PublicKey): bool =
|
2022-09-02 14:57:52 +00:00
|
|
|
let hash = keccakHash(msg)
|
2020-07-07 08:56:26 +00:00
|
|
|
verify(sig, SkMessage(hash.data), key)
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func ecdhRaw*(seckey: PrivateKey, pubkey: PublicKey): SharedSecret =
|
|
|
|
let tmp = ecdhRaw(SkSecretKey(seckey), SkPublicKey(pubkey))
|
|
|
|
|
|
|
|
# Remove first byte!
|
|
|
|
copyMem(addr result.data[0], unsafeAddr(tmp.data[1]), sizeof(result))
|
2020-04-04 16:44:01 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
func ecdhRawFull*(seckey: PrivateKey, pubkey: PublicKey): SharedSecretFull =
|
|
|
|
SharedSecretFull(ecdhRaw(SkSecretKey(seckey), SkPublicKey(pubkey)))
|