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

Commit fbe1492

Browse files
achingbrainoedstephhuynh18
authored
feat: add support for dag-jose codec (#4028)
`dag-jose` is now supported out of the box without any other configuration required. Closes: ceramicnetwork/.github#207 Co-authored-by: Joel Thorstensson <[email protected]> Co-authored-by: Joel Thorstensson <[email protected]> Co-authored-by: stephhuynh18 <[email protected]>
1 parent a6b201a commit fbe1492

File tree

8 files changed

+111
-12
lines changed

8 files changed

+111
-12
lines changed

.github/workflows/test.yml

+8-10
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,10 @@ jobs:
210210
with:
211211
build: |
212212
npm run link
213-
- run: |
214-
npm install
215-
npm run build
216-
npm run link
217-
npm run test:interop -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -- -t ${{ matrix.type }}
213+
- run: npm install
214+
- run: npm run build
215+
- run: npm run link
216+
- run: npm run test:interop -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -- -t ${{ matrix.type }}
218217
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
219218
with:
220219
flags: interop-${{ matrix.type }}
@@ -241,11 +240,10 @@ jobs:
241240
- uses: actions/setup-node@v2
242241
with:
243242
node-version: lts/*
244-
- run: |
245-
npm install
246-
npm run build
247-
npm run link
248-
npm run ${{ matrix.suite }} -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -t ${{ matrix.type }}
243+
- run: npm install
244+
- run: npm run build
245+
- run: npm run link
246+
- run: npm run ${{ matrix.suite }} -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -t ${{ matrix.type }}
249247
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
250248
with:
251249
flags: interface-${{ matrix.type }}

packages/interface-ipfs-core/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
"aegir": "^36.0.1",
7070
"blockstore-core": "^1.0.2",
7171
"copyfiles": "^2.4.1",
72+
"dag-jose": "^1.0.0",
7273
"delay": "^5.0.0",
74+
"did-jwt": "^5.12.1",
7375
"err-code": "^3.0.1",
7476
"ipfs-core-types": "^0.9.0",
7577
"ipfs-unixfs": "^6.0.3",

packages/interface-ipfs-core/src/dag/get.js

+80
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
44
import * as dagPB from '@ipld/dag-pb'
55
import * as dagCBOR from '@ipld/dag-cbor'
6+
import * as dagJOSE from 'dag-jose'
67
import { importer } from 'ipfs-unixfs-importer'
78
import { UnixFS } from 'ipfs-unixfs'
89
import all from 'it-all'
910
import { CID } from 'multiformats/cid'
1011
import { sha256 } from 'multiformats/hashes/sha2'
1112
import { base32 } from 'multiformats/bases/base32'
13+
import { base64url } from 'multiformats/bases/base64'
1214
import { expect } from 'aegir/utils/chai.js'
1315
import { getDescribe, getIt } from '../utils/mocha.js'
1416
import testTimeout from '../utils/test-timeout.js'
1517
import { identity } from 'multiformats/hashes/identity'
1618
import blockstore from '../utils/blockstore-adapter.js'
19+
import { ES256KSigner, createJWS } from 'did-jwt'
1720

1821
/**
1922
* @typedef {import('ipfsd-ctl').Factory} Factory
@@ -42,6 +45,10 @@ export function testGet (factory, options) {
4245
* @type {any}
4346
*/
4447
let cborNode
48+
/**
49+
* @type {dagJOSE.DagJWE}
50+
*/
51+
let joseNode
4552
/**
4653
* @type {dagPB.PBNode}
4754
*/
@@ -50,6 +57,10 @@ export function testGet (factory, options) {
5057
* @type {any}
5158
*/
5259
let nodeCbor
60+
/**
61+
* @type {string}
62+
*/
63+
let nodeJose
5364
/**
5465
* @type {CID}
5566
*/
@@ -58,6 +69,10 @@ export function testGet (factory, options) {
5869
* @type {CID}
5970
*/
6071
let cidCbor
72+
/**
73+
* @type {CID}
74+
*/
75+
let cidJose
6176

6277
before(async () => {
6378
const someData = uint8ArrayFromString('some other data')
@@ -68,6 +83,12 @@ export function testGet (factory, options) {
6883
cborNode = {
6984
data: someData
7085
}
86+
joseNode = {
87+
protected: 'eyJhbGciOiJkaXIiLCJlbmMiOiJYQzIwUCJ9',
88+
iv: 'DhVb9URR_o_85MOl-hCellwPTtQ_dj6d',
89+
ciphertext: 'EtUsNJcKzEKdFM9DW5Ua5tVyaQRCKsAD',
90+
tag: '-vG17pRSVB2Vycf2MZRgBA'
91+
}
7192

7293
nodePb = {
7394
Data: uint8ArrayFromString('I am inside a Protobuf'),
@@ -83,6 +104,11 @@ export function testGet (factory, options) {
83104

84105
await ipfs.dag.put(nodePb, { storeCodec: 'dag-pb', hashAlg: 'sha2-256' })
85106
await ipfs.dag.put(nodeCbor, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
107+
108+
const signer = ES256KSigner('278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f')
109+
nodeJose = await createJWS(base64url.encode(cidCbor.bytes).slice(1), signer)
110+
cidJose = CID.createV1(dagJOSE.code, await sha256.digest(dagJOSE.encode(nodeJose)))
111+
await ipfs.dag.put(nodeJose, { storeCodec: dagJOSE.name, hashAlg: 'sha2-256' })
86112
})
87113

88114
it('should respect timeout option when getting a DAG node', () => {
@@ -158,6 +184,14 @@ export function testGet (factory, options) {
158184
it.skip('should get dag-cbor node value two levels deep', (done) => {})
159185
it.skip('should get dag-cbor value via dag-pb node', (done) => {})
160186

187+
it('should get only a CID, due to resolving locally only', async function () {
188+
const result = await ipfs.dag.get(cidCbor, {
189+
path: 'pb/Data',
190+
localResolve: true
191+
})
192+
expect(result.value.equals(cidPb)).to.be.true()
193+
})
194+
161195
it('should get dag-pb value via dag-cbor node', async function () {
162196
const result = await ipfs.dag.get(cidCbor, {
163197
path: 'pb/Data'
@@ -305,5 +339,51 @@ export function testGet (factory, options) {
305339

306340
expect(atPath).to.have.deep.property('value').that.is.an.instanceOf(CID)
307341
})
342+
343+
it('should get a dag-jose node', async () => {
344+
const cid = await ipfs.dag.put(joseNode, {
345+
storeCodec: 'dag-jose',
346+
hashAlg: 'sha2-256'
347+
})
348+
349+
const result = await ipfs.dag.get(cid)
350+
351+
const node = result.value
352+
expect(joseNode).to.eql(node)
353+
})
354+
355+
it('should get a dag-jose node with path', async () => {
356+
const result = await ipfs.dag.get(cidJose, {
357+
path: '/'
358+
})
359+
360+
const node = result.value
361+
362+
const cid = CID.createV1(dagJOSE.code, await sha256.digest(dagJOSE.encode(node)))
363+
expect(cid.equals(cidJose)).to.be.true()
364+
})
365+
366+
it('should get a dag-jose node local value', async () => {
367+
const result = await ipfs.dag.get(cidJose, {
368+
path: 'payload'
369+
})
370+
const converted = dagJOSE.toGeneral(nodeJose)
371+
expect(result.value).to.eql('payload' in converted && converted.payload)
372+
})
373+
374+
it('should get dag-cbor value via dag-jose node', async function () {
375+
const result = await ipfs.dag.get(cidJose, {
376+
path: 'link/someData'
377+
})
378+
expect(result.value).to.eql('I am inside a Cbor object')
379+
})
380+
381+
it('should get dag-cbor cid via dag-jose node if local resolve', async function () {
382+
const result = await ipfs.dag.get(cidJose, {
383+
path: 'link',
384+
localResolve: true
385+
})
386+
expect(result.value).to.eql(cidCbor)
387+
})
308388
})
309389
}

packages/interface-ipfs-core/src/dag/put.js

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function testPut (factory, options) {
3434
const cborNode = {
3535
data: uint8ArrayFromString('some other data')
3636
}
37+
const joseNode = 'eyJhbGciOiJFUzI1NksifQ.AXESICjDGMg3fEBSX7_fpbBUYF4E61TXLysmLJgfGEpFG8Pu.z7a2MvPWLsd7leOeHyfeA1OcAFC9yy5rn1HD8xCeHz3nFrwyn_Su5xXUoaIxAre3fXhGjPkVSNiCE36AKiaMng'
3738

3839
it('should put dag-pb with default hash func (sha2-256)', () => {
3940
return ipfs.dag.put(pbNode, {
@@ -63,6 +64,20 @@ export function testPut (factory, options) {
6364
})
6465
})
6566

67+
it('should put dag-jose with default hash func (sha2-256)', () => {
68+
return ipfs.dag.put(joseNode, {
69+
storeCodec: 'dag-jose',
70+
hashAlg: 'sha2-256'
71+
})
72+
})
73+
74+
it('should put dag-jose with non-default hash func (sha2-512)', () => {
75+
return ipfs.dag.put(joseNode, {
76+
storeCodec: 'dag-jose',
77+
hashAlg: 'sha2-512'
78+
})
79+
})
80+
6681
it('should return the cid', async () => {
6782
const cid = await ipfs.dag.put(cborNode, {
6883
storeCodec: 'dag-cbor',

packages/ipfs-core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"array-shuffle": "^2.0.0",
7878
"blockstore-core": "^1.0.2",
7979
"blockstore-datastore-adapter": "^2.0.2",
80+
"dag-jose": "^1.0.0",
8081
"datastore-core": "^7.0.0",
8182
"datastore-pubsub": "^2.0.0",
8283
"debug": "^4.1.1",

packages/ipfs-core/src/components/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { UnixFS } from 'ipfs-unixfs'
66
import * as dagPB from '@ipld/dag-pb'
77
import * as dagCBOR from '@ipld/dag-cbor'
88
import * as dagJSON from '@ipld/dag-json'
9+
import * as dagJOSE from 'dag-jose'
910
import { identity } from 'multiformats/hashes/identity'
1011
import { bases, hashes, codecs } from 'multiformats/basics'
1112
import { initAssets } from 'ipfs-core-config/init-assets'
@@ -289,7 +290,7 @@ export async function create (options = {}) {
289290
/** @type {BlockCodec[]} */
290291
const blockCodecs = Object.values(codecs);
291292

292-
[dagPB, dagCBOR, dagJSON, id].concat((options.ipld && options.ipld.codecs) || []).forEach(codec => blockCodecs.push(codec))
293+
[dagPB, dagCBOR, dagJSON, dagJOSE, id].concat((options.ipld && options.ipld.codecs) || []).forEach(codec => blockCodecs.push(codec))
293294

294295
const multicodecs = new Multicodecs({
295296
codecs: blockCodecs,

packages/ipfs-http-client/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@ipld/dag-json": "^8.0.1",
6060
"@ipld/dag-pb": "^2.1.3",
6161
"any-signal": "^3.0.0",
62+
"dag-jose": "^1.0.0",
6263
"debug": "^4.1.1",
6364
"err-code": "^3.0.1",
6465
"ipfs-core-types": "^0.9.0",

packages/ipfs-http-client/src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Multihashes } from 'ipfs-core-utils/multihashes'
66
import * as dagPB from '@ipld/dag-pb'
77
import * as dagCBOR from '@ipld/dag-cbor'
88
import * as dagJSON from '@ipld/dag-json'
9+
import * as dagJOSE from 'dag-jose'
910
import { identity } from 'multiformats/hashes/identity'
1011
import { bases, hashes, codecs } from 'multiformats/basics'
1112
import { createBitswap } from './bitswap/index.js'
@@ -80,7 +81,7 @@ export function create (options = {}) {
8081
/** @type {BlockCodec[]} */
8182
const blockCodecs = Object.values(codecs);
8283

83-
[dagPB, dagCBOR, dagJSON, id].concat((options.ipld && options.ipld.codecs) || []).forEach(codec => blockCodecs.push(codec))
84+
[dagPB, dagCBOR, dagJSON, dagJOSE, id].concat((options.ipld && options.ipld.codecs) || []).forEach(codec => blockCodecs.push(codec))
8485

8586
const multicodecs = new Multicodecs({
8687
codecs: blockCodecs,

0 commit comments

Comments
 (0)