* bump nim-eth, fix deprecated calls
This commit is contained in:
Jacek Sieka 2020-04-05 15:12:48 +02:00 committed by GitHub
parent 7a721c58af
commit 1d472cf090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 100 additions and 80 deletions

View File

@ -190,7 +190,7 @@ proc privateChainConfig*(): ChainConfig =
constantinopleBlock: config.customGenesis.constantinopleBlock, constantinopleBlock: config.customGenesis.constantinopleBlock,
) )
trace "Custom genesis block configuration loaded", configuration=result trace "Custom genesis block configuration loaded", configuration=result
proc publicChainConfig*(id: PublicNetwork): ChainConfig = proc publicChainConfig*(id: PublicNetwork): ChainConfig =
result = case id result = case id
of MainNet: of MainNet:
@ -254,7 +254,7 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
proc parseConfig(T: type, c: JsonNode, field: string): T = proc parseConfig(T: type, c: JsonNode, field: string): T =
when T is string: when T is string:
c[field].getStr() c[field].getStr()
elif T is Uint256: elif T is Uint256:
parseHexInt(c[field].getStr()).u256 parseHexInt(c[field].getStr()).u256
elif T is bool: elif T is bool:
@ -273,7 +273,7 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
hexToSeqByte(c[field].getStr()) hexToSeqByte(c[field].getStr())
elif T is int64: elif T is int64:
fromHex[int64](c[field].getStr()) fromHex[int64](c[field].getStr())
template validateConfigValue(chainDetails, field, jtype, T: untyped, checkError: static[bool] = true) = template validateConfigValue(chainDetails, field, jtype, T: untyped, checkError: static[bool] = true) =
let fieldName = field.astToStr() let fieldName = field.astToStr()
@ -287,10 +287,10 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
when checkError: when checkError:
error "No value found in genesis block for", fieldName error "No value found in genesis block for", fieldName
quit(1) quit(1)
let config = getConfiguration() let config = getConfiguration()
result = Success result = Success
var var
chainId = 0.uint chainId = 0.uint
homesteadBlock, daoForkblock, eip150Block, eip155Block, eip158Block, byzantiumBlock, constantinopleBlock = 0.toBlockNumber homesteadBlock, daoForkblock, eip150Block, eip155Block, eip158Block, byzantiumBlock, constantinopleBlock = 0.toBlockNumber
eip150Hash, mixHash : MDigest[256] eip150Hash, mixHash : MDigest[256]
@ -313,7 +313,7 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
validateConfigValue(forkDetails, daoForkSupport, JBool, bool, checkError=false) validateConfigValue(forkDetails, daoForkSupport, JBool, bool, checkError=false)
if daoForkSupport == true: if daoForkSupport == true:
checkForFork(forkDetails, daoForkBlock, 0.toBlockNumber) checkForFork(forkDetails, daoForkBlock, 0.toBlockNumber)
checkForFork(forkDetails, eip150Block, homesteadBlock) checkForFork(forkDetails, eip150Block, homesteadBlock)
validateConfigValue(forkDetails, eip150Hash, JString, Hash256) validateConfigValue(forkDetails, eip150Hash, JString, Hash256)
checkForFork(forkDetails, eip155Block, eip150Block) checkForFork(forkDetails, eip155Block, eip150Block)
@ -333,7 +333,7 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
validateConfigValue(customGenesis, mixHash, JString, Hash256) validateConfigValue(customGenesis, mixHash, JString, Hash256)
validateConfigValue(customGenesis, coinbase, JString, EthAddress, checkError = false) validateConfigValue(customGenesis, coinbase, JString, EthAddress, checkError = false)
validateConfigValue(customGenesis, timestamp, JString, EthTime, checkError = false) validateConfigValue(customGenesis, timestamp, JString, EthTime, checkError = false)
config.customGenesis = CustomGenesisConfig( config.customGenesis = CustomGenesisConfig(
chainId: chainId, chainId: chainId,
homesteadBlock: homesteadBlock, homesteadBlock: homesteadBlock,
@ -353,7 +353,7 @@ proc processCustomGenesisConfig(customGenesis: JsonNode): ConfigStatus =
coinbase: coinbase, coinbase: coinbase,
timestamp: timestamp timestamp: timestamp
) )
proc processList(v: string, o: var seq[string]) = proc processList(v: string, o: var seq[string]) =
## Process comma-separated list of strings. ## Process comma-separated list of strings.
if len(v) > 0: if len(v) > 0:
@ -450,11 +450,12 @@ proc processENodesList(v: string, o: var seq[ENode]): ConfigStatus =
proc processPrivateKey(v: string, o: var PrivateKey): ConfigStatus = proc processPrivateKey(v: string, o: var PrivateKey): ConfigStatus =
## Convert hexadecimal string to private key object. ## Convert hexadecimal string to private key object.
try: let seckey = PrivateKey.fromHex(v)
o = initPrivateKey(v) if seckey.isOk():
result = Success o = seckey[]
except CatchableError: return Success
result = ErrorParseOption
result = ErrorParseOption
# proc processHexBytes(v: string, o: var seq[byte]): ConfigStatus = # proc processHexBytes(v: string, o: var seq[byte]): ConfigStatus =
# ## Convert hexadecimal string to seq[byte]. # ## Convert hexadecimal string to seq[byte].

View File

@ -75,12 +75,10 @@ proc start() =
setupCommonRpc(nimbus.rpcServer) setupCommonRpc(nimbus.rpcServer)
## Creating P2P Server ## Creating P2P Server
if conf.net.nodekey.isZeroKey(): if not conf.net.nodekey.verify():
conf.net.nodekey = newPrivateKey() conf.net.nodekey = PrivateKey.random().tryGet()
var keypair: KeyPair let keypair = conf.net.nodekey.toKeyPair().tryGet()
keypair.seckey = conf.net.nodekey
keypair.pubkey = conf.net.nodekey.getPublicKey()
var address: Address var address: Address
address.ip = parseIpAddress("0.0.0.0") address.ip = parseIpAddress("0.0.0.0")
@ -113,7 +111,7 @@ proc start() =
if canonicalHeadHashKey().toOpenArray notin trieDB: if canonicalHeadHashKey().toOpenArray notin trieDB:
initializeEmptyDb(chainDb) initializeEmptyDb(chainDb)
doAssert(canonicalHeadHashKey().toOpenArray in trieDB) doAssert(canonicalHeadHashKey().toOpenArray in trieDB)
nimbus.ethNode = newEthereumNode(keypair, address, conf.net.networkId, nimbus.ethNode = newEthereumNode(keypair, address, conf.net.networkId,
nil, nimbusClientId, nil, nimbusClientId,
addAllCapabilities = false, addAllCapabilities = false,
@ -145,7 +143,7 @@ proc start() =
nimbus.state = Stopping nimbus.state = Stopping
result = "EXITING" result = "EXITING"
nimbus.rpcServer.start() nimbus.rpcServer.start()
# metrics server # metrics server
when defined(insecure): when defined(insecure):
if conf.net.metricsServer: if conf.net.metricsServer:

View File

@ -211,10 +211,10 @@ proc `%`*(value: Bytes): JsonNode =
# Helpers for the fromJson procs # Helpers for the fromJson procs
proc toPublicKey*(key: string): PublicKey {.inline.} = proc toPublicKey*(key: string): PublicKey {.inline.} =
result = initPublicKey(key[4 .. ^1]) result = PublicKey.fromHex(key[4 .. ^1]).tryGet()
proc toPrivateKey*(key: string): PrivateKey {.inline.} = proc toPrivateKey*(key: string): PrivateKey {.inline.} =
result = initPrivateKey(key[2 .. ^1]) result = PrivateKey.fromHex(key[2 .. ^1]).tryGet()
proc toSymKey*(key: string): SymKey {.inline.} = proc toSymKey*(key: string): SymKey {.inline.} =
hexToByteArray(key[2 .. ^1], result) hexToByteArray(key[2 .. ^1], result)

View File

@ -226,7 +226,7 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB, rpcsrv: RpcServer) =
template sign(privateKey: PrivateKey, message: string): string = template sign(privateKey: PrivateKey, message: string): string =
# TODO: Is message length encoded as bytes or characters? # TODO: Is message length encoded as bytes or characters?
let msgData = "\x19Ethereum Signed Message:\n" & $message.len & message let msgData = "\x19Ethereum Signed Message:\n" & $message.len & message
$signMessage(privateKey, msgData) $sign(privateKey, msgData.toBytes()).tryGet()
rpcsrv.rpc("eth_sign") do(data: EthAddressStr, message: HexDataStr) -> HexDataStr: rpcsrv.rpc("eth_sign") do(data: EthAddressStr, message: HexDataStr) -> HexDataStr:
## The sign method calculates an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))). ## The sign method calculates an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))).

View File

@ -1,5 +1,5 @@
import import
json_rpc/rpcserver, tables, options, sequtils, json_rpc/rpcserver, tables, options,
eth/[common, rlp, keys, p2p], eth/p2p/rlpx_protocols/waku_protocol, eth/[common, rlp, keys, p2p], eth/p2p/rlpx_protocols/waku_protocol,
nimcrypto/[sysrand, hmac, sha2, pbkdf2], nimcrypto/[sysrand, hmac, sha2, pbkdf2],
rpc_types, hexstrings, key_storage rpc_types, hexstrings, key_storage
@ -70,7 +70,7 @@ proc setupWakuRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) =
## ##
## Returns key identifier on success and an error on failure. ## Returns key identifier on success and an error on failure.
result = generateRandomID().Identifier result = generateRandomID().Identifier
keys.asymKeys.add(result.string, newKeyPair()) keys.asymKeys.add(result.string, KeyPair.random().tryGet())
rpcsrv.rpc("waku_addPrivateKey") do(key: PrivateKey) -> Identifier: rpcsrv.rpc("waku_addPrivateKey") do(key: PrivateKey) -> Identifier:
## Stores the key pair, and returns its ID. ## Stores the key pair, and returns its ID.
@ -80,7 +80,7 @@ proc setupWakuRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) =
## Returns key identifier on success and an error on failure. ## Returns key identifier on success and an error on failure.
result = generateRandomID().Identifier result = generateRandomID().Identifier
keys.asymKeys.add(result.string, key.toKeyPair()) keys.asymKeys.add(result.string, key.toKeyPair().tryGet())
rpcsrv.rpc("waku_deleteKeyPair") do(id: Identifier) -> bool: rpcsrv.rpc("waku_deleteKeyPair") do(id: Identifier) -> bool:
## Deletes the specifies key if it exists. ## Deletes the specifies key if it exists.

View File

@ -71,7 +71,7 @@ proc setupWhisperRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) =
## ##
## Returns key identifier on success and an error on failure. ## Returns key identifier on success and an error on failure.
result = generateRandomID().Identifier result = generateRandomID().Identifier
keys.asymKeys.add(result.string, newKeyPair()) keys.asymKeys.add(result.string, KeyPair.random().tryGet())
rpcsrv.rpc("shh_addPrivateKey") do(key: PrivateKey) -> Identifier: rpcsrv.rpc("shh_addPrivateKey") do(key: PrivateKey) -> Identifier:
## Stores the key pair, and returns its ID. ## Stores the key pair, and returns its ID.
@ -81,7 +81,7 @@ proc setupWhisperRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) =
## Returns key identifier on success and an error on failure. ## Returns key identifier on success and an error on failure.
result = generateRandomID().Identifier result = generateRandomID().Identifier
keys.asymKeys.add(result.string, key.toKeyPair()) keys.asymKeys.add(result.string, key.toKeyPair().tryGet())
rpcsrv.rpc("shh_deleteKeyPair") do(id: Identifier) -> bool: rpcsrv.rpc("shh_deleteKeyPair") do(id: Identifier) -> bool:
## Deletes the specifies key if it exists. ## Deletes the specifies key if it exists.

View File

@ -44,7 +44,11 @@ proc getSignature*(transaction: Transaction, output: var Signature): bool =
return false return false
bytes[64] = byte(v - 27) bytes[64] = byte(v - 27)
result = recoverSignature(bytes, output) == EthKeysStatus.Success let sig = Signature.fromRaw(bytes)
if sig.isOk:
output = sig[]
return true
return false
proc toSignature*(transaction: Transaction): Signature = proc toSignature*(transaction: Transaction): Signature =
if not getSignature(transaction, result): if not getSignature(transaction, result):
@ -54,10 +58,10 @@ proc getSender*(transaction: Transaction, output: var EthAddress): bool =
## Find the address the transaction was sent from. ## Find the address the transaction was sent from.
var sig: Signature var sig: Signature
if transaction.getSignature(sig): if transaction.getSignature(sig):
var pubKey: PublicKey
var txHash = transaction.txHashNoSignature var txHash = transaction.txHashNoSignature
if recoverSignatureKey(sig, txHash.data, pubKey) == EthKeysStatus.Success: let pubkey = recover(sig, txHash)
output = pubKey.toCanonicalAddress() if pubkey.isOk:
output = pubkey[].toCanonicalAddress()
result = true result = true
proc getSender*(transaction: Transaction): EthAddress = proc getSender*(transaction: Transaction): EthAddress =

View File

@ -42,8 +42,10 @@ proc getSignature(computation: Computation): (array[32, byte], Signature) =
# Copy message data to buffer # Copy message data to buffer
bytes[0..(maxPos-64)] = data[64..maxPos] bytes[0..(maxPos-64)] = data[64..maxPos]
if recoverSignature(bytes, result[1]) != EthKeysStatus.Success: let sig = Signature.fromRaw(bytes)
if sig.isErr:
raise newException(ValidationError, "Could not recover signature computation") raise newException(ValidationError, "Could not recover signature computation")
result[1] = sig[]
# extract message hash, only need to copy when there is a valid signature # extract message hash, only need to copy when there is a valid signature
result[0][0..31] = data[0..31] result[0][0..31] = data[0..31]
@ -98,14 +100,14 @@ proc ecRecover*(computation: Computation) =
var var
(msgHash, sig) = computation.getSignature() (msgHash, sig) = computation.getSignature()
pubKey: PublicKey
if sig.recoverSignatureKey(msgHash, pubKey) != EthKeysStatus.Success: var pubkey = recover(sig, SkMessage(data: msgHash))
if pubkey.isErr:
raise newException(ValidationError, "Could not derive public key from computation") raise newException(ValidationError, "Could not derive public key from computation")
computation.output.setLen(32) computation.output.setLen(32)
computation.output[12..31] = pubKey.toCanonicalAddress() computation.output[12..31] = pubkey[].toCanonicalAddress()
trace "ECRecover precompile", derivedKey = pubKey.toCanonicalAddress() trace "ECRecover precompile", derivedKey = pubkey[].toCanonicalAddress()
proc sha256*(computation: Computation) = proc sha256*(computation: Computation) =
let let

View File

@ -192,9 +192,9 @@ proc getFixtureTransaction*(j: JsonNode, dataIndex, gasIndex, valueIndex: int):
var secretKey = j["secretKey"].getStr var secretKey = j["secretKey"].getStr
removePrefix(secretKey, "0x") removePrefix(secretKey, "0x")
let privateKey = initPrivateKey(secretKey) let privateKey = PrivateKey.fromHex(secretKey).tryGet()
let sig = signMessage(privateKey, result.rlpEncode) let sig = sign(privateKey, result.rlpEncode).tryGet()
let raw = sig.getRaw() let raw = sig.toRaw()
result.R = fromBytesBE(Uint256, raw[0..31]) result.R = fromBytesBE(Uint256, raw[0..31])
result.S = fromBytesBE(Uint256, raw[32..63]) result.S = fromBytesBE(Uint256, raw[32..63])
@ -206,15 +206,16 @@ proc hashLogEntries*(logs: seq[Log]): string =
proc setupEthNode*(capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode = proc setupEthNode*(capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
var var
conf = getConfiguration() conf = getConfiguration()
keypair: KeyPair if not conf.net.nodekey.verify():
keypair.seckey = conf.net.nodekey conf.net.nodekey = PrivateKey.random().tryGet()
keypair.pubkey = conf.net.nodekey.getPublicKey() let keypair = conf.net.nodekey.toKeyPair().tryGet()
var srvAddress: Address var srvAddress: Address
srvAddress.ip = parseIpAddress("0.0.0.0") srvAddress.ip = parseIpAddress("0.0.0.0")
srvAddress.tcpPort = Port(conf.net.bindPort) srvAddress.tcpPort = Port(conf.net.bindPort)
srvAddress.udpPort = Port(conf.net.discPort) srvAddress.udpPort = Port(conf.net.discPort)
result = newEthereumNode(keypair, srvAddress, conf.net.networkId, result = newEthereumNode(
nil, "nimbus 0.1.0", addAllCapabilities = false) keypair, srvAddress, conf.net.networkId, nil, "nimbus 0.1.0",
addAllCapabilities = false)
for capability in capabilities: for capability in capabilities:
result.addCapability capability result.addCapability capability

View File

@ -65,7 +65,7 @@ proc doTests {.async.} =
let keyID2 = await client.shh_addPrivateKey(privkey) let keyID2 = await client.shh_addPrivateKey(privkey)
check: check:
await(client.shh_getPublicKey(keyID2)) == pubkey.toPublicKey await(client.shh_getPublicKey(keyID2)) == pubkey.toPublicKey
await(client.shh_getPrivateKey(keyID2)) == privkey.toPrivateKey await(client.shh_getPrivateKey(keyID2)).toRaw() == privkey.toPrivateKey.toRaw()
await(client.shh_hasKeyPair(keyID2)) == true await(client.shh_hasKeyPair(keyID2)) == true
await(client.shh_deleteKeyPair(keyID2)) == true await(client.shh_deleteKeyPair(keyID2)) == true
await(client.shh_hasKeyPair(keyID2)) == false await(client.shh_hasKeyPair(keyID2)) == false

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 9c442bf65b52a4c857cc6e51efe901352e8b6ebf Subproject commit ac5bbe4d3d04ca1baf455f5a7e22a04692bcc73a

2
vendor/nim-stew vendored

@ -1 +1 @@
Subproject commit 76beeb769e30adc912d648c014fd95bf748fef24 Subproject commit 9414202d53fac99a0b1af33acac816ff9236e6d0

View File

@ -84,7 +84,7 @@ type
nodekey* {. nodekey* {.
desc: "P2P node private key as hex.", desc: "P2P node private key as hex.",
defaultValue: newKeyPair() defaultValue: KeyPair.random().tryGet()
name: "nodekey" }: KeyPair name: "nodekey" }: KeyPair
# TODO: Add nodekey file option # TODO: Add nodekey file option
@ -135,8 +135,8 @@ type
proc parseCmdArg*(T: type KeyPair, p: TaintedString): T = proc parseCmdArg*(T: type KeyPair, p: TaintedString): T =
try: try:
# TODO: add isValidPrivateKey check from Nimbus? # TODO: add isValidPrivateKey check from Nimbus?
result.seckey = initPrivateKey(p) result.seckey = PrivateKey.fromHex(string(p)).tryGet()
result.pubkey = result.seckey.getPublicKey() result.pubkey = result.seckey.toPublicKey()[]
except CatchableError as e: except CatchableError as e:
raise newException(ConfigurationError, "Invalid private key") raise newException(ConfigurationError, "Invalid private key")

View File

@ -45,7 +45,7 @@ proc initNodeCmd(nodeType: NodeType, shift: int, staticNodes: seq[string] = @[],
discovery = false, bootNodes: seq[string] = @[], topicInterest = false, discovery = false, bootNodes: seq[string] = @[], topicInterest = false,
master = false, label: string): NodeInfo = master = false, label: string): NodeInfo =
let let
keypair = newKeyPair() keypair = KeyPair.random().tryGet()
address = Address(ip: parseIpAddress("127.0.0.1"), address = Address(ip: parseIpAddress("127.0.0.1"),
udpPort: (30303 + shift).Port, tcpPort: (30303 + shift).Port) udpPort: (30303 + shift).Port, tcpPort: (30303 + shift).Port)
enode = ENode(pubkey: keypair.pubkey, address: address) enode = ENode(pubkey: keypair.pubkey, address: address)

View File

@ -95,16 +95,21 @@ proc nimbus_start(port: uint16, startListening: bool, enableDiscovery: bool,
var keypair: KeyPair var keypair: KeyPair
if privateKey.isNil: if privateKey.isNil:
keypair = newKeyPair() var kp = KeyPair.random()
if kp.isErr:
error "Can't generate keypair", err = kp.error
return false
keypair = kp[]
else: else:
try: let
let privKey = initPrivateKey(makeOpenArray(privateKey, 32)) privKey = PrivateKey.fromRaw(makeOpenArray(privateKey, 32))
keypair = privKey.toKeyPair() kp = privKey and privKey[].toKeyPair()
if kp.isErr:
except EthKeysException:
error "Passed an invalid private key." error "Passed an invalid private key."
return false return false
keypair = kp[]
node = newEthereumNode(keypair, address, 1, nil, addAllCapabilities = false) node = newEthereumNode(keypair, address, 1, nil, addAllCapabilities = false)
node.addCapability Whisper node.addCapability Whisper
@ -135,7 +140,7 @@ proc nimbus_add_peer(nodeId: cstring): bool {.exportc, dynlib.} =
discard initENode($nodeId, whisperENode) discard initENode($nodeId, whisperENode)
try: try:
whisperNode = newNode(whisperENode) whisperNode = newNode(whisperENode)
except Secp256k1Exception: except CatchableError:
return false return false
# TODO: call can create `Exception`, why? # TODO: call can create `Exception`, why?
@ -145,7 +150,7 @@ proc nimbus_add_peer(nodeId: cstring): bool {.exportc, dynlib.} =
# Whisper API (Similar to Whisper JSON-RPC API) # Whisper API (Similar to Whisper JSON-RPC API)
proc nimbus_channel_to_topic(channel: cstring): CTopic proc nimbus_channel_to_topic(channel: cstring): CTopic
{.exportc, dynlib, raises: [].} = {.exportc, dynlib, raises: [Defect].} =
# Only used for the example, to conveniently convert channel to topic. # Only used for the example, to conveniently convert channel to topic.
doAssert(not channel.isNil, "Channel cannot be nil.") doAssert(not channel.isNil, "Channel cannot be nil.")
@ -156,32 +161,41 @@ proc nimbus_channel_to_topic(channel: cstring): CTopic
# Asymmetric Keys # Asymmetric Keys
proc nimbus_new_keypair(id: var Identifier): bool proc nimbus_new_keypair(id: var Identifier): bool
{.exportc, dynlib, raises: [].} = {.exportc, dynlib, raises: [Defect].} =
## Caller needs to provide as id a pointer to 32 bytes allocation. ## Caller needs to provide as id a pointer to 32 bytes allocation.
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.") doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
id = generateRandomID() id = generateRandomID()
try: try:
whisperKeys.asymKeys.add(id.toHex(), newKeyPair()) whisperKeys.asymKeys.add(id.toHex(), KeyPair.random().tryGet())
result = true result = true
except Secp256k1Exception: except CatchableError:
# Don't think this can actually happen, comes from the `getPublicKey` part # Don't think this can actually happen, comes from the `getPublicKey` part
# in `newKeyPair` # in `newKeyPair`
discard discard
proc nimbus_add_keypair(privateKey: ptr byte, id: var Identifier): proc nimbus_add_keypair(privateKey: ptr byte, id: var Identifier):
bool {.exportc, dynlib, raises: [OSError, IOError, ValueError].} = bool {.exportc, dynlib, raises: [Defect, OSError, IOError, ValueError].} =
## Caller needs to provide as id a pointer to 32 bytes allocation. ## Caller needs to provide as id a pointer to 32 bytes allocation.
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.") doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
doAssert(not privateKey.isNil, "Private key cannot be nil.") doAssert(not privateKey.isNil, "Private key cannot be nil.")
var keypair: KeyPair var keypair: KeyPair
try: if privateKey.isNil:
let privKey = initPrivateKey(makeOpenArray(privateKey, 32)) var kp = KeyPair.random()
keypair = privKey.toKeyPair() if kp.isErr:
except EthKeysException, Secp256k1Exception: error "Can't generate keypair", err = kp.error
error "Passed an invalid private key." return false
return false keypair = kp[]
else:
let
privKey = PrivateKey.fromRaw(makeOpenArray(privateKey, 32))
kp = privKey and privKey[].toKeyPair()
if kp.isErr:
error "Passed an invalid private key."
return false
keypair = kp[]
result = true result = true
id = generateRandomID() id = generateRandomID()
@ -277,11 +291,11 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc, dynlib.} =
return false return false
if not message.pubKey.isNil(): if not message.pubKey.isNil():
try: let pubkey = PublicKey.fromRaw(makeOpenArray(message.pubKey, 64))
asymKey = some(initPublicKey(makeOpenArray(message.pubKey, 64))) if pubkey.isErr:
except EthKeysException:
error "Passed an invalid public key for encryption." error "Passed an invalid public key for encryption."
return false return false
asymKey = some(pubkey[])
try: try:
if not message.symKeyID.isNil(): if not message.symKeyID.isNil():
@ -340,11 +354,11 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
return false return false
if not options.source.isNil(): if not options.source.isNil():
try: let pubkey = PublicKey.fromRaw(makeOpenArray(options.source, 64))
src = some(initPublicKey(makeOpenArray(options.source, 64))) if pubkey.isErr:
except EthKeysException:
error "Passed an invalid public key as source." error "Passed an invalid public key as source."
return false return false
src = some(pubkey[])
try: try:
if not options.symKeyID.isNil(): if not options.symKeyID.isNil():
@ -377,11 +391,11 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
recipientPublicKey: array[RawPublicKeySize, byte] recipientPublicKey: array[RawPublicKeySize, byte]
if msg.decoded.src.isSome(): if msg.decoded.src.isSome():
# Need to pass the serialized form # Need to pass the serialized form
source = msg.decoded.src.get().getRaw() source = msg.decoded.src.get().toRaw()
cmsg.source = addr source[0] cmsg.source = addr source[0]
if msg.dst.isSome(): if msg.dst.isSome():
# Need to pass the serialized form # Need to pass the serialized form
recipientPublicKey = msg.decoded.src.get().getRaw() recipientPublicKey = msg.decoded.src.get().toRaw()
cmsg.recipientPublicKey = addr recipientPublicKey[0] cmsg.recipientPublicKey = addr recipientPublicKey[0]
handler(addr cmsg, udata) handler(addr cmsg, udata)
@ -456,7 +470,7 @@ proc nimbus_join_public_chat(channel: cstring,
# TODO: How would we do key management? In nimbus (like in rpc) or in status go? # TODO: How would we do key management? In nimbus (like in rpc) or in status go?
proc nimbus_post_public(channel: cstring, payload: cstring) proc nimbus_post_public(channel: cstring, payload: cstring)
{.exportc, dynlib.} = {.exportc, dynlib.} =
let encPrivateKey = initPrivateKey("5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3") let encPrivateKey = PrivateKey.fromHex("5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3")[]
var ctx: HMAC[sha256] var ctx: HMAC[sha256]
var symKey: SymKey var symKey: SymKey