From a95eea4ff38b7aff86a61557f8cccb2145052106 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 24 Nov 2022 17:19:25 +0000 Subject: [PATCH] fix: add proof of voucher redeem (#213) Ported change from https://github.com/web3-storage/w3protocol/pull/207/files#diff-69a4efe733b2d7920dc103a0370eb1a285403e39c41e1b5e9a0718ea66b5a32fR445 --- packages/access-client/src/agent.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/access-client/src/agent.js b/packages/access-client/src/agent.js index ecaf09b09..ca92c4dbb 100644 --- a/packages/access-client/src/agent.js +++ b/packages/access-client/src/agent.js @@ -319,11 +319,16 @@ export class Agent { async registerSpace(email, opts) { const space = this.currentSpace() const service = await this.service() + const spaceMeta = space ? this.data.spaces.get(space) : undefined - if (!space) { + if (!space || !spaceMeta) { throw new Error('No space selected') } + if (spaceMeta && spaceMeta.isRegistered) { + throw new Error('Space already registered with web3.storage.') + } + const inv = await this.invokeAndExecute(Voucher.claim, { nb: { identity: URI.from(`mailto:${email}`), @@ -337,9 +342,11 @@ export class Agent { } const voucherRedeem = await this.#waitForVoucherRedeem(opts) + await this.addProof(voucherRedeem) const delegationToService = await this.delegate({ abilities: ['*'], audience: service, + expiration: Infinity, audienceMeta: { name: 'w3access', type: 'service', @@ -360,7 +367,12 @@ export class Agent { throw new Error('Space registration failed', { cause: accInv }) } - await this.addProof(voucherRedeem) + spaceMeta.isRegistered = true + + this.data.spaces.set(space, spaceMeta) + this.data.delegations.delete(voucherRedeem.cid.toString()) + + this.store.save(this.data) } /**