commands/block: use CIDv1 with custom mhtype
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jan 8, 2018 at 14:22 UTC
8c176d26ebdad5d0a225f83a467c7efcd2285295
2 files changed
+41
-12
core/commands/block.go
+29
-12
@@ -3,6 +3,7 @@ package commands
3
import (
4
"bytes"
5
"context"
6
+ "errors"
7
"fmt"
8
"io"
9
"io/ioutil"
@@ -121,6 +122,9 @@ var blockPutCmd = &cmds.Command{
122
ShortDescription: `
123
'ipfs block put' is a plumbing command for storing raw IPFS blocks.
124
It reads from stdin, and <key> is a base58 encoded multihash.
125
+
126
+By default CIDv0 is going to be generated. Setting 'mhtype' to anything other
127
+than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
128
`,
129
},
130
@@ -128,7 +132,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.
132
cmdkit.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
133
},
134
Options: []cmdkit.Option{
131
- cmdkit.StringOption("format", "f", "cid format for blocks to be created with.").WithDefault("v0"),
135
+ cmdkit.StringOption("format", "f", "cid format for blocks to be created with.").WithDefault(""),
136
cmdkit.StringOption("mhtype", "multihash hash function").WithDefault("sha2-256"),
137
cmdkit.IntOption("mhlen", "multihash hash length").WithDefault(-1),
138
},
@@ -157,27 +161,40 @@ It reads from stdin, and <key> is a base58 encoded multihash.
161
return
162
}
163
164
+ mhtype, _ := req.Options["mhtype"].(string)
165
+ mhtval, ok := mh.Names[mhtype]
166
+ if !ok {
167
+ err := fmt.Errorf("unrecognized multihash function: %s", mhtype)
168
+ res.SetError(err, cmdkit.ErrNormal)
169
+ return
170
+ }
171
+
172
var pref cid.Prefix
173
pref.Version = 1
174
163
- format, _ := req.Options["format"].(string)
164
- formatval, ok := cid.Codecs[format]
165
- if !ok {
166
- res.SetError(fmt.Errorf("unrecognized format: %s", format), cmdkit.ErrNormal)
167
- return
175
+ format := req.Options["format"].(string)
176
+ if format == "" {
177
+ if mhtval == mh.SHA2_256 {
178
+ format = "v0"
179
+ } else {
180
+ format = "protobuf"
181
+ }
182
}
183
+
184
if format == "v0" {
185
pref.Version = 0
186
}
172
- pref.Codec = formatval
173
-
174
- mhtype, _ := req.Options["mhtype"].(string)
175
- mhtval, ok := mh.Names[mhtype]
187
+ formatval, ok := cid.Codecs[format]
188
if !ok {
177
- err := fmt.Errorf("unrecognized multihash function: %s", mhtype)
178
- res.SetError(err, cmdkit.ErrNormal)
189
+ res.SetError(fmt.Errorf("unrecognized format: %s", format), cmdkit.ErrNormal)
190
+ return
191
+ }
192
+ if mhtval != mh.SHA2_256 && pref.Version == 0 {
193
+ res.SetError(errors.New("cannot generate CIDv0 with non-sha256 hash function"), cmdkit.ErrNormal)
194
return
195
}
196
+
197
+ pref.Codec = formatval
198
pref.MhType = mhtval
199
200
mhlen, ok := req.Options["mhlen"].(int)
test/sharness/t0050-block.sh
+12
@@ -209,4 +209,16 @@ test_expect_success "no panic in output" '
209
test_expect_code 1 grep "panic" stat_out
210
'
211
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
+
224
test_done