Skip to content

Commit 7d2f39b

Browse files
authored
Merge pull request #6817 from ipfs/feat/cid-codec-convert
make it possible to change the codec with the `ipfs cid` subcommand
2 parents ca2767a + e58a32a commit 7d2f39b

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

core/commands/cid.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var CidCmd = &cmds.Command{
3131
const (
3232
cidFormatOptionName = "f"
3333
cidVerisonOptionName = "v"
34+
cidCodecOptionName = "codec"
3435
cidMultibaseOptionName = "b"
3536
)
3637

@@ -49,11 +50,13 @@ The optional format string is a printf style format string:
4950
Options: []cmds.Option{
5051
cmds.StringOption(cidFormatOptionName, "Printf style format string.").WithDefault("%s"),
5152
cmds.StringOption(cidVerisonOptionName, "CID version to convert to."),
53+
cmds.StringOption(cidCodecOptionName, "CID codec to convert to."),
5254
cmds.StringOption(cidMultibaseOptionName, "Multibase to display CID in."),
5355
},
5456
Run: func(req *cmds.Request, resp cmds.ResponseEmitter, env cmds.Environment) error {
5557
fmtStr, _ := req.Options[cidFormatOptionName].(string)
5658
verStr, _ := req.Options[cidVerisonOptionName].(string)
59+
codecStr, _ := req.Options[cidCodecOptionName].(string)
5760
baseStr, _ := req.Options[cidMultibaseOptionName].(string)
5861

5962
opts := cidFormatOpts{}
@@ -63,10 +66,21 @@ The optional format string is a printf style format string:
6366
}
6467
opts.fmtStr = fmtStr
6568

69+
if codecStr != "" {
70+
codec, ok := cid.Codecs[codecStr]
71+
if !ok {
72+
return fmt.Errorf("unknown IPLD codec: %s", codecStr)
73+
}
74+
opts.newCodec = codec
75+
} // otherwise, leave it as 0 (not a valid IPLD codec)
76+
6677
switch verStr {
6778
case "":
6879
// noop
6980
case "0":
81+
if opts.newCodec != 0 && opts.newCodec != cid.DagProtobuf {
82+
return fmt.Errorf("cannot convert to CIDv0 with any codec other than DagPB")
83+
}
7084
opts.verConv = toCidV0
7185
case "1":
7286
opts.verConv = toCidV1
@@ -125,9 +139,10 @@ var base32Cmd = &cmds.Command{
125139
}
126140

127141
type cidFormatOpts struct {
128-
fmtStr string
129-
newBase mbase.Encoding
130-
verConv func(cid cid.Cid) (cid.Cid, error)
142+
fmtStr string
143+
newBase mbase.Encoding
144+
verConv func(cid cid.Cid) (cid.Cid, error)
145+
newCodec uint64
131146
}
132147

133148
type argumentIterator struct {
@@ -169,10 +184,11 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
169184
emitErr = resp.Emit(res)
170185
continue
171186
}
172-
base := opts.newBase
173-
if base == -1 {
174-
base, _ = cid.ExtractEncoding(cidStr)
187+
188+
if opts.newCodec != 0 && opts.newCodec != c.Type() {
189+
c = cid.NewCidV1(opts.newCodec, c.Hash())
175190
}
191+
176192
if opts.verConv != nil {
177193
c, err = opts.verConv(c)
178194
if err != nil {
@@ -181,6 +197,16 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
181197
continue
182198
}
183199
}
200+
201+
base := opts.newBase
202+
if base == -1 {
203+
if c.Version() == 0 {
204+
base = mbase.Base58BTC
205+
} else {
206+
base, _ = cid.ExtractEncoding(cidStr)
207+
}
208+
}
209+
184210
str, err := cidutil.Format(opts.fmtStr, base, c)
185211
if _, ok := err.(cidutil.FormatStringError); ok {
186212
// no point in continuing if there is a problem with the format string

test/sharness/t0290-cid.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ CIDv0="QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv"
1010
CIDv1="zdj7WZAAFKPvYPPzyJLso2hhxo8a7ZACFQ4DvvfrNXTHidofr"
1111
CIDb32="bafybeibxm2nsadl3fnxv2sxcxmxaco2jl53wpeorjdzidjwf5aqdg7wa6u"
1212

13+
CIDbase="QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6"
14+
CIDb32pb="bafybeievd6mwe6vcwnkwo3eizs3h7w3a34opszbyfxziqdxguhjw7imdve"
15+
CIDb32raw="bafkreievd6mwe6vcwnkwo3eizs3h7w3a34opszbyfxziqdxguhjw7imdve"
16+
1317
test_expect_success "cid base32 works" '
1418
echo $CIDb32 > expected &&
1519
ipfs cid base32 $CIDv0 > actual1 &&
@@ -26,6 +30,12 @@ test_expect_success "cid format -v 1 -b base58btc" '
2630
test_cmp expected actual2
2731
'
2832

33+
test_expect_success "cid format -v 0" '
34+
echo $CIDv0 > expected &&
35+
ipfs cid format -v 0 $CIDb32 > actual &&
36+
test_cmp expected actual
37+
'
38+
2939
cat <<EOF > various_cids
3040
QmZZRTyhDpL5Jgift1cHbAhexeE1m2Hw8x8g7rTcPahDvo
3141
QmPhk6cJkRcFfZCdYam4c9MKYjFG9V29LswUnbrFNhtk2S
@@ -228,4 +238,17 @@ test_expect_success "cid hashes --numeric" '
228238
test_cmp hashes_expect actual
229239
'
230240

241+
test_expect_success "cid format -c raw" '
242+
echo $CIDb32raw > expected &&
243+
ipfs cid format --codec raw -b base32 $CIDb32pb > actual &&
244+
test_cmp actual expected
245+
'
246+
247+
test_expect_success "cid format -c protobuf -v 0" '
248+
echo $CIDbase > expected &&
249+
ipfs cid format --codec protobuf -v 0 $CIDb32raw > actual &&
250+
test_cmp actual expected
251+
'
252+
253+
231254
test_done

0 commit comments

Comments
 (0)