Skip to content

Commit 70956a6

Browse files
authored
Merge pull request ipfs#1476 from ElPaisano/1449
Update JS examples to v1, add code sandbox
2 parents de65490 + a00bee8 commit 70956a6

File tree

1 file changed

+55
-37
lines changed

1 file changed

+55
-37
lines changed

docs/concepts/content-addressing.md

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -135,66 +135,73 @@ $ ipfs cid format -v 1 -b base32 QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR
135135
bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi
136136
```
137137

138-
JavaScript users can also leverage the `toV1()` method provided by the [`cids`](https://www.npmjs.com/package/cids) library:
138+
JavaScript users can also leverage the `toV1()` method provided by the [`multiformats`](https://www.npmjs.com/package/multiformats) library:
139+
139140
```js
140-
const CID = require('cids')
141-
new CID('QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR').toV1().toString('base32')
142-
// → bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi
141+
const v0 = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
142+
v0.toString()
143+
//> 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
144+
v0.toV1().toString()
145+
//> 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku'
143146
```
144147

145-
### Text to binary
148+
### v1 to v0
149+
150+
Given a CID v1, JS users can convert back to v0 using the `toV0()` method provided by the [`multiformats`](https://www.npmjs.com/package/multiformats) library:
151+
152+
```js
153+
const v1 = CID.parse('bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku')
154+
v1.toString()
155+
//> 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku'
156+
v1.toV0().toString()
157+
//> 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
158+
```
146159

147-
A CID can be represented as both text and as a stream of bytes. The latter may be a better choice when speed and storage efficiency are considerations.
160+
:::callout
161+
**See CID conversion in action**
162+
See the [interactive code sandbox](#codesandbox-converting-between-cid-versions-and-encodings) for an example JS application that converts between CID versions and encodings.
163+
:::
148164

149-
To convert a CIDv1 from text to binary form, simply read the first character
150-
and then decode the remainder using the encoding specified in the [multibase table](https://github.com/multiformats/multibase#multibase-table).
165+
### Converting between CID base encodings
151166

152-
JS users can leverage the [`cids`](https://www.npmjs.com/package/cids) library to get a binary version as `Uint8Array`:
167+
A CID can be encoded using any of the encodings specified in the [multibase table](https://github.com/multiformats/multibase#multibase-table). The use of different encodings can impact speed and storage efficiency.
153168

169+
To convert a CIDv1 `cidV1` from one encoding to another, use the `toString()` method. By default, `toString()` will return the `base32` string representation of the CID, but you can use other string representations:
154170

155171
```js
156-
const CID = require('cids')
157-
new CID('bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi').bytes
158-
// → Uint8Array [ 1, 112, 18, 32, 195, 196, 115, 62, ... ]
172+
const cidV1StringBase32 = cidV1.toString();
159173
```
160174

161-
::: warning Be mindful about parsing CIDs correctly. Avoid shortcuts.
162-
163-
Unless you are the one who imported the data to IPFS, the length of a CID is not deterministic and depends on the length of the multihash inside of it.
175+
The following example returns the base256 emoji encoding of the CID:
176+
```js
177+
const cidV1StringBase256 = cidV1.toString(base256emoji);
178+
```
164179

165-
To illustrate, passing a custom hash function will produce CIDs of varying lengths:
180+
Using `.bytes`, the following example returns the raw bytes of the CID:
166181

182+
```js
183+
const cidV1Bytes = cidV1.bytes
167184
```
168-
$ ipfs add --cid-version 1 --hash sha2-256 -nq cat.jpg | wc -c
169-
60
170-
$ ipfs add --cid-version 1 --hash blake2b-256 -nq cat.jpg | wc -c
171-
63
172-
$ ipfs add --cid-version 1 --hash sha3-512 -nq cat.jpg | wc -c
173-
111
174-
```
185+
186+
:::callout
187+
**See CID conversion in action**
188+
See the [interactive code sandbox](#codesandbox-converting-between-cid-versions-and-encodings) for an example JS application that converts between CID versions and encodings.
175189
:::
176190

177191

178192
### CID to hex
179193

180194
Sometimes, a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) representation of raw bytes is preferred for debug purposes.
181-
To get the hex for raw `.bytes` of an entire CID, one can use built-in support for `base16` encoding and skip the `f` (multibase prefix):
182-
183-
```javascript
184-
> cid.toString('base16')
185-
'f01701220c3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a'
195+
To get the hex for raw `.bytes` of a CIDv1 `cidV1`, use `base16` encoding:
186196

187-
> cid.toString('base16').substring(1)
188-
'01701220c3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a' // "cid as hex"
197+
```js
198+
const cidV1StringBase256 = cidV1.toString(base16);
189199
```
190200

191-
To convert back to a CIDv1, prepend the hex value with `f` ([multibase prefix](https://github.com/multiformats/multibase#multibase-table) for lowercase base16).
192-
Use it as-is (it is a [valid CID](https://ipfs.io/ipfs/f01701220c3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a)), or convert to a different multibase by passing it as an argument to `toString`:
193-
194-
```javascript
195-
> new CID('f' +'01701220c3c4733ec8affd06cf9e9ff50ffc6bcd2ec85a6170004bb709669c31de94391a').toString('base32')
196-
// → bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi
197-
```
201+
:::callout
202+
**See CID conversion in action**
203+
See the [interactive code sandbox](#codesandbox-converting-between-cid-versions-and-encodings) for an example JS application that converts between CID versions and encodings.
204+
:::
198205

199206
::: tip
200207
[Subdomain gateways](../how-to/address-ipfs-on-web.md#subdomain-gateway) convert paths with custom bases like base16 to base32 or base36, in an effort to fit a CID in a DNS label:
@@ -204,6 +211,17 @@ Use it as-is (it is a [valid CID](https://ipfs.io/ipfs/f01701220c3c4733ec8affd06
204211
:::
205212

206213

214+
### CodeSandbox: Converting between CID versions and encodings
215+
216+
For a hand-on, interactive application that converts between CID versions and encodings, use the CodeSandbox below.
217+
218+
<iframe src="https://codesandbox.io/embed/converting-between-cid-versions-xrvqop?fontsize=14&hidenavigation=1&theme=dark"
219+
style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"
220+
title="Converting between CID versions"
221+
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
222+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
223+
></iframe>
224+
207225
## Further resources
208226

209227
Check out these links for more information on CIDs and how they work:

0 commit comments

Comments
 (0)