Skip to content

Commit

Permalink
Refactor invalid keys handling (#126)
Browse files Browse the repository at this point in the history
* Refactor invalid keys handling

* Fix tests
  • Loading branch information
w4ll3 committed Oct 10, 2022
1 parent 07ac0f2 commit 80feb1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
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 @@ -16,7 +16,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 @@ -202,34 +202,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;
}

0 comments on commit 80feb1b

Please sign in to comment.