Skip to content

Commit abfb399

Browse files
committed
Add initial messaging.
Turns out we might not need DID as the public key is embedded in the To and From strings. So we have all we need. One public key per object or player.
1 parent 464a577 commit abfb399

File tree

3 files changed

+101
-231
lines changed

3 files changed

+101
-231
lines changed

did/did.go

Lines changed: 0 additions & 230 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/libp2p/go-libp2p-kad-dht v0.24.3
1010
github.com/libp2p/go-libp2p-pubsub v0.9.3
1111
github.com/mr-tron/base58 v1.2.0
12+
github.com/multiformats/go-multibase v0.2.0
1213
github.com/sirupsen/logrus v1.9.3
1314
go.deanishe.net/env v0.5.1
1415
gocloud.dev v0.33.0
@@ -92,7 +93,6 @@ require (
9293
github.com/multiformats/go-multiaddr v0.10.1 // indirect
9394
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
9495
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
95-
github.com/multiformats/go-multibase v0.2.0 // indirect
9696
github.com/multiformats/go-multicodec v0.9.0 // indirect
9797
github.com/multiformats/go-multihash v0.2.3 // indirect
9898
github.com/multiformats/go-multistream v0.4.1 // indirect

message/message.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package message
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"time"
7+
8+
"github.com/libp2p/go-libp2p/core/crypto"
9+
"github.com/libp2p/go-libp2p/core/peer"
10+
)
11+
12+
const Version = "1"
13+
14+
type Message struct {
15+
From string `json:"from"`
16+
To string `json:"to"`
17+
Version string `json:"version"`
18+
Data []byte `json:"data"`
19+
Created time.Time `json:"created"`
20+
Received time.Time `json:"received"`
21+
Signature []byte `json:"signature"`
22+
}
23+
24+
func New(from string, to string, data []byte) *Message {
25+
return &Message{
26+
From: from,
27+
To: to,
28+
Version: Version,
29+
Created: time.Now(),
30+
Data: data,
31+
}
32+
}
33+
34+
func Marshal(m *Message) ([]byte, error) {
35+
return json.Marshal(m)
36+
}
37+
38+
func (m *Message) UnsignedMessage() *Message {
39+
40+
// This returns
41+
c := &Message{
42+
To: m.To,
43+
From: m.From,
44+
Data: m.Data,
45+
Created: m.Created,
46+
Version: m.Version,
47+
}
48+
49+
return c
50+
}
51+
52+
func Unmarshal(data []byte) (*Message, error) {
53+
var msg Message
54+
if err := json.Unmarshal(data, &msg); err != nil {
55+
return nil, err
56+
}
57+
return &msg, nil
58+
}
59+
60+
func (m *Message) Sign(privateKey crypto.PrivKey) error {
61+
data, err := Marshal(m.UnsignedMessage())
62+
if err != nil {
63+
return fmt.Errorf("failed to marshal message: %v", err)
64+
}
65+
66+
sig, err := privateKey.Sign(data)
67+
if err != nil {
68+
return fmt.Errorf("failed to sign message: %v", err)
69+
}
70+
m.Signature = sig
71+
return nil
72+
}
73+
74+
func (m *Message) Verify() (bool, error) {
75+
publicKey, err := extractPublicKeyFromIPNS(m.From)
76+
if err != nil {
77+
return false, err
78+
}
79+
80+
data, err := Marshal(m.UnsignedMessage())
81+
if err != nil {
82+
return false, fmt.Errorf("failed to marshal message: %v", err)
83+
}
84+
85+
isValid, err := publicKey.Verify(data, m.Signature)
86+
if err != nil {
87+
return false, fmt.Errorf("error verifying message: %v", err)
88+
}
89+
90+
return isValid, nil
91+
}
92+
93+
func extractPublicKeyFromIPNS(ipns string) (crypto.PubKey, error) {
94+
pid, err := peer.Decode(ipns)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
return pid.ExtractPublicKey()
100+
}

0 commit comments

Comments
 (0)