From 6cb082e2c3af8dc0ba06b4265886dc5b823974ac Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Thu, 16 Feb 2023 16:23:16 +0100 Subject: [PATCH] Parse DAG-JSON CIDs (#496) * Try parsing legacy skeleton CID * Improve comment * Write test for DAG-JSON cid * Bump version * Update changelog * Improve CID test --- CHANGELOG.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- src/common/cid.node.test.ts | 14 ++++++++++++++ src/common/cid.ts | 12 ++++++++++++ src/common/version.ts | 2 +- 6 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 src/common/cid.node.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e060631b4..56c3cab11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/package-lock.json b/package-lock.json index 350b95a51..fedb43e3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "webnative", - "version": "0.36.2", + "version": "0.36.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "webnative", - "version": "0.36.2", + "version": "0.36.3", "license": "Apache-2.0", "dependencies": { "@ipld/dag-cbor": "^8.0.0", diff --git a/package.json b/package.json index 066286a7f..58e4eec8b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webnative", - "version": "0.36.2", + "version": "0.36.3", "description": "Webnative SDK", "keywords": [ "WebCrypto", diff --git a/src/common/cid.node.test.ts b/src/common/cid.node.test.ts new file mode 100644 index 000000000..3415c461d --- /dev/null +++ b/src/common/cid.node.test.ts @@ -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) + }) + +}) \ No newline at end of file diff --git a/src/common/cid.ts b/src/common/cid.ts index 07087c1f4..ff51683b5 100644 --- a/src/common/cid.ts +++ b/src/common/cid.ts @@ -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") && @@ -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)}`) } diff --git a/src/common/version.ts b/src/common/version.ts index e43ea63fb..2ad942a8a 100644 --- a/src/common/version.ts +++ b/src/common/version.ts @@ -1,2 +1,2 @@ -export const VERSION = "0.36.2" +export const VERSION = "0.36.3" export const WASM_WNFS_VERSION = "0.1.7"