status-go/server/pairing/peers/payload.go

51 lines
1018 B
Go
Raw Normal View History

package peers
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
udpp2p "github.com/schollz/peerdiscovery"
"github.com/status-im/status-go/protocol/protobuf"
)
type LocalPairingPeerHello struct {
protobuf.LocalPairingPeerHello
Discovered udpp2p.Discovered
}
func NewLocalPairingPeerHello(id []byte, name, deviceType string, k *ecdsa.PrivateKey) (*LocalPairingPeerHello, error) {
h := new(LocalPairingPeerHello)
h.PeerId = id
h.DeviceName = name
h.DeviceType = deviceType
err := h.sign(k)
if err != nil {
return nil, err
}
return h, nil
}
func (h *LocalPairingPeerHello) hash() []byte {
dHash := sha256.Sum256(append(h.PeerId, []byte(h.DeviceName+h.DeviceType)...))
return dHash[:]
}
func (h *LocalPairingPeerHello) sign(k *ecdsa.PrivateKey) error {
s, err := ecdsa.SignASN1(rand.Reader, k, h.hash())
if err != nil {
return err
}
h.Signature = s
return nil
}
func (h *LocalPairingPeerHello) verify(k *ecdsa.PublicKey) bool {
return ecdsa.VerifyASN1(k, h.hash(), h.Signature)
}