|
| 1 | +import { bufferToHex, privateToAddress } from '@ethereumjs/util'; |
| 2 | + |
| 3 | +import { |
| 4 | + signEIP7702Authorization, |
| 5 | + recoverEIP7702Authorization, |
| 6 | + EIP7702Authorization, |
| 7 | + hashEIP7702Authorization, |
| 8 | +} from './sign-eip7702-authorization'; |
| 9 | + |
| 10 | +const TEST_PRIVATE_KEY = Buffer.from( |
| 11 | + '4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0', |
| 12 | + 'hex', |
| 13 | +); |
| 14 | + |
| 15 | +const TEST_ADDRESS = bufferToHex(privateToAddress(TEST_PRIVATE_KEY)); |
| 16 | + |
| 17 | +const TEST_AUTHORIZATION: EIP7702Authorization = [ |
| 18 | + 8545, |
| 19 | + '0x1234567890123456789012345678901234567890', |
| 20 | + 1, |
| 21 | +]; |
| 22 | + |
| 23 | +const EXPECTED_AUTHORIZATION_HASH = Buffer.from( |
| 24 | + 'b847dee5b33802280f3279d57574e1eb6bf5d628d7f63049e3cb20bad211056c', |
| 25 | + 'hex', |
| 26 | +); |
| 27 | + |
| 28 | +const EXPECTED_SIGNATURE = |
| 29 | + '0xebea1ac12f17a56a514dfecbcbc8bbee7b089fa3fcee31680d1e2c1588f623df7973cab74e12536678995377da38c96c65c52897750b73462c6760ef2737dba41b'; |
| 30 | + |
| 31 | +describe('signAuthorization', () => { |
| 32 | + describe('signAuthorization()', () => { |
| 33 | + it('should produce the correct signature', () => { |
| 34 | + const signature = signEIP7702Authorization({ |
| 35 | + privateKey: TEST_PRIVATE_KEY, |
| 36 | + authorization: TEST_AUTHORIZATION, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(signature).toBe(EXPECTED_SIGNATURE); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should throw if private key is null', () => { |
| 43 | + expect(() => |
| 44 | + signEIP7702Authorization({ |
| 45 | + privateKey: null as any, |
| 46 | + authorization: TEST_AUTHORIZATION, |
| 47 | + }), |
| 48 | + ).toThrow('Missing privateKey parameter'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should throw if private key is undefined', () => { |
| 52 | + expect(() => |
| 53 | + signEIP7702Authorization({ |
| 54 | + privateKey: undefined as any, |
| 55 | + authorization: TEST_AUTHORIZATION, |
| 56 | + }), |
| 57 | + ).toThrow('Missing privateKey parameter'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw if authorization is null', () => { |
| 61 | + expect(() => |
| 62 | + signEIP7702Authorization({ |
| 63 | + privateKey: TEST_PRIVATE_KEY, |
| 64 | + authorization: null as any, |
| 65 | + }), |
| 66 | + ).toThrow('Missing authorization parameter'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should throw if authorization is undefined', () => { |
| 70 | + expect(() => |
| 71 | + signEIP7702Authorization({ |
| 72 | + privateKey: TEST_PRIVATE_KEY, |
| 73 | + authorization: undefined as any, |
| 74 | + }), |
| 75 | + ).toThrow('Missing authorization parameter'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should throw if chainId is null', () => { |
| 79 | + expect(() => |
| 80 | + signEIP7702Authorization({ |
| 81 | + privateKey: TEST_PRIVATE_KEY, |
| 82 | + authorization: [ |
| 83 | + null as unknown as number, |
| 84 | + TEST_AUTHORIZATION[1], |
| 85 | + TEST_AUTHORIZATION[2], |
| 86 | + ], |
| 87 | + }), |
| 88 | + ).toThrow('Missing chainId parameter'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should throw if contractAddress is null', () => { |
| 92 | + expect(() => |
| 93 | + signEIP7702Authorization({ |
| 94 | + privateKey: TEST_PRIVATE_KEY, |
| 95 | + authorization: [ |
| 96 | + TEST_AUTHORIZATION[0], |
| 97 | + null as unknown as string, |
| 98 | + TEST_AUTHORIZATION[2], |
| 99 | + ], |
| 100 | + }), |
| 101 | + ).toThrow('Missing contractAddress parameter'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should throw if nonce is null', () => { |
| 105 | + expect(() => |
| 106 | + signEIP7702Authorization({ |
| 107 | + privateKey: TEST_PRIVATE_KEY, |
| 108 | + authorization: [ |
| 109 | + TEST_AUTHORIZATION[0], |
| 110 | + TEST_AUTHORIZATION[1], |
| 111 | + null as unknown as number, |
| 112 | + ], |
| 113 | + }), |
| 114 | + ).toThrow('Missing nonce parameter'); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('hashAuthorization()', () => { |
| 119 | + it('should produce the correct hash', () => { |
| 120 | + const hash = hashEIP7702Authorization(TEST_AUTHORIZATION); |
| 121 | + |
| 122 | + expect(hash).toStrictEqual(EXPECTED_AUTHORIZATION_HASH); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should throw if authorization is null', () => { |
| 126 | + expect(() => |
| 127 | + hashEIP7702Authorization(null as unknown as EIP7702Authorization), |
| 128 | + ).toThrow('Missing authorization parameter'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should throw if authorization is undefined', () => { |
| 132 | + expect(() => |
| 133 | + hashEIP7702Authorization(undefined as unknown as EIP7702Authorization), |
| 134 | + ).toThrow('Missing authorization parameter'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should throw if chainId is null', () => { |
| 138 | + expect(() => |
| 139 | + hashEIP7702Authorization([ |
| 140 | + null as unknown as number, |
| 141 | + TEST_AUTHORIZATION[1], |
| 142 | + TEST_AUTHORIZATION[2], |
| 143 | + ]), |
| 144 | + ).toThrow('Missing chainId parameter'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should throw if contractAddress is null', () => { |
| 148 | + expect(() => |
| 149 | + hashEIP7702Authorization([ |
| 150 | + TEST_AUTHORIZATION[0], |
| 151 | + null as unknown as string, |
| 152 | + TEST_AUTHORIZATION[2], |
| 153 | + ]), |
| 154 | + ).toThrow('Missing contractAddress parameter'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should throw if nonce is null', () => { |
| 158 | + expect(() => |
| 159 | + hashEIP7702Authorization([ |
| 160 | + TEST_AUTHORIZATION[0], |
| 161 | + TEST_AUTHORIZATION[1], |
| 162 | + null as unknown as number, |
| 163 | + ]), |
| 164 | + ).toThrow('Missing nonce parameter'); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + describe('recoverAuthorization()', () => { |
| 169 | + it('should recover the address from a signature', () => { |
| 170 | + const recoveredAddress = recoverEIP7702Authorization({ |
| 171 | + authorization: TEST_AUTHORIZATION, |
| 172 | + signature: EXPECTED_SIGNATURE, |
| 173 | + }); |
| 174 | + |
| 175 | + expect(recoveredAddress).toBe(TEST_ADDRESS); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should throw if signature is null', () => { |
| 179 | + expect(() => |
| 180 | + recoverEIP7702Authorization({ |
| 181 | + signature: null as unknown as string, |
| 182 | + authorization: TEST_AUTHORIZATION, |
| 183 | + }), |
| 184 | + ).toThrow('Missing signature parameter'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should throw if signature is undefined', () => { |
| 188 | + expect(() => |
| 189 | + recoverEIP7702Authorization({ |
| 190 | + signature: undefined as unknown as string, |
| 191 | + authorization: TEST_AUTHORIZATION, |
| 192 | + }), |
| 193 | + ).toThrow('Missing signature parameter'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should throw if authorization is null', () => { |
| 197 | + expect(() => |
| 198 | + recoverEIP7702Authorization({ |
| 199 | + signature: EXPECTED_SIGNATURE, |
| 200 | + authorization: null as unknown as EIP7702Authorization, |
| 201 | + }), |
| 202 | + ).toThrow('Missing authorization parameter'); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should throw if authorization is undefined', () => { |
| 206 | + expect(() => |
| 207 | + recoverEIP7702Authorization({ |
| 208 | + signature: EXPECTED_SIGNATURE, |
| 209 | + authorization: undefined as unknown as EIP7702Authorization, |
| 210 | + }), |
| 211 | + ).toThrow('Missing authorization parameter'); |
| 212 | + }); |
| 213 | + }); |
| 214 | + |
| 215 | + describe('sign-and-recover', () => { |
| 216 | + const testCases = { |
| 217 | + zeroChainId: [0, '0x1234567890123456789012345678901234567890', 1], |
| 218 | + highChainId: [98765, '0x1234567890123456789012345678901234567890', 1], |
| 219 | + zeroNonce: [8545, '0x1234567890123456789012345678901234567890', 0], |
| 220 | + highNonce: [8545, '0x1234567890123456789012345678901234567890', 98765], |
| 221 | + zeroContractAddress: [1, '0x0000000000000000000000000000000000000000', 1], |
| 222 | + allZeroValues: [0, '0x0000000000000000000000000000000000000000', 0], |
| 223 | + } as { [key: string]: EIP7702Authorization }; |
| 224 | + |
| 225 | + it.each(Object.entries(testCases))( |
| 226 | + 'should sign and recover %s', |
| 227 | + (_, authorization) => { |
| 228 | + const signature = signEIP7702Authorization({ |
| 229 | + privateKey: TEST_PRIVATE_KEY, |
| 230 | + authorization, |
| 231 | + }); |
| 232 | + |
| 233 | + const recoveredAddress = recoverEIP7702Authorization({ |
| 234 | + authorization, |
| 235 | + signature, |
| 236 | + }); |
| 237 | + |
| 238 | + expect(recoveredAddress).toBe(TEST_ADDRESS); |
| 239 | + }, |
| 240 | + ); |
| 241 | + }); |
| 242 | +}); |
0 commit comments