Skip to content

Commit e6634fe

Browse files
olizillavmx
authored andcommitted
docs: add multibaseName param with better examples
- Clarify how to use the mutlibaseName param in the README. - Update the usage section with clear examples
1 parent a75da83 commit e6634fe

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

README.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,51 @@ You will need to use Node.js `Buffer` API compatible, if you are running inside
5454

5555
## Usage
5656

57-
Basic usage is quite simple.
57+
You can create an instance from a CID string or CID Buffer
5858

5959
```js
6060
const CID = require('cids')
6161

62-
const cid = new CID(1, 'dag-pb', multihash)
62+
const cid = new CID('zdj7WkRPAX9o9nb9zPbXzwG7JEs78uyhwbUs8JSUayB98DWWY')
63+
64+
cid.version // 1
65+
cid.codec // 'dag-pb'
66+
cid.multibaseName // 'base58btc'
67+
cid.toString()
68+
// 'zdj7WkRPAX9o9nb9zPbXzwG7JEs78uyhwbUs8JSUayB98DWWY'
69+
```
70+
71+
or by specifying the [cid version](https://github.com/multiformats/cid#versions), [multicodec name](https://github.com/multiformats/multicodec/blob/master/table.csv) and [multihash](https://github.com/multiformats/multihash):
72+
73+
```js
74+
const CID = require('cids')
75+
const multihashing = require('multihashing-async')
76+
77+
multihashing(Buffer.from('OMG!'), 'sha2-256', (err, hash) => {
78+
const cid = new CID(1, 'dag-pb', hash)
79+
console.log(cid.toString())
80+
// zdj7WkRPAX9o9nb9zPbXzwG7JEs78uyhwbUs8JSUayB98DWWY
81+
})
82+
```
83+
84+
The string form of CIDs currently defaults to `base58btc` flavour. (This is [soon to change to `base32`](https://github.com/ipfs/ipfs/issues/337). When creating a new instance you can optionally specify the default multibase to use when calling `toBaseEncodedString()` or `toString()`
85+
86+
87+
```js
88+
const cid = new CID(1, 'raw', hash, 'base32')
89+
console.log(cid.toString())
90+
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu
6391
```
6492

65-
If you have a base encoded string for a multihash you can also create
66-
an instance from the encoded string.
93+
If you construct an instance from a valid CID string, the base you provided will be preserved as the default.
6794

6895
```js
69-
const cid = new CID(base58Multihash)
96+
const cid = new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
97+
cid.toString()
98+
// bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy
7099
```
71100

101+
72102
## API
73103

74104
### CID.isCID(cid)
@@ -80,17 +110,19 @@ order to handle CID objects from different versions of this module.
80110

81111
### CID.validateCID(cid)
82112

83-
Validates the different components (version, codec, multihash) of the CID
113+
Validates the different components (version, codec, multihash, multibaseName) of the CID
84114
instance. Returns true if valid, false if not valid.
85115

86-
### new CID(version, codec, multihash)
116+
### new CID(version, codec, multihash, [multibaseName])
87117

88-
`version` must be either 0 or 1.
118+
`version` must be [either 0 or 1](https://github.com/multiformats/cid#versions).
89119

90120
`codec` must be a string of a valid [registered codec](https://github.com/multiformats/multicodec/blob/master/table.csv).
91121

92122
`multihash` must be a `Buffer` instance of a valid [multihash](https://github.com/multiformats/multihash).
93123

124+
`multibaseName` optional string. Must be a valid [multibase](https://github.com/multiformats/multibase/blob/master/multibase.csv) name. Default is `base58btc`.
125+
94126
### new CID(baseEncodedString)
95127

96128
Additionally, you can instantiate an instance from a base encoded
@@ -112,6 +144,10 @@ Property containing the CID version integer.
112144

113145
Property containing the multihash buffer.
114146

147+
#### cid.multibaseName
148+
149+
Property containing the default base to use when calling `.toString`
150+
115151
#### cid.buffer
116152

117153
Property containing the full CID encoded as a `Buffer`.
@@ -131,9 +167,13 @@ Throws if codec is not `dag-pb`.
131167

132168
Returns the CID encoded in version 1.
133169

134-
#### cid.toBaseEncodedString(base='base58btc')
170+
#### cid.toBaseEncodedString(base=this.multibaseName)
171+
172+
Returns a base encodec string of the CID. Defaults to the base encoding in `this.multibaseName`
173+
174+
#### cid.toString(base=this.multibaseName)
135175

136-
Returns a base encodec string of the CID. Defaults to `base58btc`.
176+
Shorthand for `cid.toBaseEncodedString` described above.
137177

138178
#### cid.equals(cid)
139179

0 commit comments

Comments
 (0)