Skip to content

Commit

Permalink
feat: added signature validation to orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Jun 25, 2024
1 parent 200fddd commit 45a47da
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 27 deletions.
6 changes: 5 additions & 1 deletion src/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export class Gateway {
checkVersion();

const client = new GraphQLClient(url, {
headers: { Authorization: `Bearer ${token}`, 'x-api-key': apiKey },
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': apiKey,
'user-agent': `GATEWAY_SDK/v2`,
},
});

this.sdk = getSdk(client, logging ? clientTimingWrapper : undefined);
Expand Down
6 changes: 5 additions & 1 deletion src/GatewayV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export class GatewayV3 {
checkVersion();

const client = new GraphQLClient(url, {
headers: { Authorization: `Bearer ${token}`, 'x-api-key': apiKey },
headers: {
Authorization: `Bearer ${token}`,
'x-api-key': apiKey,
'user-agent': `GATEWAY_SDK/v3`,
},
});

this.sdk = getSdk(client, logging ? clientTimingWrapper : undefined);
Expand Down
6 changes: 5 additions & 1 deletion src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const clientTimingWrapper: SdkFunctionWrapper = async <T>(
return result;
};

export const checkVersion = async () => {
export const checkVersion = async (): Promise<string> => {
let inMemory = true;
let version = '0.0.0';
if (inMemory) {
const result = await (
await fetch('https://registry.npmjs.org/@gateway-dao/sdk/latest')
Expand All @@ -29,6 +30,7 @@ export const checkVersion = async () => {

try {
const packageJson = JSON.parse(data);
version = packageJson.version;
if (packageJson.version !== result.version)
console.error(
`You are using Gateway SDK (${packageJson.version}) which is outdated. Please update to latest version of Gateway SDK (${result.version}).\nhttps://www.npmjs.com/package/@gateway-dao/sdk `,
Expand All @@ -39,7 +41,9 @@ export const checkVersion = async () => {
});

inMemory = false;
return version;
}
return version;
};

export const parameterChecker = (
Expand Down
79 changes: 66 additions & 13 deletions src/v3/organization/organization.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
CreateOrganizationInput,
MemberInput,
MemberOrganizationInput,
OrganizationIdentifierType,
organizations_queryQueryVariables,
Sdk,
TransferMemberInput,
TransferMemberOrganizationInput,
UpdateOrganizationInput,
} from '../../../gatewaySdk/sources/GatewayV3';
import { SignCipherEnum } from '../../types';
Expand Down Expand Up @@ -52,39 +52,64 @@ export class Organization {
input: organizationInput,
});
} catch (error) {
console.log(error);
throw new Error(errorHandler(error));
}
}

/**
* The function adds a member to an organization using the provided input.
* @param {MemberInput} memberInput - The `memberInput` parameter is an object that contains the
* @param {MemberOrganizationInput} memberInput - The `memberInput` parameter is an object that contains the
* information needed to add a member to an organization. It likely includes properties such as the
* member's name, email, role, and any other relevant details.
* @returns the result of the `addMemberToOrganization_mutation` mutation call.
*/
async addMemberToOrganization(memberInput: MemberInput) {
async addMemberToOrganization(memberInput: MemberOrganizationInput) {
try {
let signCipher: SignCipherEnum;
if (memberInput.signingCipher === undefined) {
signCipher = SignCipherEnum.SECP256K1;
} else if (memberInput.signingCipher === SignCipherEnum.ED25519) {
signCipher = SignCipherEnum.ED25519;
} else signCipher = SignCipherEnum.SECP256K1;

validateSignature({
data: memberInput.data,
signature: memberInput.signature,
signingKey: memberInput.signingKey,
signingCipher: signCipher,
});

return await this.sdk.addMemberToOrganization_mutation({
input: memberInput,
});
} catch (error) {
console.log(error);
throw new Error(errorHandler(error));
}
}

/**
* The function "changeMemberRole" is an asynchronous function that takes a "memberInput" parameter
* and calls a mutation function to change the role of a member, handling any errors that occur.
* @param {MemberInput} memberInput - The `memberInput` parameter is an object that contains the
* @param {MemberOrganizationInput} memberInput - The `memberInput` parameter is an object that contains the
* necessary information to change the role of a member. It likely includes properties such as the
* member's ID and the new role they should be assigned to.
* @returns the result of the `changeMemberRole_mutation` mutation, which is being awaited.
*/
async changeMemberRole(memberInput: MemberInput) {
async changeMemberRole(memberInput: MemberOrganizationInput) {
try {
let signCipher: SignCipherEnum;
if (memberInput.signingCipher === undefined) {
signCipher = SignCipherEnum.SECP256K1;
} else if (memberInput.signingCipher === SignCipherEnum.ED25519) {
signCipher = SignCipherEnum.ED25519;
} else signCipher = SignCipherEnum.SECP256K1;

validateSignature({
data: memberInput.data,
signature: memberInput.signature,
signingKey: memberInput.signingKey,
signingCipher: signCipher,
});
return await this.sdk.changeMemberRole_mutation({ input: memberInput });
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -93,32 +118,60 @@ export class Organization {

/**
* The function removes a member from an organization using the provided input.
* @param {TransferMemberInput} memberInput - The `memberInput` parameter is an object that contains
* @param {TransferMemberOrganizationInput} memberInput - The `memberInput` parameter is an object that contains
* the necessary information to remove a member from an organization. It likely includes properties
* such as the member's ID or username, and any additional data required to complete the removal
* process.
* @returns the result of the `removeMemberFromOrganization_mutation` mutation call.
*/
async removeMemberFromOrganization(memberInput: TransferMemberInput) {
async removeMemberFromOrganization(
memberInput: TransferMemberOrganizationInput,
) {
try {
let signCipher: SignCipherEnum;
if (memberInput.signingCipher === undefined) {
signCipher = SignCipherEnum.SECP256K1;
} else if (memberInput.signingCipher === SignCipherEnum.ED25519) {
signCipher = SignCipherEnum.ED25519;
} else signCipher = SignCipherEnum.SECP256K1;

validateSignature({
data: memberInput.data,
signature: memberInput.signature,
signingKey: memberInput.signingKey,
signingCipher: signCipher,
});
return await this.sdk.removeMemberFromOrganization_mutation({
input: memberInput,
});
} catch (error) {
} catch (error: any) {
throw new Error(errorHandler(error));
}
}

/**
* The function allows the current owner to transfer its ownership to another member of organization using the provided input.
* @param {TransferMemberInput} ownershipInput - The `ownershipInput` parameter is an object that contains
* @param {TransferMemberOrganizationInput} ownershipInput - The `ownershipInput` parameter is an object that contains
* the necessary information to remove a member from an organization. It likely includes properties
* such as the member's ID or username, and any additional data required to complete the removal
* process.
* @returns the result of the `transferOwnership_mutationMutation` mutation call.
*/
async transferOwnership(ownershipInput: TransferMemberInput) {
async transferOwnership(ownershipInput: TransferMemberOrganizationInput) {
try {
let signCipher: SignCipherEnum;
if (ownershipInput.signingCipher === undefined) {
signCipher = SignCipherEnum.SECP256K1;
} else if (ownershipInput.signingCipher === SignCipherEnum.ED25519) {
signCipher = SignCipherEnum.ED25519;
} else signCipher = SignCipherEnum.SECP256K1;

validateSignature({
data: ownershipInput.data,
signature: ownershipInput.signature,
signingKey: ownershipInput.signingKey,
signingCipher: signCipher,
});
return await this.sdk.transferOwnership_mutation({
input: ownershipInput,
});
Expand Down
3 changes: 1 addition & 2 deletions src/v3/pda/pda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ export class PDA {
data: input.data,
});
return await this.sdk.changePDAStatus_mutation({ input });
} catch (error: any) {
console.log(error.request.variables.input);
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand Down
51 changes: 42 additions & 9 deletions src/v3/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
requestsReceived_queryQueryVariables,
requestsSent_queryQueryVariables,
} from '../../../gatewaySdk/sources/GatewayV3';
import { SignCipherEnum } from '../../types';
import { errorHandler } from '../../utils/errorHandler';
import { validateSignature } from '../../utils/v3-crypto-helper';
import { isUUIDValid, validateObjectProperties } from '../../utils/validators';

export class Request {
Expand All @@ -29,9 +31,27 @@ export class Request {
*/
async createDataRequest(inputSchema: CreateDataRequestInput) {
try {
let signCipher: SignCipherEnum;
if (inputSchema.signingCipher === undefined) {
signCipher = SignCipherEnum.SECP256K1;
} else if (inputSchema.signingCipher === SignCipherEnum.ED25519) {
signCipher = SignCipherEnum.ED25519;
} else signCipher = SignCipherEnum.SECP256K1;

validateSignature({
data: inputSchema.data,
signature: inputSchema.signature,
signingKey: inputSchema.signingKey,
signingCipher: signCipher,
});
validateObjectProperties(inputSchema);
return await this.sdk.createDataRequest_mutation({ input: inputSchema });
} catch (error: any) {
console.log(
error,
error.request.variables.input,
error.request.variables.input.data.schema,
);
throw new Error(errorHandler(error));
}
}
Expand All @@ -45,9 +65,22 @@ export class Request {

async updateDataRequest(inputSchema: UpdateDataRequestInput) {
try {
let signCipher: SignCipherEnum;
if (inputSchema.signingCipher === undefined) {
signCipher = SignCipherEnum.SECP256K1;
} else if (inputSchema.signingCipher === SignCipherEnum.ED25519) {
signCipher = SignCipherEnum.ED25519;
} else signCipher = SignCipherEnum.SECP256K1;

validateSignature({
data: inputSchema.data,
signature: inputSchema.signature,
signingKey: inputSchema.signingKey,
signingCipher: signCipher,
});
validateObjectProperties(inputSchema);
return await this.sdk.updateDataRequest_mutation({ input: inputSchema });
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -65,7 +98,7 @@ export class Request {
try {
isUUIDValid(requestId);
return await this.sdk.dataRequest_query({ requestId });
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -82,7 +115,7 @@ export class Request {
async getDataRequests(filterVariables?: FilterDataRequestInput) {
try {
return await this.sdk.dataRequests_query({ filter: filterVariables });
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -98,7 +131,7 @@ export class Request {
async getDataRequestCount(filterVariables?: FilterDataRequestInput) {
try {
return await this.sdk.dataRequestCount_query({ filter: filterVariables });
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -114,7 +147,7 @@ export class Request {
try {
isUUIDValid(requestId);
return await this.sdk.dataRequestStatus_query({ requestId });
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -130,7 +163,7 @@ export class Request {
async getRequestsReceived(variables?: requestsReceived_queryQueryVariables) {
try {
return await this.sdk.requestsReceived_query(variables);
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -148,7 +181,7 @@ export class Request {
return await this.sdk.requestsReceivedCount_query({
filter: filterVariables,
});
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -165,7 +198,7 @@ export class Request {
async getRequestsSent(variables?: requestsSent_queryQueryVariables) {
try {
return await this.sdk.requestsSent_query(variables);
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand All @@ -183,7 +216,7 @@ export class Request {
return await this.sdk.requestsSentCount_query({
filter: filterVariables,
});
} catch (error: any) {
} catch (error) {
throw new Error(errorHandler(error));
}
}
Expand Down

0 comments on commit 45a47da

Please sign in to comment.