2019-11-21 16:19:22 +00:00
|
|
|
// +build nimbus
|
|
|
|
|
|
|
|
package nimbusbridge
|
|
|
|
|
|
|
|
// https://golang.org/cmd/cgo/
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libnimbus.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"unsafe"
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-11-21 16:19:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nimbusPublicWhisperAPIWrapper struct {
|
|
|
|
filterMessagesMu *sync.Mutex
|
|
|
|
filterMessages *map[string]*list.List
|
|
|
|
routineQueue *RoutineQueue
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
// NewNimbusPublicWhisperAPIWrapper returns an object that wraps Nimbus's PublicWhisperAPI in a types interface
|
|
|
|
func NewNimbusPublicWhisperAPIWrapper(filterMessagesMu *sync.Mutex, filterMessages *map[string]*list.List, routineQueue *RoutineQueue) types.PublicWhisperAPI {
|
2019-11-21 16:19:22 +00:00
|
|
|
return &nimbusPublicWhisperAPIWrapper{
|
|
|
|
filterMessagesMu: filterMessagesMu,
|
|
|
|
filterMessages: filterMessages,
|
|
|
|
routineQueue: routineQueue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPrivateKey imports the given private key.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *nimbusPublicWhisperAPIWrapper) AddPrivateKey(ctx context.Context, privateKey types.HexBytes) (string, error) {
|
2019-12-18 13:18:31 +00:00
|
|
|
retVal := w.routineQueue.Send(func(c chan<- callReturn) {
|
2019-11-21 16:19:22 +00:00
|
|
|
privKeyC := C.CBytes(privateKey)
|
|
|
|
defer C.free(unsafe.Pointer(privKeyC))
|
|
|
|
|
|
|
|
idC := C.malloc(C.size_t(C.ID_LEN))
|
|
|
|
defer C.free(idC)
|
|
|
|
if C.nimbus_add_keypair((*C.uchar)(privKeyC), (*C.uchar)(idC)) {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{value: types.EncodeHex(C.GoBytes(idC, C.ID_LEN))}
|
2019-11-21 16:19:22 +00:00
|
|
|
} else {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{err: errors.New("failed to add private key to Nimbus")}
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
})
|
2019-12-18 13:18:31 +00:00
|
|
|
if retVal.err != nil {
|
|
|
|
return "", retVal.err
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:18:31 +00:00
|
|
|
return retVal.value.(string), nil
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateSymKeyFromPassword derives a key from the given password, stores it, and returns its ID.
|
|
|
|
func (w *nimbusPublicWhisperAPIWrapper) GenerateSymKeyFromPassword(ctx context.Context, passwd string) (string, error) {
|
2019-12-18 13:18:31 +00:00
|
|
|
retVal := w.routineQueue.Send(func(c chan<- callReturn) {
|
2019-11-21 16:19:22 +00:00
|
|
|
passwordC := C.CString(passwd)
|
|
|
|
defer C.free(unsafe.Pointer(passwordC))
|
|
|
|
|
|
|
|
idC := C.malloc(C.size_t(C.ID_LEN))
|
|
|
|
defer C.free(idC)
|
|
|
|
if C.nimbus_add_symkey_from_password(passwordC, (*C.uchar)(idC)) {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{value: types.EncodeHex(C.GoBytes(idC, C.ID_LEN))}
|
2019-11-21 16:19:22 +00:00
|
|
|
} else {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{err: errors.New("failed to add symkey to Nimbus")}
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
})
|
2019-12-18 13:18:31 +00:00
|
|
|
if retVal.err != nil {
|
|
|
|
return "", retVal.err
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:18:31 +00:00
|
|
|
return retVal.value.(string), nil
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteKeyPair removes the key with the given key if it exists.
|
|
|
|
func (w *nimbusPublicWhisperAPIWrapper) DeleteKeyPair(ctx context.Context, key string) (bool, error) {
|
2019-12-18 13:18:31 +00:00
|
|
|
retVal := w.routineQueue.Send(func(c chan<- callReturn) {
|
2019-11-21 16:19:22 +00:00
|
|
|
keyC, err := decodeHexID(key)
|
|
|
|
if err != nil {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{err: err}
|
2019-11-21 16:19:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer C.free(unsafe.Pointer(keyC))
|
|
|
|
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{value: C.nimbus_delete_keypair(keyC)}
|
2019-11-21 16:19:22 +00:00
|
|
|
})
|
2019-12-18 13:18:31 +00:00
|
|
|
if retVal.err != nil {
|
|
|
|
return false, retVal.err
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:18:31 +00:00
|
|
|
return retVal.value.(bool), nil
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessageFilter creates a new filter that can be used to poll for
|
|
|
|
// (new) messages that satisfy the given criteria.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *nimbusPublicWhisperAPIWrapper) NewMessageFilter(req types.Criteria) (string, error) {
|
2019-11-21 16:19:22 +00:00
|
|
|
// topics := make([]whisper.TopicType, len(req.Topics))
|
|
|
|
// for index, tt := range req.Topics {
|
|
|
|
// topics[index] = whisper.TopicType(tt)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// criteria := whisper.Criteria{
|
|
|
|
// SymKeyID: req.SymKeyID,
|
|
|
|
// PrivateKeyID: req.PrivateKeyID,
|
|
|
|
// Sig: req.Sig,
|
|
|
|
// MinPow: req.MinPow,
|
|
|
|
// Topics: topics,
|
|
|
|
// AllowP2P: req.AllowP2P,
|
|
|
|
// }
|
|
|
|
// return w.publicWhisperAPI.NewMessageFilter(criteria)
|
|
|
|
// TODO
|
|
|
|
return "", errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilterMessages returns the messages that match the filter criteria and
|
|
|
|
// are received between the last poll and now.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *nimbusPublicWhisperAPIWrapper) GetFilterMessages(id string) ([]*types.Message, error) {
|
2019-11-21 16:19:22 +00:00
|
|
|
idC := C.CString(id)
|
|
|
|
defer C.free(unsafe.Pointer(idC))
|
|
|
|
|
|
|
|
var (
|
|
|
|
messageList *list.List
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
w.filterMessagesMu.Lock()
|
|
|
|
defer w.filterMessagesMu.Unlock()
|
|
|
|
if messageList, ok = (*w.filterMessages)[id]; !ok {
|
|
|
|
return nil, fmt.Errorf("no filter with ID %s", id)
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
retVal := make([]*types.Message, messageList.Len())
|
2019-11-21 16:19:22 +00:00
|
|
|
if messageList.Len() == 0 {
|
|
|
|
return retVal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
elem := messageList.Front()
|
|
|
|
index := 0
|
|
|
|
for elem != nil {
|
2019-11-23 17:57:05 +00:00
|
|
|
retVal[index] = (elem.Value).(*types.Message)
|
2019-11-21 16:19:22 +00:00
|
|
|
index++
|
|
|
|
next := elem.Next()
|
|
|
|
messageList.Remove(elem)
|
|
|
|
elem = next
|
|
|
|
}
|
|
|
|
return retVal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post posts a message on the Whisper network.
|
|
|
|
// returns the hash of the message in case of success.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *nimbusPublicWhisperAPIWrapper) Post(ctx context.Context, req types.NewMessage) ([]byte, error) {
|
2019-12-18 13:18:31 +00:00
|
|
|
retVal := w.routineQueue.Send(func(c chan<- callReturn) {
|
2019-11-21 16:19:22 +00:00
|
|
|
msg := C.post_message{
|
|
|
|
ttl: C.uint32_t(req.TTL),
|
|
|
|
powTime: C.double(req.PowTime),
|
|
|
|
powTarget: C.double(req.PowTarget),
|
|
|
|
}
|
|
|
|
if req.SigID != "" {
|
|
|
|
sourceID, err := decodeHexID(req.SigID)
|
|
|
|
if err != nil {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{err: err}
|
2019-11-21 16:19:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
msg.sourceID = sourceID
|
|
|
|
defer C.free(unsafe.Pointer(sourceID))
|
|
|
|
}
|
|
|
|
if req.SymKeyID != "" {
|
|
|
|
symKeyID, err := decodeHexID(req.SymKeyID)
|
|
|
|
if err != nil {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{err: err}
|
2019-11-21 16:19:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
msg.symKeyID = symKeyID
|
|
|
|
defer C.free(unsafe.Pointer(symKeyID))
|
|
|
|
}
|
|
|
|
if req.PublicKey != nil && len(req.PublicKey) > 0 {
|
2020-01-10 14:53:35 +00:00
|
|
|
msg.pubKey = (*C.uchar)(C.CBytes(req.PublicKey[1:]))
|
2019-11-21 16:19:22 +00:00
|
|
|
defer C.free(unsafe.Pointer(msg.pubKey))
|
|
|
|
}
|
|
|
|
msg.payloadLen = C.size_t(len(req.Payload))
|
|
|
|
msg.payload = (*C.uchar)(C.CBytes(req.Payload))
|
|
|
|
defer C.free(unsafe.Pointer(msg.payload))
|
|
|
|
msg.paddingLen = C.size_t(len(req.Padding))
|
|
|
|
msg.padding = (*C.uchar)(C.CBytes(req.Padding))
|
|
|
|
defer C.free(unsafe.Pointer(msg.padding))
|
|
|
|
copyTopicToCBuffer(&msg.topic[0], req.Topic[:])
|
|
|
|
|
|
|
|
// TODO: return envelope hash once nimbus_post is improved to return it
|
|
|
|
if C.nimbus_post(&msg) {
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{value: make([]byte, 0)}
|
2019-11-21 16:19:22 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-18 13:18:31 +00:00
|
|
|
c <- callReturn{err: fmt.Errorf("failed to post message symkeyid=%s pubkey=%#x topic=%#x", req.SymKeyID, req.PublicKey, req.Topic[:])}
|
2019-11-21 16:19:22 +00:00
|
|
|
// hashC := C.nimbus_post(&msg)
|
|
|
|
// if hashC == nil {
|
|
|
|
// return nil, errors.New("Nimbus failed to post message")
|
|
|
|
// }
|
|
|
|
// return hex.DecodeString(C.GoString(hashC))
|
|
|
|
})
|
2019-12-18 13:18:31 +00:00
|
|
|
if retVal.err != nil {
|
|
|
|
return nil, retVal.err
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 13:18:31 +00:00
|
|
|
return retVal.value.([]byte), nil
|
2019-11-21 16:19:22 +00:00
|
|
|
}
|