Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-cid
Commits
f62e35b8
Commit
f62e35b8
authored
Aug 14, 2017
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement basic 'cid-fmt' utility.
Currently only implements printing the Cid prefix.
parent
5652e6f7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
0 deletions
+90
-0
cid-fmt/main.go
cid-fmt/main.go
+69
-0
cid.go
cid.go
+21
-0
No files found.
cid-fmt/main.go
0 → 100644
View file @
f62e35b8
package
main
import
(
"fmt"
"os"
c
"github.com/ipfs/go-cid"
mh
"github.com/multiformats/go-multihash"
)
func
main
()
{
if
len
(
os
.
Args
)
<
2
{
fmt
.
Fprintf
(
os
.
Stderr
,
"usage: %s prefix ...
\n
"
,
os
.
Args
[
0
])
os
.
Exit
(
1
)
}
switch
os
.
Args
[
1
]
{
case
"prefix"
:
err
:=
prefixCmd
(
os
.
Args
[
2
:
])
if
err
!=
nil
{
fmt
.
Fprintf
(
os
.
Stderr
,
"%v
\n
"
,
err
)
os
.
Exit
(
1
)
}
default
:
fmt
.
Fprintf
(
os
.
Stderr
,
"usage: %s prefix ...
\n
"
)
os
.
Exit
(
1
)
}
}
func
prefixCmd
(
args
[]
string
)
error
{
for
_
,
cid
:=
range
args
{
p
,
err
:=
prefix
(
cid
)
if
err
!=
nil
{
return
err
}
fmt
.
Fprintf
(
os
.
Stdout
,
"%s
\n
"
,
p
)
}
return
nil
}
func
prefix
(
str
string
)
(
string
,
error
)
{
cid
,
err
:=
c
.
Decode
(
str
)
if
err
!=
nil
{
return
""
,
err
}
p
:=
cid
.
Prefix
()
return
fmt
.
Sprintf
(
"cidv%d-%s-%s-%d"
,
p
.
Version
,
codecToStr
(
p
.
Codec
),
mhToStr
(
p
.
MhType
),
p
.
MhLength
,
),
nil
}
func
codecToStr
(
num
uint64
)
string
{
name
,
ok
:=
c
.
CodecToStr
[
num
]
if
!
ok
{
return
fmt
.
Sprintf
(
"c?%d"
,
num
)
}
return
name
}
func
mhToStr
(
num
uint64
)
string
{
name
,
ok
:=
mh
.
Codes
[
num
]
if
!
ok
{
return
fmt
.
Sprintf
(
"h?%d"
,
num
)
}
return
name
}
cid.go
View file @
f62e35b8
...
...
@@ -101,6 +101,27 @@ var Codecs = map[string]uint64{
"zcash-tx"
:
ZcashTx
,
}
// CodecToStr maps the numeric codec to its name
var
CodecToStr
=
map
[
uint64
]
string
{
Raw
:
"raw"
,
DagProtobuf
:
"protobuf"
,
DagCBOR
:
"cbor"
,
GitRaw
:
"git-raw"
,
EthBlock
:
"eth-block"
,
EthBlockList
:
"eth-block-list"
,
EthTxTrie
:
"eth-tx-trie"
,
EthTx
:
"eth-tx"
,
EthTxReceiptTrie
:
"eth-tx-receipt-trie"
,
EthTxReceipt
:
"eth-tx-receipt"
,
EthStateTrie
:
"eth-state-trie"
,
EthAccountSnapshot
:
"eth-account-snapshot"
,
EthStorageTrie
:
"eth-storage-trie"
,
BitcoinBlock
:
"bitcoin-block"
,
BitcoinTx
:
"bitcoin-tx"
,
ZcashBlock
:
"zcash-block"
,
ZcashTx
:
"zcash-tx"
,
}
// NewCidV0 returns a Cid-wrapped multihash.
// They exist to allow IPFS to work with Cids while keeping
// compatibility with the plain-multihash format used used in IPFS.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment