generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 83
/
provider_bitswap.go
184 lines (146 loc) · 4.2 KB
/
provider_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
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"
)
const SchemaBitswap = "bitswap"
var _ WriteProviderRecord = &WriteBitswapProviderRecord{}
// WriteBitswapProviderRecord is used when we want to add a new provider record that is using bitswap.
type WriteBitswapProviderRecord struct {
Protocol string
Schema 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 (*WriteBitswapProviderRecord) IsWriteProviderRecord() {}
type tmpBWPR WriteBitswapProviderRecord
func (p *WriteBitswapProviderRecord) 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 *WriteBitswapProviderRecord) IsSigned() bool {
return p.Signature != ""
}
func (p *WriteBitswapProviderRecord) 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 *WriteBitswapProviderRecord) 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 *WriteBitswapProviderRecord) 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 _ ProviderResponse = &WriteBitswapProviderRecordResponse{}
// WriteBitswapProviderRecordResponse will be returned as a result of WriteBitswapProviderRecord
type WriteBitswapProviderRecordResponse struct {
Protocol string
Schema string
AdvisoryTTL *Duration
}
func (wbprr *WriteBitswapProviderRecordResponse) GetProtocol() string {
return wbprr.Protocol
}
func (wbprr *WriteBitswapProviderRecordResponse) GetSchema() string {
return wbprr.Schema
}
var _ ReadProviderRecord = &ReadBitswapProviderRecord{}
var _ ProviderResponse = &ReadBitswapProviderRecord{}
// ReadBitswapProviderRecord is a provider result with parameters for bitswap providers
type ReadBitswapProviderRecord struct {
Protocol string
Schema string
ID *peer.ID
Addrs []Multiaddr
}
func (rbpr *ReadBitswapProviderRecord) GetProtocol() string {
return rbpr.Protocol
}
func (rbpr *ReadBitswapProviderRecord) GetSchema() string {
return rbpr.Schema
}
func (*ReadBitswapProviderRecord) IsReadProviderRecord() {}