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

Commit

Permalink
feat: add support for dag-jose codec (#4028)
Browse files Browse the repository at this point in the history
`dag-jose` is now supported out of the box without any other configuration required.

Closes: ceramicnetwork/.github#207

Co-authored-by: Joel Thorstensson <oed@3box.io>
Co-authored-by: Joel Thorstensson <joel@oed.earth>
Co-authored-by: stephhuynh18 <steph.huynh18@gmail.com>
  • Loading branch information
4 people committed Jan 27, 2022
1 parent a6b201a commit fbe1492
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 12 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,10 @@ jobs:
with:
build: |
npm run link
- run: |
npm install
npm run build
npm run link
npm run test:interop -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -- -t ${{ matrix.type }}
- run: npm install
- run: npm run build
- run: npm run link
- run: npm run test:interop -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -- -t ${{ matrix.type }}
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
flags: interop-${{ matrix.type }}
Expand All @@ -241,11 +240,10 @@ jobs:
- uses: actions/setup-node@v2
with:
node-version: lts/*
- run: |
npm install
npm run build
npm run link
npm run ${{ matrix.suite }} -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -t ${{ matrix.type }}
- run: npm install
- run: npm run build
- run: npm run link
- run: npm run ${{ matrix.suite }} -- --since ${{ github.event.pull_request.base.sha }} --concurrency 1 -- -- -t ${{ matrix.type }}
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
flags: interface-${{ matrix.type }}
Expand Down
2 changes: 2 additions & 0 deletions packages/interface-ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
"aegir": "^36.0.1",
"blockstore-core": "^1.0.2",
"copyfiles": "^2.4.1",
"dag-jose": "^1.0.0",
"delay": "^5.0.0",
"did-jwt": "^5.12.1",
"err-code": "^3.0.1",
"ipfs-core-types": "^0.9.0",
"ipfs-unixfs": "^6.0.3",
Expand Down
80 changes: 80 additions & 0 deletions packages/interface-ipfs-core/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import * as dagPB from '@ipld/dag-pb'
import * as dagCBOR from '@ipld/dag-cbor'
import * as dagJOSE from 'dag-jose'
import { importer } from 'ipfs-unixfs-importer'
import { UnixFS } from 'ipfs-unixfs'
import all from 'it-all'
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'
import { base32 } from 'multiformats/bases/base32'
import { base64url } from 'multiformats/bases/base64'
import { expect } from 'aegir/utils/chai.js'
import { getDescribe, getIt } from '../utils/mocha.js'
import testTimeout from '../utils/test-timeout.js'
import { identity } from 'multiformats/hashes/identity'
import blockstore from '../utils/blockstore-adapter.js'
import { ES256KSigner, createJWS } from 'did-jwt'

/**
* @typedef {import('ipfsd-ctl').Factory} Factory
Expand Down Expand Up @@ -42,6 +45,10 @@ export function testGet (factory, options) {
* @type {any}
*/
let cborNode
/**
* @type {dagJOSE.DagJWE}
*/
let joseNode
/**
* @type {dagPB.PBNode}
*/
Expand All @@ -50,6 +57,10 @@ export function testGet (factory, options) {
* @type {any}
*/
let nodeCbor
/**
* @type {string}
*/
let nodeJose
/**
* @type {CID}
*/
Expand All @@ -58,6 +69,10 @@ export function testGet (factory, options) {
* @type {CID}
*/
let cidCbor
/**
* @type {CID}
*/
let cidJose

before(async () => {
const someData = uint8ArrayFromString('some other data')
Expand All @@ -68,6 +83,12 @@ export function testGet (factory, options) {
cborNode = {
data: someData
}
joseNode = {
protected: 'eyJhbGciOiJkaXIiLCJlbmMiOiJYQzIwUCJ9',
iv: 'DhVb9URR_o_85MOl-hCellwPTtQ_dj6d',
ciphertext: 'EtUsNJcKzEKdFM9DW5Ua5tVyaQRCKsAD',
tag: '-vG17pRSVB2Vycf2MZRgBA'
}

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

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

const signer = ES256KSigner('278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f')
nodeJose = await createJWS(base64url.encode(cidCbor.bytes).slice(1), signer)
cidJose = CID.createV1(dagJOSE.code, await sha256.digest(dagJOSE.encode(nodeJose)))
await ipfs.dag.put(nodeJose, { storeCodec: dagJOSE.name, hashAlg: 'sha2-256' })
})

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

it('should get only a CID, due to resolving locally only', async function () {
const result = await ipfs.dag.get(cidCbor, {
path: 'pb/Data',
localResolve: true
})
expect(result.value.equals(cidPb)).to.be.true()
})

it('should get dag-pb value via dag-cbor node', async function () {
const result = await ipfs.dag.get(cidCbor, {
path: 'pb/Data'
Expand Down Expand Up @@ -305,5 +339,51 @@ export function testGet (factory, options) {

expect(atPath).to.have.deep.property('value').that.is.an.instanceOf(CID)
})

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

const result = await ipfs.dag.get(cid)

const node = result.value
expect(joseNode).to.eql(node)
})

it('should get a dag-jose node with path', async () => {
const result = await ipfs.dag.get(cidJose, {
path: '/'
})

const node = result.value

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

it('should get a dag-jose node local value', async () => {
const result = await ipfs.dag.get(cidJose, {
path: 'payload'
})
const converted = dagJOSE.toGeneral(nodeJose)
expect(result.value).to.eql('payload' in converted && converted.payload)
})

it('should get dag-cbor value via dag-jose node', async function () {
const result = await ipfs.dag.get(cidJose, {
path: 'link/someData'
})
expect(result.value).to.eql('I am inside a Cbor object')
})

it('should get dag-cbor cid via dag-jose node if local resolve', async function () {
const result = await ipfs.dag.get(cidJose, {
path: 'link',
localResolve: true
})
expect(result.value).to.eql(cidCbor)
})
})
}
15 changes: 15 additions & 0 deletions packages/interface-ipfs-core/src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function testPut (factory, options) {
const cborNode = {
data: uint8ArrayFromString('some other data')
}
const joseNode = 'eyJhbGciOiJFUzI1NksifQ.AXESICjDGMg3fEBSX7_fpbBUYF4E61TXLysmLJgfGEpFG8Pu.z7a2MvPWLsd7leOeHyfeA1OcAFC9yy5rn1HD8xCeHz3nFrwyn_Su5xXUoaIxAre3fXhGjPkVSNiCE36AKiaMng'

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

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

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

it('should return the cid', async () => {
const cid = await ipfs.dag.put(cborNode, {
storeCodec: 'dag-cbor',
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"array-shuffle": "^2.0.0",
"blockstore-core": "^1.0.2",
"blockstore-datastore-adapter": "^2.0.2",
"dag-jose": "^1.0.0",
"datastore-core": "^7.0.0",
"datastore-pubsub": "^2.0.0",
"debug": "^4.1.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-core/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UnixFS } from 'ipfs-unixfs'
import * as dagPB from '@ipld/dag-pb'
import * as dagCBOR from '@ipld/dag-cbor'
import * as dagJSON from '@ipld/dag-json'
import * as dagJOSE from 'dag-jose'
import { identity } from 'multiformats/hashes/identity'
import { bases, hashes, codecs } from 'multiformats/basics'
import { initAssets } from 'ipfs-core-config/init-assets'
Expand Down Expand Up @@ -289,7 +290,7 @@ export async function create (options = {}) {
/** @type {BlockCodec[]} */
const blockCodecs = Object.values(codecs);

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

const multicodecs = new Multicodecs({
codecs: blockCodecs,
Expand Down
1 change: 1 addition & 0 deletions packages/ipfs-http-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@ipld/dag-json": "^8.0.1",
"@ipld/dag-pb": "^2.1.3",
"any-signal": "^3.0.0",
"dag-jose": "^1.0.0",
"debug": "^4.1.1",
"err-code": "^3.0.1",
"ipfs-core-types": "^0.9.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/ipfs-http-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Multihashes } from 'ipfs-core-utils/multihashes'
import * as dagPB from '@ipld/dag-pb'
import * as dagCBOR from '@ipld/dag-cbor'
import * as dagJSON from '@ipld/dag-json'
import * as dagJOSE from 'dag-jose'
import { identity } from 'multiformats/hashes/identity'
import { bases, hashes, codecs } from 'multiformats/basics'
import { createBitswap } from './bitswap/index.js'
Expand Down Expand Up @@ -80,7 +81,7 @@ export function create (options = {}) {
/** @type {BlockCodec[]} */
const blockCodecs = Object.values(codecs);

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

const multicodecs = new Multicodecs({
codecs: blockCodecs,
Expand Down

0 comments on commit fbe1492

Please sign in to comment.