2022-07-25 15:28:17 +00:00
|
|
|
// Implements gomobile bindings for go-waku. Contains a set of functions that
|
|
|
|
// are exported when go-waku is exported as libraries for mobile devices
|
2022-04-12 12:12:14 +00:00
|
|
|
package gowaku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2023-02-10 20:17:23 +00:00
|
|
|
"database/sql"
|
2022-04-12 12:12:14 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2022-10-20 15:14:46 +00:00
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2022-12-19 23:21:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2023-03-10 18:41:19 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-02-16 16:17:52 +00:00
|
|
|
libp2pProtocol "github.com/libp2p/go-libp2p/core/protocol"
|
2022-04-12 12:12:14 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-02-10 20:17:23 +00:00
|
|
|
"github.com/waku-org/go-waku/waku"
|
|
|
|
"github.com/waku-org/go-waku/waku/persistence"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
2022-12-16 00:47:14 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/payload"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
type WakuState struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
|
|
|
node *node.WakuNode
|
|
|
|
|
|
|
|
relayTopics []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var wakuState WakuState
|
2022-04-12 12:12:14 +00:00
|
|
|
|
|
|
|
var errWakuNodeNotReady = errors.New("go-waku not initialized")
|
|
|
|
|
|
|
|
func randomHex(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNode(configJSON string) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errors.New("go-waku already initialized. stop it first"))
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config, err := getConfig(configJSON)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hostAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", *config.Host, *config.Port))
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prvKey *ecdsa.PrivateKey
|
|
|
|
if config.NodeKey != nil {
|
|
|
|
prvKey, err = crypto.HexToECDSA(*config.NodeKey)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
key, err := randomHex(32)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
prvKey, err = crypto.HexToECDSA(key)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []node.WakuNodeOption{
|
|
|
|
node.WithPrivateKey(prvKey),
|
|
|
|
node.WithHostAddress(hostAddr),
|
|
|
|
node.WithKeepAlive(time.Duration(*config.KeepAliveInterval) * time.Second),
|
2023-01-25 20:12:43 +00:00
|
|
|
node.NoDefaultWakuTopic(),
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *config.EnableRelay {
|
2023-03-10 18:41:19 +00:00
|
|
|
var pubsubOpt []pubsub.Option
|
|
|
|
if config.GossipSubParams != nil {
|
|
|
|
params := GetGossipSubParams(config.GossipSubParams)
|
|
|
|
pubsubOpt = append(pubsubOpt, pubsub.WithGossipSubParams(params))
|
|
|
|
}
|
|
|
|
|
2023-03-14 10:38:56 +00:00
|
|
|
pubsubOpt = append(pubsubOpt, pubsub.WithSeenMessagesTTL(GetSeenTTL(config)))
|
|
|
|
|
2023-03-10 18:41:19 +00:00
|
|
|
opts = append(opts, node.WithWakuRelayAndMinPeers(*config.MinPeersToPublish, pubsubOpt...))
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
if *config.EnableFilter {
|
|
|
|
opts = append(opts, node.WithWakuFilter(false))
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
if *config.EnableStore {
|
|
|
|
var db *sql.DB
|
|
|
|
var migrationFn func(*sql.DB) error
|
|
|
|
db, migrationFn, err = waku.ExtractDBAndMigration(*config.DatabaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return MakeJSONResponse(err)
|
|
|
|
}
|
|
|
|
opts = append(opts, node.WithWakuStore())
|
|
|
|
dbStore, err := persistence.NewDBStore(utils.Logger(),
|
|
|
|
persistence.WithDB(db),
|
|
|
|
persistence.WithMigrations(migrationFn),
|
|
|
|
persistence.WithRetentionPolicy(*config.RetentionMaxMessages, time.Duration(*config.RetentionTimeSeconds)*time.Second),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return MakeJSONResponse(err)
|
|
|
|
}
|
|
|
|
opts = append(opts, node.WithMessageProvider(dbStore))
|
|
|
|
}
|
|
|
|
|
2022-12-19 23:21:14 +00:00
|
|
|
if *config.EnableDiscV5 {
|
|
|
|
var bootnodes []*enode.Node
|
|
|
|
for _, addr := range config.DiscV5BootstrapNodes {
|
|
|
|
bootnode, err := enode.Parse(enode.ValidSchemes, addr)
|
|
|
|
if err != nil {
|
|
|
|
return MakeJSONResponse(err)
|
|
|
|
}
|
|
|
|
bootnodes = append(bootnodes, bootnode)
|
|
|
|
}
|
2023-01-13 23:58:22 +00:00
|
|
|
opts = append(opts, node.WithDiscoveryV5(*config.DiscV5UDPPort, bootnodes, true))
|
2022-12-19 23:21:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
wakuState.relayTopics = config.RelayTopics
|
2023-01-25 20:12:43 +00:00
|
|
|
|
2023-02-10 20:17:23 +00:00
|
|
|
lvl, err := zapcore.ParseLevel(*config.LogLevel)
|
2022-10-20 15:14:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return MakeJSONResponse(err)
|
|
|
|
}
|
2023-02-10 20:17:23 +00:00
|
|
|
|
|
|
|
opts = append(opts, node.WithLogLevel(lvl))
|
2022-10-20 15:14:46 +00:00
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
w, err := node.New(opts...)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
wakuState.node = w
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Start() string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
wakuState.ctx, wakuState.cancel = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
if err := wakuState.node.Start(wakuState.ctx); err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node.DiscV5() != nil {
|
|
|
|
if err := wakuState.node.DiscV5().Start(context.Background()); err != nil {
|
|
|
|
wakuState.node.Stop()
|
2023-01-04 18:46:22 +00:00
|
|
|
return MakeJSONResponse(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
for _, topic := range wakuState.relayTopics {
|
2023-02-17 17:30:59 +00:00
|
|
|
err := relaySubscribe(topic)
|
2023-01-25 20:12:43 +00:00
|
|
|
if err != nil {
|
2023-02-16 22:09:21 +00:00
|
|
|
wakuState.node.Stop()
|
2023-02-17 17:30:59 +00:00
|
|
|
return MakeJSONResponse(err)
|
2023-01-25 20:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Stop() string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
wakuState.node.Stop()
|
|
|
|
|
|
|
|
wakuState.cancel()
|
2022-07-26 18:23:26 +00:00
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
wakuState.node = nil
|
2022-04-12 12:12:14 +00:00
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 18:23:26 +00:00
|
|
|
func IsStarted() string {
|
2023-02-16 22:09:21 +00:00
|
|
|
return PrepareJSONResponse(wakuState.node != nil, nil)
|
2022-07-26 18:23:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
func PeerID() string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
return PrepareJSONResponse(wakuState.node.ID(), nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ListenAddresses() string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var addresses []string
|
2023-02-16 22:09:21 +00:00
|
|
|
for _, addr := range wakuState.node.ListenAddresses() {
|
2022-04-12 12:12:14 +00:00
|
|
|
addresses = append(addresses, addr.String())
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(addresses, nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 21:50:36 +00:00
|
|
|
func AddPeer(address string, protocolID string) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ma, err := multiaddr.NewMultiaddr(address)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 21:50:36 +00:00
|
|
|
peerID, err := wakuState.node.AddPeer(ma, libp2pProtocol.ID(protocolID))
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(peerID, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Connect(address string, ms int) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
if ms > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
err := wakuState.node.DialPeer(ctx, address)
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectPeerID(peerID string, ms int) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
pID, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ms > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
} else {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
err = wakuState.node.DialPeerByID(ctx, pID)
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Disconnect(peerID string) string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pID, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
err = wakuState.node.ClosePeerById(pID)
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PeerCnt() string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
return PrepareJSONResponse(wakuState.node.PeerCount(), nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ContentTopic(applicationName string, applicationVersion int, contentTopicName string, encoding string) string {
|
|
|
|
return protocol.NewContentTopic(applicationName, uint(applicationVersion), contentTopicName, encoding).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func PubsubTopic(name string, encoding string) string {
|
2023-03-07 22:11:52 +00:00
|
|
|
return protocol.NewNamedShardingPubsubTopic(name + "/" + encoding).String()
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultPubsubTopic() string {
|
|
|
|
return protocol.DefaultPubsubTopic().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTopic(topic string) string {
|
|
|
|
if topic == "" {
|
|
|
|
return protocol.DefaultPubsubTopic().String()
|
|
|
|
}
|
|
|
|
return topic
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriptionMsg struct {
|
2022-10-09 15:08:46 +00:00
|
|
|
MessageID string `json:"messageId"`
|
2022-04-12 12:12:14 +00:00
|
|
|
PubsubTopic string `json:"pubsubTopic"`
|
|
|
|
Message *pb.WakuMessage `json:"wakuMessage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSubscriptionMessage(msg *protocol.Envelope) *subscriptionMsg {
|
|
|
|
return &subscriptionMsg{
|
|
|
|
MessageID: hexutil.Encode(msg.Hash()),
|
|
|
|
PubsubTopic: msg.PubsubTopic(),
|
|
|
|
Message: msg.Message(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Peers() string {
|
2023-02-16 22:09:21 +00:00
|
|
|
if wakuState.node == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 22:09:21 +00:00
|
|
|
peers, err := wakuState.node.Peers()
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(peers, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalPubkey(pub []byte) (ecdsa.PublicKey, error) {
|
|
|
|
x, y := elliptic.Unmarshal(secp256k1.S256(), pub)
|
|
|
|
if x == nil {
|
|
|
|
return ecdsa.PublicKey{}, errors.New("invalid public key")
|
|
|
|
}
|
|
|
|
return ecdsa.PublicKey{Curve: secp256k1.S256(), X: x, Y: y}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:47:14 +00:00
|
|
|
func extractPubKeyAndSignature(payload *payload.DecodedPayload) (pubkey string, signature string) {
|
2022-10-14 15:41:27 +00:00
|
|
|
pkBytes := crypto.FromECDSAPub(payload.PubKey)
|
|
|
|
if len(pkBytes) != 0 {
|
|
|
|
pubkey = hexutil.Encode(pkBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(payload.Signature) != 0 {
|
|
|
|
signature = hexutil.Encode(payload.Signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
func DecodeSymmetric(messageJSON string, symmetricKey string) string {
|
|
|
|
var msg pb.WakuMessage
|
|
|
|
err := json.Unmarshal([]byte(messageJSON), &msg)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Version == 0 {
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(msg.Payload, nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
} else if msg.Version > 1 {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errors.New("unsupported wakumessage version"))
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:47:14 +00:00
|
|
|
keyInfo := &payload.KeyInfo{
|
|
|
|
Kind: payload.Symmetric,
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 19:19:04 +00:00
|
|
|
keyInfo.SymKey, err = utils.DecodeHexString(symmetricKey)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:47:14 +00:00
|
|
|
payload, err := payload.DecodePayload(&msg, keyInfo)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:41:27 +00:00
|
|
|
pubkey, signature := extractPubKeyAndSignature(payload)
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
response := struct {
|
2022-10-14 15:41:27 +00:00
|
|
|
PubKey string `json:"pubkey,omitempty"`
|
|
|
|
Signature string `json:"signature,omitempty"`
|
2022-04-12 12:12:14 +00:00
|
|
|
Data []byte `json:"data"`
|
|
|
|
Padding []byte `json:"padding"`
|
|
|
|
}{
|
2022-10-14 15:41:27 +00:00
|
|
|
PubKey: pubkey,
|
|
|
|
Signature: signature,
|
2022-04-12 12:12:14 +00:00
|
|
|
Data: payload.Data,
|
|
|
|
Padding: payload.Padding,
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(response, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeAsymmetric(messageJSON string, privateKey string) string {
|
|
|
|
var msg pb.WakuMessage
|
|
|
|
err := json.Unmarshal([]byte(messageJSON), &msg)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Version == 0 {
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(msg.Payload, nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
} else if msg.Version > 1 {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errors.New("unsupported wakumessage version"))
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:47:14 +00:00
|
|
|
keyInfo := &payload.KeyInfo{
|
|
|
|
Kind: payload.Asymmetric,
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 19:19:04 +00:00
|
|
|
keyBytes, err := utils.DecodeHexString(privateKey)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyInfo.PrivKey, err = crypto.ToECDSA(keyBytes)
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:47:14 +00:00
|
|
|
payload, err := payload.DecodePayload(&msg, keyInfo)
|
2022-04-12 12:12:14 +00:00
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 15:41:27 +00:00
|
|
|
pubkey, signature := extractPubKeyAndSignature(payload)
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
response := struct {
|
2022-10-14 15:41:27 +00:00
|
|
|
PubKey string `json:"pubkey,omitempty"`
|
|
|
|
Signature string `json:"signature,omitempty"`
|
2022-04-12 12:12:14 +00:00
|
|
|
Data []byte `json:"data"`
|
|
|
|
Padding []byte `json:"padding"`
|
|
|
|
}{
|
2022-10-14 15:41:27 +00:00
|
|
|
PubKey: pubkey,
|
|
|
|
Signature: signature,
|
2022-04-12 12:12:14 +00:00
|
|
|
Data: payload.Data,
|
|
|
|
Padding: payload.Padding,
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(response, err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|