@@ -12,7 +12,6 @@ const withIs = require('class-is')
12
12
* @param {string } codec
13
13
* @param {number } version
14
14
* @param {Buffer } multihash
15
- *
16
15
*/
17
16
18
17
/**
@@ -35,81 +34,106 @@ class CID {
35
34
*
36
35
* The algorithm for argument input is roughly:
37
36
* ```
38
- * if (str)
37
+ * if (cid)
38
+ * -> create a copy
39
+ * else if (str)
39
40
* if (1st char is on multibase table) -> CID String
40
41
* else -> bs58 encoded multihash
41
42
* else if (Buffer)
42
- * if (0 or 1) -> CID
43
+ * if (1st byte is 0 or 1) -> CID
43
44
* else -> multihash
44
45
* else if (Number)
45
46
* -> construct CID by parts
46
- *
47
- * ..if only JS had traits..
48
47
* ```
49
48
*
50
49
* @param {string|Buffer } version
51
50
* @param {string } [codec]
52
51
* @param {Buffer } [multihash]
52
+ * @param {string } [multibaseName]
53
53
*
54
54
* @example
55
- *
56
- * new CID(<version>, <codec>, <multihash>)
55
+ * new CID(<version>, <codec>, <multihash>, <multibaseName>)
57
56
* new CID(<cidStr>)
58
57
* new CID(<cid.buffer>)
59
58
* new CID(<multihash>)
60
59
* new CID(<bs58 encoded multihash>)
61
60
* new CID(<cid>)
62
- *
63
61
*/
64
- constructor ( version , codec , multihash ) {
62
+ constructor ( version , codec , multihash , multibaseName = 'base58btc' ) {
65
63
if ( module . exports . isCID ( version ) ) {
66
- let cid = version
64
+ // version is an exising CID instance
65
+ const cid = version
67
66
this . version = cid . version
68
67
this . codec = cid . codec
69
68
this . multihash = Buffer . from ( cid . multihash )
69
+ this . multibaseName = cid . multibaseName
70
70
return
71
71
}
72
+
72
73
if ( typeof version === 'string' ) {
73
- if ( multibase . isEncoded ( version ) ) { // CID String (encoded with multibase)
74
+ // e.g. 'base32' or false
75
+ const baseName = multibase . isEncoded ( version )
76
+ if ( baseName ) {
77
+ // version is a CID String encoded with multibase, so v1
74
78
const cid = multibase . decode ( version )
75
- version = parseInt ( cid . slice ( 0 , 1 ) . toString ( 'hex' ) , 16 )
76
- codec = multicodec . getCodec ( cid . slice ( 1 ) )
77
- multihash = multicodec . rmPrefix ( cid . slice ( 1 ) )
78
- } else { // bs58 string encoded multihash
79
- codec = 'dag-pb'
80
- multihash = mh . fromB58String ( version )
81
- version = 0
79
+ this . version = parseInt ( cid . slice ( 0 , 1 ) . toString ( 'hex' ) , 16 )
80
+ this . codec = multicodec . getCodec ( cid . slice ( 1 ) )
81
+ this . multihash = multicodec . rmPrefix ( cid . slice ( 1 ) )
82
+ this . multibaseName = baseName
83
+ } else {
84
+ // version is a base58btc string multihash, so v0
85
+ this . version = 0
86
+ this . codec = 'dag-pb'
87
+ this . multihash = mh . fromB58String ( version )
88
+ this . multibaseName = 'base58btc'
82
89
}
83
- } else if ( Buffer . isBuffer ( version ) ) {
90
+ CID . validateCID ( this )
91
+ return
92
+ }
93
+
94
+ if ( Buffer . isBuffer ( version ) ) {
84
95
const firstByte = version . slice ( 0 , 1 )
85
96
const v = parseInt ( firstByte . toString ( 'hex' ) , 16 )
86
- if ( v === 0 || v === 1 ) { // CID
97
+ if ( v === 0 || v === 1 ) {
98
+ // version is a CID buffer
87
99
const cid = version
88
- version = v
89
- codec = multicodec . getCodec ( cid . slice ( 1 ) )
90
- multihash = multicodec . rmPrefix ( cid . slice ( 1 ) )
91
- } else { // multihash
92
- codec = 'dag-pb'
93
- multihash = version
94
- version = 0
100
+ this . version = v
101
+ this . codec = multicodec . getCodec ( cid . slice ( 1 ) )
102
+ this . multihash = multicodec . rmPrefix ( cid . slice ( 1 ) )
103
+ this . multibaseName = ( v === 0 ) ? 'base58btc' : multibaseName
104
+ } else {
105
+ // version is a raw multihash buffer, so v0
106
+ this . version = 0
107
+ this . codec = 'dag-pb'
108
+ this . multihash = version
109
+ this . multibaseName = 'base58btc'
95
110
}
111
+ CID . validateCID ( this )
112
+ return
96
113
}
97
114
98
- /**
99
- * @type {string }
100
- */
101
- this . codec = codec
115
+ // otherwise, assemble the CID from the parameters
102
116
103
117
/**
104
118
* @type {number }
105
119
*/
106
120
this . version = version
107
121
122
+ /**
123
+ * @type {string }
124
+ */
125
+ this . codec = codec
126
+
108
127
/**
109
128
* @type {Buffer }
110
129
*/
111
130
this . multihash = multihash
112
131
132
+ /**
133
+ * @type {string }
134
+ */
135
+ this . multibaseName = multibaseName
136
+
113
137
CID . validateCID ( this )
114
138
}
115
139
@@ -193,12 +217,10 @@ class CID {
193
217
/**
194
218
* Encode the CID into a string.
195
219
*
196
- * @param {string } [base='base58btc' ] - Base encoding to use.
220
+ * @param {string } [base=this.multibaseName ] - Base encoding to use.
197
221
* @returns {string }
198
222
*/
199
- toBaseEncodedString ( base ) {
200
- base = base || 'base58btc'
201
-
223
+ toBaseEncodedString ( base = this . multibaseName ) {
202
224
switch ( this . version ) {
203
225
case 0 : {
204
226
if ( base !== 'base58btc' ) {
0 commit comments