Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 2b0487c

Browse files
committed
refactor: rework @ganache/ethereum-address to extend ethereumjs/util's Address (#3777)
1 parent 05ce874 commit 2b0487c

File tree

22 files changed

+248
-182
lines changed

22 files changed

+248
-182
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { Data, JsonRpcDataInputArg } from "@ganache/utils";
1+
import { Address as EJSAddress } from "@ethereumjs/util";
2+
import { JsonRpcDataInputArg, Data } from "@ganache/utils";
23

3-
export class Address extends Data {
4+
export class Address extends EJSAddress {
45
static ByteLength = 20;
56

6-
constructor(value: string | Buffer) {
7-
super(value, Address.ByteLength);
7+
constructor(value: Buffer) {
8+
super(value);
89
}
910

1011
public static from<T extends string | Buffer = string | Buffer>(value: T) {
11-
return new Address(value);
12+
return new Address(Data.toBuffer(value, Address.ByteLength));
1213
}
1314

1415
static toBuffer(value: JsonRpcDataInputArg): Buffer {
@@ -18,4 +19,8 @@ export class Address extends Data {
1819
static toString(value: JsonRpcDataInputArg): string {
1920
return Address.from(value).toString();
2021
}
22+
23+
toJSON() {
24+
return this.toString();
25+
}
2126
}

src/chains/ethereum/address/package-lock.json

Lines changed: 134 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/chains/ethereum/address/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"typescript": "4.7.4"
5757
},
5858
"dependencies": {
59+
"@ethereumjs/util": "8.0.0",
5960
"@ganache/utils": "0.6.0"
6061
}
6162
}

src/chains/ethereum/address/tests/index.test.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Address } from "../";
44
describe("@ganache/ethereum-address", () => {
55
describe("toString()", () => {
66
it("should pad an address to 20 bytes", () => {
7-
const address = new Address("0x1");
7+
const address = Address.from("0x1");
88
const stringifiedAddress = address.toString();
99

1010
assert.strictEqual(
@@ -13,15 +13,10 @@ describe("@ganache/ethereum-address", () => {
1313
);
1414
});
1515

16-
it("should truncate an address to the specified length", () => {
17-
const address = new Address("0x1");
18-
const stringifiedAddress = address.toString(1);
19-
20-
assert.strictEqual(stringifiedAddress, "0x01");
21-
});
22-
2316
it("should stringify a 20 byte address string", () => {
24-
const address = new Address("0x2104859394604359378433865360947116707876");
17+
const address = Address.from(
18+
"0x2104859394604359378433865360947116707876"
19+
);
2520
const stringifiedAddress = address.toString();
2621

2722
assert.strictEqual(
@@ -42,23 +37,18 @@ describe("@ganache/ethereum-address", () => {
4237

4338
describe("toBuffer()", () => {
4439
it("should pad an address to 20 bytes", () => {
45-
const address = new Address("0x1");
40+
const address = Address.from("0x1");
4641
const bufferAddress = address.toBuffer();
4742
const expected = Buffer.alloc(20);
4843
expected[19] = 1;
4944

5045
assert.deepStrictEqual(bufferAddress, expected);
5146
});
5247

53-
it("should truncate an address to the specified length", () => {
54-
const address = new Address("0x2104859394604359378433865360947116707876");
55-
const stringifiedAddress = address.toString(1);
56-
57-
assert.strictEqual(stringifiedAddress, "0x21");
58-
});
59-
6048
it("should convert a 20 byte address to a buffer", () => {
61-
const address = new Address("0x2104859394604359378433865360947116707876");
49+
const address = Address.from(
50+
"0x2104859394604359378433865360947116707876"
51+
);
6252
const bufferAddress = address.toBuffer();
6353
const expected = Buffer.from([
6454
0x21, 0x04, 0x85, 0x93, 0x94, 0x60, 0x43, 0x59, 0x37, 0x84, 0x33, 0x86,
@@ -75,4 +65,16 @@ describe("@ganache/ethereum-address", () => {
7565
assert.deepStrictEqual(bufferAddress, expected);
7666
});
7767
});
68+
69+
describe("toJSON()", () => {
70+
it("should return the address as a string", () => {
71+
const address = Address.from("0x1");
72+
const stringifiedAddress = address.toJSON();
73+
74+
assert.strictEqual(
75+
stringifiedAddress,
76+
"0x0000000000000000000000000000000000000001"
77+
);
78+
});
79+
});
7880
});

src/chains/ethereum/block/src/runtime-block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class RuntimeBlock {
7777
parentHash: Buffer;
7878
difficulty: bigint;
7979
totalDifficulty: Buffer;
80-
coinbase: { buf: Buffer; toBuffer: () => Buffer };
80+
coinbase: Address;
8181
number: bigint;
8282
gasLimit: bigint;
8383
gasUsed: bigint;
@@ -105,7 +105,7 @@ export class RuntimeBlock {
105105
const coinbaseBuffer = coinbase.toBuffer();
106106
this.header = {
107107
parentHash: parentHash.toBuffer(),
108-
coinbase: { buf: coinbaseBuffer, toBuffer: () => coinbaseBuffer },
108+
coinbase: new Address(coinbaseBuffer),
109109
number: number.toBigInt(),
110110
difficulty: difficulty.toBigInt(),
111111
totalDifficulty: Quantity.toBuffer(

src/chains/ethereum/block/tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("@ganache/ethereum-block", async () => {
3232
});
3333
const wallet = new Wallet(options.wallet, options.logging);
3434
const [from, to] = wallet.addresses;
35-
const fromAddress = new Address(from);
35+
const fromAddress = Address.from(from);
3636
const tx: Transaction = {
3737
type: "0x2",
3838
from: from,

0 commit comments

Comments
 (0)