mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 10:49:24 +08:00
cmd/block: add selection of multihash parameters to block put command
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
This commit is contained in:
@ -117,6 +117,8 @@ It reads from stdin, and <key> is a base58 encoded multihash.
|
||||
},
|
||||
Options: []cmds.Option{
|
||||
cmds.StringOption("format", "f", "cid format for blocks to be created with.").Default("v0"),
|
||||
cmds.StringOption("mhtype", "multihash hash function").Default("sha2-256"),
|
||||
cmds.IntOption("mhlen", "multihash hash length").Default(-1),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
n, err := req.InvocContext().GetNode()
|
||||
@ -143,11 +145,10 @@ It reads from stdin, and <key> is a base58 encoded multihash.
|
||||
return
|
||||
}
|
||||
|
||||
format, _, _ := req.Option("format").String()
|
||||
var pref cid.Prefix
|
||||
pref.MhType = mh.SHA2_256
|
||||
pref.MhLength = -1
|
||||
pref.Version = 1
|
||||
|
||||
format, _, _ := req.Option("format").String()
|
||||
switch format {
|
||||
case "cbor":
|
||||
pref.Codec = cid.DagCBOR
|
||||
@ -163,6 +164,21 @@ It reads from stdin, and <key> is a base58 encoded multihash.
|
||||
return
|
||||
}
|
||||
|
||||
mhtype, _, _ := req.Option("mhtype").String()
|
||||
mhtval, ok := mh.Names[mhtype]
|
||||
if !ok {
|
||||
res.SetError(fmt.Errorf("unrecognized multihash function: %s", mhtype), cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
pref.MhType = mhtval
|
||||
|
||||
mhlen, _, err := req.Option("mhlen").Int()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
pref.MhLength = mhlen
|
||||
|
||||
bcid, err := pref.Sum(data)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
|
@ -184,6 +184,19 @@ test_expect_success "block get output looks right" '
|
||||
test_cmp pb_block_out ../t0051-object-data/testPut.pb
|
||||
'
|
||||
|
||||
test_expect_success "can set multihash type and length on block put" '
|
||||
HASH=$(echo "foooo" | ipfs block put --format=raw --mhtype=sha3 --mhlen=16)
|
||||
'
|
||||
|
||||
test_expect_success "output looks good" '
|
||||
test "z25ScPysKoxJBcPxczn9NvuHiZU5" = "$HASH"
|
||||
'
|
||||
|
||||
test_expect_success "can read block with different hash" '
|
||||
ipfs block get $HASH > blk_get_out &&
|
||||
echo "foooo" > blk_get_exp &&
|
||||
test_cmp blk_get_exp blk_get_out
|
||||
'
|
||||
#
|
||||
# Misc tests
|
||||
#
|
||||
|
Reference in New Issue
Block a user