Skip to content

Commit fe0ffbe

Browse files
committed
verify signatures
1 parent 6f3cf57 commit fe0ffbe

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

js/ethereum-utils/src/signature.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,25 @@ export async function signEip712(
5151
return { Ecdsa: signature } as EcdsaSignature;
5252
}
5353

54+
/**
55+
* Verify EIP-712 signatures
56+
* @param ethereumAddress
57+
* @param signature
58+
* @param payload
59+
*/
60+
export function verifyEip712Signature(
61+
ethereumAddress: HexString,
62+
signature: HexString,
63+
payload: SupportedPayload
64+
): boolean {
65+
const types = getTypesFor(payload.type);
66+
const normalizedPayload = normalizePayload(payload);
67+
// TODO: use correct chainID for different networks
68+
// TODO: use correct contract address for different payloads
69+
const recoveredAddress = ethers.verifyTypedData(EIP712_DOMAIN_DEFAULT, types, normalizedPayload, signature);
70+
return recoveredAddress.toLowerCase() === ethereumAddress.toLowerCase();
71+
}
72+
5473
function normalizePayload(payload: SupportedPayload): Record<string, any> {
5574
const clonedPayload = Object.assign({}, payload);
5675
switch (clonedPayload.type) {

js/ethereum-utils/test/signature.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createPasskeyPublicKey,
1212
signEip712,
1313
HexString,
14+
verifyEip712Signature,
1415
} from '../src';
1516

1617
describe('Signature related tests', function () {
@@ -140,6 +141,10 @@ describe('Signature related tests', function () {
140141
};
141142

142143
assert.deepEqual(signature, expected);
144+
assert(
145+
verifyEip712Signature('0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', expected.Ecdsa, payload1),
146+
'should verify'
147+
);
143148
});
144149

145150
it('should create a valid signature for PaginatedDeleteSignaturePayloadV2', async function () {
@@ -152,6 +157,10 @@ describe('Signature related tests', function () {
152157
};
153158

154159
assert.deepEqual(signature, expected);
160+
assert(
161+
verifyEip712Signature('0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', expected.Ecdsa, payload1),
162+
'should verify'
163+
);
155164
});
156165

157166
it('should create a valid signature for ItemizedSignaturePayloadV2', async function () {
@@ -167,6 +176,10 @@ describe('Signature related tests', function () {
167176
};
168177

169178
assert.deepEqual(signature, expected);
179+
assert(
180+
verifyEip712Signature('0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', expected.Ecdsa, payload1),
181+
'should verify'
182+
);
170183
});
171184

172185
it('should create a valid signature for AddKeyData', async function () {
@@ -183,6 +196,10 @@ describe('Signature related tests', function () {
183196
};
184197

185198
assert.deepEqual(signature, expected);
199+
assert(
200+
verifyEip712Signature('0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', expected.Ecdsa, payload1),
201+
'should verify'
202+
);
186203
});
187204

188205
it('should create a valid signature for AddProvider', async function () {
@@ -195,6 +212,10 @@ describe('Signature related tests', function () {
195212
};
196213

197214
assert.deepEqual(signature, expected);
215+
assert(
216+
verifyEip712Signature('0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', expected.Ecdsa, payload1),
217+
'should verify'
218+
);
198219
});
199220

200221
it('should create a valid signature for ClaimHandlePayload', async function () {
@@ -207,6 +228,10 @@ describe('Signature related tests', function () {
207228
};
208229

209230
assert.deepEqual(signature, expected);
231+
assert(
232+
verifyEip712Signature('0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', expected.Ecdsa, payload1),
233+
'should verify'
234+
);
210235
});
211236

212237
it('should create a valid signature for PasskeyPublicKey', async function () {
@@ -219,6 +244,10 @@ describe('Signature related tests', function () {
219244
};
220245

221246
assert.deepEqual(signature, expected);
247+
assert(
248+
verifyEip712Signature('0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac', expected.Ecdsa, payload1),
249+
'should verify'
250+
);
222251
});
223252
});
224253
});

0 commit comments

Comments
 (0)