|
1 | 1 | /*
|
2 | 2 | This file contains functions to sign message for executeRelayCall.
|
3 | 3 | */
|
4 |
| -import Account from 'eth-lib/lib/account'; |
5 |
| -import { bufferToHex, keccak256 } from 'ethereumjs-util'; |
6 |
| -import utils from 'web3-utils'; |
7 |
| - |
8 |
| -import { Message } from './interfaces'; |
| 4 | +import { |
| 5 | + type Address, |
| 6 | + type ByteArray, |
| 7 | + hexToBytes, |
| 8 | + isAddress, |
| 9 | + isBytes, |
| 10 | + isHex, |
| 11 | + keccak256, |
| 12 | + recoverAddress, |
| 13 | + serializeSignature, |
| 14 | + stringToBytes, |
| 15 | +} from 'viem'; |
| 16 | +import { sign } from 'viem/accounts'; |
9 | 17 |
|
| 18 | +import type { Message } from './interfaces'; |
10 | 19 | export class EIP191Signer {
|
11 |
| - hashEthereumSignedMessage(message: string) { |
12 |
| - const messageHex = utils.isHexStrict(message) |
| 20 | + hashEthereumSignedMessage(message: string | ByteArray): `0x${string}` { |
| 21 | + const messageBytes = isBytes(message) |
13 | 22 | ? message
|
14 |
| - : utils.utf8ToHex(message); |
15 |
| - const messageBytes = utils.hexToBytes(messageHex); |
16 |
| - const messageBuffer = Buffer.from(messageBytes); |
17 |
| - const preamble = |
18 |
| - '\x19' + '\x45' + 'thereum Signed Message:\n' + messageBytes.length; |
19 |
| - const preambleBuffer = Buffer.from(preamble); |
20 |
| - const ethMessage = Buffer.concat([preambleBuffer, messageBuffer]); |
21 |
| - return bufferToHex(keccak256(ethMessage)); |
| 23 | + : isHex(message, { strict: true }) |
| 24 | + ? hexToBytes(message) |
| 25 | + : stringToBytes(message); |
| 26 | + const encoder = new TextEncoder(); |
| 27 | + const preambleBytes = encoder.encode( |
| 28 | + `\x19\x45thereum Signed Message:\n${messageBytes.length}`, |
| 29 | + ); |
| 30 | + const ethMessage = new Uint8Array( |
| 31 | + preambleBytes.length + messageBytes.length, |
| 32 | + ); |
| 33 | + ethMessage.set(preambleBytes, 0); // Add the byte array starting at index 0 |
| 34 | + ethMessage.set(messageBytes, preambleBytes.length); // Add the string bytes after |
| 35 | + return keccak256(ethMessage, 'hex'); |
22 | 36 | }
|
23 |
| - |
24 |
| - hashDataWithIntendedValidator(validatorAddress: string, data: string) { |
| 37 | + hashDataWithIntendedValidator( |
| 38 | + validatorAddress: Address, |
| 39 | + data: string | ByteArray, |
| 40 | + ) { |
25 | 41 | // validator address
|
26 |
| - if (!utils.isAddress(validatorAddress)) |
| 42 | + if (!isAddress(validatorAddress)) { |
27 | 43 | throw new Error('Validator needs to be a valid address');
|
28 |
| - |
29 |
| - const validatorBuffer = Buffer.from(utils.hexToBytes(validatorAddress)); |
| 44 | + } |
| 45 | + const validatorBytes = hexToBytes(validatorAddress); |
30 | 46 | // data to sign
|
31 |
| - const dataHex = utils.isHexStrict(data) ? data : utils.utf8ToHex(data); |
32 |
| - const dataBuffer = Buffer.from(utils.hexToBytes(dataHex)); |
33 |
| - |
| 47 | + const dataBytes = isBytes(data) |
| 48 | + ? data |
| 49 | + : isHex(data, { strict: true }) |
| 50 | + ? hexToBytes(data) |
| 51 | + : stringToBytes(data); |
34 | 52 | // concatenate it
|
35 |
| - const preambleBuffer = Buffer.from('\x19'); |
36 |
| - const versionBuffer = Buffer.from('\x00'); |
37 |
| - const ethMessage = Buffer.concat([ |
38 |
| - preambleBuffer, |
39 |
| - versionBuffer, |
40 |
| - validatorBuffer, |
41 |
| - dataBuffer, |
42 |
| - ]); |
43 |
| - return bufferToHex(keccak256(ethMessage)); |
| 53 | + const encoder = new TextEncoder(); |
| 54 | + const preambleBytes = encoder.encode('\x19\x00'); |
| 55 | + const ethMessage = new Uint8Array( |
| 56 | + preambleBytes.length + validatorBytes.length + dataBytes.length, |
| 57 | + ); |
| 58 | + ethMessage.set(preambleBytes, 0); // Add the byte array starting at index 0 |
| 59 | + ethMessage.set(validatorBytes, preambleBytes.length); // Add the string bytes after |
| 60 | + ethMessage.set(dataBytes, preambleBytes.length + validatorBytes.length); // Add the string bytes after |
| 61 | + return keccak256(ethMessage, 'hex'); |
44 | 62 | }
|
45 | 63 |
|
46 |
| - signEthereumSignedMessage(message: string, privateKey: string): Message { |
47 |
| - if (!privateKey.startsWith('0x')) { |
48 |
| - privateKey = '0x' + privateKey; |
49 |
| - } |
50 |
| - |
51 |
| - // 64 hex characters + hex-prefix |
52 |
| - if (privateKey.length !== 66) { |
53 |
| - throw new Error('Private key must be 32 bytes long'); |
54 |
| - } |
55 |
| - |
| 64 | + async signEthereumSignedMessage( |
| 65 | + message: string, |
| 66 | + privateKey: `0x${string}`, |
| 67 | + ): Promise<Message> { |
56 | 68 | const hash = this.hashEthereumSignedMessage(message);
|
57 |
| - const signature = Account.sign(hash, privateKey); |
58 |
| - const vrs = Account.decodeSignature(signature); |
| 69 | + const signature = await sign({ hash, privateKey, to: 'object' }); |
59 | 70 | return {
|
| 71 | + v: BigInt(0), |
| 72 | + ...signature, |
60 | 73 | message: message,
|
61 | 74 | messageHash: hash,
|
62 |
| - v: vrs[0], |
63 |
| - r: vrs[1], |
64 |
| - s: vrs[2], |
65 |
| - signature: signature, |
| 75 | + signature: serializeSignature(signature), |
66 | 76 | };
|
67 | 77 | }
|
68 | 78 |
|
69 |
| - signDataWithIntendedValidator( |
70 |
| - validatorAddress: string, |
71 |
| - data: string, |
72 |
| - privateKey: string, |
73 |
| - ): Message { |
74 |
| - if (!privateKey.startsWith('0x')) { |
75 |
| - privateKey = '0x' + privateKey; |
76 |
| - } |
77 |
| - |
78 |
| - // 64 hex characters + hex-prefix |
79 |
| - if (privateKey.length !== 66) { |
80 |
| - throw new Error('Private key must be 32 bytes long'); |
81 |
| - } |
| 79 | + async signDataWithIntendedValidator( |
| 80 | + validatorAddress: Address, |
| 81 | + data: string | ByteArray, |
| 82 | + privateKey: `0x${string}`, |
| 83 | + ): Promise<Message> { |
82 | 84 | const hash = this.hashDataWithIntendedValidator(validatorAddress, data);
|
83 |
| - const signature = Account.sign(hash, privateKey); |
84 |
| - const vrs = Account.decodeSignature(signature); |
85 |
| - |
| 85 | + const signature = await sign({ hash, privateKey, to: 'object' }); |
86 | 86 | return {
|
| 87 | + v: BigInt(0), |
| 88 | + ...signature, |
87 | 89 | message: data,
|
88 | 90 | messageHash: hash,
|
89 |
| - v: vrs[0], |
90 |
| - r: vrs[1], |
91 |
| - s: vrs[2], |
92 |
| - signature: signature, |
| 91 | + signature: serializeSignature(signature), |
93 | 92 | };
|
94 | 93 | }
|
95 | 94 |
|
96 |
| - recover(messageHash: string | Message, signature: string): string { |
| 95 | + async recover( |
| 96 | + messageHash: `0x${string}` | Message, |
| 97 | + signature: `0x${string}`, |
| 98 | + ): Promise<Address> { |
97 | 99 | if (!!messageHash && typeof messageHash === 'object') {
|
98 | 100 | return this.recover(
|
99 | 101 | messageHash.messageHash,
|
100 |
| - Account.encodeSignature([messageHash.v, messageHash.r, messageHash.s]), |
| 102 | + serializeSignature(messageHash), |
101 | 103 | );
|
102 | 104 | }
|
103 |
| - return Account.recover(messageHash, signature); |
| 105 | + return await recoverAddress({ |
| 106 | + hash: messageHash as `0x${string}`, |
| 107 | + signature, |
| 108 | + }); |
104 | 109 | }
|
105 | 110 | }
|
0 commit comments