generated from ipfs/ipfs-repository-template
/
record_bitswap.go
186 lines (151 loc) · 4.05 KB
/
record_bitswap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package types
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"github.com/ipfs/boxo/routing/http/internal/drjson"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multibase"
)
// Deprecated: use the more versatile [SchemaPeer] instead. For more information, read [IPIP-417].
//
// [IPIP-417]: https://github.com/ipfs/specs/pull/417
const SchemaBitswap = "bitswap"
var (
_ Record = &BitswapRecord{}
)
// Deprecated: use the more versatile [PeerRecord] instead. For more information, read [IPIP-417].
//
// [IPIP-417]: https://github.com/ipfs/specs/pull/417
type BitswapRecord struct {
Schema string
Protocol string
ID *peer.ID
Addrs []Multiaddr `json:",omitempty"`
}
func (br *BitswapRecord) GetSchema() string {
return br.Schema
}
var _ Record = &WriteBitswapRecord{}
// Deprecated: protocol-agnostic provide is being worked on in [IPIP-378]:
//
// [IPIP-378]: https://github.com/ipfs/specs/pull/378
type WriteBitswapRecord struct {
Schema string
Protocol string
Signature string
// this content must be untouched because it is signed and we need to verify it
RawPayload json.RawMessage `json:"Payload"`
Payload BitswapPayload `json:"-"`
}
type BitswapPayload struct {
Keys []CID
Timestamp *Time
AdvisoryTTL *Duration
ID *peer.ID
Addrs []Multiaddr
}
func (wr *WriteBitswapRecord) GetSchema() string {
return wr.Schema
}
type tmpBWPR WriteBitswapRecord
func (p *WriteBitswapRecord) UnmarshalJSON(b []byte) error {
var bwp tmpBWPR
err := json.Unmarshal(b, &bwp)
if err != nil {
return err
}
p.Protocol = bwp.Protocol
p.Schema = bwp.Schema
p.Signature = bwp.Signature
p.RawPayload = bwp.RawPayload
return json.Unmarshal(bwp.RawPayload, &p.Payload)
}
func (p *WriteBitswapRecord) IsSigned() bool {
return p.Signature != ""
}
func (p *WriteBitswapRecord) setRawPayload() error {
payloadBytes, err := drjson.MarshalJSONBytes(p.Payload)
if err != nil {
return fmt.Errorf("marshaling bitswap write provider payload: %w", err)
}
p.RawPayload = payloadBytes
return nil
}
func (p *WriteBitswapRecord) Sign(peerID peer.ID, key crypto.PrivKey) error {
if p.IsSigned() {
return errors.New("already signed")
}
if key == nil {
return errors.New("no key provided")
}
sid, err := peer.IDFromPrivateKey(key)
if err != nil {
return err
}
if sid != peerID {
return errors.New("not the correct signing key")
}
err = p.setRawPayload()
if err != nil {
return err
}
hash := sha256.Sum256([]byte(p.RawPayload))
sig, err := key.Sign(hash[:])
if err != nil {
return err
}
sigStr, err := multibase.Encode(multibase.Base64, sig)
if err != nil {
return fmt.Errorf("multibase-encoding signature: %w", err)
}
p.Signature = sigStr
return nil
}
func (p *WriteBitswapRecord) Verify() error {
if !p.IsSigned() {
return errors.New("not signed")
}
if p.Payload.ID == nil {
return errors.New("peer ID must be specified")
}
// note that we only generate and set the payload if it hasn't already been set
// to allow for passing through the payload untouched if it is already provided
if p.RawPayload == nil {
err := p.setRawPayload()
if err != nil {
return err
}
}
pk, err := p.Payload.ID.ExtractPublicKey()
if err != nil {
return fmt.Errorf("extracing public key from peer ID: %w", err)
}
_, sigBytes, err := multibase.Decode(p.Signature)
if err != nil {
return fmt.Errorf("multibase-decoding signature to verify: %w", err)
}
hash := sha256.Sum256([]byte(p.RawPayload))
ok, err := pk.Verify(hash[:], sigBytes)
if err != nil {
return fmt.Errorf("verifying hash with signature: %w", err)
}
if !ok {
return errors.New("signature failed to verify")
}
return nil
}
var _ Record = &WriteBitswapRecordResponse{}
// Deprecated: protocol-agnostic provide is being worked on in [IPIP-378]:
//
// [IPIP-378]: https://github.com/ipfs/specs/pull/378
type WriteBitswapRecordResponse struct {
Schema string
Protocol string
AdvisoryTTL *Duration
}
func (r *WriteBitswapRecordResponse) GetSchema() string {
return r.Schema
}