Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor invalid keys handling #126

Merged
merged 2 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/siwe/lib/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe(`Unit`, () => {
} catch (e) {
expect(e.success).toBeFalsy();
expect(e.error).toEqual(
new Error('invalidKey is not a valid key for VerifyParams.')
new Error('invalidKey is/are not valid key(s) for VerifyParams.')
);
}
});
Expand All @@ -256,7 +256,7 @@ describe(`Unit`, () => {
} catch (e) {
expect(e.success).toBeFalsy();
expect(e.error).toEqual(
new Error('invalidKey is not a valid key for VerifyOpts.')
new Error('invalidKey is/are not valid key(s) for VerifyOpts.')
);
}
});
Expand Down
44 changes: 21 additions & 23 deletions packages/siwe/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
VerifyParams,
VerifyParamsKeys,
} from './types';
import { checkContractWalletSignature, generateNonce } from './utils';
import { checkContractWalletSignature, checkInvalidKeys, generateNonce } from './utils';

export class SiweMessage {
/**RFC 4501 dns authority that is requesting the signing. */
Expand Down Expand Up @@ -204,34 +204,32 @@ export class SiweMessage {
opts: VerifyOpts = { suppressExceptions: false }
): Promise<SiweResponse> {
return new Promise<SiweResponse>((resolve, reject) => {
Object.keys(params).forEach((key: keyof VerifyParams) => {
if (!VerifyParamsKeys.includes(key)) {
reject({
success: false,
data: this,
error: new Error(`${key} is not a valid key for VerifyParams.`),
});
}
});

Object.keys(opts).forEach((key: keyof VerifyOpts) => {
if (!VerifyOptsKeys.includes(key)) {
reject({
success: false,
data: this,
error: new Error(`${key} is not a valid key for VerifyOpts.`),
});
}
});

const fail = result => {
if (opts.suppressExceptions) {
resolve(result);
return resolve(result);
} else {
reject(result);
return reject(result);
}
};

const invalidParams: Array<keyof VerifyParams> = checkInvalidKeys<VerifyParams>(params, VerifyParamsKeys);
if(invalidParams.length > 0) {
fail({
success: false,
data: this,
error: new Error(`${invalidParams.join(', ')} is/are not valid key(s) for VerifyParams.`),
})
}

const invalidOpts: Array<keyof VerifyOpts> = checkInvalidKeys<VerifyOpts>(opts, VerifyOptsKeys);
if(invalidParams.length > 0) {
fail({
success: false,
data: this,
error: new Error(`${invalidOpts.join(', ')} is/are not valid key(s) for VerifyOpts.`),
})
}

const { signature, domain, nonce, time } = params;

/** Domain binding */
Expand Down
10 changes: 10 additions & 0 deletions packages/siwe/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ export const checkContractWalletSignature = async (
export const generateNonce = (): string => {
return randomStringForEntropy(96);
};

export const checkInvalidKeys = <T>(obj: T, keys: Array<keyof T>) : Array<keyof T> => {
const invalidKeys: Array<keyof T> = [];
Object.keys(obj).forEach(key => {
if (!keys.includes(key as keyof T)) {
invalidKeys.push(key as keyof T);
}
});
return invalidKeys;
}