diff --git a/wrappers/libnimbus.nim b/wrappers/libnimbus.nim index d61be9248..59851a7da 100644 --- a/wrappers/libnimbus.nim +++ b/wrappers/libnimbus.nim @@ -111,8 +111,6 @@ proc subscribeChannel( # notice "no luck parsing", message=getCurrentExceptionMsg() proc nimbus_start(port: uint16 = 30303) {.exportc.} = - setupForeignThreadGc() - let address = Address( udpPort: port.Port, tcpPort: port.Port, ip: parseIpAddress("0.0.0.0")) @@ -140,20 +138,12 @@ proc nimbus_start(port: uint16 = 30303) {.exportc.} = asyncCheck node.peerPool.connectToNode(whisperNode) - tearDownForeignThreadGc() - proc nimbus_poll() {.exportc.} = - setupForeignThreadGc() - poll() - tearDownForeignThreadGc() - proc nimbus_join_public_chat(channel: cstring, handler: proc (msg: ptr CReceivedMessage) {.gcsafe, cdecl.}) {.exportc.} = - setupForeignThreadGc() - if handler.isNil: subscribeChannel($channel, nil) else: @@ -172,13 +162,9 @@ proc nimbus_join_public_chat(channel: cstring, subscribeChannel($channel, c_handler) - tearDownForeignThreadGc() - # TODO: Add signing key as parameter # TODO: How would we do key management? In nimbus (like in rpc) or in status go? proc nimbus_post_public(channel: cstring, payload: cstring) {.exportc.} = - setupForeignThreadGc() - let encPrivateKey = initPrivateKey("5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3") var ctx: HMAC[sha256] @@ -199,97 +185,65 @@ proc nimbus_post_public(channel: cstring, payload: cstring) {.exportc.} = payload = npayload, powTarget = 0.002) - tearDownForeignThreadGc() - proc nimbus_add_peer(nodeId: cstring) {.exportc.} = - setupForeignThreadGc() - var whisperENode: ENode discard initENode($nodeId, whisperENode) var whisperNode = newNode(whisperENode) asyncCheck node.peerPool.connectToNode(whisperNode) - tearDownForeignThreadGc() - # Whisper API (Similar to Whisper RPC API) # Mostly an example for now, lots of things to fix if continued like this. proc nimbus_string_to_topic(s: cstring): CTopic {.exportc.} = - setupForeignThreadGc() - let hash = digest(keccak256, $s) for i in 0..<4: result.topic[i] = hash.data[i] - tearDownForeignThreadGc() - # Asymmetric Keys API proc nimbus_new_keypair(): cstring {.exportc.} = ## It is important that the caller makes a copy of the returned cstring before ## doing any other API calls. - setupForeignThreadGc() - result = generateRandomID() whisperKeys.asymKeys.add($result, newKeyPair()) - tearDownForeignThreadGc() - proc nimbus_add_keypair(key: ptr PrivateKey): cstring {.exportc.} = ## It is important that the caller makes a copy of the returned cstring before ## doing any other API calls. - setupForeignThreadGc() - result = generateRandomID() # Creating a KeyPair here does a copy of the key and so does the add whisperKeys.asymKeys.add($result, KeyPair(seckey: key[], pubkey: key[].getPublicKey())) - tearDownForeignThreadGc() - proc nimbus_delete_keypair(id: cstring): bool {.exportc.} = - setupForeignThreadGc() - var unneeded: KeyPair result = whisperKeys.asymKeys.take($id, unneeded) - tearDownForeignThreadGc() - proc nimbus_get_private_key(id: cstring, privateKey: ptr PrivateKey): bool {.exportc.} = - setupForeignThreadGc() - try: privateKey[] = whisperKeys.asymkeys[$id].seckey result = true except KeyError: result = false - tearDownForeignThreadGc() - # Symmetric Keys API proc nimbus_add_symkey(key: ptr SymKey): cstring {.exportc.} = ## It is important that the caller makes a copy of the returned cstring before ## doing any other API calls. - setupForeignThreadGc() - result = generateRandomID().cstring # Copy of key happens at add whisperKeys.symKeys.add($result, key[]) - tearDownForeignThreadGc() - proc nimbus_add_symkey_from_password(password: cstring): cstring {.exportc.} = ## It is important that the caller makes a copy of the returned cstring before ## doing any other API calls. - setupForeignThreadGc() - var ctx: HMAC[sha256] var symKey: SymKey if pbkdf2(ctx, $password, "", 65356, symKey) != sizeof(SymKey): @@ -299,36 +253,24 @@ proc nimbus_add_symkey_from_password(password: cstring): whisperKeys.symKeys.add($result, symKey) - tearDownForeignThreadGc() - proc nimbus_delete_symkey(id: cstring): bool {.exportc.} = - setupForeignThreadGc() - var unneeded: SymKey result = whisperKeys.symKeys.take($id, unneeded) - tearDownForeignThreadGc() - proc nimbus_get_symkey(id: cstring, symKey: ptr SymKey): bool {.exportc.} = - setupForeignThreadGc() - try: symKey[] = whisperKeys.symkeys[$id] result = true except KeyError: result = false - tearDownForeignThreadGc() - # Whisper message posting and receiving API proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} = ## Encryption is not mandatory. ## A symKey, an asymKey, or nothing can be provided. asymKey has precedence. ## Providing a payload is mandatory. - setupForeignThreadGc() - var sigPrivKey: Option[PrivateKey] asymKey: Option[PublicKey] @@ -366,15 +308,11 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} = powTime = message.powTime, powTarget = message.powTarget) - tearDownForeignThreadGc() - proc nimbus_subscribe_filter(options: ptr CFilterOptions, handler: proc (msg: ptr CReceivedMessage, udata: pointer) {.gcsafe, cdecl.}, udata: pointer = nil): cstring {.exportc.} = ## In case of a passed handler, the received msg needs to be copied before the ## handler ends. - setupForeignThreadGc() - var src: Option[PublicKey] symKey: Option[SymKey] @@ -422,25 +360,11 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions, # TODO: better to have an "adding" proc here traceAsyncErrors node.setBloomFilter(node.filtersToBloom()) - tearDownForeignThreadGc() - proc nimbus_unsubscribe_filter(id: cstring): bool {.exportc.} = - setupForeignThreadGc() - result = node.unsubscribeFilter($id) - tearDownForeignThreadGc() - proc nimbus_get_min_pow(): float64 {.exportc.} = - setupForeignThreadGc() - result = node.protocolState(Whisper).config.powRequirement - tearDownForeignThreadGc() - proc nimbus_get_bloom_filter(bloom: ptr Bloom) {.exportc.} = - setupForeignThreadGc() - bloom[] = node.protocolState(Whisper).config.bloom - - tearDownForeignThreadGc()