This repository has been archived by the owner on Feb 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
multicodec.go
131 lines (116 loc) · 2.54 KB
/
multicodec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package mcpacked
import (
"encoding/binary"
)
type Code uint64
const (
Unknown = Code(0)
Git = Code(0x69)
DagProtobuf = Code(0x70)
DagCBOR = Code(0x71)
Raw = Code(0x55)
RLP = Code(0x60)
Bencode = Code(0x63)
Multicodec = Code(0x30)
Multihash = Code(0x31)
Multiaddr = Code(0x32)
Multibase = Code(0x33)
StellarBlock = Code(0xd0)
StellarTx = Code(0xd1)
TorrentInfo = Code(0x7b)
TorrentFile = Code(0x7c)
EthBlock = Code(0x90)
EthBlockList = Code(0x91)
EthTxTrie = Code(0x92)
EthTx = Code(0x93)
EthTxReceiptTrie = Code(0x94)
EthTxReceipt = Code(0x95)
EthStateTrie = Code(0x96)
EthAccountSnapshot = Code(0x97)
EthStorageTrie = Code(0x98)
BitcoinBlock = Code(0xb0)
BitcoinTx = Code(0xb1)
ZcashBlock = Code(0xc0)
ZcashTx = Code(0xc1)
Ed25519Pub = Code(0xed)
)
func (c Code) String() string {
return CodeToString(c)
}
const UnknownMulticodecString = "<Unknown Multicodec>"
func CodeToString(c Code) string {
switch c {
case Git:
return "git"
case DagProtobuf:
return "dag-pb"
case DagCBOR:
return "dag-cbor"
case Raw:
return "bin"
case RLP:
return "rlp"
case Bencode:
return "bencode"
case Multicodec:
return "multicodec"
case Multihash:
return "multihash"
case Multiaddr:
return "multiaddr"
case Multibase:
return "multibase"
case BitcoinBlock:
return "bitcoin-block"
case BitcoinTx:
return "bitcoin-tx"
case EthBlock:
return "eth-block"
case EthBlockList:
return "eth-block-list"
case EthTxTrie:
return "eth-tx-trie"
case EthTx:
return "eth-tx"
case EthTxReceiptTrie:
return "eth-tx-receipt-trie"
case EthTxReceipt:
return "eth-tx-receipt"
case EthStateTrie:
return "eth-state-trie"
case EthAccountSnapshot:
return "eth-account-snapshot"
case EthStorageTrie:
return "eth-storage-trie"
case ZcashBlock:
return "zcash-block"
case ZcashTx:
return "zcash-tx"
case StellarBlock:
return "stellar-block"
case StellarTx:
return "stellar-tx"
case TorrentInfo:
return "torrent-info"
case TorrentFile:
return "torrent-file"
case Ed25519Pub:
return "ed25519-pub"
default:
return UnknownMulticodecString
}
}
func GetCode(data []byte) Code {
c, _ := binary.Uvarint(data)
return Code(c)
}
func AddPrefix(c Code, data []byte) []byte {
buf := make([]byte, len(data)+binary.MaxVarintLen64)
n := binary.PutUvarint(buf, uint64(c))
copy(buf[n:], data)
return buf[:n+len(data)]
}
func SplitPrefix(data []byte) (Code, []byte) {
c, n := binary.Uvarint(data)
return Code(c), data[n:]
}