|
1 | | -import { FarcasterNetwork } from '../protobufs'; |
2 | | -import { blake3 } from '@noble/hashes/blake3'; |
3 | | -import { Signer as EthersSigner, Wallet, randomBytes } from 'ethers'; |
4 | | -import { bytesToHexString } from '../bytes'; |
5 | | -import { eip712 } from '../crypto'; |
6 | | -import { Factories } from '../factories'; |
7 | | -import { VerificationEthAddressClaim, makeVerificationEthAddressClaim } from '../verifications'; |
| 1 | +import { Wallet } from 'ethers'; |
8 | 2 | import { EthersEip712Signer } from './ethersEip712Signer'; |
| 3 | +import { testEip712Signer } from './testUtils'; |
9 | 4 |
|
10 | 5 | describe('EthersEip712Signer', () => { |
11 | | - let signer: EthersEip712Signer; |
12 | | - let ethersSigner: EthersSigner; |
13 | | - let signerKey: Uint8Array; |
14 | | - |
15 | | - beforeAll(async () => { |
16 | | - ethersSigner = Wallet.createRandom(); |
17 | | - signer = new EthersEip712Signer(ethersSigner); |
18 | | - signerKey = (await signer.getSignerKey())._unsafeUnwrap(); |
19 | | - }); |
20 | | - |
21 | | - describe('instanceMethods', () => { |
22 | | - describe('signMessageHash', () => { |
23 | | - test('generates valid signature', async () => { |
24 | | - const bytes = randomBytes(32); |
25 | | - const hash = blake3(bytes, { dkLen: 20 }); |
26 | | - const signature = (await signer.signMessageHash(hash))._unsafeUnwrap(); |
27 | | - const recoveredAddress = (await eip712.verifyMessageHashSignature(hash, signature))._unsafeUnwrap(); |
28 | | - expect(recoveredAddress).toEqual(signerKey); |
29 | | - }); |
30 | | - }); |
31 | | - |
32 | | - describe('signVerificationEthAddressClaim', () => { |
33 | | - let claim: VerificationEthAddressClaim; |
34 | | - let signature: Uint8Array; |
35 | | - |
36 | | - beforeAll(async () => { |
37 | | - claim = makeVerificationEthAddressClaim( |
38 | | - Factories.Fid.build(), |
39 | | - signerKey, |
40 | | - FarcasterNetwork.TESTNET, |
41 | | - Factories.BlockHash.build() |
42 | | - )._unsafeUnwrap(); |
43 | | - signature = (await signer.signVerificationEthAddressClaim(claim))._unsafeUnwrap(); |
44 | | - }); |
45 | | - |
46 | | - test('succeeds', async () => { |
47 | | - expect(signature).toBeTruthy(); |
48 | | - const recoveredAddress = eip712.verifyVerificationEthAddressClaimSignature(claim, signature)._unsafeUnwrap(); |
49 | | - expect(recoveredAddress).toEqual(signerKey); |
50 | | - }); |
51 | | - |
52 | | - test('succeeds when encoding twice', async () => { |
53 | | - const claim2: VerificationEthAddressClaim = { ...claim }; |
54 | | - const signature2 = (await signer.signVerificationEthAddressClaim(claim2))._unsafeUnwrap(); |
55 | | - expect(signature2).toEqual(signature); |
56 | | - expect(bytesToHexString(signature2)).toEqual(bytesToHexString(signature)); |
57 | | - }); |
58 | | - }); |
| 6 | + describe('with ethers Wallet', () => { |
| 7 | + const wallet = Wallet.createRandom(); |
| 8 | + const signer = new EthersEip712Signer(wallet); |
| 9 | + testEip712Signer(signer); |
59 | 10 | }); |
60 | 11 | }); |
0 commit comments