Skip to content

Commit 22dd4b9

Browse files
committed
refactor: migrate 'ipfs name' sharness tests
1 parent 2e721ad commit 22dd4b9

File tree

11 files changed

+354
-446
lines changed

11 files changed

+354
-446
lines changed

core/commands/name/name.go

+73-90
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ package name
22

33
import (
44
"bytes"
5-
"encoding/json"
5+
"encoding/hex"
66
"fmt"
77
"io"
88
"text/tabwriter"
99
"time"
1010

1111
"github.com/ipfs/boxo/ipns"
1212
ipns_pb "github.com/ipfs/boxo/ipns/pb"
13+
"github.com/ipfs/boxo/path"
1314
cmds "github.com/ipfs/go-ipfs-cmds"
1415
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
15-
"github.com/ipld/go-ipld-prime"
16-
"github.com/ipld/go-ipld-prime/codec/dagcbor"
17-
"github.com/ipld/go-ipld-prime/codec/dagjson"
18-
"github.com/libp2p/go-libp2p/core/crypto"
19-
mbase "github.com/multiformats/go-multibase"
2016
"google.golang.org/protobuf/proto"
2117
)
2218

@@ -83,27 +79,25 @@ Resolve the value of a dnslink:
8379
}
8480

8581
type IpnsInspectValidation struct {
86-
Valid bool
87-
Reason string
88-
PublicKey string
82+
Valid bool
83+
Reason string
84+
Name string
8985
}
9086

9187
// IpnsInspectEntry contains the deserialized values from an IPNS Entry:
9288
// https://github.com/ipfs/specs/blob/main/ipns/IPNS.md#record-serialization-format
9389
type IpnsInspectEntry struct {
94-
Value string
95-
ValidityType *ipns_pb.IpnsRecord_ValidityType
90+
Value *path.Path
91+
ValidityType *ipns.ValidityType
9692
Validity *time.Time
97-
Sequence uint64
98-
TTL *uint64
99-
PublicKey string
100-
SignatureV1 string
101-
SignatureV2 string
102-
Data interface{}
93+
Sequence *uint64
94+
TTL *time.Duration
10395
}
10496

10597
type IpnsInspectResult struct {
10698
Entry IpnsInspectEntry
99+
Version string
100+
HexDump string
107101
Validation *IpnsInspectValidation
108102
}
109103

@@ -135,6 +129,7 @@ Passing --verify will verify signature against provided public key.
135129
},
136130
Options: []cmds.Option{
137131
cmds.StringOption("verify", "CID of the public IPNS key to validate against."),
132+
cmds.BoolOption("verbose", "Show a full hex dump of the raw Protobuf record."),
138133
},
139134
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
140135
file, err := cmdenv.GetFileArg(req.Files.Entries())
@@ -150,96 +145,76 @@ Passing --verify will verify signature against provided public key.
150145
return err
151146
}
152147

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-
163148
rec, err := ipns.UnmarshalRecord(b.Bytes())
164149
if err != nil {
165150
return err
166151
}
167152

168-
encoder, err := mbase.EncoderByName("base64")
169-
if err != nil {
170-
return err
171-
}
172-
173153
result := &IpnsInspectResult{
174-
Entry: IpnsInspectEntry{
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),
182-
Data: nil,
183-
},
154+
Entry: IpnsInspectEntry{},
184155
}
185156

186-
if len(pbRecord.Data) != 0 {
187-
// This is hacky. The variable node (datamodel.Node) doesn't directly marshal
188-
// to JSON. Therefore, we need to first decode from DAG-CBOR, then encode in
189-
// DAG-JSON and finally unmarshal it from JSON. Since DAG-JSON is a subset
190-
// of JSON, that should work. Then, we can store the final value in the
191-
// result.Entry.Data for further inspection.
192-
node, err := ipld.Decode(pbRecord.Data, dagcbor.Decode)
193-
if err != nil {
194-
return err
195-
}
157+
// Best effort to get the fields. Show everything we can.
158+
if v, err := rec.Value(); err == nil {
159+
result.Entry.Value = &v
160+
}
196161

197-
var buf bytes.Buffer
198-
err = dagjson.Encode(node, &buf)
199-
if err != nil {
200-
return err
201-
}
162+
if v, err := rec.ValidityType(); err == nil {
163+
result.Entry.ValidityType = &v
164+
}
202165

203-
err = json.Unmarshal(buf.Bytes(), &result.Entry.Data)
204-
if err != nil {
205-
return err
206-
}
166+
if v, err := rec.Validity(); err == nil {
167+
result.Entry.Validity = &v
207168
}
208169

209-
validity, err := rec.Validity()
210-
if err == nil {
211-
result.Entry.Validity = &validity
170+
if v, err := rec.Sequence(); err == nil {
171+
result.Entry.Sequence = &v
212172
}
213173

214-
verify, ok := req.Options["verify"].(string)
215-
if ok {
216-
name, err := ipns.NameFromString(verify)
217-
if err != nil {
218-
return err
219-
}
174+
if v, err := rec.TTL(); err == nil {
175+
result.Entry.TTL = &v
176+
}
220177

221-
pk, err := ipns.ExtractPublicKey(rec, name)
222-
if err != nil {
223-
return err
178+
// Here we need the raw protobuf just to decide the version.
179+
var pbRecord ipns_pb.IpnsRecord
180+
err = proto.Unmarshal(b.Bytes(), &pbRecord)
181+
if err != nil {
182+
return err
183+
}
184+
if pbRecord.SignatureV1 != nil || pbRecord.Value != nil {
185+
if pbRecord.Data != nil {
186+
result.Version = "V1+V2"
187+
} else {
188+
result.Version = "V2"
224189
}
190+
} else if pbRecord.Data != nil {
191+
result.Version = "V2"
192+
} else {
193+
result.Version = "Unknown"
194+
}
225195

226-
bytes, err := crypto.MarshalPublicKey(pk)
196+
if verify, ok := req.Options["verify"].(string); ok {
197+
name, err := ipns.NameFromString(verify)
227198
if err != nil {
228199
return err
229200
}
230201

231202
result.Validation = &IpnsInspectValidation{
232-
PublicKey: encoder.Encode(bytes),
203+
Name: name.String(),
233204
}
234205

235-
err = ipns.Validate(rec, pk)
206+
err = ipns.ValidateWithName(rec, name)
236207
if err == nil {
237208
result.Validation.Valid = true
238209
} else {
239210
result.Validation.Reason = err.Error()
240211
}
241212
}
242213

214+
if verbose, ok := req.Options["verbose"].(bool); ok && verbose {
215+
result.HexDump = hex.Dump(b.Bytes())
216+
}
217+
243218
return cmds.EmitOnce(res, result)
244219
},
245220
Type: IpnsInspectResult{},
@@ -248,24 +223,25 @@ Passing --verify will verify signature against provided public key.
248223
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
249224
defer tw.Flush()
250225

251-
fmt.Fprintf(tw, "Value:\t%q\n", string(out.Entry.Value))
252-
fmt.Fprintf(tw, "Validity Type:\t%q\n", out.Entry.ValidityType)
226+
if out.Entry.Value != nil {
227+
fmt.Fprintf(tw, "Value:\t%q\n", out.Entry.Value.String())
228+
}
229+
230+
if out.Entry.ValidityType != nil {
231+
fmt.Fprintf(tw, "Validity Type:\t%q\n", *out.Entry.ValidityType)
232+
}
233+
253234
if out.Entry.Validity != nil {
254-
fmt.Fprintf(tw, "Validity:\t%s\n", out.Entry.Validity.Format(time.RFC3339Nano))
235+
fmt.Fprintf(tw, "Validity:\t%q\n", out.Entry.Validity.Format(time.RFC3339))
255236
}
256-
fmt.Fprintf(tw, "Sequence:\t%d\n", out.Entry.Sequence)
257-
if out.Entry.TTL != nil {
258-
fmt.Fprintf(tw, "TTL:\t%d\n", *out.Entry.TTL)
237+
238+
if out.Entry.Sequence != nil {
239+
fmt.Fprintf(tw, "Sequence:\t%d\n", *out.Entry.Sequence)
259240
}
260-
fmt.Fprintf(tw, "PublicKey:\t%q\n", out.Entry.PublicKey)
261-
fmt.Fprintf(tw, "Signature V1:\t%q\n", out.Entry.SignatureV1)
262-
fmt.Fprintf(tw, "Signature V2:\t%q\n", out.Entry.SignatureV2)
263241

264-
data, err := json.Marshal(out.Entry.Data)
265-
if err != nil {
266-
return err
242+
if out.Entry.TTL != nil {
243+
fmt.Fprintf(tw, "TTL:\t%s\n", out.Entry.TTL.String())
267244
}
268-
fmt.Fprintf(tw, "Data:\t%s\n", string(data))
269245

270246
if out.Validation == nil {
271247
tw.Flush()
@@ -278,7 +254,14 @@ Passing --verify will verify signature against provided public key.
278254
if out.Validation.Reason != "" {
279255
fmt.Fprintf(tw, "\tReason:\t%s\n", out.Validation.Reason)
280256
}
281-
fmt.Fprintf(tw, "\tPublicKey:\t%s\n", out.Validation.PublicKey)
257+
fmt.Fprintf(tw, "\tName:\t%s\n", out.Validation.Name)
258+
}
259+
260+
if out.HexDump != "" {
261+
tw.Flush()
262+
263+
fmt.Fprintf(w, "\nHex Dump:\n")
264+
fmt.Fprintf(w, out.HexDump)
282265
}
283266

284267
return nil

docs/changelogs/v0.22.md

+9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ Imported 1 blocks (1618 bytes)
3535
[exit code 1]
3636
```
3737

38+
#### `ipfs name publish` now supports V2 only IPNS records
39+
40+
When publishing an IPNS record, you are now able to create v2 only records
41+
by passing `--v1compat=false`. By default, we still create V1+V2 records, such
42+
that there is the highest chance of backwards compatibility. The goal is to move
43+
to V2 only in the future.
44+
45+
**TODO**: add links to IPIP.
46+
3847
### 📝 Changelog
3948

4049
### 👨‍👩‍👧‍👦 Contributors

docs/examples/kubo-as-a-library/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go 1.18
77
replace github.com/ipfs/kubo => ./../../..
88

99
require (
10-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
10+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
1111
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
1212
github.com/libp2p/go-libp2p v0.27.6
1313
github.com/multiformats/go-multiaddr v0.9.0

docs/examples/kubo-as-a-library/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
320320
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
321321
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
322322
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
323-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
324-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
323+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
324+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
325325
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
326326
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
327327
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/fsnotify/fsnotify v1.6.0
1616
github.com/google/uuid v1.3.0
1717
github.com/hashicorp/go-multierror v1.1.1
18-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd
18+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d
1919
github.com/ipfs/go-block-format v0.1.2
2020
github.com/ipfs/go-cid v0.4.1
2121
github.com/ipfs/go-cidutil v0.1.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
355355
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
356356
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
357357
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
358-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd h1:mR5STWmJw1n+JZ2t7Jx0vPxp+iL9vuAM4RFxoqFyww0=
359-
github.com/ipfs/boxo v0.10.1-0.20230615083040-d5e141ea82cd/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
358+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d h1:vgxYdy3FL2y/oBFpoioPImg3QccT7Y03fyCxfIhm3HY=
359+
github.com/ipfs/boxo v0.10.1-0.20230615091437-c5a71fad393d/go.mod h1:IwBbXi5P7fA0HzLhsw/FtAj9RAMacODuOCPPsBcvqcE=
360360
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
361361
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
362362
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=

test/cli/fixtures/TestName.car

421 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)