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"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2022-11-02 10:39:13 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
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-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-04-12 12:12:14 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
2022-09-14 19:19:04 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2022-04-12 12:12:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var wakuNode *node.WakuNode
|
2022-07-26 18:23:26 +00:00
|
|
|
var wakuStarted = false
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type wakuConfig struct {
|
|
|
|
Host *string `json:"host,omitempty"`
|
|
|
|
Port *int `json:"port,omitempty"`
|
|
|
|
AdvertiseAddress *string `json:"advertiseAddr,omitempty"`
|
|
|
|
NodeKey *string `json:"nodeKey,omitempty"`
|
2022-10-20 15:14:46 +00:00
|
|
|
LogLevel *string `json:"logLevel,omitempty"`
|
2022-04-12 12:12:14 +00:00
|
|
|
KeepAliveInterval *int `json:"keepAliveInterval,omitempty"`
|
|
|
|
EnableRelay *bool `json:"relay"`
|
2022-08-09 13:48:23 +00:00
|
|
|
EnableFilter *bool `json:"filter"`
|
2022-04-12 12:12:14 +00:00
|
|
|
MinPeersToPublish *int `json:"minPeersToPublish"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultHost = "0.0.0.0"
|
|
|
|
var defaultPort = 60000
|
|
|
|
var defaultKeepAliveInterval = 20
|
|
|
|
var defaultEnableRelay = true
|
|
|
|
var defaultMinPeersToPublish = 0
|
2022-08-09 13:48:23 +00:00
|
|
|
var defaultEnableFilter = false
|
2022-10-20 15:14:46 +00:00
|
|
|
var defaultLogLevel = "INFO"
|
2022-04-12 12:12:14 +00:00
|
|
|
|
|
|
|
func getConfig(configJSON string) (wakuConfig, error) {
|
|
|
|
var config wakuConfig
|
|
|
|
if configJSON != "" {
|
|
|
|
err := json.Unmarshal([]byte(configJSON), &config)
|
|
|
|
if err != nil {
|
|
|
|
return wakuConfig{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Host == nil {
|
|
|
|
config.Host = &defaultHost
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.EnableRelay == nil {
|
|
|
|
config.EnableRelay = &defaultEnableRelay
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
if config.EnableFilter == nil {
|
|
|
|
config.EnableFilter = &defaultEnableFilter
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
if config.Host == nil {
|
|
|
|
config.Host = &defaultHost
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Port == nil {
|
|
|
|
config.Port = &defaultPort
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.KeepAliveInterval == nil {
|
|
|
|
config.KeepAliveInterval = &defaultKeepAliveInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.MinPeersToPublish == nil {
|
|
|
|
config.MinPeersToPublish = &defaultMinPeersToPublish
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:14:46 +00:00
|
|
|
if config.LogLevel == nil {
|
|
|
|
config.LogLevel = &defaultLogLevel
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNode(configJSON string) string {
|
|
|
|
if wakuNode != 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),
|
|
|
|
}
|
|
|
|
|
|
|
|
if *config.EnableRelay {
|
|
|
|
opts = append(opts, node.WithWakuRelayAndMinPeers(*config.MinPeersToPublish))
|
|
|
|
}
|
|
|
|
|
2022-08-09 13:48:23 +00:00
|
|
|
if *config.EnableFilter {
|
|
|
|
opts = append(opts, node.WithWakuFilter(false))
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:14:46 +00:00
|
|
|
// for go-libp2p loggers
|
|
|
|
lvl, err := logging.LevelFromString(*config.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return MakeJSONResponse(err)
|
|
|
|
}
|
|
|
|
logging.SetAllLoggers(lvl)
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
w, err := node.New(ctx, opts...)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wakuNode = w
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Start() string {
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := wakuNode.Start(); err != nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 18:23:26 +00:00
|
|
|
wakuStarted = true
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Stop() string {
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wakuNode.Stop()
|
2022-07-26 18:23:26 +00:00
|
|
|
|
|
|
|
wakuStarted = false
|
2022-04-12 12:12:14 +00:00
|
|
|
wakuNode = nil
|
|
|
|
|
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 {
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(wakuStarted, nil)
|
2022-07-26 18:23:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
func PeerID() string {
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(wakuNode.ID(), nil)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ListenAddresses() string {
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var addresses []string
|
|
|
|
for _, addr := range wakuNode.ListenAddresses() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func AddPeer(address string, protocolID string) string {
|
|
|
|
if wakuNode == 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
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
peerID, err := wakuNode.AddPeer(ma, 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 {
|
|
|
|
if wakuNode == 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := wakuNode.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 {
|
|
|
|
if wakuNode == 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = wakuNode.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 {
|
|
|
|
if wakuNode == 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
|
|
|
}
|
|
|
|
|
|
|
|
err = wakuNode.ClosePeerById(pID)
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(err)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PeerCnt() string {
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 15:08:46 +00:00
|
|
|
return PrepareJSONResponse(wakuNode.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 {
|
|
|
|
return protocol.NewPubsubTopic(name, encoding).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if wakuNode == nil {
|
2022-10-09 15:08:46 +00:00
|
|
|
return MakeJSONResponse(errWakuNodeNotReady)
|
2022-04-12 12:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
peers, err := wakuNode.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-10-14 15:41:27 +00:00
|
|
|
func extractPubKeyAndSignature(payload *node.DecodedPayload) (pubkey string, signature string) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
keyInfo := &node.KeyInfo{
|
|
|
|
Kind: node.Symmetric,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := node.DecodePayload(&msg, keyInfo)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
keyInfo := &node.KeyInfo{
|
|
|
|
Kind: node.Asymmetric,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := node.DecodePayload(&msg, keyInfo)
|
|
|
|
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
|
|
|
}
|