Testing asymetric messages
p4u p4u@dabax.net
Mon, 25 Feb 2019 13:52:45 +0100
1 files changed,
22 insertions(+),
5 deletions(-)
jump to
M
net/swarm/swarm.go
→
net/swarm/swarm.go
@@ -3,10 +3,10 @@
import ( "context" "crypto/ecdsa" - "encoding/hex" "fmt" "os" "os/user" + "strings" "time" "github.com/ethereum/go-ethereum/common/hexutil"@@ -300,14 +300,31 @@ if subType == "raw" {
err = sn.Pss.SendRaw(hexutil.Bytes(dstAddr), dstTopic, hexutil.Bytes(msg)) } if subType == "asym" { - pubKeyBytes, err := hex.DecodeString(key) + if hasHexPrefix := strings.HasPrefix(key, "0x"); !hasHexPrefix { + key = "0x" + key + } + topics, addresses, err := sn.Pss.GetPublickeyPeers(key) if err != nil { return err } - err = sn.Pss.SetPeerPublicKey(pubKeyBytes, dstTopic, dstAddr) - if err != nil { - return err + topicFound := false + for i, t := range topics { + if dstTopic == t && fmt.Sprintf("%x", addresses[i]) == fmt.Sprintf("%x", dstAddr) { + topicFound = true + break + } + } + if !topicFound { + pubKeyBytes, err := hexutil.Decode(key) + if err != nil { + return err + } + err = sn.Pss.SetPeerPublicKey(pubKeyBytes, dstTopic, dstAddr) + if err != nil { + return err + } } + err = sn.Pss.SendAsym(key, dstTopic, hexutil.Bytes(msg)) } return err