Skip to content

Commit

Permalink
Parse DAG-JSON CIDs (#496)
Browse files Browse the repository at this point in the history
* Try parsing legacy skeleton CID

* Improve comment

* Write test for DAG-JSON cid

* Bump version

* Update changelog

* Improve CID test
  • Loading branch information
icidasset committed Feb 16, 2023
1 parent 3ae07b8 commit 6cb082e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### V0.36.3

Now parses DAG-JSON formatted CIDs.

### v0.36.2

Allow symlinks to other file systems to be created.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webnative",
"version": "0.36.2",
"version": "0.36.3",
"description": "Webnative SDK",
"keywords": [
"WebCrypto",
Expand Down
14 changes: 14 additions & 0 deletions src/common/cid.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import expect from "expect"
import { decodeCID } from "./cid.js"


describe("CIDs", () => {

it("decodes DAG-JSON cids", () => {
const CID_STRING = "bafkreicu646jao2xjpkbmk3buom6hmxsexmbwyju22k6wmtnky2ljisv3e"
const cidInstance = decodeCID({ "/": CID_STRING })

expect(cidInstance.toString()).toEqual(CID_STRING)
})

})
12 changes: 12 additions & 0 deletions src/common/cid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function decodeCID(val: CID | object | string): CID {
return CID.create(val.version, val.code, val.multihash)
}

// Older version of the above
if (
typeof val === "object" &&
hasProp(val, "version") &&
Expand All @@ -43,6 +44,17 @@ export function decodeCID(val: CID | object | string): CID {
return CID.create(val.version as Version, val.code, multihash)
}

// Sometimes we can encounter a DAG-JSON encoded CID
// https://github.com/fission-codes/webnative/issues/459
// Related to the `ensureSkeletonStringCIDs` function in the `PrivateTree` class
if (
typeof val === "object" &&
hasProp(val, "/") &&
typeof val[ "/" ] === "string"
) {
return CID.parse(val[ "/" ])
}

// Unrecognisable CID
throw new Error(`Could not decode CID: ${JSON.stringify(val)}`)
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const VERSION = "0.36.2"
export const VERSION = "0.36.3"
export const WASM_WNFS_VERSION = "0.1.7"

0 comments on commit 6cb082e

Please sign in to comment.