Skip to content

Commit

Permalink
Adds type checking to EIP1271 Magic Value (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
w4ll3 authored Jun 29, 2023
1 parent 2ae2213 commit e3ca3ce
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/siwe/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { Contract, providers, Signer } from 'ethers';
import type { SiweMessage } from './client';
import { hashMessage } from './ethersCompat';

const EIP1271_ABI = ["function isValidSignature(bytes32 _message, bytes _signature) public view returns (bytes4)"];
const EIP1271_MAGICVALUE = "0x1626ba7e";
const ISO8601 = /^(?<date>[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(.[0-9]+)?(([Zz])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
const EIP1271_ABI = [
'function isValidSignature(bytes32 _message, bytes _signature) public view returns (bytes4)',
];
const EIP1271_MAGICVALUE = '0x1626ba7e';
const ISO8601 =
/^(?<date>[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(.[0-9]+)?(([Zz])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;

/**
* This method calls the EIP-1271 method for Smart Contract wallets
Expand All @@ -27,7 +30,7 @@ export const checkContractWalletSignature = async (
const walletContract = new Contract(message.address, EIP1271_ABI, provider);
const hashedMessage = hashMessage(message.prepareMessage());
const res = await walletContract.isValidSignature(hashedMessage, signature);
return res == EIP1271_MAGICVALUE;
return res === EIP1271_MAGICVALUE;
};

/**
Expand Down Expand Up @@ -72,14 +75,17 @@ export const isValidISO8601Date = (inputDate: string): boolean => {

/* Compare remaining fields */
return inputMatch.groups.date === parsedInputMatch.groups.date;
}
};

export const checkInvalidKeys = <T>(obj: T, keys: Array<keyof T>): Array<keyof T> => {
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 e3ca3ce

Please sign in to comment.