Unverified Commit a9dfe410 authored by Whyrusleeping's avatar Whyrusleeping Committed by GitHub

Merge pull request #4563 from ipfs/fix/block-mhtype

commands/block: use CIDv1 with custom mhtype
parents 5c6f8578 efb74199
...@@ -3,6 +3,7 @@ package commands ...@@ -3,6 +3,7 @@ package commands
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
...@@ -121,6 +122,9 @@ var blockPutCmd = &cmds.Command{ ...@@ -121,6 +122,9 @@ var blockPutCmd = &cmds.Command{
ShortDescription: ` ShortDescription: `
'ipfs block put' is a plumbing command for storing raw IPFS blocks. 'ipfs block put' is a plumbing command for storing raw IPFS blocks.
It reads from stdin, and <key> is a base58 encoded multihash. It reads from stdin, and <key> is a base58 encoded multihash.
By default CIDv0 is going to be generated. Setting 'mhtype' to anything other
than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
`, `,
}, },
...@@ -128,7 +132,7 @@ It reads from stdin, and <key> is a base58 encoded multihash. ...@@ -128,7 +132,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.
cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(), cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.StringOption("format", "f", "cid format for blocks to be created with.").WithDefault("v0"), cmdkit.StringOption("format", "f", "cid format for blocks to be created with."),
cmdkit.StringOption("mhtype", "multihash hash function").WithDefault("sha2-256"), cmdkit.StringOption("mhtype", "multihash hash function").WithDefault("sha2-256"),
cmdkit.IntOption("mhlen", "multihash hash length").WithDefault(-1), cmdkit.IntOption("mhlen", "multihash hash length").WithDefault(-1),
}, },
...@@ -157,27 +161,40 @@ It reads from stdin, and <key> is a base58 encoded multihash. ...@@ -157,27 +161,40 @@ It reads from stdin, and <key> is a base58 encoded multihash.
return return
} }
mhtype, _ := req.Options["mhtype"].(string)
mhtval, ok := mh.Names[mhtype]
if !ok {
err := fmt.Errorf("unrecognized multihash function: %s", mhtype)
res.SetError(err, cmdkit.ErrNormal)
return
}
var pref cid.Prefix var pref cid.Prefix
pref.Version = 1 pref.Version = 1
format, _ := req.Options["format"].(string) format, formatSet := req.Options["format"].(string)
formatval, ok := cid.Codecs[format] if !formatSet {
if !ok { if mhtval == mh.SHA2_256 {
res.SetError(fmt.Errorf("unrecognized format: %s", format), cmdkit.ErrNormal) format = "v0"
return } else {
format = "protobuf"
}
} }
if format == "v0" { if format == "v0" {
pref.Version = 0 pref.Version = 0
} }
pref.Codec = formatval formatval, ok := cid.Codecs[format]
mhtype, _ := req.Options["mhtype"].(string)
mhtval, ok := mh.Names[mhtype]
if !ok { if !ok {
err := fmt.Errorf("unrecognized multihash function: %s", mhtype) res.SetError(fmt.Errorf("unrecognized format: '%s'", format), cmdkit.ErrNormal)
res.SetError(err, cmdkit.ErrNormal) return
}
if mhtval != mh.SHA2_256 && pref.Version == 0 {
res.SetError(errors.New("cannot generate CIDv0 with non-sha256 hash function"), cmdkit.ErrNormal)
return return
} }
pref.Codec = formatval
pref.MhType = mhtval pref.MhType = mhtval
mhlen, ok := req.Options["mhlen"].(int) mhlen, ok := req.Options["mhlen"].(int)
......
...@@ -209,4 +209,16 @@ test_expect_success "no panic in output" ' ...@@ -209,4 +209,16 @@ test_expect_success "no panic in output" '
test_expect_code 1 grep "panic" stat_out test_expect_code 1 grep "panic" stat_out
' '
test_expect_success "can set multihash type and length on block put without format" '
HASH=$(echo "foooo" | ipfs block put --mhtype=sha3 --mhlen=20)
'
test_expect_success "output looks good" '
test "z8bwYCvQPhyDY7VUTsUdGdE8Evm1ktSPV" = "$HASH"
'
test_expect_success "put with sha3 and cidv0 fails" '
echo "foooo" | test_must_fail ipfs block put --mhtype=sha3 --mhlen=20 --format=v0
'
test_done test_done
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment