diff --git a/wrappers/libnimbus.h b/wrappers/libnimbus.h index 5d7926eac..350323ad5 100644 --- a/wrappers/libnimbus.h +++ b/wrappers/libnimbus.h @@ -12,8 +12,8 @@ extern "C" { typedef struct { uint8_t* decoded; size_t decodedLen; - uint8_t source[64]; /* TODO: change to ptr so they can be checked on nil? */ - uint8_t recipientPublicKey[64]; /* TODO: change to ptr so they can be checked on nil? */ + uint8_t* source; /* 64 bytes public key, can be nil */ + uint8_t* recipientPublicKey; /* 64 bytes public key, can be nil */ uint32_t timestamp; uint32_t ttl; uint8_t topic[4]; @@ -22,11 +22,11 @@ typedef struct { } received_message; typedef struct { - const char* symKeyID; - const char* privateKeyID; - uint8_t* source; /* 64 bytes public key */ + const char* symKeyID; /* Identifier for symmetric key, set to nil if none */ + const char* privateKeyID; /* Identifier for asymmetric key, set to nil if none */ + uint8_t* source; /* 64 bytes public key, set to nil if none */ double minPow; - uint8_t topic[4]; + uint8_t topic[4]; /* Will default to 0x00000000 if not provided */ int allowP2P; } filter_options; @@ -35,10 +35,10 @@ typedef struct { uint8_t* pubKey; /* 64 bytes public key, set to nil if none */ const char* sourceID; /* Identifier for asymmetric key, set to nil if none */ uint32_t ttl; - uint8_t topic[4]; /* default of 0 is OK */ - uint8_t* payload; /* payload to be send, can be len=0 but can not be nil */ + uint8_t topic[4]; /* Will default to 0x00000000 if not provided */ + uint8_t* payload; /* Payload to be send, can be len=0 but can not be nil */ size_t payloadLen; - uint8_t* padding; /* custom padding, can be set to nil */ + uint8_t* padding; /* Custom padding, can be set to nil */ size_t paddingLen; double powTime; double powTarget; diff --git a/wrappers/libnimbus.nim b/wrappers/libnimbus.nim index ee2ed61bf..fa16b2b29 100644 --- a/wrappers/libnimbus.nim +++ b/wrappers/libnimbus.nim @@ -31,8 +31,8 @@ type CReceivedMessage* = object decoded*: ptr byte decodedLen*: csize - source*: PublicKey - recipientPublicKey*: PublicKey + source*: ref PublicKey + recipientPublicKey*: ref PublicKey timestamp*: uint32 ttl*: uint32 topic*: Topic @@ -430,11 +430,18 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions, hash: msg.hash ) - # TODO: change this to ptr, so that C/go code can check on nil? + # Should be GCed when handler goes out of scope + var + source: ref PublicKey + recipientPublicKey: ref PublicKey if msg.decoded.src.isSome(): - cmsg.source = msg.decoded.src.get() + new(source) + source[] = msg.decoded.src.get() + cmsg.source = source if msg.dst.isSome(): - cmsg.recipientPublicKey = msg.dst.get() + new(recipientPublicKey) + recipientPublicKey[] = msg.dst.get() + cmsg.recipientPublicKey = recipientPublicKey handler(addr cmsg, udata) diff --git a/wrappers/wrapper_whisper_example.go b/wrappers/wrapper_whisper_example.go index da7d272ea..cb5975750 100644 --- a/wrappers/wrapper_whisper_example.go +++ b/wrappers/wrapper_whisper_example.go @@ -37,7 +37,10 @@ func poll() { func receiveHandler(msg *C.received_message, udata unsafe.Pointer) { receivedMsg := C.GoBytes(unsafe.Pointer(msg.decoded), C.int(msg.decodedLen)) fmt.Printf("[nim-status] received message %s\n", string(receivedMsg)) - fmt.Printf("[nim-status] source public key %x\n", msg.source) + if msg.source != nil { + source := C.GoBytes(unsafe.Pointer(msg.source), 64) + fmt.Printf("[nim-status] source public key %x\n", string(source)) + } msgCount := (*int)(udata) *msgCount += 1 fmt.Printf("[nim-status] message count %d\n", *msgCount)