Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 3c4a6d0

Browse files
committed
feat: switch to raw multihashes for blocks
Part of: ipfs/kubo#6815
1 parent 28ab304 commit 3c4a6d0

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ require (
44
github.com/ipfs/go-cid v0.0.3
55
github.com/ipfs/go-datastore v0.1.1
66
github.com/multiformats/go-base32 v0.0.3
7+
github.com/multiformats/go-multihash v0.0.1
78
)
9+
10+
go 1.13

go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@ github.com/gxed/hashland/keccakpg v0.0.1 h1:wrk3uMNaMxbXiHibbPO4S0ymqJMm41WiudyF
66
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
77
github.com/gxed/hashland/murmur3 v0.0.1 h1:SheiaIt0sda5K+8FLz952/1iWS9zrnKsEJaOJu4ZbSc=
88
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
9-
github.com/ipfs/go-cid v0.0.1 h1:GBjWPktLnNyX0JiQCNFpUuUSoMw5KMyqrsejHYlILBE=
10-
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
119
github.com/ipfs/go-cid v0.0.3 h1:UIAh32wymBpStoe83YCzwVQQ5Oy/H0FdxvUS6DJDzms=
1210
github.com/ipfs/go-cid v0.0.3/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
13-
github.com/ipfs/go-datastore v0.0.1 h1:AW/KZCScnBWlSb5JbnEnLKFWXL224LBEh/9KXXOrUms=
14-
github.com/ipfs/go-datastore v0.0.1/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE=
15-
github.com/ipfs/go-datastore v0.1.0 h1:TOxI04l8CmO4zGtesENhzm4PwkFwJXY3rKiYaaMf9fI=
16-
github.com/ipfs/go-datastore v0.1.0/go.mod h1:d4KVXhMt913cLBEI/PXAy6ko+W7e9AhyAKBGh803qeE=
1711
github.com/ipfs/go-datastore v0.1.1 h1:F4k0TkTAZGLFzBOrVKDAvch6JZtuN4NHkfdcEZL50aI=
1812
github.com/ipfs/go-datastore v0.1.1/go.mod h1:w38XXW9kVFNp57Zj5knbKWM2T+KOZCGDRVNdgPHtbHw=
1913
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=

key.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
cid "github.com/ipfs/go-cid"
77
"github.com/ipfs/go-datastore"
88
"github.com/multiformats/go-base32"
9+
mh "github.com/multiformats/go-multihash"
910
)
1011

1112
// NewKeyFromBinary creates a new key from a byte slice.
@@ -21,16 +22,30 @@ func BinaryFromDsKey(k datastore.Key) ([]byte, error) {
2122
return base32.RawStdEncoding.DecodeString(k.String()[1:])
2223
}
2324

25+
// MultihashToDsKey creates a Key from the given Multihash.
26+
func MultihashToDsKey(k mh.Multihash) datastore.Key {
27+
return NewKeyFromBinary(k)
28+
}
29+
30+
// DsKeyToMultihash converts a dsKey to the corresponding Multihash.
31+
func DsKeyToMultihash(dsKey datastore.Key) (mh.Multihash, error) {
32+
kb, err := BinaryFromDsKey(dsKey)
33+
if err != nil {
34+
return nil, err
35+
}
36+
return mh.Cast(kb)
37+
}
38+
2439
// CidToDsKey creates a Key from the given Cid.
2540
func CidToDsKey(k cid.Cid) datastore.Key {
26-
return NewKeyFromBinary(k.Bytes())
41+
return MultihashToDsKey(k.Hash())
2742
}
2843

2944
// DsKeyToCid converts the given Key to its corresponding Cid.
3045
func DsKeyToCid(dsKey datastore.Key) (cid.Cid, error) {
31-
kb, err := BinaryFromDsKey(dsKey)
46+
hash, err := DsKeyToMultihash(dsKey)
3247
if err != nil {
33-
return cid.Cid{}, err
48+
return cid.Cid{}, nil
3449
}
35-
return cid.Cast(kb)
50+
return cid.NewCidV1(cid.Raw, hash), nil
3651
}

0 commit comments

Comments
 (0)