Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: update DAG API to match go-ipfs@0.10 changes (#3917)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Rataj <lidel@lidel.org>
Co-authored-by: achingbrain <alex@achingbrain.net>

BREAKING CHANGE: `ipfs.dag.put` no longer accepts a `format` arg, it is now `storeCodec` and `inputCodec`.  `'json'` has become `'dag-json'`, `'cbor'` has become `'dag-cbor'` and so on
  • Loading branch information
rvagg authored Dec 3, 2021
1 parent 0c269cf commit 38c01be
Show file tree
Hide file tree
Showing 36 changed files with 584 additions and 384 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ jobs:
- name: ipfs custom ipfs repo
repo: https://github.com/ipfs-examples/js-ipfs-custom-ipfs-repo.git
deps: ipfs-core@$PWD/packages/ipfs-core/dist
- name: ipfs custom ipld formats
repo: https://github.com/ipfs-examples/js-ipfs-custom-ipld-formats.git
deps: ipfs-core@$PWD/packages/ipfs-core/dist,ipfs-daemon@$PWD/packages/ipfs-daemon/dist,ipfs-http-client@$PWD/packages/ipfs-http-client/dist
#- name: ipfs custom ipld formats
# repo: https://github.com/ipfs-examples/js-ipfs-custom-ipld-formats.git
# deps: ipfs-core@$PWD/packages/ipfs-core/dist,ipfs-daemon@$PWD/packages/ipfs-daemon/dist,ipfs-http-client@$PWD/packages/ipfs-http-client/dist
- name: ipfs custom libp2p
repo: https://github.com/ipfs-examples/js-ipfs-custom-libp2p.git
deps: ipfs-core@$PWD/packages/ipfs-core/dist
Expand Down Expand Up @@ -396,9 +396,9 @@ jobs:
- name: ipfs running multiple nodes
repo: https://github.com/ipfs-examples/js-ipfs-running-multiple-nodes.git
deps: ipfs@$PWD/packages/ipfs/dist
- name: ipfs traverse ipld graphs
repo: https://github.com/ipfs-examples/js-ipfs-traverse-ipld-graphs.git
deps: ipfs-core@$PWD/packages/ipfs-core/dist
#- name: ipfs traverse ipld graphs
# repo: https://github.com/ipfs-examples/js-ipfs-traverse-ipld-graphs.git
# deps: ipfs-core@$PWD/packages/ipfs-core/dist
- name: types with typescript
repo: https://github.com/ipfs-examples/js-ipfs-types-use-ipfs-from-ts.git
deps: ipfs-core@$PWD/packages/ipfs-core/dist
Expand Down
11 changes: 6 additions & 5 deletions docs/core-api/DAG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ An optional object which may have the following keys:

| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| format | `String` | `'dag-cbor'` | The IPLD format multicodec |
| storeCodec | `String` | `'dag-cbor'` | The codec that the stored object will be encoded with |
| inputCodec | `String` | `undefined` | If an already encoded object is provided (as a `Uint8Array`), the codec that the object is encoded with, otherwise it is assumed the `dagNode` argument is an object to be encoded |
| hashAlg | `String` | `'sha2-256'` | The hash algorithm to be used over the serialized DAG node |
| cid | [CID][] | `'dag-cbor'` | The IPLD format multicodec |
| pin | `boolean` | `false` | Pin this node when adding to the blockstore |
| timeout | `Number` | `undefined` | A timeout in ms |
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |

**Note**: You should pass `cid` or the `format` & `hashAlg` pair but _not both_.

Expand All @@ -107,7 +108,7 @@ An optional object which may have the following keys:

```JavaScript
const obj = { simple: 'object' }
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-512' })
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-512' })

console.log(cid.toString())
// zBwWX9ecx5F4X54WAjmFLErnBT6ByfNxStr5ovowTL7AhaUR98RWvXPS1V3HqV1qs3r5Ec5ocv7eCdbqYQREXNUfYNuKG
Expand Down Expand Up @@ -160,7 +161,7 @@ const obj = {
}
}

const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
console.log(cid.toString())
// zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5

Expand Down Expand Up @@ -272,7 +273,7 @@ const obj = {
}
}

const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
console.log(cid.toString())
// bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq

Expand Down
16 changes: 8 additions & 8 deletions packages/interface-ipfs-core/src/block/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function testRm (factory, options) {

it('should remove by CID object', async () => {
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
})

Expand All @@ -61,15 +61,15 @@ export function testRm (factory, options) {
it('should remove multiple CIDs', async () => {
const cids = await Promise.all([
ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
}),
ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
}),
ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
})
])
Expand All @@ -86,7 +86,7 @@ export function testRm (factory, options) {

it('should error when removing non-existent blocks', async () => {
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
})

Expand All @@ -102,7 +102,7 @@ export function testRm (factory, options) {

it('should not error when force removing non-existent blocks', async () => {
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
})

Expand All @@ -119,7 +119,7 @@ export function testRm (factory, options) {

it('should return empty output when removing blocks quietly', async () => {
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
})
const result = await all(ipfs.block.rm(cid, { quiet: true }))
Expand All @@ -129,7 +129,7 @@ export function testRm (factory, options) {

it('should error when removing pinned blocks', async () => {
const cid = await ipfs.dag.put(uint8ArrayFromString(nanoid()), {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
})
await ipfs.pin.add(cid)
Expand Down
22 changes: 11 additions & 11 deletions packages/interface-ipfs-core/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ export function testGet (factory, options) {
Data: uint8ArrayFromString('I am inside a Protobuf'),
Links: []
}
cidPb = CID.createV0(await sha256.digest(dagPB.encode(nodePb)))
cidPb = CID.createV1(dagPB.code, await sha256.digest(dagPB.encode(nodePb)))
nodeCbor = {
someData: 'I am inside a Cbor object',
pb: cidPb
}

cidCbor = CID.createV1(dagCBOR.code, await sha256.digest(dagCBOR.encode(nodeCbor)))

await ipfs.dag.put(nodePb, { format: 'dag-pb', hashAlg: 'sha2-256' })
await ipfs.dag.put(nodeCbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
await ipfs.dag.put(nodePb, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
await ipfs.dag.put(nodeCbor, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
})

it('should respect timeout option when getting a DAG node', () => {
Expand All @@ -93,7 +93,7 @@ export function testGet (factory, options) {

it('should get a dag-pb node', async () => {
const cid = await ipfs.dag.put(pbNode, {
format: 'dag-pb',
storeCodec: 'dag-pb',
hashAlg: 'sha2-256'
})

Expand All @@ -105,7 +105,7 @@ export function testGet (factory, options) {

it('should get a dag-cbor node', async () => {
const cid = await ipfs.dag.put(cborNode, {
format: 'dag-cbor',
storeCodec: 'dag-cbor',
hashAlg: 'sha2-256'
})

Expand All @@ -122,7 +122,7 @@ export function testGet (factory, options) {

const node = result.value

const cid = CID.createV0(await sha256.digest(dagPB.encode(node)))
const cid = CID.createV1(dagPB.code, await sha256.digest(dagPB.encode(node)))
expect(cid.equals(cidPb)).to.be.true()
})

Expand Down Expand Up @@ -192,7 +192,7 @@ export function testGet (factory, options) {
}

const cid = await ipfs.dag.put(node, {
format: 'dag-pb',
storeCodec: 'dag-pb',
hashAlg: 'sha2-256',
version: 0
})
Expand Down Expand Up @@ -226,7 +226,7 @@ export function testGet (factory, options) {
foo: 'dag-cbor-bar'
}

const cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put(cbor, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
expect(cid.code).to.equal(dagCBOR.code)
expect(cid.toString(base32)).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')

Expand All @@ -241,10 +241,10 @@ export function testGet (factory, options) {
foo: 'dag-cbor-bar'
}

const cid1 = await ipfs.dag.put(cbor1, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid1 = await ipfs.dag.put(cbor1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
const cbor2 = { other: cid1 }

const cid2 = await ipfs.dag.put(cbor2, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid2 = await ipfs.dag.put(cbor2, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

const result = await ipfs.dag.get(cid2, {
path: 'other/foo'
Expand All @@ -256,7 +256,7 @@ export function testGet (factory, options) {
const buf = Uint8Array.from([0, 1, 2, 3])

const cid = await ipfs.dag.put(buf, {
format: 'raw',
storeCodec: 'raw',
hashAlg: 'sha2-256'
})

Expand Down
4 changes: 3 additions & 1 deletion packages/interface-ipfs-core/src/dag/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { testGet } from './get.js'
import { testPut } from './put.js'
import { testImport } from './import.js'
import { testResolve } from './resolve.js'
import { testDagSharnessT0053 } from './sharness-t0053-dag.js'

const tests = {
export: testExport,
get: testGet,
put: testPut,
import: testImport,
resolve: testResolve
resolve: testResolve,
dagSharnessT0053: testDagSharnessT0053
}

export default createSuite(tests)
12 changes: 6 additions & 6 deletions packages/interface-ipfs-core/src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,35 @@ export function testPut (factory, options) {

it('should put dag-pb with default hash func (sha2-256)', () => {
return ipfs.dag.put(pbNode, {
format: 'dag-pb',
storeCodec: 'dag-pb',
hashAlg: 'sha2-256'
})
})

it('should put dag-pb with non-default hash func (sha2-512)', () => {
return ipfs.dag.put(pbNode, {
format: 'dag-pb',
storeCodec: 'dag-pb',
hashAlg: 'sha2-512'
})
})

it('should put dag-cbor with default hash func (sha2-256)', () => {
return ipfs.dag.put(cborNode, {
format: 'dag-cbor',
storeCodec: 'dag-cbor',
hashAlg: 'sha2-256'
})
})

it('should put dag-cbor with non-default hash func (sha2-512)', () => {
return ipfs.dag.put(cborNode, {
format: 'dag-cbor',
storeCodec: 'dag-cbor',
hashAlg: 'sha2-512'
})
})

it('should return the cid', async () => {
const cid = await ipfs.dag.put(cborNode, {
format: 'dag-cbor',
storeCodec: 'dag-cbor',
hashAlg: 'sha2-256'
})
expect(cid).to.exist()
Expand All @@ -90,7 +90,7 @@ export function testPut (factory, options) {

it('should override hash algorithm default and resolve with it', async () => {
const cid = await ipfs.dag.put(cborNode, {
format: 'dag-cbor',
storeCodec: 'dag-cbor',
hashAlg: 'sha2-512'
})
expect(cid.code).to.equal(dagCBOR.code)
Expand Down
20 changes: 10 additions & 10 deletions packages/interface-ipfs-core/src/dag/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function testResolve (factory, options) {
after(() => factory.clean())

it('should respect timeout option when resolving a path within a DAG node', async () => {
const cid = await ipfs.dag.put({}, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put({}, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

return testTimeout(() => ipfs.dag.resolve(cid, {
timeout: 1
Expand All @@ -43,7 +43,7 @@ export function testResolve (factory, options) {
}
}

const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

const result = await ipfs.dag.resolve(`${cid}/c/cb`)
expect(result).to.have.deep.property('cid', cid)
Expand All @@ -60,7 +60,7 @@ export function testResolve (factory, options) {
}
}

const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

const result = await ipfs.dag.resolve(cid, { path: '/c/cb' })
expect(result).to.have.deep.property('cid', cid)
Expand All @@ -72,15 +72,15 @@ export function testResolve (factory, options) {
ca: [5, 6, 7],
cb: 'foo'
}
const cid0 = await ipfs.dag.put(obj0, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid0 = await ipfs.dag.put(obj0, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

const obj1 = {
a: 1,
b: [1, 2, 3],
c: cid0
}

const cid1 = await ipfs.dag.put(obj1, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid1 = await ipfs.dag.put(obj1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

const result = await ipfs.dag.resolve(`/ipfs/${cid1}/c/cb`)
expect(result).to.have.deep.property('cid', cid0)
Expand All @@ -92,15 +92,15 @@ export function testResolve (factory, options) {
ca: [5, 6, 7],
cb: 'foo'
}
const cid0 = await ipfs.dag.put(obj0, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid0 = await ipfs.dag.put(obj0, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

const obj1 = {
a: 1,
b: [1, 2, 3],
c: cid0
}

const cid1 = await ipfs.dag.put(obj1, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid1 = await ipfs.dag.put(obj1, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })

const result = await ipfs.dag.resolve(cid1, { path: '/c/cb' })
expect(result).to.have.deep.property('cid', cid0)
Expand All @@ -109,7 +109,7 @@ export function testResolve (factory, options) {

it('should resolve a raw node', async () => {
const node = uint8ArrayFromString('hello world')
const cid = await ipfs.dag.put(node, { format: 'raw', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put(node, { storeCodec: 'raw', hashAlg: 'sha2-256' })

const result = await ipfs.dag.resolve(cid, { path: '/' })
expect(result).to.have.deep.property('cid', cid)
Expand All @@ -122,7 +122,7 @@ export function testResolve (factory, options) {
Data: someData,
Links: []
}
const childCid = await ipfs.dag.put(childNode, { format: 'dag-pb', hashAlg: 'sha2-256' })
const childCid = await ipfs.dag.put(childNode, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })

const linkToChildNode = {
Name: 'foo',
Expand All @@ -133,7 +133,7 @@ export function testResolve (factory, options) {
Data: uint8ArrayFromString('derp'),
Links: [linkToChildNode]
}
const parentCid = await ipfs.dag.put(parentNode, { format: 'dag-pb', hashAlg: 'sha2-256' })
const parentCid = await ipfs.dag.put(parentNode, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })

const result = await ipfs.dag.resolve(parentCid, { path: '/foo' })
expect(result).to.have.deep.property('cid', childCid)
Expand Down
Loading

0 comments on commit 38c01be

Please sign in to comment.