-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathethereum.test.ts
164 lines (129 loc) · 4.63 KB
/
ethereum.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { expect } from "$std/expect/mod.ts";
import { describe, it } from "$std/testing/bdd.ts";
import {
decodeRlp,
isAddress,
recoverAddress,
Signature,
Transaction,
verifyMessage,
Wallet,
} from "$ethers";
import { ECDSA } from "./ethereum.ts";
import { Point } from "./point.ts";
import { int2Hex } from "./utils.ts";
describe("Ethereum ECDSA", () => {
it("should compute a correct address", async () => {
const ecdsa = new ECDSA();
const address = await ecdsa.getAddress();
const isValid = isAddress(address);
expect(isValid).toBe(true);
});
it("should generate a valid message signature", async () => {
const privateKey = BigInt(
"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
);
const message = new Uint8Array([0, 1, 2, 3, 4]);
const ecdsa = new ECDSA(privateKey);
const address = await ecdsa.getAddress();
const signature = await ecdsa.signMessage(message);
const r = signature.r;
const s = signature.s;
const v = signature.v!;
const recoveredAddress = verifyMessage(
message,
Signature.from({
r: int2Hex(r),
s: int2Hex(s),
v,
}),
);
expect(
address.toLocaleLowerCase(),
).toBe(
recoveredAddress.toLocaleLowerCase(),
);
});
it("should verify a valid message signature", async () => {
const privateKey =
"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
const message = new Uint8Array([0, 1, 2, 3, 4]);
const signer = new Wallet(privateKey);
const signature = signer.signMessageSync(message);
// Deserialize uncompressed public key.
const { publicKey: rawPublicKey } = signer.signingKey;
const publicKey = rawPublicKey.slice(4, rawPublicKey.length); // Remove trailing `0x04`.
const x = `0x${publicKey.slice(0, publicKey.length / 2)}`; // The x-value is the first half.
const y = `0x${publicKey.slice(publicKey.length / 2, publicKey.length)}`; // The y-value is the second half.
// Deserialize signature (one hex value = 1 nibble).
const r = `0x${signature.slice(2, 66)}`; // 66 - 2 = 64 (nibbles) -> 64 * 4 = 256 bit.
const s = `0x${signature.slice(66, 130)}`; // 130 - 66 = 64 (nibbles) -> 64 * 4 = 256 bit.
const ecdsa = new ECDSA();
const pk = new Point(ecdsa.curve, BigInt(x), BigInt(y));
const isValid = await ecdsa.verifyMessage(pk, message, {
r: BigInt(r),
s: BigInt(s),
});
expect(isValid).toBe(true);
});
it("should generate a valid transaction signature", async () => {
const privateKey = BigInt(
"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
);
const ecdsa = new ECDSA(privateKey);
const address = await ecdsa.getAddress();
const serializedTx = Transaction.from({
to: "0x6502a0EC977DE9C2d4E3864A8D6Cc07E38646E6f",
value: 1n,
});
const signature = await ecdsa.sign(serializedTx.unsignedHash);
const r = signature.r;
const s = signature.s;
const v = signature.v!;
serializedTx.signature = Signature.from({
r: int2Hex(r),
s: int2Hex(s),
v,
});
const recoveredAddress = recoverAddress(
serializedTx.unsignedHash,
serializedTx.signature,
);
expect(
address.toLocaleLowerCase(),
).toBe(
recoveredAddress.toLocaleLowerCase(),
);
});
it("should verify a valid transaction signature", async () => {
const privateKey =
"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
const serializedTx = Transaction.from({
to: "0x6502a0EC977DE9C2d4E3864A8D6Cc07E38646E6f",
value: 1n,
});
const signer = new Wallet(privateKey);
const signature = await signer.signTransaction(serializedTx);
// Deserialize uncompressed public key.
const { publicKey: rawPublicKey } = signer.signingKey;
const publicKey = rawPublicKey.slice(4, rawPublicKey.length); // Remove trailing `0x04`.
const x = `0x${publicKey.slice(0, publicKey.length / 2)}`; // The x-value is the first half.
const y = `0x${publicKey.slice(publicKey.length / 2, publicKey.length)}`; // The y-value is the second half.
// Deserialize signature.
const rlp = signature.slice(4);
const decoded = decodeRlp(`0x${rlp}`);
const r = decoded[decoded.length - 2] as string;
const s = decoded[decoded.length - 1] as string;
const ecdsa = new ECDSA();
const pk = new Point(ecdsa.curve, BigInt(x), BigInt(y));
const isValid = await ecdsa.verifyTransaction(
pk,
serializedTx.unsignedHash,
{
r: BigInt(r),
s: BigInt(s),
},
);
expect(isValid).toBe(true);
});
});