Browse Source

Testing asymetric messages

feature/pss_asym
p4u 5 years ago
parent
commit
cfb681462a
1 changed files with 22 additions and 5 deletions
  1. +22
    -5
      net/swarm/swarm.go

+ 22
- 5
net/swarm/swarm.go

@ -3,10 +3,10 @@ package swarm
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex"
"fmt" "fmt"
"os" "os"
"os/user" "os/user"
"strings"
"time" "time"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -300,14 +300,31 @@ func (sn *SwarmNet) PssPub(subType, key, topic, msg, address string) error {
err = sn.Pss.SendRaw(hexutil.Bytes(dstAddr), dstTopic, hexutil.Bytes(msg)) err = sn.Pss.SendRaw(hexutil.Bytes(dstAddr), dstTopic, hexutil.Bytes(msg))
} }
if subType == "asym" { if subType == "asym" {
pubKeyBytes, err := hex.DecodeString(key)
if err != nil {
return err
if hasHexPrefix := strings.HasPrefix(key, "0x"); !hasHexPrefix {
key = "0x" + key
} }
err = sn.Pss.SetPeerPublicKey(pubKeyBytes, dstTopic, dstAddr)
topics, addresses, err := sn.Pss.GetPublickeyPeers(key)
if err != nil { if err != nil {
return err 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)) err = sn.Pss.SendAsym(key, dstTopic, hexutil.Bytes(msg))
} }
return err return err

Loading…
Cancel
Save