This repository has been archived by the owner on Jun 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.ts
633 lines (579 loc) · 19.3 KB
/
util.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import { Script, Hash, utils, HexNumber, HexString } from "@ckb-lumos/base";
import { GodwokenUtils, RawL2Transaction, L2Transaction } from "./godwoken";
import {
SerializeL2Transaction,
SerializeRawL2Transaction,
} from "./godwoken/schemas";
import {
NormalizeL2Transaction,
NormalizeRawL2Transaction,
} from "./godwoken/normalizer";
import { Reader } from "ckb-js-toolkit";
const jaysonBrowserClient = require("jayson/lib/client/browser");
const U128_MIN = BigInt(0);
const U128_MAX = BigInt(2) ** BigInt(128) - BigInt(1);
const EMPTY_ETH_ADDRESS = "0x" + "00".repeat(20);
export type EthTransaction = {
from: HexString;
to: HexString;
gas?: HexNumber;
gasPrice?: HexNumber;
value: HexNumber;
data: HexString;
nonce?: HexNumber;
};
export type L2TransactionArgs = {
to_id: number;
value: bigint;
data: HexString;
};
export type GodwokerOption = {
godwoken: {
rollup_type_hash: Hash;
eth_account_lock: Omit<Script, "args">;
};
queryEthAddressByShortAddress?: (short_address: string) => string;
saveEthAddressShortAddressMapping?: (
eth_address: string,
short_address: string
) => void;
request_option?: object;
};
export type RequestRpcResult = {
err: any;
data?: string;
};
export class Godwoker {
private eth_account_lock: Omit<Script, "args">;
private rollup_type_hash: string;
private client: any;
private godwkenUtils: GodwokenUtils;
private queryEthAddressByShortAddress;
private saveEthAddressShortAddressMapping;
constructor(host: string, option: GodwokerOption) {
const callServer = function (request: any, callback: any) {
const opt = option.request_option || {
method: "POST",
body: request,
headers: {
"Content-Type": "application/json",
},
};
fetch(host, opt)
.then(function (res) {
return res.text();
})
.then(function (text) {
callback(null, text);
})
.catch(function (err) {
callback(err);
});
};
this.client = jaysonBrowserClient(callServer);
this.godwkenUtils = new GodwokenUtils(option.godwoken.rollup_type_hash);
this.eth_account_lock = option.godwoken.eth_account_lock;
this.rollup_type_hash = option.godwoken.rollup_type_hash;
this.queryEthAddressByShortAddress = option.queryEthAddressByShortAddress;
this.saveEthAddressShortAddressMapping =
option.saveEthAddressShortAddressMapping;
}
packSignature(_signature: Hash): Hash {
let v = Number.parseInt(_signature.slice(-2), 16);
if (v >= 27) v -= 27;
const signature = _signature.slice(0, -2) + v.toString(16).padStart(2, "0");
return signature;
}
computeScriptHashByEoaEthAddress(eth_address: string): string {
const layer2_lock: Script = {
code_hash: this.eth_account_lock.code_hash,
hash_type: this.eth_account_lock.hash_type as "type" | "data",
args: this.rollup_type_hash + eth_address.slice(2),
};
const lock_hash = utils.computeScriptHash(layer2_lock);
return lock_hash;
}
async getScriptByScriptHash(_script_hash: string): Promise<Script> {
return new Promise((resolve, reject) => {
this.client.request(
"gw_get_script",
[_script_hash],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(`unable to fetch script from ${_script_hash}`)
);
return resolve(res.result);
}
);
});
}
async getScriptHashByAccountId(account_id: number): Promise<string> {
return new Promise((resolve, reject) => {
this.client.request(
"gw_get_script_hash",
[`0x${BigInt(account_id).toString(16)}`],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(
`unable to fetch account script hash from 0x${BigInt(
account_id
).toString(16)}`
)
);
return resolve(res.result);
}
);
});
}
async getAccountIdByScriptHash(script_hash: string): Promise<string> {
return new Promise((resolve, reject) => {
this.client.request(
"gw_get_account_id_by_script_hash",
[script_hash],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(
`unable to fetch account id from script hash ${script_hash}`
)
);
return resolve(res.result);
}
);
});
}
async getAccountIdByEoaEthAddress(eth_address: string): Promise<string> {
const layer2_lock: Script = {
code_hash: this.eth_account_lock.code_hash,
hash_type: this.eth_account_lock.hash_type as "type" | "data",
args: this.rollup_type_hash + eth_address.slice(2),
};
const lock_hash = utils.computeScriptHash(layer2_lock);
return new Promise((resolve, reject) => {
this.client.request(
"gw_get_account_id_by_script_hash",
[lock_hash],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(
`unable to fetch account id from ${eth_address}, lock_hash is ${lock_hash}`
)
);
return resolve(res.result);
}
);
});
}
async getScriptHashByShortAddress(_address: string): Promise<string> {
return new Promise((resolve, reject) => {
this.client.request(
"gw_get_script_hash_by_short_address",
[_address],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || !res || res.result === undefined || res.result === null)
return reject(
new Error(
`unable to fetch script hash from short address: ${_address}`
)
);
return resolve(res.result);
}
);
});
}
computeShortAddressByEoaEthAddress(
_address: string,
write_callback?: (eth_address: string, short_address: string) => void
): string {
const short_address = this.computeScriptHashByEoaEthAddress(_address).slice(
0,
42
);
if (write_callback) {
write_callback(_address, short_address);
}
return short_address;
}
async getShortAddressByAllTypeEthAddress(_address: string): Promise<string> {
// todo: support create2 address in such case that it haven't create real contract yet.
try {
// assume it is an contract address (thus already an short address)
await this.getScriptHashByShortAddress(_address);
return _address;
} catch (error) {
// script hash not exist with short address, assume it is EOA address..
// remember to save the script and eoa address mapping with default or user-specific callback
const write_callback = this.saveEthAddressShortAddressMapping
? this.saveEthAddressShortAddressMapping
: this.defaultSaveEthAddressShortAddressMapping.bind(this);
return this.computeShortAddressByEoaEthAddress(_address, write_callback);
}
}
async getEthAddressByAllTypeShortAddress(_short_address: string) {
// todo: support create2 address in such case which it haven't create real contract yet.
try {
// first, query on-chain
const script_hash = await this.getScriptHashByShortAddress(
_short_address
);
const script = await this.getScriptByScriptHash(script_hash);
if (script.code_hash === this.eth_account_lock.code_hash) {
return "0x" + script.args.slice(66, 106);
}
// assume it is normal contract address.
return _short_address;
} catch (error) {
// not on-chain, asume it is eoa address
// which haven't create account on godwoken yet
const query_callback = this.queryEthAddressByShortAddress
? this.queryEthAddressByShortAddress
: this.defaultQueryEthAddressByShortAddress.bind(this);
const eth_address = await query_callback(_short_address);
// check address and short_address indeed matched.
if (this.checkEthAddressIsEoa(eth_address, _short_address)) {
return eth_address;
} else {
throw Error(
`query result of eoa address ${_short_address} with ${_short_address} is not match!`
);
}
}
}
// re-compute the eth address with code_hash info to make sure
// it indeed match with short_address
checkEthAddressIsEoa(
eth_address: string,
_target_short_address: string
): boolean {
const source_short_address =
this.computeShortAddressByEoaEthAddress(eth_address);
console.log(source_short_address, _target_short_address);
return (
source_short_address.toLowerCase() === _target_short_address.toLowerCase()
);
}
// default method
async defaultQueryEthAddressByShortAddress(
_short_address: string
): Promise<string> {
return new Promise((resolve, reject) => {
this.client.request(
"poly_getEthAddressByGodwokenShortAddress",
[_short_address],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(`unable to fetch eth address from ${_short_address}`)
);
return resolve(res.result);
}
);
});
}
// default method
async defaultSaveEthAddressShortAddressMapping(
_eth_address: string,
_short_address: string
) {
return new Promise((resolve, reject) => {
this.client.request(
"poly_saveEthAddressGodwokenShortAddressMapping",
[_eth_address, _short_address],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result !== "ok")
return reject(
new Error(
`unable to save eth address and short address in web3 server.`
)
);
return resolve(res.result);
}
);
});
}
async getNonce(account_id: number): Promise<string> {
return new Promise((resolve, reject) => {
this.client.request(
"gw_get_nonce",
[`0x${account_id.toString(16)}`],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(
`unable to fetch nonce, account_id:${account_id}, ${JSON.stringify(
res
)}`
)
);
return resolve(res.result);
}
);
});
}
async assembleRawL2Transaction(
eth_tx: EthTransaction
): Promise<RawL2Transaction> {
const from = await this.getAccountIdByEoaEthAddress(eth_tx.from);
const to = await this.allTypeEthAddressToAccountId(eth_tx.to);
const nonce = await this.getNonce(parseInt(from));
const encodedArgs = this.encodeArgs(eth_tx);
const tx: RawL2Transaction = {
from_id: "0x" + BigInt(from).toString(16),
to_id: "0x" + BigInt(to).toString(16),
args: encodedArgs,
nonce: "0x" + BigInt(nonce).toString(16),
};
return tx;
}
generateTransactionMessageToSign(
tx: RawL2Transaction,
sender_script_hash: string,
receiver_script_hash: string,
is_add_prefix_in_signing_message: boolean = false
) {
return this.godwkenUtils.generateTransactionMessageToSign(
tx,
sender_script_hash,
receiver_script_hash,
is_add_prefix_in_signing_message
);
}
async generateMessageFromEthTransaction(tx: EthTransaction) {
const { from, to } = tx;
const to_id = await this.allTypeEthAddressToAccountId(to);
const sender_script_hash = this.computeScriptHashByEoaEthAddress(from);
const receiver_script_hash = await this.getScriptHashByAccountId(
parseInt(to_id)
);
const polyjuice_tx = await this.assembleRawL2Transaction(tx);
const message = this.generateTransactionMessageToSign(
polyjuice_tx,
sender_script_hash,
receiver_script_hash,
true // with personal sign prefixed
);
return message;
}
serializeL2Transaction(tx: L2Transaction) {
const _tx = NormalizeL2Transaction(tx);
return new Reader(SerializeL2Transaction(_tx)).serializeJson();
}
serializeRawL2Transaction(tx: RawL2Transaction) {
const _tx = NormalizeRawL2Transaction(tx);
return new Reader(SerializeRawL2Transaction(_tx)).serializeJson();
}
async gw_executeL2Tranaction(
raw_tx: RawL2Transaction,
signature: HexString
): Promise<string> {
const l2_tx = { raw: raw_tx, signature: signature };
const serialize_tx = this.serializeL2Transaction(l2_tx);
return new Promise((resolve, reject) => {
this.client.request(
"gw_execute_l2_tranaction",
[serialize_tx],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(
`failed to send gw_executeL2Tranaction rpc, ${JSON.stringify(
res
)}`
)
);
return resolve(res.result);
}
);
});
}
async gw_executeRawL2Transaction(raw_tx: RawL2Transaction): Promise<any> {
const serialize_tx = this.serializeRawL2Transaction(raw_tx);
return new Promise((resolve, reject) => {
this.client.request(
"gw_execute_raw_l2transaction",
[serialize_tx],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(
`failed to send gw_executeRawL2Tranaction rpc, ${JSON.stringify(
res
)}`
)
);
return resolve(res.result);
}
);
});
}
async gw_submitL2Transaction(
raw_tx: RawL2Transaction,
signature: HexString
): Promise<string> {
const l2_tx = { raw: raw_tx, signature: signature };
const serialize_tx = this.serializeL2Transaction(l2_tx);
return new Promise((resolve, reject) => {
this.client.request(
"gw_submit_l2transaction",
[serialize_tx],
(err: any, res: any) => {
if (err) return reject(err);
if (!res || res.result === undefined || res.result === null)
return reject(
new Error(
`failed to send gw_submitL2Transaction rpc, ${JSON.stringify(
res
)}`
)
);
return resolve(res.result);
}
);
});
}
async gw_getTransactionReceipt(tx_hash: Hash): Promise<string> {
return new Promise((resolve, reject) => {
this.client.request(
"gw_get_transaction_receipt",
[tx_hash],
(err: any, res: any) => {
if (err) return reject(err);
//if(!res || res.result === undefined || res.result === null) resolve( Error(`failed to send gw_getTransactionReceipt rpc, ${JSON.stringify(res)}`);
return resolve(res.result);
}
);
});
}
async eth_getTransactionReceipt(tx_hash: Hash): Promise<string> {
return new Promise((resolve, reject) => {
this.client.request(
"eth_getTransactionReceipt",
[tx_hash],
(err: any, res: any) => {
if (err) return reject(err);
//if(!res || res.result === undefined || res.result === null) resolve( Error(`failed to send gw_getTransactionReceipt rpc, ${JSON.stringify(res)}`);
return resolve(res.result);
}
);
});
}
async waitForTransactionReceipt(tx_hash: Hash) {
while (true) {
await this.asyncSleep(3000);
const tx_receipt = await this.eth_getTransactionReceipt(tx_hash);
console.log(`keep waitting for tx_receipt..`);
if (tx_receipt) {
break;
}
}
return;
}
asyncSleep(ms = 0) {
return new Promise((r) => setTimeout(r, ms));
}
async allTypeEthAddressToAccountId(_address: HexString): Promise<HexNumber> {
// todo: support create2 address in such case that it haven't create real contract yet.
const address = Buffer.from(_address.slice(2), "hex");
if (address.byteLength !== 20)
throw new Error(`Invalid eth address length: ${address.byteLength}`);
if (address.equals(Buffer.from(Array(20).fill(0))))
// special-case: meta-contract address should return creator
return "0x3";
try {
// assume it is normal contract address, thus an godwoken-short-address
const script_hash = await this.getScriptHashByShortAddress(_address);
return await this.getAccountIdByScriptHash(script_hash);
} catch (error) {
if (
!JSON.stringify(error).includes(
"unable to fetch script hash from short address"
)
)
throw error;
// otherwise, assume it is EOA address
const script_hash = this.computeScriptHashByEoaEthAddress(_address);
const accountId = await this.getAccountIdByScriptHash(script_hash);
return accountId;
}
}
encodeArgs(_tx: EthTransaction) {
const { to, gasPrice, gas: gasLimit, value, data } = _tx;
// header
const args_0_7 =
"0x" +
Buffer.from("FFFFFF", "hex").toString("hex") +
Buffer.from("POLY", "utf8").toString("hex");
// gas limit
const args_8_16 = this.UInt64ToLeBytes(BigInt(gasLimit));
// gas price
const args_16_32 = this.UInt128ToLeBytes(
gasPrice === "0x" ? BigInt(0) : BigInt(gasPrice)
);
// value
const args_32_48 = this.UInt128ToLeBytes(
value === "0x" ? BigInt(0) : BigInt(value)
);
const dataByteLength = Buffer.from(data.slice(2), "hex").length;
// data length
const args_48_52 = this.UInt32ToLeBytes(dataByteLength);
// data
const args_data = data;
let args_7 = "";
if (to === EMPTY_ETH_ADDRESS || to === "0x" || to === "0x0") {
args_7 = "0x03";
} else {
args_7 = "0x00";
}
const args =
"0x" +
args_0_7.slice(2) +
args_7.slice(2) +
args_8_16.slice(2) +
args_16_32.slice(2) +
args_32_48.slice(2) +
args_48_52.slice(2) +
args_data.slice(2);
return args;
}
// todo: move to another file
UInt32ToLeBytes(num: number): HexString {
const buf = Buffer.allocUnsafe(4);
buf.writeUInt32LE(+num, 0);
return "0x" + buf.toString("hex");
}
UInt64ToLeBytes(num: bigint): HexString {
num = BigInt(num);
const buf = Buffer.alloc(8);
buf.writeBigUInt64LE(num);
return `0x${buf.toString("hex")}`;
}
UInt128ToLeBytes(u128: bigint): HexString {
if (u128 < U128_MIN) {
throw new Error(`u128 ${u128} too small`);
}
if (u128 > U128_MAX) {
throw new Error(`u128 ${u128} too large`);
}
const buf = Buffer.alloc(16);
buf.writeBigUInt64LE(u128 & BigInt("0xFFFFFFFFFFFFFFFF"), 0);
buf.writeBigUInt64LE(u128 >> BigInt(64), 8);
return "0x" + buf.toString("hex");
}
LeBytesToUInt32(hex: HexString): number {
const buf = Buffer.from(hex.slice(2), "hex");
return buf.readUInt32LE();
}
}