Skip to content

Commit

Permalink
nfts-upload.spec creates real values for W3_NFTSTORAGE envs
Browse files Browse the repository at this point in the history
  • Loading branch information
gobengo committed Mar 21, 2024
1 parent d9b6b29 commit 06b0718
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
33 changes: 32 additions & 1 deletion packages/api/src/utils/w3up.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { StoreMemory } from '@web3-storage/access/stores/store-memory'
import { CID } from 'multiformats/cid'
import { base64 } from 'multiformats/bases/base64'
import { identity } from 'multiformats/hashes/identity'
import { CarReader } from '@ipld/car'
import { CarReader, CarWriter } from '@ipld/car'
import { importDAG } from '@ucanto/core/delegation'
import * as ucanto from '@ucanto/core'

/**
* @param {object} env
Expand Down Expand Up @@ -70,3 +71,33 @@ export async function readProofFromBytes(bytes) {
throw err
}
}

/**
* @param {import('@ucanto/interface').Delegation} delegation - delegation to encode
*/
export async function encodeDelegationAsCid(delegation) {
const { writer, out } = CarWriter.create()
/** @type {Array<Uint8Array>} */
const carChunks = []
await Promise.all([
// write delegation blocks
(async () => {
for (const block of delegation.export()) {
// @ts-expect-error different Block types
await writer.put(block)
}
await writer.close()
})(),
// read out
(async () => {
for await (const chunk of out) {
carChunks.push(chunk)
}
})(),
])
// now get car chunks
const car = new Blob(carChunks)
const bytes = new Uint8Array(await car.arrayBuffer())
const cid = CID.createV1(ucanto.CAR.code, identity.digest(bytes))
return cid
}
22 changes: 19 additions & 3 deletions packages/api/test/nfts-upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import path from 'node:path'
import { FormData } from 'undici'
import { createCarCid } from '../src/utils/car.js'
import { createServer } from 'node:http'
import { ed25519 } from '@ucanto/principal'
import { delegate } from '@ucanto/core'
import { encodeDelegationAsCid } from '../src/utils/w3up.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

const nftStorageSpace = ed25519.generate()
const nftStorageApiPrincipal = ed25519.generate()
const mockW3up = createListeningMockW3up()

test.before(async (t) => {
Expand All @@ -38,9 +43,20 @@ test.before(async (t) => {
overrides: {
LINKDEX_URL: linkdexUrl,
W3UP_URL: (await mockW3up).url.toString(),
W3_NFTSTORAGE_SPACE: `did:key:zTodo`,
W3_NFTSTORAGE_PRINCIPAL: 'zTodo',
W3_NFTSTORAGE_PROOF: 'zTodo',
W3_NFTSTORAGE_SPACE: (await nftStorageSpace).did(),
W3_NFTSTORAGE_PRINCIPAL: (await nftStorageApiPrincipal).did(),
W3_NFTSTORAGE_PROOF: (
await encodeDelegationAsCid(
await delegate({
issuer: await nftStorageSpace,
audience: await nftStorageApiPrincipal,
capabilities: [
{ can: 'store/add', with: (await nftStorageSpace).did() },
{ can: 'upload/add', with: (await nftStorageSpace).did() },
],
})
)
).toString(),
},
})
})
Expand Down

0 comments on commit 06b0718

Please sign in to comment.