Skip to content

Commit

Permalink
feat(client-sdk): update grants var names
Browse files Browse the repository at this point in the history
  • Loading branch information
ditoglez committed Dec 5, 2024
1 parent b78b575 commit 545b148
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 81 deletions.
80 changes: 50 additions & 30 deletions packages/idos-sdk-js/src/lib/grants/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Omit<Grant, "lockedUntil">> = {}): Promise<Grant[]> {
if (owner === ZERO_ADDRESS && grantee === ZERO_ADDRESS)
Expand All @@ -414,30 +414,40 @@ 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,
}: Omit<Grant, "owner"> & { 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.insertGrant(
grantee,
granteeAddress,
dataId,
lockedUntil,
)) as TransactionResponse;
Expand All @@ -448,17 +458,22 @@ export class EvmGrants implements GrantChild {
}

async messageForCreateBySignature({
owner,
grantee,
ownerAddress,
granteeAddress,
dataId,
lockedUntil,
}: Grant): Promise<string> {
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,
Expand All @@ -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,
Expand All @@ -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<Grant, "owner"> & { wait?: boolean }): Promise<{
}: Omit<Grant, "ownerAddress"> & { 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;
Expand All @@ -515,17 +530,22 @@ export class EvmGrants implements GrantChild {
}

async messageForRevokeBySignature({
owner,
grantee,
ownerAddress,
granteeAddress,
dataId,
lockedUntil,
}: Grant): Promise<string> {
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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/idos-sdk-js/src/lib/grants/grant-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import type Grant from "./grant";
export interface GrantChild {
list(_: Partial<Omit<Grant, "lockedUntil">>): Promise<Grant[]>;
create(
_: Omit<Grant, "owner"> & { wait?: boolean },
_: Omit<Grant, "ownerAddress"> & { wait?: boolean },
): Promise<{ grant: Grant; transactionId: string }>;
messageForCreateBySignature(_: Grant): Promise<string>;
createBySignature(_: Grant & { signature: Uint8Array; wait?: boolean }): Promise<{
grant: Grant;
transactionId: string;
}>;
revoke(
_: Omit<Grant, "owner"> & { wait?: boolean },
_: Omit<Grant, "ownerAddress"> & { wait?: boolean },
): Promise<{ grant: Grant; transactionId: string }>;
messageForRevokeBySignature(_: Grant): Promise<string>;
revokeBySignature(_: Grant & { signature: Uint8Array; wait?: boolean }): Promise<{
Expand Down
10 changes: 5 additions & 5 deletions packages/idos-sdk-js/src/lib/grants/grant.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
38 changes: 21 additions & 17 deletions packages/idos-sdk-js/src/lib/grants/grants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export class Grants {

async list(
_args: {
owner?: string;
grantee?: string;
ownerAddress?: string;
granteeAddress?: string;
dataId?: string;
} = {},
): Promise<Grant[]> {
Expand All @@ -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(
Expand All @@ -135,11 +135,11 @@ export class Grants {
pick: Record<string, string>;
omit: Record<string, string>;
},
_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.");
}
}

Expand Down Expand Up @@ -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,
});
Expand All @@ -188,9 +188,9 @@ class ConnectedGrants extends Grants {
pick: Record<string, string>;
omit: Record<string, string>;
},
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[];

Expand Down Expand Up @@ -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,
});
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 545b148

Please sign in to comment.