Skip to content

Commit

Permalink
Renamed a couple of authorization symbols to explicitly be EIP-7702, …
Browse files Browse the repository at this point in the history
…shuffled non-exported members to the bottom of the file, and renamed a few test constants to aid readability. Also used it.each for multiple test cases.
  • Loading branch information
jeffsmale90 committed Jan 13, 2025
1 parent 9d7ea1b commit 0222c87
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 113 deletions.
6 changes: 3 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Array [
"decrypt",
"decryptSafely",
"getEncryptionPublicKey",
"signAuthorization",
"recoverAuthorization",
"hashAuthorization",
"signEIP7702Authorization",
"recoverEIP7702Authorization",
"hashEIP7702Authorization",
]
`);
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './personal-sign';
export * from './sign-typed-data';
export * from './encryption';
export * from './sign-authorization';
export * from './sign-eip7702-authorization';
export { concatSig, normalize } from './utils';
Original file line number Diff line number Diff line change
@@ -1,113 +1,113 @@
import { bufferToHex, privateToAddress } from '@ethereumjs/util';

import {
signAuthorization,
recoverAuthorization,
Authorization,
hashAuthorization,
} from './sign-authorization';
signEIP7702Authorization,
recoverEIP7702Authorization,
EIP7702Authorization,
hashEIP7702Authorization,
} from './sign-eip7702-authorization';

const testPrivateKey = Buffer.from(
const TEST_PRIVATE_KEY = Buffer.from(
'4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0',
'hex',
);

const testAddress = bufferToHex(privateToAddress(testPrivateKey));
const TEST_ADDRESS = bufferToHex(privateToAddress(TEST_PRIVATE_KEY));

const testAuthorization: Authorization = [
const TEST_AUTHORIZATION: EIP7702Authorization = [
8545,
'0x1234567890123456789012345678901234567890',
1,
];

const expectedAuthorizationHash = Buffer.from(
const EXPECTED_AUTHORIZATION_HASH = Buffer.from(
'b847dee5b33802280f3279d57574e1eb6bf5d628d7f63049e3cb20bad211056c',
'hex',
);

const expectedSignature =
const EXPECTED_SIGNATURE =
'0xebea1ac12f17a56a514dfecbcbc8bbee7b089fa3fcee31680d1e2c1588f623df7973cab74e12536678995377da38c96c65c52897750b73462c6760ef2737dba41b';

describe('signAuthorization', () => {
describe('signAuthorization()', () => {
it('should produce the correct signature', () => {
const signature = signAuthorization({
privateKey: testPrivateKey,
authorization: testAuthorization,
const signature = signEIP7702Authorization({
privateKey: TEST_PRIVATE_KEY,
authorization: TEST_AUTHORIZATION,
});

expect(signature).toBe(expectedSignature);
expect(signature).toBe(EXPECTED_SIGNATURE);
});

it('should throw if private key is null', () => {
expect(() =>
signAuthorization({
signEIP7702Authorization({
privateKey: null as any,
authorization: testAuthorization,
authorization: TEST_AUTHORIZATION,
}),
).toThrow('Missing privateKey parameter');
});

it('should throw if private key is undefined', () => {
expect(() =>
signAuthorization({
signEIP7702Authorization({
privateKey: undefined as any,
authorization: testAuthorization,
authorization: TEST_AUTHORIZATION,
}),
).toThrow('Missing privateKey parameter');
});

it('should throw if authorization is null', () => {
expect(() =>
signAuthorization({
privateKey: testPrivateKey,
signEIP7702Authorization({
privateKey: TEST_PRIVATE_KEY,
authorization: null as any,
}),
).toThrow('Missing authorization parameter');
});

it('should throw if authorization is undefined', () => {
expect(() =>
signAuthorization({
privateKey: testPrivateKey,
signEIP7702Authorization({
privateKey: TEST_PRIVATE_KEY,
authorization: undefined as any,
}),
).toThrow('Missing authorization parameter');
});

it('should throw if chainId is null', () => {
expect(() =>
signAuthorization({
privateKey: testPrivateKey,
signEIP7702Authorization({
privateKey: TEST_PRIVATE_KEY,
authorization: [
null as unknown as number,
testAuthorization[1],
testAuthorization[2],
TEST_AUTHORIZATION[1],
TEST_AUTHORIZATION[2],
],
}),
).toThrow('Missing chainId parameter');
});

it('should throw if contractAddress is null', () => {
expect(() =>
signAuthorization({
privateKey: testPrivateKey,
signEIP7702Authorization({
privateKey: TEST_PRIVATE_KEY,
authorization: [
testAuthorization[0],
TEST_AUTHORIZATION[0],
null as unknown as string,
testAuthorization[2],
TEST_AUTHORIZATION[2],
],
}),
).toThrow('Missing contractAddress parameter');
});

it('should throw if nonce is null', () => {
expect(() =>
signAuthorization({
privateKey: testPrivateKey,
signEIP7702Authorization({
privateKey: TEST_PRIVATE_KEY,
authorization: [
testAuthorization[0],
testAuthorization[1],
TEST_AUTHORIZATION[0],
TEST_AUTHORIZATION[1],
null as unknown as number,
],
}),
Expand All @@ -117,48 +117,48 @@ describe('signAuthorization', () => {

describe('hashAuthorization()', () => {
it('should produce the correct hash', () => {
const hash = hashAuthorization(testAuthorization);
const hash = hashEIP7702Authorization(TEST_AUTHORIZATION);

expect(hash).toStrictEqual(expectedAuthorizationHash);
expect(hash).toStrictEqual(EXPECTED_AUTHORIZATION_HASH);
});

it('should throw if authorization is null', () => {
expect(() => hashAuthorization(null as unknown as Authorization)).toThrow(
'Missing authorization parameter',
);
expect(() =>
hashEIP7702Authorization(null as unknown as EIP7702Authorization),
).toThrow('Missing authorization parameter');
});

it('should throw if authorization is undefined', () => {
expect(() =>
hashAuthorization(undefined as unknown as Authorization),
hashEIP7702Authorization(undefined as unknown as EIP7702Authorization),
).toThrow('Missing authorization parameter');
});

it('should throw if chainId is null', () => {
expect(() =>
hashAuthorization([
hashEIP7702Authorization([
null as unknown as number,
testAuthorization[1],
testAuthorization[2],
TEST_AUTHORIZATION[1],
TEST_AUTHORIZATION[2],
]),
).toThrow('Missing chainId parameter');
});

it('should throw if contractAddress is null', () => {
expect(() =>
hashAuthorization([
testAuthorization[0],
hashEIP7702Authorization([
TEST_AUTHORIZATION[0],
null as unknown as string,
testAuthorization[2],
TEST_AUTHORIZATION[2],
]),
).toThrow('Missing contractAddress parameter');
});

it('should throw if nonce is null', () => {
expect(() =>
hashAuthorization([
testAuthorization[0],
testAuthorization[1],
hashEIP7702Authorization([
TEST_AUTHORIZATION[0],
TEST_AUTHORIZATION[1],
null as unknown as number,
]),
).toThrow('Missing nonce parameter');
Expand All @@ -167,46 +167,46 @@ describe('signAuthorization', () => {

describe('recoverAuthorization()', () => {
it('should recover the address from a signature', () => {
const recoveredAddress = recoverAuthorization({
authorization: testAuthorization,
signature: expectedSignature,
const recoveredAddress = recoverEIP7702Authorization({
authorization: TEST_AUTHORIZATION,
signature: EXPECTED_SIGNATURE,
});

expect(recoveredAddress).toBe(testAddress);
expect(recoveredAddress).toBe(TEST_ADDRESS);
});

it('should throw if signature is null', () => {
expect(() =>
recoverAuthorization({
recoverEIP7702Authorization({
signature: null as unknown as string,
authorization: testAuthorization,
authorization: TEST_AUTHORIZATION,
}),
).toThrow('Missing signature parameter');
});

it('should throw if signature is undefined', () => {
expect(() =>
recoverAuthorization({
recoverEIP7702Authorization({
signature: undefined as unknown as string,
authorization: testAuthorization,
authorization: TEST_AUTHORIZATION,
}),
).toThrow('Missing signature parameter');
});

it('should throw if authorization is null', () => {
expect(() =>
recoverAuthorization({
signature: expectedSignature,
authorization: null as unknown as Authorization,
recoverEIP7702Authorization({
signature: EXPECTED_SIGNATURE,
authorization: null as unknown as EIP7702Authorization,
}),
).toThrow('Missing authorization parameter');
});

it('should throw if authorization is undefined', () => {
expect(() =>
recoverAuthorization({
signature: expectedSignature,
authorization: undefined as unknown as Authorization,
recoverEIP7702Authorization({
signature: EXPECTED_SIGNATURE,
authorization: undefined as unknown as EIP7702Authorization,
}),
).toThrow('Missing authorization parameter');
});
Expand All @@ -220,22 +220,23 @@ describe('signAuthorization', () => {
highNonce: [8545, '0x1234567890123456789012345678901234567890', 98765],
zeroContractAddress: [1, '0x0000000000000000000000000000000000000000', 1],
allZeroValues: [0, '0x0000000000000000000000000000000000000000', 0],
} as { [key: string]: Authorization };
} as { [key: string]: EIP7702Authorization };

for (const [label, authorization] of Object.entries(testCases)) {
it(`should sign and recover ${label}`, () => {
const signature = signAuthorization({
privateKey: testPrivateKey,
it.each(Object.entries(testCases))(
'should sign and recover %s',
(_, authorization) => {
const signature = signEIP7702Authorization({
privateKey: TEST_PRIVATE_KEY,
authorization,
});

const recoveredAddress = recoverAuthorization({
const recoveredAddress = recoverEIP7702Authorization({
authorization,
signature,
});

expect(recoveredAddress).toBe(testAddress);
});
}
expect(recoveredAddress).toBe(TEST_ADDRESS);
},
);
});
});
Loading

0 comments on commit 0222c87

Please sign in to comment.