2020-05-19 17:30:28 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
2020-07-07 15:51:02 +00:00
|
|
|
json, math, strutils, strformat, typetraits, bearssl,
|
2020-06-02 19:59:51 +00:00
|
|
|
stew/[results, byteutils, bitseqs, bitops2], stew/shims/macros,
|
2020-06-23 19:11:07 +00:00
|
|
|
eth/keyfile/uuid, blscurve, json_serialization,
|
2020-07-07 15:51:02 +00:00
|
|
|
nimcrypto/[sha2, rijndael, pbkdf2, bcmode, hash, utils],
|
2020-06-16 05:45:04 +00:00
|
|
|
./datatypes, ./crypto, ./digest, ./signatures
|
2020-05-19 17:30:28 +00:00
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
export
|
2020-07-17 20:59:50 +00:00
|
|
|
results, burnMem
|
2020-05-19 17:30:28 +00:00
|
|
|
|
2020-05-20 07:01:20 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-05-19 17:30:28 +00:00
|
|
|
type
|
|
|
|
ChecksumParams = object
|
|
|
|
|
|
|
|
Checksum = object
|
|
|
|
function: string
|
|
|
|
params: ChecksumParams
|
|
|
|
message: string
|
|
|
|
|
|
|
|
CipherParams = object
|
|
|
|
iv: string
|
|
|
|
|
|
|
|
Cipher = object
|
|
|
|
function: string
|
|
|
|
params: CipherParams
|
|
|
|
message: string
|
|
|
|
|
|
|
|
KdfScrypt* = object
|
|
|
|
dklen: int
|
|
|
|
n, p, r: int
|
|
|
|
salt: string
|
|
|
|
|
|
|
|
KdfPbkdf2* = object
|
|
|
|
dklen: int
|
|
|
|
c: int
|
|
|
|
prf: string
|
|
|
|
salt: string
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
# https://github.com/ethereum/EIPs/blob/4494da0966afa7318ec0157948821b19c4248805/EIPS/eip-2386.md#specification
|
|
|
|
Wallet* = object
|
|
|
|
uuid*: UUID
|
|
|
|
name*: WalletName
|
|
|
|
version*: uint
|
|
|
|
walletType* {.serializedFieldName: "type"}: string
|
|
|
|
# TODO: The use of `JsonString` can be removed once we
|
|
|
|
# solve the serialization problem for `Crypto[T]`
|
|
|
|
crypto*: JsonString
|
|
|
|
nextAccount* {.serializedFieldName: "nextaccount".}: Natural
|
|
|
|
|
2020-05-19 17:30:28 +00:00
|
|
|
KdfParams = KdfPbkdf2 | KdfScrypt
|
|
|
|
|
|
|
|
Kdf[T: KdfParams] = object
|
|
|
|
function: string
|
|
|
|
params: T
|
|
|
|
message: string
|
|
|
|
|
|
|
|
Crypto[T: KdfParams] = object
|
|
|
|
kdf: Kdf[T]
|
|
|
|
checksum: Checksum
|
|
|
|
cipher: Cipher
|
|
|
|
|
|
|
|
Keystore[T: KdfParams] = object
|
|
|
|
crypto: Crypto[T]
|
|
|
|
pubkey: string
|
|
|
|
path: string
|
|
|
|
uuid: string
|
|
|
|
version: int
|
|
|
|
|
|
|
|
KsResult*[T] = Result[T, cstring]
|
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
Eth2KeyKind* = enum
|
|
|
|
signingKeyKind # Also known as voting key
|
|
|
|
withdrawalKeyKind
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
UUID* = distinct string
|
|
|
|
WalletName* = distinct string
|
2020-06-01 19:48:20 +00:00
|
|
|
Mnemonic* = distinct string
|
|
|
|
KeyPath* = distinct string
|
|
|
|
KeyStorePass* = distinct string
|
|
|
|
KeySeed* = distinct seq[byte]
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
KeyStoreContent* = distinct JsonString
|
|
|
|
WalletContent* = distinct JsonString
|
|
|
|
|
|
|
|
SensitiveData = Mnemonic|KeyStorePass|KeySeed
|
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
Credentials* = object
|
|
|
|
mnemonic*: Mnemonic
|
|
|
|
keyStore*: KeyStoreContent
|
|
|
|
signingKey*: ValidatorPrivKey
|
|
|
|
withdrawalKey*: ValidatorPrivKey
|
|
|
|
|
2020-05-19 17:30:28 +00:00
|
|
|
const
|
2020-05-20 07:22:21 +00:00
|
|
|
saltSize = 32
|
|
|
|
|
2020-05-19 17:30:28 +00:00
|
|
|
scryptParams = KdfScrypt(
|
2020-05-20 07:22:21 +00:00
|
|
|
dklen: saltSize,
|
2020-05-19 17:30:28 +00:00
|
|
|
n: 2^18,
|
|
|
|
r: 1,
|
|
|
|
p: 8
|
|
|
|
)
|
|
|
|
|
|
|
|
pbkdf2Params = KdfPbkdf2(
|
2020-05-20 07:22:21 +00:00
|
|
|
dklen: saltSize,
|
2020-05-19 17:30:28 +00:00
|
|
|
c: 2^18,
|
|
|
|
prf: "hmac-sha256"
|
|
|
|
)
|
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
# https://eips.ethereum.org/EIPS/eip-2334
|
|
|
|
eth2KeyPurpose = 12381
|
|
|
|
eth2CoinType* = 3600
|
|
|
|
|
|
|
|
# https://github.com/bitcoin/bips/blob/master/bip-0039/bip-0039-wordlists.md
|
|
|
|
wordListLen = 2048
|
2020-06-02 19:59:51 +00:00
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
UUID.serializesAsBaseIn Json
|
|
|
|
WalletName.serializesAsBaseIn Json
|
|
|
|
|
|
|
|
template `$`*(m: Mnemonic): string =
|
|
|
|
string(m)
|
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
template `==`*(lhs, rhs: WalletName): bool =
|
|
|
|
string(lhs) == string(rhs)
|
|
|
|
|
|
|
|
template `$`*(x: WalletName): string =
|
|
|
|
string(x)
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
template burnMem*(m: var (SensitiveData|TaintedString)) =
|
|
|
|
# TODO: `burnMem` in nimcrypto could use distinctBase
|
|
|
|
# to make its usage less error-prone.
|
|
|
|
utils.burnMem(string m)
|
|
|
|
|
2020-07-07 15:51:02 +00:00
|
|
|
proc getRandomBytes*(rng: var BrHmacDrbgContext, n: Natural): seq[byte]
|
|
|
|
{.raises: [Defect].} =
|
|
|
|
result = newSeq[byte](n)
|
|
|
|
brHmacDrbgGenerate(rng, result)
|
|
|
|
|
2020-07-14 19:00:35 +00:00
|
|
|
macro wordListArray*(filename: static string,
|
|
|
|
maxWords: static int = 0,
|
|
|
|
minWordLength: static int = 0): untyped =
|
2020-06-02 19:59:51 +00:00
|
|
|
result = newTree(nnkBracket)
|
|
|
|
var words = slurp(filename).split()
|
|
|
|
for word in words:
|
2020-07-14 19:00:35 +00:00
|
|
|
if word.len >= minWordLength:
|
|
|
|
result.add newCall("cstring", newLit(word))
|
|
|
|
if maxWords > 0 and result.len >= maxWords:
|
|
|
|
return
|
2020-06-02 19:59:51 +00:00
|
|
|
|
|
|
|
const
|
2020-07-14 19:00:35 +00:00
|
|
|
englishWords = wordListArray("english_word_list.txt",
|
|
|
|
maxWords = wordListLen)
|
2020-06-01 19:48:20 +00:00
|
|
|
|
|
|
|
iterator pathNodesImpl(path: string): Natural
|
|
|
|
{.raises: [ValueError].} =
|
|
|
|
for elem in path.split("/"):
|
|
|
|
if elem == "m": continue
|
|
|
|
yield parseInt(elem)
|
|
|
|
|
|
|
|
func append*(path: KeyPath, pathNode: Natural): KeyPath =
|
|
|
|
KeyPath(path.string & "/" & $pathNode)
|
|
|
|
|
|
|
|
func validateKeyPath*(path: TaintedString): KeyPath
|
|
|
|
{.raises: [ValueError].} =
|
|
|
|
for elem in pathNodesImpl(path.string): discard elem
|
|
|
|
KeyPath path
|
|
|
|
|
|
|
|
iterator pathNodes(path: KeyPath): Natural =
|
|
|
|
try:
|
|
|
|
for elem in pathNodesImpl(path.string):
|
|
|
|
yield elem
|
|
|
|
except ValueError:
|
|
|
|
doAssert false, "Make sure you've validated the key path with `validateKeyPath`"
|
|
|
|
|
|
|
|
func makeKeyPath*(validatorIdx: Natural,
|
|
|
|
keyType: Eth2KeyKind): KeyPath =
|
|
|
|
# https://eips.ethereum.org/EIPS/eip-2334
|
|
|
|
let use = case keyType
|
|
|
|
of withdrawalKeyKind: "0"
|
|
|
|
of signingKeyKind: "0/0"
|
|
|
|
|
|
|
|
try:
|
|
|
|
KeyPath &"m/{eth2KeyPurpose}/{eth2CoinType}/{validatorIdx}/{use}"
|
|
|
|
except ValueError:
|
|
|
|
raiseAssert "All values above can be converted successfully to strings"
|
|
|
|
|
|
|
|
func getSeed*(mnemonic: Mnemonic, password: KeyStorePass): KeySeed =
|
|
|
|
# https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#from-mnemonic-to-seed
|
|
|
|
let salt = "mnemonic-" & password.string
|
|
|
|
KeySeed sha512.pbkdf2(mnemonic.string, salt, 2048, 64)
|
|
|
|
|
2020-07-07 15:51:02 +00:00
|
|
|
proc generateMnemonic*(
|
|
|
|
rng: var BrHmacDrbgContext,
|
|
|
|
words: openarray[cstring] = englishWords,
|
|
|
|
entropyParam: openarray[byte] = @[]): Mnemonic =
|
2020-06-01 19:48:20 +00:00
|
|
|
# https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic
|
|
|
|
doAssert words.len == wordListLen
|
|
|
|
|
|
|
|
var entropy: seq[byte]
|
|
|
|
if entropyParam.len == 0:
|
2020-07-07 15:51:02 +00:00
|
|
|
setLen(entropy, 32)
|
|
|
|
brHmacDrbgGenerate(rng, entropy)
|
2020-06-01 19:48:20 +00:00
|
|
|
else:
|
|
|
|
doAssert entropyParam.len >= 128 and
|
|
|
|
entropyParam.len <= 256 and
|
|
|
|
entropyParam.len mod 32 == 0
|
|
|
|
entropy = @entropyParam
|
|
|
|
|
|
|
|
let
|
|
|
|
checksumBits = entropy.len div 4 # ranges from 4 to 8
|
|
|
|
mnemonicWordCount = 12 + (checksumBits - 4) * 3
|
|
|
|
checksum = sha256.digest(entropy)
|
|
|
|
|
|
|
|
entropy.add byte(checksum.data.getBitsBE(0 ..< checksumBits))
|
|
|
|
|
2020-06-02 19:59:51 +00:00
|
|
|
var res = ""
|
|
|
|
res.add words[entropy.getBitsBE(0..10)]
|
|
|
|
|
2020-06-01 19:48:20 +00:00
|
|
|
for i in 1 ..< mnemonicWordCount:
|
|
|
|
let
|
|
|
|
firstBit = i*11
|
|
|
|
lastBit = firstBit + 10
|
|
|
|
res.add " "
|
|
|
|
res.add words[entropy.getBitsBE(firstBit..lastBit)]
|
|
|
|
|
|
|
|
Mnemonic res
|
|
|
|
|
|
|
|
proc deriveChildKey*(parentKey: ValidatorPrivKey,
|
|
|
|
index: Natural): ValidatorPrivKey =
|
2020-06-08 14:56:56 +00:00
|
|
|
let success = derive_child_secretKey(SecretKey result,
|
|
|
|
SecretKey parentKey,
|
|
|
|
uint32 index)
|
|
|
|
# TODO `derive_child_secretKey` is reporting pre-condition
|
|
|
|
# failures with return values. We should turn the checks
|
|
|
|
# into asserts inside the function.
|
|
|
|
doAssert success
|
2020-06-01 19:48:20 +00:00
|
|
|
|
|
|
|
proc deriveMasterKey*(seed: KeySeed): ValidatorPrivKey =
|
2020-06-08 14:56:56 +00:00
|
|
|
let success = derive_master_secretKey(SecretKey result,
|
|
|
|
seq[byte] seed)
|
|
|
|
# TODO `derive_master_secretKey` is reporting pre-condition
|
|
|
|
# failures with return values. We should turn the checks
|
|
|
|
# into asserts inside the function.
|
|
|
|
doAssert success
|
2020-06-01 19:48:20 +00:00
|
|
|
|
|
|
|
proc deriveMasterKey*(mnemonic: Mnemonic,
|
|
|
|
password: KeyStorePass): ValidatorPrivKey =
|
|
|
|
deriveMasterKey(getSeed(mnemonic, password))
|
|
|
|
|
|
|
|
proc deriveChildKey*(masterKey: ValidatorPrivKey,
|
|
|
|
path: KeyPath): ValidatorPrivKey =
|
|
|
|
result = masterKey
|
|
|
|
for idx in pathNodes(path):
|
|
|
|
result = deriveChildKey(result, idx)
|
|
|
|
|
|
|
|
proc keyFromPath*(mnemonic: Mnemonic,
|
|
|
|
password: KeyStorePass,
|
|
|
|
path: KeyPath): ValidatorPrivKey =
|
|
|
|
deriveChildKey(deriveMasterKey(mnemonic, password), path)
|
|
|
|
|
2020-05-20 07:22:21 +00:00
|
|
|
proc shaChecksum(key, cipher: openarray[byte]): array[32, byte] =
|
2020-05-19 17:30:28 +00:00
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
|
|
|
ctx.update(key)
|
|
|
|
ctx.update(cipher)
|
2020-05-20 07:22:21 +00:00
|
|
|
result = ctx.finish().data
|
|
|
|
ctx.clear()
|
2020-05-19 17:30:28 +00:00
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
template tryJsonToCrypto(json: JsonNode; crypto: typedesc): untyped =
|
2020-05-20 07:01:20 +00:00
|
|
|
try:
|
2020-06-23 19:11:07 +00:00
|
|
|
json.to(Crypto[crypto])
|
2020-05-20 07:01:20 +00:00
|
|
|
except Exception:
|
|
|
|
return err "ks: failed to parse crypto"
|
|
|
|
|
|
|
|
template hexToBytes(data, name: string): untyped =
|
2020-05-19 17:30:28 +00:00
|
|
|
try:
|
2020-05-20 07:01:20 +00:00
|
|
|
hexToSeqByte(data)
|
|
|
|
except ValueError:
|
|
|
|
return err "ks: failed to parse " & name
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
proc decryptoCryptoField*(json: JsonNode,
|
|
|
|
password: KeyStorePass): KsResult[seq[byte]] =
|
2020-05-19 17:30:28 +00:00
|
|
|
var
|
|
|
|
decKey: seq[byte]
|
|
|
|
salt: seq[byte]
|
|
|
|
iv: seq[byte]
|
|
|
|
cipherMsg: seq[byte]
|
|
|
|
checksumMsg: seq[byte]
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
let kdf = json{"kdf", "function"}.getStr
|
2020-05-19 17:30:28 +00:00
|
|
|
|
|
|
|
case kdf
|
|
|
|
of "scrypt":
|
2020-06-23 19:11:07 +00:00
|
|
|
let crypto = tryJsonToCrypto(json, KdfScrypt)
|
2020-05-19 17:30:28 +00:00
|
|
|
return err "ks: scrypt not supported"
|
|
|
|
of "pbkdf2":
|
|
|
|
let
|
2020-06-23 19:11:07 +00:00
|
|
|
crypto = tryJsonToCrypto(json, KdfPbkdf2)
|
2020-05-19 17:30:28 +00:00
|
|
|
kdfParams = crypto.kdf.params
|
|
|
|
|
2020-05-20 07:01:20 +00:00
|
|
|
salt = hexToBytes(kdfParams.salt, "salt")
|
2020-06-01 19:48:20 +00:00
|
|
|
decKey = sha256.pbkdf2(password.string, salt, kdfParams.c, kdfParams.dklen)
|
2020-05-20 07:01:20 +00:00
|
|
|
iv = hexToBytes(crypto.cipher.params.iv, "iv")
|
|
|
|
cipherMsg = hexToBytes(crypto.cipher.message, "cipher")
|
|
|
|
checksumMsg = hexToBytes(crypto.checksum.message, "checksum")
|
2020-05-19 17:30:28 +00:00
|
|
|
else:
|
|
|
|
return err "ks: unknown cipher"
|
|
|
|
|
2020-05-20 07:22:21 +00:00
|
|
|
if decKey.len < saltSize:
|
2020-05-19 17:30:28 +00:00
|
|
|
return err "ks: decryption key must be at least 32 bytes"
|
|
|
|
|
2020-05-27 14:05:32 +00:00
|
|
|
if iv.len < aes128.sizeBlock:
|
|
|
|
return err "ks: invalid iv"
|
|
|
|
|
2020-05-20 07:22:21 +00:00
|
|
|
let sum = shaChecksum(decKey.toOpenArray(16, 31), cipherMsg)
|
2020-05-19 17:30:28 +00:00
|
|
|
if sum != checksumMsg:
|
|
|
|
return err "ks: invalid checksum"
|
|
|
|
|
|
|
|
var
|
|
|
|
aesCipher: CTR[aes128]
|
|
|
|
secret = newSeq[byte](cipherMsg.len)
|
|
|
|
|
2020-05-20 07:22:21 +00:00
|
|
|
aesCipher.init(decKey.toOpenArray(0, 15), iv)
|
2020-05-19 17:30:28 +00:00
|
|
|
aesCipher.decrypt(cipherMsg, secret)
|
|
|
|
aesCipher.clear()
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
ok secret
|
|
|
|
|
|
|
|
proc decryptKeystore*(data: KeyStoreContent,
|
|
|
|
password: KeyStorePass): KsResult[ValidatorPrivKey] =
|
|
|
|
# TODO: `parseJson` can raise a general `Exception`
|
|
|
|
let
|
|
|
|
ks = try: parseJson(data.string)
|
|
|
|
except Exception: return err "ks: failed to parse keystore"
|
|
|
|
secret = decryptoCryptoField(ks{"crypto"}, password)
|
|
|
|
|
|
|
|
ValidatorPrivKey.fromRaw(? secret)
|
|
|
|
|
|
|
|
proc createCryptoField(T: type[KdfParams],
|
2020-07-07 15:51:02 +00:00
|
|
|
rng: var BrHmacDrbgContext,
|
2020-06-23 19:11:07 +00:00
|
|
|
secret: openarray[byte],
|
|
|
|
password = KeyStorePass "",
|
|
|
|
salt: openarray[byte] = @[],
|
|
|
|
iv: openarray[byte] = @[]): Crypto[T] =
|
|
|
|
type AES = aes128
|
2020-05-19 17:30:28 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
decKey: seq[byte]
|
2020-06-23 19:11:07 +00:00
|
|
|
aesCipher: CTR[AES]
|
2020-05-19 17:30:28 +00:00
|
|
|
cipherMsg = newSeq[byte](secret.len)
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
let kdfSalt = if salt.len > 0:
|
2020-06-01 19:48:20 +00:00
|
|
|
doAssert salt.len == saltSize
|
2020-06-23 19:11:07 +00:00
|
|
|
@salt
|
2020-06-01 19:48:20 +00:00
|
|
|
else:
|
2020-07-07 15:51:02 +00:00
|
|
|
getRandomBytes(rng, saltSize)
|
2020-05-19 17:30:28 +00:00
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
let aesIv = if iv.len > 0:
|
|
|
|
doAssert iv.len == AES.sizeBlock
|
|
|
|
@iv
|
2020-06-01 19:48:20 +00:00
|
|
|
else:
|
2020-07-07 15:51:02 +00:00
|
|
|
getRandomBytes(rng, AES.sizeBlock)
|
2020-05-19 17:30:28 +00:00
|
|
|
|
|
|
|
when T is KdfPbkdf2:
|
2020-06-01 19:48:20 +00:00
|
|
|
decKey = sha256.pbkdf2(password.string, kdfSalt, pbkdf2Params.c,
|
2020-05-19 17:30:28 +00:00
|
|
|
pbkdf2Params.dklen)
|
|
|
|
|
|
|
|
var kdf = Kdf[KdfPbkdf2](function: "pbkdf2", params: pbkdf2Params, message: "")
|
2020-06-01 19:48:20 +00:00
|
|
|
kdf.params.salt = byteutils.toHex(kdfSalt)
|
2020-05-19 17:30:28 +00:00
|
|
|
else:
|
2020-06-23 19:11:07 +00:00
|
|
|
{.fatal: "Other KDFs are supported yet".}
|
2020-05-19 17:30:28 +00:00
|
|
|
|
2020-05-20 07:22:21 +00:00
|
|
|
aesCipher.init(decKey.toOpenArray(0, 15), aesIv)
|
2020-05-19 17:30:28 +00:00
|
|
|
aesCipher.encrypt(secret, cipherMsg)
|
|
|
|
aesCipher.clear()
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
let sum = shaChecksum(decKey.toOpenArray(16, 31), cipherMsg)
|
2020-05-19 17:30:28 +00:00
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
Crypto[T](
|
|
|
|
kdf: kdf,
|
|
|
|
checksum: Checksum(
|
|
|
|
function: "sha256",
|
|
|
|
message: byteutils.toHex(sum)),
|
|
|
|
cipher: Cipher(
|
|
|
|
function: "aes-128-ctr",
|
|
|
|
params: CipherParams(iv: byteutils.toHex(aesIv)),
|
|
|
|
message: byteutils.toHex(cipherMsg)))
|
2020-05-19 17:30:28 +00:00
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
proc encryptKeystore*(T: type[KdfParams],
|
2020-07-07 15:51:02 +00:00
|
|
|
rng: var BrHmacDrbgContext,
|
2020-06-23 19:11:07 +00:00
|
|
|
privKey: ValidatorPrivkey,
|
|
|
|
password = KeyStorePass "",
|
|
|
|
path = KeyPath "",
|
|
|
|
salt: openarray[byte] = @[],
|
|
|
|
iv: openarray[byte] = @[],
|
|
|
|
pretty = true): KeyStoreContent =
|
|
|
|
let
|
|
|
|
secret = privKey.toRaw[^32..^1]
|
2020-07-07 15:51:02 +00:00
|
|
|
cryptoField = createCryptoField(T, rng, secret, password, salt, iv)
|
2020-06-23 19:11:07 +00:00
|
|
|
pubkey = privKey.toPubKey()
|
|
|
|
uuid = uuidGenerate().expect("Random bytes should be available")
|
2020-05-19 17:30:28 +00:00
|
|
|
keystore = Keystore[T](
|
2020-06-23 19:11:07 +00:00
|
|
|
crypto: cryptoField,
|
2020-06-01 19:48:20 +00:00
|
|
|
pubkey: toHex(pubkey),
|
|
|
|
path: path.string,
|
|
|
|
uuid: $uuid,
|
|
|
|
version: 4)
|
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
KeyStoreContent if pretty: json.pretty(%keystore)
|
2020-06-02 19:59:51 +00:00
|
|
|
else: $(%keystore)
|
2020-06-01 19:48:20 +00:00
|
|
|
|
2020-06-23 19:11:07 +00:00
|
|
|
proc createWallet*(T: type[KdfParams],
|
2020-07-07 15:51:02 +00:00
|
|
|
rng: var BrHmacDrbgContext,
|
2020-06-23 19:11:07 +00:00
|
|
|
mnemonic: Mnemonic,
|
|
|
|
name = WalletName "",
|
|
|
|
salt: openarray[byte] = @[],
|
|
|
|
iv: openarray[byte] = @[],
|
|
|
|
password = KeyStorePass "",
|
|
|
|
nextAccount = none(Natural),
|
|
|
|
pretty = true): Wallet =
|
|
|
|
let
|
|
|
|
uuid = UUID $(uuidGenerate().expect("Random bytes should be available"))
|
|
|
|
# Please note that we are passing an empty password here because
|
|
|
|
# we want the wallet restoration procedure to depend only on the
|
|
|
|
# mnemonic (the user is asked to treat the mnemonic as a password).
|
|
|
|
seed = getSeed(mnemonic, KeyStorePass"")
|
2020-07-07 15:51:02 +00:00
|
|
|
cryptoField = %createCryptoField(T,rng, distinctBase seed, password, salt, iv)
|
2020-06-23 19:11:07 +00:00
|
|
|
|
|
|
|
Wallet(
|
|
|
|
uuid: uuid,
|
|
|
|
name: if name.string.len > 0: name
|
|
|
|
else: WalletName(uuid),
|
|
|
|
version: 1,
|
|
|
|
walletType: "hierarchical deterministic",
|
|
|
|
crypto: JsonString(if pretty: json.pretty(cryptoField)
|
|
|
|
else: $cryptoField),
|
|
|
|
nextAccount: nextAccount.get(0))
|
|
|
|
|
|
|
|
proc createWalletContent*(T: type[KdfParams],
|
2020-07-07 15:51:02 +00:00
|
|
|
rng: var BrHmacDrbgContext,
|
2020-06-23 19:11:07 +00:00
|
|
|
mnemonic: Mnemonic,
|
|
|
|
name = WalletName "",
|
|
|
|
salt: openarray[byte] = @[],
|
|
|
|
iv: openarray[byte] = @[],
|
|
|
|
password = KeyStorePass "",
|
|
|
|
nextAccount = none(Natural),
|
|
|
|
pretty = true): (UUID, WalletContent) =
|
2020-07-07 15:51:02 +00:00
|
|
|
let wallet = createWallet(
|
|
|
|
T, rng, mnemonic, name, salt, iv, password, nextAccount, pretty)
|
2020-06-23 19:11:07 +00:00
|
|
|
(wallet.uuid, WalletContent Json.encode(wallet, pretty = pretty))
|
|
|
|
|
2020-07-28 14:55:46 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.12.2/specs/phase0/deposit-contract.md#withdrawal-credentials
|
2020-06-01 19:48:20 +00:00
|
|
|
proc makeWithdrawalCredentials*(k: ValidatorPubKey): Eth2Digest =
|
2020-06-16 12:16:43 +00:00
|
|
|
var bytes = eth2digest(k.toRaw())
|
2020-06-01 19:48:20 +00:00
|
|
|
bytes.data[0] = BLS_WITHDRAWAL_PREFIX.uint8
|
|
|
|
bytes
|
|
|
|
|
2020-07-17 20:59:50 +00:00
|
|
|
proc prepareDeposit*(preset: RuntimePreset,
|
|
|
|
withdrawalPubKey: ValidatorPubKey,
|
|
|
|
signingKey: ValidatorPrivKey, signingPubKey: ValidatorPubKey,
|
|
|
|
amount = MAX_EFFECTIVE_BALANCE.Gwei): DepositData =
|
|
|
|
var res = DepositData(
|
|
|
|
amount: amount,
|
|
|
|
pubkey: signingPubKey,
|
|
|
|
withdrawal_credentials: makeWithdrawalCredentials(withdrawalPubKey))
|
|
|
|
|
|
|
|
res.signature = preset.get_deposit_signature(res, signingKey)
|
|
|
|
return res
|
2020-06-01 19:48:20 +00:00
|
|
|
|