Skip to content

Commit c8d00d1

Browse files
committed
feat: upgrade boxo for refactored boxo/ipns package
1 parent b685355 commit c8d00d1

File tree

21 files changed

+149
-650
lines changed

21 files changed

+149
-650
lines changed

client/rpc/name.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,20 @@ import (
1010
caopts "github.com/ipfs/boxo/coreiface/options"
1111
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
1212
"github.com/ipfs/boxo/coreiface/path"
13+
"github.com/ipfs/boxo/ipns"
1314
)
1415

1516
type NameAPI HttpApi
1617

1718
type ipnsEntry struct {
18-
JName string `json:"Name"`
19-
JValue string `json:"Value"`
20-
21-
path path.Path
22-
}
23-
24-
func (e *ipnsEntry) Name() string {
25-
return e.JName
19+
Name string `json:"Name"`
20+
Value string `json:"Value"`
2621
}
2722

28-
func (e *ipnsEntry) Value() path.Path {
29-
return e.path
30-
}
31-
32-
func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.NamePublishOption) (iface.IpnsEntry, error) {
23+
func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.NamePublishOption) (ipns.Name, error) {
3324
options, err := caopts.NamePublishOptions(opts...)
3425
if err != nil {
35-
return nil, err
26+
return ipns.Name{}, err
3627
}
3728

3829
req := api.core().Request("name/publish", p.String()).
@@ -47,10 +38,9 @@ func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.Nam
4738

4839
var out ipnsEntry
4940
if err := req.Exec(ctx, &out); err != nil {
50-
return nil, err
41+
return ipns.Name{}, err
5142
}
52-
out.path = path.New(out.JValue)
53-
return &out, out.path.IsValid()
43+
return ipns.NameFromString(out.Name)
5444
}
5545

5646
func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.NameResolveOption) (<-chan iface.IpnsResult, error) {

cmd/ipfs/init.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
fsrepo "github.com/ipfs/kubo/repo/fsrepo"
2020

2121
options "github.com/ipfs/boxo/coreiface/options"
22+
nsopts "github.com/ipfs/boxo/coreiface/options/namesys"
2223
"github.com/ipfs/boxo/files"
2324
cmds "github.com/ipfs/go-ipfs-cmds"
2425
config "github.com/ipfs/kubo/config"
@@ -262,5 +263,5 @@ func initializeIpnsKeyspace(repoRoot string) error {
262263
return err
263264
}
264265

265-
return nd.Namesys.Publish(ctx, nd.PrivateKey, path.FromCid(emptyDir.Cid()))
266+
return nd.Namesys.Publish(ctx, nd.PrivateKey, path.FromCid(emptyDir.Cid()), nsopts.PublishCompatibleWithV1(true))
266267
}

config/routing.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Router struct {
2828
Type RouterType
2929

3030
// Parameters are extra configuration that this router might need.
31-
// A common one for reframe router is "Endpoint".
31+
// A common one for HTTP router is "Endpoint".
3232
Parameters interface{}
3333
}
3434

@@ -81,8 +81,6 @@ func (r *RouterParser) UnmarshalJSON(b []byte) error {
8181
switch out.Type {
8282
case RouterTypeHTTP:
8383
p = &HTTPRouterParams{}
84-
case RouterTypeReframe:
85-
p = &ReframeRouterParams{}
8684
case RouterTypeDHT:
8785
p = &DHTRouterParams{}
8886
case RouterTypeSequential:
@@ -106,7 +104,6 @@ func (r *RouterParser) UnmarshalJSON(b []byte) error {
106104
type RouterType string
107105

108106
const (
109-
RouterTypeReframe RouterType = "reframe" // More info here: https://github.com/ipfs/specs/tree/main/reframe . Actually deprecated.
110107
RouterTypeHTTP RouterType = "http" // HTTP JSON API for delegated routing systems (IPIP-337).
111108
RouterTypeDHT RouterType = "dht" // DHT router.
112109
RouterTypeSequential RouterType = "sequential" // Router helper to execute several routers sequentially.
@@ -133,12 +130,6 @@ const (
133130

134131
var MethodNameList = []MethodName{MethodNameProvide, MethodNameFindPeers, MethodNameFindProviders, MethodNameGetIPNS, MethodNamePutIPNS}
135132

136-
type ReframeRouterParams struct {
137-
// Endpoint is the URL where the routing implementation will point to get the information.
138-
// Usually used for reframe Routers.
139-
Endpoint string
140-
}
141-
142133
type HTTPRouterParams struct {
143134
// Endpoint is the URL where the routing implementation will point to get the information.
144135
Endpoint string

config/routing_test.go

Lines changed: 12 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ func TestRouterParameters(t *testing.T) {
2323
PublicIPNetwork: false,
2424
},
2525
}},
26-
"router-reframe": {Router{
27-
Type: RouterTypeReframe,
28-
Parameters: ReframeRouterParams{
29-
Endpoint: "reframe-endpoint",
30-
},
31-
}},
3226
"router-parallel": {Router{
3327
Type: RouterTypeParallel,
3428
Parameters: ComposableRouterParams{
@@ -39,7 +33,7 @@ func TestRouterParameters(t *testing.T) {
3933
IgnoreErrors: true,
4034
},
4135
{
42-
RouterName: "router-reframe",
36+
RouterName: "router-dht",
4337
Timeout: Duration{10 * time.Second},
4438
IgnoreErrors: false,
4539
ExecuteAfter: &OptionalDuration{&sec},
@@ -58,7 +52,7 @@ func TestRouterParameters(t *testing.T) {
5852
IgnoreErrors: true,
5953
},
6054
{
61-
RouterName: "router-reframe",
55+
RouterName: "router-dht",
6256
Timeout: Duration{10 * time.Second},
6357
IgnoreErrors: false,
6458
},
@@ -69,7 +63,7 @@ func TestRouterParameters(t *testing.T) {
6963
},
7064
Methods: Methods{
7165
MethodNameFindPeers: {
72-
RouterName: "router-reframe",
66+
RouterName: "router-dht",
7367
},
7468
MethodNameFindProviders: {
7569
RouterName: "router-dht",
@@ -99,95 +93,48 @@ func TestRouterParameters(t *testing.T) {
9993
dhtp := r2.Routers["router-dht"].Parameters
10094
require.IsType(&DHTRouterParams{}, dhtp)
10195

102-
rp := r2.Routers["router-reframe"].Parameters
103-
require.IsType(&ReframeRouterParams{}, rp)
104-
10596
sp := r2.Routers["router-sequential"].Parameters
10697
require.IsType(&ComposableRouterParams{}, sp)
10798

10899
pp := r2.Routers["router-parallel"].Parameters
109100
require.IsType(&ComposableRouterParams{}, pp)
110101
}
111102

112-
func TestRouterMissingParameters(t *testing.T) {
113-
require := require.New(t)
114-
115-
r := Routing{
116-
Type: NewOptionalString("custom"),
117-
Routers: map[string]RouterParser{
118-
"router-wrong-reframe": {Router{
119-
Type: RouterTypeReframe,
120-
Parameters: DHTRouterParams{
121-
Mode: "auto",
122-
AcceleratedDHTClient: true,
123-
PublicIPNetwork: false,
124-
},
125-
}},
126-
},
127-
Methods: Methods{
128-
MethodNameFindPeers: {
129-
RouterName: "router-wrong-reframe",
130-
},
131-
MethodNameFindProviders: {
132-
RouterName: "router-wrong-reframe",
133-
},
134-
MethodNameGetIPNS: {
135-
RouterName: "router-wrong-reframe",
136-
},
137-
MethodNameProvide: {
138-
RouterName: "router-wrong-reframe",
139-
},
140-
MethodNamePutIPNS: {
141-
RouterName: "router-wrong-reframe",
142-
},
143-
},
144-
}
145-
146-
out, err := json.Marshal(r)
147-
require.NoError(err)
148-
149-
r2 := &Routing{}
150-
151-
err = json.Unmarshal(out, r2)
152-
require.NoError(err)
153-
require.Empty(r2.Routers["router-wrong-reframe"].Parameters.(*ReframeRouterParams).Endpoint)
154-
}
155-
156103
func TestMethods(t *testing.T) {
157104
require := require.New(t)
158105

159106
methodsOK := Methods{
160107
MethodNameFindPeers: {
161-
RouterName: "router-wrong-reframe",
108+
RouterName: "router-wrong",
162109
},
163110
MethodNameFindProviders: {
164-
RouterName: "router-wrong-reframe",
111+
RouterName: "router-wrong",
165112
},
166113
MethodNameGetIPNS: {
167-
RouterName: "router-wrong-reframe",
114+
RouterName: "router-wrong",
168115
},
169116
MethodNameProvide: {
170-
RouterName: "router-wrong-reframe",
117+
RouterName: "router-wrong",
171118
},
172119
MethodNamePutIPNS: {
173-
RouterName: "router-wrong-reframe",
120+
RouterName: "router-wrong",
174121
},
175122
}
176123

177124
require.NoError(methodsOK.Check())
178125

179126
methodsMissing := Methods{
180127
MethodNameFindPeers: {
181-
RouterName: "router-wrong-reframe",
128+
RouterName: "router-wrong",
182129
},
183130
MethodNameGetIPNS: {
184-
RouterName: "router-wrong-reframe",
131+
RouterName: "router-wrong",
185132
},
186133
MethodNameProvide: {
187-
RouterName: "router-wrong-reframe",
134+
RouterName: "router-wrong",
188135
},
189136
MethodNamePutIPNS: {
190-
RouterName: "router-wrong-reframe",
137+
RouterName: "router-wrong",
191138
},
192139
}
193140

core/commands/dht_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func TestKeyTranslation(t *testing.T) {
1313
pid := test.RandPeerIDFatal(t)
1414
pkname := namesys.PkKeyForID(pid)
15-
ipnsname := ipns.RecordKey(pid)
15+
ipnsname := ipns.NameFromPeer(pid).RoutingKey()
1616

1717
pkk, err := escapeDhtKey("/pk/" + pid.Pretty())
1818
if err != nil {
@@ -28,7 +28,7 @@ func TestKeyTranslation(t *testing.T) {
2828
t.Fatal("keys didn't match!")
2929
}
3030

31-
if ipnsk != ipnsname {
31+
if ipnsk != string(ipnsname) {
3232
t.Fatal("keys didn't match!")
3333
}
3434
}

core/commands/name/name.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"strings"
98
"text/tabwriter"
109
"time"
1110

12-
"github.com/gogo/protobuf/proto"
1311
"github.com/ipfs/boxo/ipns"
1412
ipns_pb "github.com/ipfs/boxo/ipns/pb"
1513
cmds "github.com/ipfs/go-ipfs-cmds"
1614
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
1715
"github.com/ipld/go-ipld-prime"
1816
"github.com/ipld/go-ipld-prime/codec/dagcbor"
1917
"github.com/ipld/go-ipld-prime/codec/dagjson"
20-
"github.com/libp2p/go-libp2p/core/peer"
18+
"github.com/libp2p/go-libp2p/core/crypto"
2119
mbase "github.com/multiformats/go-multibase"
20+
"google.golang.org/protobuf/proto"
2221
)
2322

2423
type IpnsEntry struct {
@@ -86,14 +85,14 @@ Resolve the value of a dnslink:
8685
type IpnsInspectValidation struct {
8786
Valid bool
8887
Reason string
89-
PublicKey peer.ID
88+
PublicKey string
9089
}
9190

9291
// IpnsInspectEntry contains the deserialized values from an IPNS Entry:
9392
// https://github.com/ipfs/specs/blob/main/ipns/IPNS.md#record-serialization-format
9493
type IpnsInspectEntry struct {
9594
Value string
96-
ValidityType *ipns_pb.IpnsEntry_ValidityType
95+
ValidityType *ipns_pb.IpnsRecord_ValidityType
9796
Validity *time.Time
9897
Sequence uint64
9998
TTL *uint64
@@ -151,8 +150,17 @@ Passing --verify will verify signature against provided public key.
151150
return err
152151
}
153152

154-
var entry ipns_pb.IpnsEntry
155-
err = proto.Unmarshal(b.Bytes(), &entry)
153+
// Here we use the old school raw Protobuf pbRecord because we want to inspect it,
154+
// aka, we need to be able to see all of its contents inside. While the boxo/ipns
155+
// provides a good abstraction over the Record type, it doesn't allow for introspection
156+
// of the raw value of the IpnsEntry.
157+
var pbRecord ipns_pb.IpnsRecord
158+
err = proto.Unmarshal(b.Bytes(), &pbRecord)
159+
if err != nil {
160+
return err
161+
}
162+
163+
rec, err := ipns.UnmarshalRecord(b.Bytes())
156164
if err != nil {
157165
return err
158166
}
@@ -164,24 +172,24 @@ Passing --verify will verify signature against provided public key.
164172

165173
result := &IpnsInspectResult{
166174
Entry: IpnsInspectEntry{
167-
Value: string(entry.Value),
168-
ValidityType: entry.ValidityType,
169-
Sequence: *entry.Sequence,
170-
TTL: entry.Ttl,
171-
PublicKey: encoder.Encode(entry.PubKey),
172-
SignatureV1: encoder.Encode(entry.SignatureV1),
173-
SignatureV2: encoder.Encode(entry.SignatureV2),
175+
Value: string(pbRecord.Value),
176+
ValidityType: pbRecord.ValidityType,
177+
Sequence: *pbRecord.Sequence,
178+
TTL: pbRecord.Ttl,
179+
PublicKey: encoder.Encode(pbRecord.PubKey),
180+
SignatureV1: encoder.Encode(pbRecord.SignatureV1),
181+
SignatureV2: encoder.Encode(pbRecord.SignatureV2),
174182
Data: nil,
175183
},
176184
}
177185

178-
if len(entry.Data) != 0 {
186+
if len(pbRecord.Data) != 0 {
179187
// This is hacky. The variable node (datamodel.Node) doesn't directly marshal
180188
// to JSON. Therefore, we need to first decode from DAG-CBOR, then encode in
181189
// DAG-JSON and finally unmarshal it from JSON. Since DAG-JSON is a subset
182190
// of JSON, that should work. Then, we can store the final value in the
183191
// result.Entry.Data for further inspection.
184-
node, err := ipld.Decode(entry.Data, dagcbor.Decode)
192+
node, err := ipld.Decode(pbRecord.Data, dagcbor.Decode)
185193
if err != nil {
186194
return err
187195
}
@@ -198,24 +206,33 @@ Passing --verify will verify signature against provided public key.
198206
}
199207
}
200208

201-
validity, err := ipns.GetEOL(&entry)
209+
validity, err := rec.Validity()
202210
if err == nil {
203211
result.Entry.Validity = &validity
204212
}
205213

206214
verify, ok := req.Options["verify"].(string)
207215
if ok {
208-
key := strings.TrimPrefix(verify, "/ipns/")
209-
id, err := peer.Decode(key)
216+
name, err := ipns.NameFromString(verify)
217+
if err != nil {
218+
return err
219+
}
220+
221+
pk, err := ipns.ExtractPublicKey(rec, name)
222+
if err != nil {
223+
return err
224+
}
225+
226+
bytes, err := crypto.MarshalPublicKey(pk)
210227
if err != nil {
211228
return err
212229
}
213230

214231
result.Validation = &IpnsInspectValidation{
215-
PublicKey: id,
232+
PublicKey: encoder.Encode(bytes),
216233
}
217234

218-
err = ipns.ValidateWithPeerID(id, &entry)
235+
err = ipns.Validate(rec, pk)
219236
if err == nil {
220237
result.Validation.Valid = true
221238
} else {

0 commit comments

Comments
 (0)