Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ERC-55 support to ExecutionAddress #6355

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
"types": "lib/index.d.ts",
"dependencies": {
"@chainsafe/ssz": "^0.14.0",
"@lodestar/params": "^1.15.0"
"@lodestar/params": "^1.15.0",
"@lodestar/utils": "^1.15.0",
jeluard marked this conversation as resolved.
Show resolved Hide resolved
"ethereum-cryptography": "^1.2.0"
jeluard marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [
"ethereum",
Expand Down
3 changes: 2 additions & 1 deletion packages/types/src/primitive/sszTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ByteVectorType, UintNumberType, UintBigintType, BooleanType} from "@chainsafe/ssz";
import {ExecutionAddressType} from "../utils/ExecutionAddress.js";

export const Boolean = new BooleanType();
export const Byte = new UintNumberType(1);
Expand Down Expand Up @@ -61,4 +62,4 @@ export const BLSPubkey = Bytes48;
export const BLSSignature = Bytes96;
export const Domain = Bytes32;
export const ParticipationFlags = new UintNumberType(1, {setBitwiseOR: true});
export const ExecutionAddress = Bytes20;
export const ExecutionAddress = new ExecutionAddressType();
40 changes: 40 additions & 0 deletions packages/types/src/utils/ExecutionAddress.ts
jeluard marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {keccak256} from "ethereum-cryptography/keccak.js";
import {ByteVectorType, fromHexString, toHexString} from "@chainsafe/ssz";

function isAddressValid(address: string): boolean {
return /^(0x)?[0-9a-f]{40}$/i.test(address);
}

/**
* Formats an address according to [ERC55](https://eips.ethereum.org/EIPS/eip-55)
*/
export function toChecksumAddress(address: string): string {
if (!isAddressValid(address)) {
throw new Error("Invalid address");
jeluard marked this conversation as resolved.
Show resolved Hide resolved
}

const rawAddress = address.toLowerCase().startsWith("0x") ? address.slice(2) : address;
const bytes = fromHexString(rawAddress);
const hash = toHexString(keccak256(bytes)).slice(2);
let checksumAddress = "0x";
for (let i = 0; i < rawAddress.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
checksumAddress += rawAddress[i].toUpperCase();
} else {
checksumAddress += rawAddress[i];
}
}
return checksumAddress;
}

export type ByteVector = Uint8Array;

export class ExecutionAddressType extends ByteVectorType {
jeluard marked this conversation as resolved.
Show resolved Hide resolved
constructor() {
super(20, {typeName: "ExecutionAddress"});
}
toJson(value: ByteVector): unknown {
const str = super.toJson(value) as string;
return toChecksumAddress(str);
}
}
61 changes: 61 additions & 0 deletions packages/types/test/unit/ExecutionAddress.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {describe, it, expect} from "vitest";
import {toChecksumAddress} from "../../src/utils/ExecutionAddress.js";

describe("toChecksumAddress", () => {
it("should fail with invalid addresses", () => {
expect(() => toChecksumAddress("0x1234")).toThrowError("Invalid address");
jeluard marked this conversation as resolved.
Show resolved Hide resolved
});

it("should format address as ERC55", () => {
type TestCase = {
address: string;
checksumedAddress: string;
jeluard marked this conversation as resolved.
Show resolved Hide resolved
};

expect(toChecksumAddress("52908400098527886E0F7030069857D2E4169EE7")).toBe(
jeluard marked this conversation as resolved.
Show resolved Hide resolved
"0x52908400098527886E0F7030069857D2E4169EE7"
);

const testCases: TestCase[] = [
// All caps
{
address: "0x52908400098527886E0F7030069857D2E4169EE7",
checksumedAddress: "0x52908400098527886E0F7030069857D2E4169EE7",
},
jeluard marked this conversation as resolved.
Show resolved Hide resolved
{
address: "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
checksumedAddress: "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
},
// All lower
{
address: "0xde709f2102306220921060314715629080e2fb77",
checksumedAddress: "0xdE709F2102306220921060314715629080e2fB77",
},
{
address: "0x27b1fdb04752bbc536007a920d24acb045561c26",
checksumedAddress: "0x27B1FDb04752BBC536007A920d24acB045561C26",
},
// Normal
{
address: "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
checksumedAddress: "0x5AAEB6053F3E94C9b9A09F33669435E7Ef1BeAED",
jeluard marked this conversation as resolved.
Show resolved Hide resolved
},
{
address: "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
checksumedAddress: "0xFB6916095ca1dF60BB79Ce92cE3Ea74C37C5D359",
},
{
address: "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
checksumedAddress: "0xdBF03B407c01E7CD3CBEA99509d93F8DDDC8C6FB",
},
{
address: "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
checksumedAddress: "0xD1220A0cf47C7B9BE7A2E6BA89F429762e7B9ADB",
},
];

for (const {address, checksumedAddress} of testCases) {
expect(toChecksumAddress(address)).toBe(checksumedAddress);
}
});
});
Loading