Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store metadata in IPLD CBOR format #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,28 @@
"prepare": "npm run build"
},
"dependencies": {
"@ipld/dag-cbor": "^4.0.0",
"@web-std/blob": "2.0.1",
"@web-std/fetch": "1.0.0",
"@web-std/file": "1.0.1",
"@web-std/form-data": "2.0.0"
},
"devDependencies": {
"@ssttevee/multipart-parser": "0.1.8",
"uvu": "0.5.1",
"mocha": "8.3.2",
"@types/mocha": "8.2.1",
"ipfs-unixfs-importer": "6.0.1",
"ipld": "0.29.0",
"ipld-dag-pb": "0.22.0",
"ipld-in-memory": "8.0.0",
"mocha": "8.3.2",
"multicodec": "3.0.1",
"multiformats": "4.5.3",
"multiformats": "^4.5.3",
"multihashing-async": "2.1.2",
"playwright-test": "2.1.0",
"typedoc": "0.20.32",
"rollup": "2.22.1",
"rollup-plugin-multi-input": "1.1.1"
"rollup-plugin-multi-input": "1.1.1",
"typedoc": "0.20.32",
"uvu": "0.5.1"
},
"homepage": "https://github.com/ipfs-shipyard/nft.storage/tree/main/client",
"bugs": "https://github.com/ipfs-shipyard/nft.storage/issues"
Expand Down
42 changes: 42 additions & 0 deletions client/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @module
*/

import cbor from '@ipld/dag-cbor'
import { CID } from 'multiformats'
import * as API from './lib/interface.js'
import { fetch, File, Blob, FormData } from './platform.js'

Expand Down Expand Up @@ -112,6 +114,39 @@ class NFTStorage {
}
}

/**
* @param {API.Service} service
* @param {any} metadata
* @returns {Promise<API.CIDString>}
*/
static async storeMetadata(service, metadata) {
/**
* @param {any} obj
* @returns {Promise<any>}
*/
async function transform (obj) {
if (Array.isArray(obj)) {
return Promise.all(obj.map(transform))
}
if (obj instanceof File) {
return NFTStorage.storeDirectory(service, [obj])
}
if (obj instanceof Blob) {
return NFTStorage.storeBlob(service, obj)
}
if (typeof obj === 'object') {
const ents = await Promise.all(Object.entries(obj).map(async ([k, v]) => ([k, await transform(v)])))
return Object.fromEntries(ents)
}
return obj
}
metadata = await transform(metadata)
const cid = await NFTStorage.storeBlob(service, new Blob([cbor.encode(metadata)]))
console.log(cid)
const mh = CID.parse(cid).multihash
return CID.createV1(cbor.code, mh).toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool! I was thinking of doing something slightly different that is:

  1. Create a content bundle like a car file so that
  2. It would inline all the files
  3. A JSON serialization written as metadata.json
  4. A dag-cbor with pointers to all of the above (kind of like you do here)

Then we could turn return value into:

{
   cid: CIDString // CID to the cbor with links to everything
   jsonURL: URL // ipfs://${cid} URL to a metadata.json
   data: JSON // JSON that written as `metadata.json`
}

That way:

  1. Single NFT will correspond to single dag-cbor with links to all the relevant files and encoding al data.
  2. We don't force IPLD, JSON representation of it is still there.
  3. We do also proactively make things IPLD native, and lay the upgrade path to folks that want to.
  4. Since we have all the data locally anyway, we include that in data so that URLs for all the files can be pulled out without another roundtrip.

}

/**
* @param {API.Service} service
* @param {string} cid
Expand Down Expand Up @@ -175,6 +210,13 @@ class NFTStorage {
storeBlob(blob) {
return NFTStorage.storeBlob(this, blob)
}
/**
* Stores a metadata object as an IPLD CBOR encoded DAG.
* @param {any} metadata
*/
storeMetadata(metadata) {
return NFTStorage.storeMetadata(this, metadata)
}
/**
* Stores a directory of files and returns a CID for the directory.
*
Expand Down