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

Generate CID #83

Open
jasny opened this issue Jun 19, 2023 · 4 comments
Open

Generate CID #83

jasny opened this issue Jun 19, 2023 · 4 comments
Milestone

Comments

@jasny
Copy link
Member

jasny commented Jun 19, 2023

There are issues with trying to use IPFS packages blockstore-core and ipfs-unixfs-importer in the react native app.

  • package.json isn't exported
warn Package blockstore-core has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in /home/arnold/Projects/ltonetwork/universal-wallet/node_modules/blockstore-core/package.json
  • It's complaining about main for these and other packages.
  • Submodules in defined in exports of package.json aren't being loaded.
@jasny
Copy link
Member Author

jasny commented Jun 19, 2023

utils/calculateCid.ts

import {BackHoleBlockstore} from "blockstore-core/black-hole"
import {importer} from "ipfs-unixfs-importer"

export default async function calculateCid(files: Array<{name: string, read: () => Promise<Uint8Array>}>): Promise<string> {
  const source = await Promise.all(
    files.map(async file => ({
      path: `./${file.name}`,
      content: await file.read(),
    }))
  )

  const blockstore = new BackHoleBlockstore()

  for await (const entry of importer(source, blockstore)) {
    if (entry.path === '' && entry.unixfs?.type === 'directory') return entry.cid.toString()
  }

  throw new Error("Failed to calculate directory CID: importer did not find a directory entry in the input files")
}

@jasny
Copy link
Member Author

jasny commented Jun 19, 2023

I tried patching package.json, which worked up to a point. But the rabbit hole was too deep.

It might be necessary to fork the packages and modify them. We can submit a pull request and use our fork in the meantime. The problem also occurs with some of the dependencies

https://www.npmjs.com/package/ipfs-unixfs-importer?activeTab=dependencies

jasny added a commit that referenced this issue Jun 19, 2023
@jasny jasny added this to the Ownables milestone Jun 28, 2023
@jasny
Copy link
Member Author

jasny commented Jul 7, 2023

@jasny
Copy link
Member Author

jasny commented Jul 7, 2023

Start with importing the multiformats library (which has no dependencies).

Copy/paste this code from the README and create a method in utils/calculateCid.

import { CID } from 'multiformats/cid'
import * as json from 'multiformats/codecs/json'
import { sha256 } from 'multiformats/hashes/sha2'

export function cidFromData(data: any) {
  const bytes = json.encode(data)

  const hash = await sha256.digest(bytes)
  return CID.create(1, json.code, hash)
}

Try to build the project for Android (yarn android) with this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant