Skip to content

Commit 9c43c2c

Browse files
committed
commands/block: use CIDv1 with custom mhtype
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent cfdcd98 commit 9c43c2c

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

core/commands/block.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
blocks "gx/ipfs/QmYsEQydGrsxNZfAiskvQ76N2xE9hDQtSAkRSynwMiUK3c/go-block-format"
1717
"gx/ipfs/QmceUdzxkimdYsgtX733uNgzf1DLHyBKN6ehGSp85ayppM/go-ipfs-cmdkit"
1818
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
19+
20+
"github.com/pkg/errors"
1921
)
2022

2123
type BlockStat struct {
@@ -128,7 +130,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.
128130
cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
129131
},
130132
Options: []cmdkit.Option{
131-
cmdkit.StringOption("format", "f", "cid format for blocks to be created with.").WithDefault("v0"),
133+
cmdkit.StringOption("format", "f", "cid format for blocks to be created with."),
132134
cmdkit.StringOption("mhtype", "multihash hash function").WithDefault("sha2-256"),
133135
cmdkit.IntOption("mhlen", "multihash hash length").WithDefault(-1),
134136
},
@@ -157,27 +159,32 @@ It reads from stdin, and <key> is a base58 encoded multihash.
157159
return
158160
}
159161

162+
mhtype, _ := req.Options["mhtype"].(string)
163+
mhtval, ok := mh.Names[mhtype]
164+
if !ok {
165+
err := fmt.Errorf("unrecognized multihash function: %s", mhtype)
166+
res.SetError(err, cmdkit.ErrNormal)
167+
return
168+
}
169+
160170
var pref cid.Prefix
161171
pref.Version = 1
162172

163-
format, _ := req.Options["format"].(string)
173+
format := req.Options["format"].(string)
164174
formatval, ok := cid.Codecs[format]
165175
if !ok {
166176
res.SetError(fmt.Errorf("unrecognized format: %s", format), cmdkit.ErrNormal)
167177
return
168178
}
169-
if format == "v0" {
179+
if format == "v0" || (format == "" && mhtval == mh.SHA2_256) {
170180
pref.Version = 0
171181
}
172-
pref.Codec = formatval
173-
174-
mhtype, _ := req.Options["mhtype"].(string)
175-
mhtval, ok := mh.Names[mhtype]
176-
if !ok {
177-
err := fmt.Errorf("unrecognized multihash function: %s", mhtype)
178-
res.SetError(err, cmdkit.ErrNormal)
182+
if mhtval != mh.SHA2_256 && pref.Version == 0 {
183+
res.SetError(errors.New("cannot generate CIDv0 with non-sha256 hash function"), cmdkit.ErrNormal)
179184
return
180185
}
186+
187+
pref.Codec = formatval
181188
pref.MhType = mhtval
182189

183190
mhlen, ok := req.Options["mhlen"].(int)

test/sharness/t0050-block.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,16 @@ test_expect_success "no panic in output" '
209209
test_expect_code 1 grep "panic" stat_out
210210
'
211211

212+
test_expect_success "can set multihash type and length on block put without format" '
213+
HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=16)
214+
'
215+
216+
test_expect_success "output looks good" '
217+
test "z2APJNN6rqZTWPpv7gYFHzh7ZEDX" = "$HASH"
218+
'
219+
220+
test_expect_success "put with sha3 and cidv0 fails" '
221+
echo "foooo" | test_must_fail ipfs block put --mhtype=sha3 --mhlen=16 --format=v0
222+
'
223+
212224
test_done

0 commit comments

Comments
 (0)