diff --git a/packages/core/src/delegation.js b/packages/core/src/delegation.js index 54b577c9..2eb14883 100644 --- a/packages/core/src/delegation.js +++ b/packages/core/src/delegation.js @@ -228,18 +228,20 @@ export class Delegation { return data } /** - * Attach a block to the delegation DAG so it would be included in the - * block iterator. + * Attach blocks from IPLD view to the delegation DAG so it would be included + * in the block iterator. * ⚠️ You can only attach blocks that are referenced from the `capabilities` * or `facts`. * - * @param {API.Block} block + * @param {API.IPLDView} view */ - attach(block) { - if (!this.attachedLinks.has(`${block.cid.link()}`)) { - throw new Error(`given block with ${block.cid} is not an attached link`) + attach(view) { + for (const block of view.iterateIPLDBlocks()) { + if (!this.attachedLinks.has(`${block.cid.link()}`)) { + throw new Error(`given block with ${block.cid} is not an attached link`) + } + this.blocks.set(`${block.cid}`, block) } - this.blocks.set(`${block.cid}`, block) } export() { return exportDAG(this.root, this.blocks, this.attachedLinks) @@ -407,9 +409,11 @@ export const delegate = async ( const delegation = new Delegation({ cid, bytes }, blocks) Object.defineProperties(delegation, { proofs: { value: proofs } }) - for (const block of attachedBlocks.values()) { - delegation.attach(block) - } + const attached = Array.from(attachedBlocks.values()) + attached.length && delegation.attach({ + iterateIPLDBlocks: () => attachedBlocks.values(), + root: attached[0] + }) return delegation } diff --git a/packages/core/src/invocation.js b/packages/core/src/invocation.js index 819adafb..1a20506c 100644 --- a/packages/core/src/invocation.js +++ b/packages/core/src/invocation.js @@ -87,16 +87,17 @@ class IssuedInvocation { } /** - * Attach a block to the invocation DAG so it would be included in the - * block iterator. + * Attach block from IPLD View to the invocation DAG so it would be included + * in the block iterator. * ⚠️ You should only attach blocks that are referenced from the `capabilities` - * or `facts`, if that is not the case you probably should reconsider. - * ⚠️ Once a delegation is de-serialized the attached blocks will not be re-attached. + * or `facts`. * - * @param {API.Block} block + * @param {API.IPLDView} view */ - attach(block) { - this.attachedBlocks.set(`${block.cid}`, block) + attach(view) { + for (const block of view.iterateIPLDBlocks()) { + this.attachedBlocks.set(`${block.cid}`, block) + } } delegate() { diff --git a/packages/core/test/delegation.spec.js b/packages/core/test/delegation.spec.js index af75f9e6..c136d27b 100644 --- a/packages/core/test/delegation.spec.js +++ b/packages/core/test/delegation.spec.js @@ -306,7 +306,10 @@ test('delegation.attach block in capabiliy', async () => { ], }) - ucan.attach(block) + ucan.attach({ + iterateIPLDBlocks: () => [block].values(), + root: block + }) const delegationBlocks = [] for (const b of ucan.iterateIPLDBlocks()) { @@ -334,7 +337,10 @@ test('delegation.attach block in facts', async () => { ] }) - ucan.attach(block) + ucan.attach({ + iterateIPLDBlocks: () => [block].values(), + root: block + }) const delegationBlocks = [] for (const b of ucan.iterateIPLDBlocks()) { @@ -357,5 +363,8 @@ test('delegation.attach fails to attach block with not attached link', async () }) const block = await getBlock({ test: 'inlineBlock' }) - assert.throws(() => ucan.attach(block)) + assert.throws(() => ucan.attach({ + iterateIPLDBlocks: () => [block].values(), + root: block + })) }) \ No newline at end of file diff --git a/packages/core/test/invocation.spec.js b/packages/core/test/invocation.spec.js index e209a849..8b66e148 100644 --- a/packages/core/test/invocation.spec.js +++ b/packages/core/test/invocation.spec.js @@ -47,7 +47,10 @@ test('encode invocation with attached block in capability nb', async () => { }, proofs: [], }) - add.attach(block) + add.attach({ + iterateIPLDBlocks: () => [block].values(), + root: block + }) /** @type {import('@ucanto/interface').BlockStore} */ const blockStore = new Map() diff --git a/packages/interface/src/lib.ts b/packages/interface/src/lib.ts index 9d9a3950..7ea32a1f 100644 --- a/packages/interface/src/lib.ts +++ b/packages/interface/src/lib.ts @@ -246,7 +246,7 @@ export interface Delegation toJSON(): DelegationJSON delegate(): Await> - attach(block: Block): void + attach(block: IPLDView): void } /** @@ -549,7 +549,7 @@ export interface IssuedInvocation readonly proofs: Proof[] delegate(): Await> - attach(block: Block): void + attach(block: IPLDView): void } export type ServiceInvocation<