Skip to content

Commit

Permalink
Fix smart contract wallet function (spruceid#63)
Browse files Browse the repository at this point in the history
* Fix smart contract wallet function

* Catches error for signatures longer then 65 bytes
  • Loading branch information
w4ll3 authored Apr 1, 2022
1 parent 3c48afd commit fab78c1
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions packages/siwe/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,27 @@ export class SiweMessage {
);
}

const addr = ethers.utils.verifyMessage(message, signature);
let addr;
try {
addr = ethers.utils.verifyMessage(message, signature);

if (addr !== this.address) {
try {
//EIP-1271
const isValidSignature =
await checkContractWalletSignature(this, provider);
if (!isValidSignature) {
throw new Error(
`${ErrorTypes.INVALID_SIGNATURE}: ${addr} !== ${this.address}`
);
} catch (_) { } finally {
if (addr !== this.address) {
try {
//EIP-1271
const isValidSignature =
await checkContractWalletSignature(this, signature, provider);
if (!isValidSignature) {
throw new Error(
`${ErrorTypes.INVALID_SIGNATURE}: ${addr} !== ${this.address}`
);
}
} catch (e) {
throw e;
}
} catch (e) {
throw e;
}
}

const parsedMessage = new SiweMessage(message);

if (parsedMessage.expirationTime) {
Expand Down Expand Up @@ -297,6 +302,7 @@ export class SiweMessage {
*/
export const checkContractWalletSignature = async (
message: SiweMessage,
signature: string,
provider?: any
): Promise<boolean> => {
if (!provider) {
Expand All @@ -311,7 +317,7 @@ export const checkContractWalletSignature = async (
const hashMessage = utils.hashMessage(message.signMessage());
return await walletContract.isValidSignature(
hashMessage,
message.signature
signature,
);
} catch (e) {
throw e;
Expand Down

0 comments on commit fab78c1

Please sign in to comment.