mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-27 12:35:00 +00:00
Additional checks
This commit is contained in:
parent
9e7e51b5b4
commit
1f5fcf756b
@ -42,14 +42,14 @@ type
|
|||||||
CFilterOptions* = object
|
CFilterOptions* = object
|
||||||
symKeyID*: cstring
|
symKeyID*: cstring
|
||||||
privateKeyID*: cstring
|
privateKeyID*: cstring
|
||||||
source*: ptr PublicKey
|
source*: ptr byte
|
||||||
minPow*: float64
|
minPow*: float64
|
||||||
topic*: Topic # lets go with one topic for now
|
topic*: Topic # lets go with one topic for now
|
||||||
allowP2P*: bool
|
allowP2P*: bool
|
||||||
|
|
||||||
CPostMessage* = object
|
CPostMessage* = object
|
||||||
symKeyID*: cstring
|
symKeyID*: cstring
|
||||||
pubKey*: ptr PublicKey
|
pubKey*: ptr byte
|
||||||
sourceID*: cstring
|
sourceID*: cstring
|
||||||
ttl*: uint32
|
ttl*: uint32
|
||||||
topic*: Topic
|
topic*: Topic
|
||||||
@ -208,6 +208,8 @@ proc nimbus_add_peer(nodeId: cstring) {.exportc.} =
|
|||||||
# Mostly an example for now, lots of things to fix if continued like this.
|
# Mostly an example for now, lots of things to fix if continued like this.
|
||||||
|
|
||||||
proc nimbus_channel_to_topic(channel: cstring): CTopic {.exportc.} =
|
proc nimbus_channel_to_topic(channel: cstring): CTopic {.exportc.} =
|
||||||
|
doAssert(not channel.isNil, "channel cannot be nil")
|
||||||
|
|
||||||
let hash = digest(keccak256, $channel)
|
let hash = digest(keccak256, $channel)
|
||||||
for i in 0..<4:
|
for i in 0..<4:
|
||||||
result.topic[i] = hash.data[i]
|
result.topic[i] = hash.data[i]
|
||||||
@ -239,11 +241,14 @@ proc nimbus_add_keypair(privateKey: ptr byte):
|
|||||||
pubkey: privKey.getPublicKey()))
|
pubkey: privKey.getPublicKey()))
|
||||||
|
|
||||||
proc nimbus_delete_keypair(id: cstring): bool {.exportc.} =
|
proc nimbus_delete_keypair(id: cstring): bool {.exportc.} =
|
||||||
|
doAssert(not id.isNil, "Key id cannot be nil")
|
||||||
|
|
||||||
var unneeded: KeyPair
|
var unneeded: KeyPair
|
||||||
result = whisperKeys.asymKeys.take($id, unneeded)
|
result = whisperKeys.asymKeys.take($id, unneeded)
|
||||||
|
|
||||||
proc nimbus_get_private_key(id: cstring, privateKey: ptr PrivateKey):
|
proc nimbus_get_private_key(id: cstring, privateKey: ptr PrivateKey):
|
||||||
bool {.exportc.} =
|
bool {.exportc.} =
|
||||||
|
doAssert(not id.isNil, "Key id cannot be nil")
|
||||||
doAssert(not privateKey.isNil, "Passed a null pointer as privateKey")
|
doAssert(not privateKey.isNil, "Passed a null pointer as privateKey")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -269,6 +274,8 @@ proc nimbus_add_symkey_from_password(password: cstring):
|
|||||||
cstring {.exportc.} =
|
cstring {.exportc.} =
|
||||||
## It is important that the caller makes a copy of the returned cstring before
|
## It is important that the caller makes a copy of the returned cstring before
|
||||||
## doing any other API calls. This might not hold for all types of GC.
|
## doing any other API calls. This might not hold for all types of GC.
|
||||||
|
doAssert(not password.isNil, "password can not be nil")
|
||||||
|
|
||||||
var ctx: HMAC[sha256]
|
var ctx: HMAC[sha256]
|
||||||
var symKey: SymKey
|
var symKey: SymKey
|
||||||
if pbkdf2(ctx, $password, "", 65356, symKey) != sizeof(SymKey):
|
if pbkdf2(ctx, $password, "", 65356, symKey) != sizeof(SymKey):
|
||||||
@ -279,11 +286,14 @@ proc nimbus_add_symkey_from_password(password: cstring):
|
|||||||
whisperKeys.symKeys.add($result, symKey)
|
whisperKeys.symKeys.add($result, symKey)
|
||||||
|
|
||||||
proc nimbus_delete_symkey(id: cstring): bool {.exportc.} =
|
proc nimbus_delete_symkey(id: cstring): bool {.exportc.} =
|
||||||
|
doAssert(not id.isNil, "Key id cannot be nil")
|
||||||
|
|
||||||
var unneeded: SymKey
|
var unneeded: SymKey
|
||||||
result = whisperKeys.symKeys.take($id, unneeded)
|
result = whisperKeys.symKeys.take($id, unneeded)
|
||||||
|
|
||||||
proc nimbus_get_symkey(id: cstring, symKey: ptr SymKey):
|
proc nimbus_get_symkey(id: cstring, symKey: ptr SymKey):
|
||||||
bool {.exportc.} =
|
bool {.exportc.} =
|
||||||
|
doAssert(not id.isNil, "Key id cannot be nil")
|
||||||
doAssert(not symKey.isNil, "Passed a null pointer as symKey")
|
doAssert(not symKey.isNil, "Passed a null pointer as symKey")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -298,6 +308,8 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} =
|
|||||||
## Encryption is mandatory.
|
## Encryption is mandatory.
|
||||||
## A symmetric key or an asymmetric key must be provided. Both is not allowed.
|
## A symmetric key or an asymmetric key must be provided. Both is not allowed.
|
||||||
## Providing a payload is mandatory, it cannot be nil, but can be of length 0.
|
## Providing a payload is mandatory, it cannot be nil, but can be of length 0.
|
||||||
|
doAssert(not message.isNil, "Message pointer cannot be nil")
|
||||||
|
|
||||||
var
|
var
|
||||||
sigPrivKey: Option[PrivateKey]
|
sigPrivKey: Option[PrivateKey]
|
||||||
asymKey: Option[PublicKey]
|
asymKey: Option[PublicKey]
|
||||||
@ -314,7 +326,11 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} =
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
if not message.pubKey.isNil():
|
if not message.pubKey.isNil():
|
||||||
asymKey = some(message.pubKey[])
|
try:
|
||||||
|
asymKey = some(initPublicKey(makeOpenArray(message.pubKey, 64)))
|
||||||
|
except EthKeysException:
|
||||||
|
error "Passed an invalid public key for encryption"
|
||||||
|
return false
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not message.symKeyID.isNil():
|
if not message.symKeyID.isNil():
|
||||||
@ -353,6 +369,8 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
|||||||
## A symmetric key or an asymmetric key must be provided. Both is not allowed.
|
## A symmetric key or an asymmetric key must be provided. Both is not allowed.
|
||||||
## In case of a passed handler, the received msg needs to be copied before the
|
## In case of a passed handler, the received msg needs to be copied before the
|
||||||
## handler ends.
|
## handler ends.
|
||||||
|
doAssert(not options.isNil, "Options pointer cannot be nil")
|
||||||
|
|
||||||
var
|
var
|
||||||
src: Option[PublicKey]
|
src: Option[PublicKey]
|
||||||
symKey: Option[SymKey]
|
symKey: Option[SymKey]
|
||||||
@ -367,7 +385,11 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
if not options.source.isNil():
|
if not options.source.isNil():
|
||||||
src = some(options.source[])
|
try:
|
||||||
|
src = some(initPublicKey(makeOpenArray(options.source, 64)))
|
||||||
|
except EthKeysException:
|
||||||
|
error "Passed an invalid public key as source"
|
||||||
|
return ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not options.symKeyID.isNil():
|
if not options.symKeyID.isNil():
|
||||||
@ -409,10 +431,14 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
|||||||
traceAsyncErrors node.setBloomFilter(node.filtersToBloom())
|
traceAsyncErrors node.setBloomFilter(node.filtersToBloom())
|
||||||
|
|
||||||
proc nimbus_unsubscribe_filter(id: cstring): bool {.exportc.} =
|
proc nimbus_unsubscribe_filter(id: cstring): bool {.exportc.} =
|
||||||
|
doAssert(not id.isNil, "Filter id cannot be nil")
|
||||||
|
|
||||||
result = node.unsubscribeFilter($id)
|
result = node.unsubscribeFilter($id)
|
||||||
|
|
||||||
proc nimbus_get_min_pow(): float64 {.exportc.} =
|
proc nimbus_get_min_pow(): float64 {.exportc.} =
|
||||||
result = node.protocolState(Whisper).config.powRequirement
|
result = node.protocolState(Whisper).config.powRequirement
|
||||||
|
|
||||||
proc nimbus_get_bloom_filter(bloom: ptr Bloom) {.exportc.} =
|
proc nimbus_get_bloom_filter(bloom: ptr Bloom) {.exportc.} =
|
||||||
|
doAssert(not bloom.isNil, "Bloom pointer cannot be nil")
|
||||||
|
|
||||||
bloom[] = node.protocolState(Whisper).config.bloom
|
bloom[] = node.protocolState(Whisper).config.bloom
|
||||||
|
Loading…
x
Reference in New Issue
Block a user