Skip to content

Commit

Permalink
refactor: upgrade to refactored IPNS package
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jun 12, 2023
1 parent ad9e208 commit 68fdeba
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 30 deletions.
3 changes: 2 additions & 1 deletion cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion core/commands/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestKeyTranslation(t *testing.T) {
pid := test.RandPeerIDFatal(t)
pkname := namesys.PkKeyForID(pid)
ipnsname := ipns.RecordKey(pid)
ipnsname := ipns.RoutingRecordKey(pid)

pkk, err := escapeDhtKey("/pk/" + pid.Pretty())
if err != nil {
Expand Down
39 changes: 24 additions & 15 deletions core/commands/name/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -198,24 +207,24 @@ 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
}

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 {
Expand Down
4 changes: 4 additions & 0 deletions core/commands/name/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
ttlOptionName = "ttl"
keyOptionName = "key"
quieterOptionName = "quieter"
compatibleWithV1 = "compatible-v1"
)

var PublishCmd = &cmds.Command{
Expand Down Expand Up @@ -83,6 +84,7 @@ Alternatively, publish an <ipfs-path> 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 {
Expand All @@ -96,6 +98,7 @@ Alternatively, publish an <ipfs-path> 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)
Expand All @@ -108,6 +111,7 @@ Alternatively, publish an <ipfs-path> 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 {
Expand Down
5 changes: 3 additions & 2 deletions core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kubo-as-a-library/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230612085926-cfaa3b85f919
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
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/kubo-as-a-library/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230612085926-cfaa3b85f919 h1:gXy+LZIvuH85lhwUMwWub4cpjFA7bGmb93ue8kqLRuY=
github.com/ipfs/boxo v0.10.1-0.20230612085926-cfaa3b85f919/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=
Expand Down
3 changes: 2 additions & 1 deletion fuse/ipns/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion fuse/ipns/ipns_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230612085926-cfaa3b85f919
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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230612085926-cfaa3b85f919 h1:gXy+LZIvuH85lhwUMwWub4cpjFA7bGmb93ue8kqLRuY=
github.com/ipfs/boxo v0.10.1-0.20230612085926-cfaa3b85f919/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=
Expand Down
2 changes: 1 addition & 1 deletion test/dependencies/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230612085926-cfaa3b85f919
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-cidutil v0.1.0
github.com/ipfs/go-datastore v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions test/dependencies/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.20230612085926-cfaa3b85f919 h1:gXy+LZIvuH85lhwUMwWub4cpjFA7bGmb93ue8kqLRuY=
github.com/ipfs/boxo v0.10.1-0.20230612085926-cfaa3b85f919/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=
Expand Down

0 comments on commit 68fdeba

Please sign in to comment.