From 545b1489be2b98d4aee08ba7e9366b69de886082 Mon Sep 17 00:00:00 2001 From: Fernando Gonzalez Goncharov Date: Thu, 5 Dec 2024 16:29:25 +0200 Subject: [PATCH] feat(client-sdk): update grants var names --- packages/idos-sdk-js/src/lib/grants/evm.ts | 80 ++++++++++++------- .../idos-sdk-js/src/lib/grants/grant-child.ts | 4 +- packages/idos-sdk-js/src/lib/grants/grant.ts | 10 +-- packages/idos-sdk-js/src/lib/grants/grants.ts | 38 +++++---- packages/idos-sdk-js/src/lib/grants/near.ts | 62 +++++++------- 5 files changed, 113 insertions(+), 81 deletions(-) diff --git a/packages/idos-sdk-js/src/lib/grants/evm.ts b/packages/idos-sdk-js/src/lib/grants/evm.ts index c3661cd97..03e12e316 100644 --- a/packages/idos-sdk-js/src/lib/grants/evm.ts +++ b/packages/idos-sdk-js/src/lib/grants/evm.ts @@ -403,8 +403,8 @@ export class EvmGrants implements GrantChild { } async list({ - owner = ZERO_ADDRESS, - grantee = ZERO_ADDRESS, + ownerAddress: owner = ZERO_ADDRESS, + granteeAddress: grantee = ZERO_ADDRESS, dataId = ZERO_DATA_ID, }: Partial> = {}): Promise { if (owner === ZERO_ADDRESS && grantee === ZERO_ADDRESS) @@ -414,12 +414,17 @@ export class EvmGrants implements GrantChild { return grants.map( ([owner, grantee, dataId, lockedUntil]: [string, string, string, bigint]) => - new Grant({ owner, grantee, dataId, lockedUntil: Number(lockedUntil) }), + new Grant({ + ownerAddress: owner, + granteeAddress: grantee, + dataId, + lockedUntil: Number(lockedUntil), + }), ); } async create({ - grantee = ZERO_ADDRESS, + granteeAddress = ZERO_ADDRESS, dataId = ZERO_DATA_ID, lockedUntil = ZERO_TIMELOCK, wait = true, @@ -427,17 +432,22 @@ export class EvmGrants implements GrantChild { grant: Grant; transactionId: string; }> { - if (grantee === ZERO_ADDRESS || dataId === ZERO_DATA_ID) { + if (granteeAddress === ZERO_ADDRESS || dataId === ZERO_DATA_ID) { throw new Error("Must provide `grantee` and `dataId`"); } - const owner = await this.signer.getAddress(); - const grant: Grant = { owner, grantee, dataId, lockedUntil }; + const ownerAddress = await this.signer.getAddress(); + const grant: Grant = { + ownerAddress, + granteeAddress, + dataId, + lockedUntil, + }; let transaction: TransactionResponse; try { transaction = (await this.#contract.insertGrant( - grantee, + granteeAddress, dataId, lockedUntil, )) as TransactionResponse; @@ -448,17 +458,22 @@ export class EvmGrants implements GrantChild { } async messageForCreateBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, }: Grant): Promise { - return await this.#contract.insertGrantBySignatureMessage(owner, grantee, dataId, lockedUntil); + return await this.#contract.insertGrantBySignatureMessage( + ownerAddress, + granteeAddress, + dataId, + lockedUntil, + ); } async createBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, signature, @@ -467,13 +482,13 @@ export class EvmGrants implements GrantChild { grant: Grant; transactionId: string; }> { - const grant: Grant = { owner, grantee, dataId, lockedUntil }; + const grant: Grant = { ownerAddress, granteeAddress, dataId, lockedUntil }; let transaction: TransactionResponse; try { transaction = (await this.#contract.insertGrantBySignature( - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, signature, @@ -485,25 +500,25 @@ export class EvmGrants implements GrantChild { } async revoke({ - grantee = ZERO_ADDRESS, + granteeAddress = ZERO_ADDRESS, dataId = ZERO_DATA_ID, lockedUntil = ZERO_TIMELOCK, wait = true, - }: Omit & { wait?: boolean }): Promise<{ + }: Omit & { wait?: boolean }): Promise<{ grant: Grant; transactionId: string; }> { - if (grantee === ZERO_ADDRESS || dataId === ZERO_DATA_ID) { + if (granteeAddress === ZERO_ADDRESS || dataId === ZERO_DATA_ID) { throw new Error("Must provide `grantee` and `dataId`"); } - const owner = await this.signer.getAddress(); - const grant: Grant = { owner, grantee, dataId, lockedUntil }; + const ownerAddress = await this.signer.getAddress(); + const grant: Grant = { ownerAddress, granteeAddress, dataId, lockedUntil }; let transaction: TransactionResponse; try { transaction = (await this.#contract.deleteGrant( - grantee, + granteeAddress, dataId, lockedUntil, )) as TransactionResponse; @@ -515,17 +530,22 @@ export class EvmGrants implements GrantChild { } async messageForRevokeBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, }: Grant): Promise { - return await this.#contract.deleteGrantBySignatureMessage(owner, grantee, dataId, lockedUntil); + return await this.#contract.deleteGrantBySignatureMessage( + ownerAddress, + granteeAddress, + dataId, + lockedUntil, + ); } async revokeBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, signature, @@ -534,13 +554,13 @@ export class EvmGrants implements GrantChild { grant: Grant; transactionId: string; }> { - const grant: Grant = { owner, grantee, dataId, lockedUntil }; + const grant: Grant = { ownerAddress, granteeAddress, dataId, lockedUntil }; let transaction: TransactionResponse; try { transaction = (await this.#contract.deleteGrantBySignature( - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, signature, diff --git a/packages/idos-sdk-js/src/lib/grants/grant-child.ts b/packages/idos-sdk-js/src/lib/grants/grant-child.ts index 16564e2f2..9d9e041b3 100644 --- a/packages/idos-sdk-js/src/lib/grants/grant-child.ts +++ b/packages/idos-sdk-js/src/lib/grants/grant-child.ts @@ -3,7 +3,7 @@ import type Grant from "./grant"; export interface GrantChild { list(_: Partial>): Promise; create( - _: Omit & { wait?: boolean }, + _: Omit & { wait?: boolean }, ): Promise<{ grant: Grant; transactionId: string }>; messageForCreateBySignature(_: Grant): Promise; createBySignature(_: Grant & { signature: Uint8Array; wait?: boolean }): Promise<{ @@ -11,7 +11,7 @@ export interface GrantChild { transactionId: string; }>; revoke( - _: Omit & { wait?: boolean }, + _: Omit & { wait?: boolean }, ): Promise<{ grant: Grant; transactionId: string }>; messageForRevokeBySignature(_: Grant): Promise; revokeBySignature(_: Grant & { signature: Uint8Array; wait?: boolean }): Promise<{ diff --git a/packages/idos-sdk-js/src/lib/grants/grant.ts b/packages/idos-sdk-js/src/lib/grants/grant.ts index 4b4428c19..64d876e91 100644 --- a/packages/idos-sdk-js/src/lib/grants/grant.ts +++ b/packages/idos-sdk-js/src/lib/grants/grant.ts @@ -1,12 +1,12 @@ export default class Grant { - owner: string; - grantee: string; + ownerAddress: string; + granteeAddress: string; dataId: string; lockedUntil: number; - constructor({ owner, grantee, dataId, lockedUntil }: Grant) { - this.owner = owner; - this.grantee = grantee; + constructor({ ownerAddress, granteeAddress, dataId, lockedUntil }: Grant) { + this.ownerAddress = ownerAddress; + this.granteeAddress = granteeAddress; this.dataId = dataId; this.lockedUntil = lockedUntil; } diff --git a/packages/idos-sdk-js/src/lib/grants/grants.ts b/packages/idos-sdk-js/src/lib/grants/grants.ts index ba66a5fff..5a334a866 100644 --- a/packages/idos-sdk-js/src/lib/grants/grants.ts +++ b/packages/idos-sdk-js/src/lib/grants/grants.ts @@ -100,8 +100,8 @@ export class Grants { async list( _args: { - owner?: string; - grantee?: string; + ownerAddress?: string; + granteeAddress?: string; dataId?: string; } = {}, ): Promise { @@ -113,19 +113,19 @@ export class Grants { _recordId: string, _address: string, _lockedUntil: number, - _receiverPublicKey: string, + _receiverEncryptionPublicKey: string, ): Promise<{ grant: Grant; transactionId: string }> { - throw new Error("Call idOS.setSigner first."); + throw new Error("Call `idOS.setSigner` first."); } async revoke( _tableName: string, _recordId: string, - _grantee: string, + _granteeAddress: string, _dataId: string, _lockedUntil: number, ): Promise<{ grant: Grant; transactionId: string }> { - throw new Error("Call idOS.setSigner first."); + throw new Error("Call `idOS.setSigner` first."); } async shareMatchingEntry( @@ -135,11 +135,11 @@ export class Grants { pick: Record; omit: Record; }, - _address: string, + _granteeAddress: string, _lockedUntil: number, - _receiverPublicKey: string, + _receiverEncryptionPublicKey: string, ): Promise<{ grant: Grant; transactionId: string }> { - throw new Error("Call idOS.setSigner first."); + throw new Error("Call `idOS.setSigner` first."); } } @@ -168,14 +168,14 @@ class ConnectedGrants extends Grants { async create( tableName: string, recordId: string, - address: string, + granteeAddress: string, lockedUntil: number, granteeEncryptionPublicKey: string, ): Promise<{ grant: Grant; transactionId: string }> { const share = await this.data.share(tableName, recordId, granteeEncryptionPublicKey); return await this.#child.create({ - grantee: address, + granteeAddress, dataId: share.id, lockedUntil: lockedUntil, }); @@ -188,9 +188,9 @@ class ConnectedGrants extends Grants { pick: Record; omit: Record; }, - address: string, + granteeAddress: string, lockedUntil: number, - receiverPublicKey: string, + receiverEncryptionPublicKey: string, ): Promise<{ grant: Grant; transactionId: string }> { const allEntries = (await this.data.list(tableName)) as unknown as idOSCredential[]; @@ -226,10 +226,14 @@ class ConnectedGrants extends Grants { if (!eligibleEntries.length) throw new Error("No matching credentials"); const selectedEntry = eligibleEntries[0]; - const { id: dataId } = await this.data.share(tableName, selectedEntry.id, receiverPublicKey); + const { id: dataId } = await this.data.share( + tableName, + selectedEntry.id, + receiverEncryptionPublicKey, + ); return await this.#child.create({ - grantee: address, + granteeAddress, dataId, lockedUntil, }); @@ -238,13 +242,13 @@ class ConnectedGrants extends Grants { async revoke( tableName: string, recordId: string, - grantee: string, + granteeAddress: string, dataId: string, lockedUntil: number, ): Promise<{ grant: Grant; transactionId: string }> { await this.data.unshare(tableName, recordId); - return this.#child.revoke({ grantee, dataId, lockedUntil }); + return this.#child.revoke({ granteeAddress, dataId, lockedUntil }); } async messageForCreateBySignature(grant: Grant) { diff --git a/packages/idos-sdk-js/src/lib/grants/near.ts b/packages/idos-sdk-js/src/lib/grants/near.ts index 2e2b6eb21..8405564f0 100644 --- a/packages/idos-sdk-js/src/lib/grants/near.ts +++ b/packages/idos-sdk-js/src/lib/grants/near.ts @@ -107,8 +107,8 @@ export class NearGrants implements GrantChild { return { grant: { - owner: grant.owner, - grantee: grant.grantee, + ownerAddress: grant.owner, + granteeAddress: grant.grantee, lockedUntil: grant.locked_until, dataId: grant.data_id, }, @@ -141,8 +141,8 @@ export class NearGrants implements GrantChild { } async list({ - owner, - grantee, + ownerAddress: owner, + granteeAddress: grantee, dataId: data_id, }: Partial> = {}): Promise { if (!(owner || grantee)) throw new Error("Must provide `owner` and/or `grantee`"); @@ -159,9 +159,11 @@ export class NearGrants implements GrantChild { ) => Promise; return (await method(grantsFilter)).map( - ({ data_id, locked_until, ...values }) => + ({ data_id, locked_until, owner, grantee, ...values }) => new Grant({ ...values, + ownerAddress: owner, + granteeAddress: grantee, dataId: data_id, lockedUntil: locked_until / 1e6, }), @@ -173,16 +175,19 @@ export class NearGrants implements GrantChild { /// NOTE: NEAR is problematic for the current implementation. The only way to create an AG in the contract if to /// create an dAG for yourself. async create({ - grantee, + granteeAddress, dataId, lockedUntil, - }: Omit & { wait?: boolean }): Promise<{ grant: Grant; transactionId: string }> { - const owner = this.#publicKey; + }: Omit & { wait?: boolean }): Promise<{ + grant: Grant; + transactionId: string; + }> { + const ownerAddress = this.#publicKey; const recipient = await this.messageRecipient(); const message = await this.messageForCreateBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, }); @@ -190,8 +195,8 @@ export class NearGrants implements GrantChild { const { nonce, signature } = await this.#sign(message, recipient); return this.createBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, signature, @@ -209,8 +214,8 @@ export class NearGrants implements GrantChild { } async messageForCreateBySignature({ - owner, - grantee, + ownerAddress: owner, + granteeAddress: grantee, dataId: data_id, lockedUntil, }: Grant): Promise { @@ -230,8 +235,8 @@ export class NearGrants implements GrantChild { } async createBySignature({ - owner, - grantee, + ownerAddress: owner, + granteeAddress: grantee, dataId: data_id, lockedUntil, signature, @@ -282,16 +287,19 @@ export class NearGrants implements GrantChild { /// NOTE: NEAR is problematic for the current implementation. The only way to revoke an AG in the contract if to /// create an dAG for yourself. async revoke({ - grantee, + granteeAddress, dataId, lockedUntil, - }: Omit & { wait?: boolean }): Promise<{ grant: Grant; transactionId: string }> { - const owner = this.#publicKey; + }: Omit & { wait?: boolean }): Promise<{ + grant: Grant; + transactionId: string; + }> { + const ownerAddress = this.#publicKey; const recipient = await this.messageRecipient(); const message = await this.messageForRevokeBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, }); @@ -299,8 +307,8 @@ export class NearGrants implements GrantChild { const { nonce, signature } = await this.#sign(message, recipient); return this.revokeBySignature({ - owner, - grantee, + ownerAddress, + granteeAddress, dataId, lockedUntil, signature, @@ -309,8 +317,8 @@ export class NearGrants implements GrantChild { } async messageForRevokeBySignature({ - owner, - grantee, + ownerAddress: owner, + granteeAddress: grantee, dataId: data_id, lockedUntil, }: Grant): Promise { @@ -330,8 +338,8 @@ export class NearGrants implements GrantChild { } async revokeBySignature({ - owner, - grantee, + ownerAddress: owner, + granteeAddress: grantee, dataId: data_id, lockedUntil, signature,