From 0a5140aa6b551dedbb513cce89be6c8e9b4a0100 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 12 Jun 2023 10:44:47 +0200 Subject: [PATCH] refactor: upgrade to refactored IPNS package --- cmd/ipfs/init.go | 3 +- core/commands/dht_test.go | 2 +- core/commands/name/name.go | 39 ++++++++++++++++---------- core/commands/name/publish.go | 4 +++ core/core_test.go | 5 ++-- core/coreapi/name.go | 1 + docs/examples/kubo-as-a-library/go.mod | 2 +- docs/examples/kubo-as-a-library/go.sum | 4 +-- fuse/ipns/common.go | 3 +- fuse/ipns/ipns_unix.go | 2 +- go.mod | 2 +- go.sum | 4 +-- test/dependencies/go.mod | 2 +- test/dependencies/go.sum | 4 +-- 14 files changed, 47 insertions(+), 30 deletions(-) diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 28845faa5685..3c1cfefe33e1 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -19,6 +19,7 @@ import ( fsrepo "github.com/ipfs/kubo/repo/fsrepo" options "github.com/ipfs/boxo/coreiface/options" + nsopts "github.com/ipfs/boxo/coreiface/options/namesys" "github.com/ipfs/boxo/files" cmds "github.com/ipfs/go-ipfs-cmds" config "github.com/ipfs/kubo/config" @@ -262,5 +263,5 @@ func initializeIpnsKeyspace(repoRoot string) error { return err } - return nd.Namesys.Publish(ctx, nd.PrivateKey, path.FromCid(emptyDir.Cid())) + return nd.Namesys.Publish(ctx, nd.PrivateKey, path.FromCid(emptyDir.Cid()), nsopts.PublishCompatibleWithV1(true)) } diff --git a/core/commands/dht_test.go b/core/commands/dht_test.go index 961d906ab786..76b2493b54a3 100644 --- a/core/commands/dht_test.go +++ b/core/commands/dht_test.go @@ -12,7 +12,7 @@ import ( func TestKeyTranslation(t *testing.T) { pid := test.RandPeerIDFatal(t) pkname := namesys.PkKeyForID(pid) - ipnsname := ipns.RecordKey(pid) + ipnsname := ipns.RoutingKey(pid) pkk, err := escapeDhtKey("/pk/" + pid.Pretty()) if err != nil { diff --git a/core/commands/name/name.go b/core/commands/name/name.go index 94f05290cfce..dba5a3785d53 100644 --- a/core/commands/name/name.go +++ b/core/commands/name/name.go @@ -151,8 +151,17 @@ Passing --verify will verify signature against provided public key. return err } - var entry ipns_pb.IpnsEntry - err = proto.Unmarshal(b.Bytes(), &entry) + // Here we use the old school raw Protobuf pbRecord because we want to inspect it, + // aka, we need to be able to see all of its contents inside. While the boxo/ipns + // provides a good abstraction over the Record type, it doesn't allow for introspection + // of the raw value of the IpnsEntry. + var pbRecord ipns_pb.IpnsEntry + err = proto.Unmarshal(b.Bytes(), &pbRecord) + if err != nil { + return err + } + + rec, err := ipns.UnmarshalRecord(b.Bytes()) if err != nil { return err } @@ -164,24 +173,24 @@ Passing --verify will verify signature against provided public key. result := &IpnsInspectResult{ Entry: IpnsInspectEntry{ - Value: string(entry.Value), - ValidityType: entry.ValidityType, - Sequence: *entry.Sequence, - TTL: entry.Ttl, - PublicKey: encoder.Encode(entry.PubKey), - SignatureV1: encoder.Encode(entry.SignatureV1), - SignatureV2: encoder.Encode(entry.SignatureV2), + Value: string(pbRecord.Value), + ValidityType: pbRecord.ValidityType, + Sequence: *pbRecord.Sequence, + TTL: pbRecord.Ttl, + PublicKey: encoder.Encode(pbRecord.PubKey), + SignatureV1: encoder.Encode(pbRecord.SignatureV1), + SignatureV2: encoder.Encode(pbRecord.SignatureV2), Data: nil, }, } - if len(entry.Data) != 0 { + if len(pbRecord.Data) != 0 { // This is hacky. The variable node (datamodel.Node) doesn't directly marshal // to JSON. Therefore, we need to first decode from DAG-CBOR, then encode in // DAG-JSON and finally unmarshal it from JSON. Since DAG-JSON is a subset // of JSON, that should work. Then, we can store the final value in the // result.Entry.Data for further inspection. - node, err := ipld.Decode(entry.Data, dagcbor.Decode) + node, err := ipld.Decode(pbRecord.Data, dagcbor.Decode) if err != nil { return err } @@ -198,7 +207,7 @@ Passing --verify will verify signature against provided public key. } } - validity, err := ipns.GetEOL(&entry) + validity, err := rec.Validity() if err == nil { result.Entry.Validity = &validity } @@ -206,16 +215,16 @@ Passing --verify will verify signature against provided public key. verify, ok := req.Options["verify"].(string) if ok { key := strings.TrimPrefix(verify, "/ipns/") - id, err := peer.Decode(key) + pid, err := peer.Decode(key) if err != nil { return err } result.Validation = &IpnsInspectValidation{ - PublicKey: id, + PublicKey: pid, } - err = ipns.ValidateWithPeerID(id, &entry) + err = ipns.ValidateWithPeerID(rec, pid) if err == nil { result.Validation.Valid = true } else { diff --git a/core/commands/name/publish.go b/core/commands/name/publish.go index b104aaeb5944..ac16b91697bd 100644 --- a/core/commands/name/publish.go +++ b/core/commands/name/publish.go @@ -28,6 +28,7 @@ const ( ttlOptionName = "ttl" keyOptionName = "key" quieterOptionName = "quieter" + compatibleWithV1 = "compatible-v1" ) var PublishCmd = &cmds.Command{ @@ -83,6 +84,7 @@ Alternatively, publish an using a valid PeerID (as listed by cmds.StringOption(ttlOptionName, "Time duration this record should be cached for. Uses the same syntax as the lifetime option. (caution: experimental)"), cmds.StringOption(keyOptionName, "k", "Name of the key to be used or a valid PeerID, as listed by 'ipfs key list -l'.").WithDefault("self"), cmds.BoolOption(quieterOptionName, "Q", "Write only final hash."), + cmds.BoolOption(compatibleWithV1, "Create a V1-compatible IPNS Record.").WithDefault(true), ke.OptionIPNSBase, }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { @@ -96,6 +98,7 @@ Alternatively, publish an using a valid PeerID (as listed by } allowOffline, _ := req.Options[allowOfflineOptionName].(bool) + compatibleWithV1, _ := req.Options[compatibleWithV1].(bool) kname, _ := req.Options[keyOptionName].(string) validTimeOpt, _ := req.Options[lifeTimeOptionName].(string) @@ -108,6 +111,7 @@ Alternatively, publish an using a valid PeerID (as listed by options.Name.AllowOffline(allowOffline), options.Name.Key(kname), options.Name.ValidTime(validTime), + options.Name.CompatibleWithV1(compatibleWithV1), } if ttl, found := req.Options[ttlOptionName].(string); found { diff --git a/core/core_test.go b/core/core_test.go index 2d7e8927cf35..9653cd7bd764 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -12,6 +12,7 @@ import ( context "context" "github.com/ipfs/boxo/ipns" + ipfspath "github.com/ipfs/boxo/path" "github.com/ipfs/go-cid" "github.com/ipfs/go-delegated-routing/client" "github.com/ipfs/kubo/core/node/libp2p" @@ -300,11 +301,11 @@ func (drs *delegatedRoutingService) GetIPNS(ctx context.Context, id []byte) (<-c var out client.GetIPNSAsyncResult switch peer.ID(id) { case drs.goodPeerID: - ie, err := ipns.Create(drs.pk1, []byte(fmt.Sprintf("RECORD FROM SERVICE %d", drs.serviceID)), 0, time.Now().Add(10*time.Hour), 100*time.Hour) + rec, err := ipns.NewRecord(drs.pk1, ipfspath.Path(fmt.Sprintf("RECORD FROM SERVICE %d", drs.serviceID)), 0, time.Now().Add(10*time.Hour), 100*time.Hour, ipns.CompatibleWithV1(true)) if err != nil { log.Fatal(err) } - ieb, err := ie.Marshal() + ieb, err := ipns.MarshalRecord(rec) if err != nil { log.Fatal(err) } diff --git a/core/coreapi/name.go b/core/coreapi/name.go index f07cfcd0c7a8..87999c4569a5 100644 --- a/core/coreapi/name.go +++ b/core/coreapi/name.go @@ -79,6 +79,7 @@ func (api *NameAPI) Publish(ctx context.Context, p path.Path, opts ...caopts.Nam publishOptions := []nsopts.PublishOption{ nsopts.PublishWithEOL(eol), + nsopts.PublishCompatibleWithV1(options.CompatibleWithV1), } if options.TTL != nil { diff --git a/docs/examples/kubo-as-a-library/go.mod b/docs/examples/kubo-as-a-library/go.mod index 8066f3286561..c103cab71d9d 100644 --- a/docs/examples/kubo-as-a-library/go.mod +++ b/docs/examples/kubo-as-a-library/go.mod @@ -7,7 +7,7 @@ go 1.18 replace github.com/ipfs/kubo => ./../../.. require ( - github.com/ipfs/boxo v0.10.0 + github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d github.com/ipfs/kubo v0.0.0-00010101000000-000000000000 github.com/libp2p/go-libp2p v0.27.5 github.com/multiformats/go-multiaddr v0.9.0 diff --git a/docs/examples/kubo-as-a-library/go.sum b/docs/examples/kubo-as-a-library/go.sum index c3b246b39600..1700076efbcb 100644 --- a/docs/examples/kubo-as-a-library/go.sum +++ b/docs/examples/kubo-as-a-library/go.sum @@ -319,8 +319,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= -github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY= -github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM= +github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d h1:59AAX3a0HibI9LLo3jIRvSVWzkLoWXptGgGB8ZwWgCc= +github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d/go.mod h1:vRJqn2gSu+LkTTVGzxRJxggmiHXnWl0Ws5/YAwQbJLM= github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA= github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU= github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY= diff --git a/fuse/ipns/common.go b/fuse/ipns/common.go index db231fe4570d..261928205020 100644 --- a/fuse/ipns/common.go +++ b/fuse/ipns/common.go @@ -3,6 +3,7 @@ package ipns import ( "context" + nsopts "github.com/ipfs/boxo/coreiface/options/namesys" ft "github.com/ipfs/boxo/ipld/unixfs" nsys "github.com/ipfs/boxo/namesys" path "github.com/ipfs/boxo/path" @@ -30,5 +31,5 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error { pub := nsys.NewIpnsPublisher(n.Routing, n.Repo.Datastore()) - return pub.Publish(ctx, key, path.FromCid(emptyDir.Cid())) + return pub.Publish(ctx, key, path.FromCid(emptyDir.Cid()), nsopts.PublishCompatibleWithV1(true)) } diff --git a/fuse/ipns/ipns_unix.go b/fuse/ipns/ipns_unix.go index 9db0d61f951d..6f157f35d194 100644 --- a/fuse/ipns/ipns_unix.go +++ b/fuse/ipns/ipns_unix.go @@ -85,7 +85,7 @@ type Root struct { func ipnsPubFunc(ipfs iface.CoreAPI, key iface.Key) mfs.PubFunc { return func(ctx context.Context, c cid.Cid) error { - _, err := ipfs.Name().Publish(ctx, path.IpfsPath(c), options.Name.Key(key.Name())) + _, err := ipfs.Name().Publish(ctx, path.IpfsPath(c), options.Name.Key(key.Name()), options.Name.CompatibleWithV1(true)) return err } } diff --git a/go.mod b/go.mod index 53f517571574..ec99e6c1eb39 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/gogo/protobuf v1.3.2 github.com/google/uuid v1.3.0 github.com/hashicorp/go-multierror v1.1.1 - github.com/ipfs/boxo v0.10.0 + github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d github.com/ipfs/go-block-format v0.1.2 github.com/ipfs/go-cid v0.4.1 github.com/ipfs/go-cidutil v0.1.0 diff --git a/go.sum b/go.sum index ea70323c8d64..0a002e68c53c 100644 --- a/go.sum +++ b/go.sum @@ -354,8 +354,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= -github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY= -github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM= +github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d h1:59AAX3a0HibI9LLo3jIRvSVWzkLoWXptGgGB8ZwWgCc= +github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d/go.mod h1:vRJqn2gSu+LkTTVGzxRJxggmiHXnWl0Ws5/YAwQbJLM= github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA= github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU= github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY= diff --git a/test/dependencies/go.mod b/test/dependencies/go.mod index 579fd5a30c20..208e50810d33 100644 --- a/test/dependencies/go.mod +++ b/test/dependencies/go.mod @@ -7,7 +7,7 @@ replace github.com/ipfs/kubo => ../../ require ( github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd github.com/golangci/golangci-lint v1.49.0 - github.com/ipfs/boxo v0.10.0 + github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d github.com/ipfs/go-cid v0.4.1 github.com/ipfs/go-cidutil v0.1.0 github.com/ipfs/go-datastore v0.6.0 diff --git a/test/dependencies/go.sum b/test/dependencies/go.sum index bb8d2a267f1a..6e237473d6b9 100644 --- a/test/dependencies/go.sum +++ b/test/dependencies/go.sum @@ -412,8 +412,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs= github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0= -github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY= -github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM= +github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d h1:59AAX3a0HibI9LLo3jIRvSVWzkLoWXptGgGB8ZwWgCc= +github.com/ipfs/boxo v0.10.1-0.20230612090609-11f817fc465d/go.mod h1:vRJqn2gSu+LkTTVGzxRJxggmiHXnWl0Ws5/YAwQbJLM= github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA= github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU= github.com/ipfs/go-block-format v0.1.2 h1:GAjkfhVx1f4YTODS6Esrj1wt2HhrtwTnhEr+DyPUaJo=