This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutil.ts
1593 lines (1423 loc) · 47.2 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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Script, Hash, utils, HexNumber, HexString } from "@ckb-lumos/base";
import {
GodwokenUtils,
RawL2Transaction,
L2Transaction,
RunResult,
TransactionReceipt as GwTransactionReceipt,
} from "@polyjuice-provider/godwoken";
import {
SerializeSUDTArgs,
SerializeL2Transaction,
SerializeRawL2Transaction,
} from "@polyjuice-provider/godwoken/schemas";
import {
AddressMapping,
RawL2TransactionWithAddressMapping,
L2TransactionWithAddressMapping,
AddressMappingItem,
} from "@polyjuice-provider/godwoken/lib/addressTypes";
import {
SerializeAddressMapping,
SerializeL2TransactionWithAddressMapping,
SerializeRawL2TransactionWithAddressMapping,
L2TransactionWithAddressMapping as L2TransactionWithAddressMappingClass,
RawL2TransactionWithAddressMapping as RawL2TransactionWithAddressMappingClass,
AddressMapping as AddressMappingClass,
} from "@polyjuice-provider/godwoken/schemas/addressMapping/addressMapping";
import {
UnionType,
SUDTTransfer,
NormalizeSUDTTransfer,
NormalizeL2Transaction,
NormalizeAddressMapping,
NormalizeL2TransactionWithAddressMapping,
NormalizeRawL2Transaction,
NormalizeRawL2TransactionWithAddressMapping,
} from "@polyjuice-provider/godwoken/lib/normalizer";
import {
U128_MIN,
U128_MAX,
DEFAULT_EMPTY_ETH_ADDRESS,
HEX_CHARACTERS,
POLY_MAX_TRANSACTION_GAS_LIMIT,
POLY_MIN_GAS_PRICE,
EMPTY_ABI_ITEM_SERIALIZE_STR,
WAIT_TIMEOUT_MILSECS,
WAIT_LOOP_INTERVAL_MILSECS,
DEFAULT_ETH_TO_CKB_SUDT_DECIMAL,
} from "./constant";
import { Reader } from "@ckb-lumos/toolkit";
import crossFetch from "cross-fetch"; // for nodejs compatibility polyfill
import { Buffer } from "buffer"; // for browser compatibility polyfill
import {
AbiItems,
BlockParameter,
ShortAddress,
ShortAddressType,
SigningMessageType,
} from "./types";
import { AbiItem } from "web3-utils";
import {
MAX_CONTRACT_CODE_SIZE_IN_BYTE,
CONTRACT_BYTE_CODE_HASH_HEAD_IN_BYTE,
CONTRACT_BYTE_CODE_ID_OFFSET,
} from "./constant";
import keccak256 from "keccak256";
// replace for buffer polyfill under 0.6 version.
// eg: for react project using webpack 4 (this is the most common case when created by running `npx create-react-app`),
// the default react-scripts config just use [email protected] which doesn't include writeBigUint64LE function.
// code copy from https://github.com/feross/buffer/blob/master/index.js#L1497-L1513
function writeBigUint64LE(buf: any, value: any, offset = 0) {
let lo = Number(value & BigInt(0xffffffff));
buf[offset++] = lo;
lo = lo >> 8;
buf[offset++] = lo;
lo = lo >> 8;
buf[offset++] = lo;
lo = lo >> 8;
buf[offset++] = lo;
let hi = Number((value >> BigInt(32)) & BigInt(0xffffffff));
buf[offset++] = hi;
hi = hi >> 8;
buf[offset++] = hi;
hi = hi >> 8;
buf[offset++] = hi;
hi = hi >> 8;
buf[offset++] = hi;
return offset;
}
Buffer.prototype.writeBigUInt64LE = function (value, offset) {
return writeBigUint64LE(this, value, offset);
};
const jaysonBrowserClient = require("jayson/lib/client/browser");
declare global {
interface Window {
fetch: any;
}
}
const fetch = typeof window !== "undefined" ? window.fetch : crossFetch;
export interface EthTransactionReceipt {
transactionHash: Hash;
blockHash: Hash;
blockNumber: HexNumber;
transactionIndex: HexNumber;
gasUsed: HexNumber;
cumulativeGasUsed: HexNumber;
logsBloom: HexString;
logs: EthLogItem[];
contractAddress: HexString;
status: EthTransactionStatus;
}
export interface EthLogItem {
address: HexString;
blockHash: Hash;
blockNumber: HexNumber;
transactionIndex: HexNumber;
transactionHash: Hash;
data: HexString;
logIndex: HexNumber;
topics: HexString[];
removed: boolean;
}
export enum EthTransactionStatus {
success = "0x1",
failure = "0x0",
}
export type EthAddress = HexString;
export type EthTransaction = {
from: HexString;
to: HexString;
gas?: HexNumber;
gasPrice?: HexNumber;
value: HexNumber;
data: HexString;
nonce?: HexNumber;
};
export type InformalEthTransaction = {
from?: HexString;
to?: HexString;
gas?: HexNumber | bigint | number;
gasLimit?: HexNumber | bigint | number;
gasPrice?: HexNumber | bigint | number;
value?: HexNumber | bigint | number;
data?: HexString;
nonce?: HexNumber | bigint | number;
};
export type L2TransactionArgs = {
to_id: number;
value: bigint;
data: HexString;
};
export type GodwokerOption = {
godwoken?: {
rollup_type_hash?: Hash;
eth_account_lock?: Omit<Script, "args">;
};
polyjuice?: {
creator_id?: HexNumber;
default_from_address?: HexString;
};
queryEthAddressByShortAddress?: (short_address: string) => string;
saveEthAddressShortAddressMapping?: (
eth_address: string,
short_address: string
) => void;
request_option?: object;
};
export type RequestRpcResult = {
err: any;
data?: string;
};
export enum RequireResult {
canBeEmpty,
canNotBeEmpty,
}
export enum GetTxVerbose {
TxWithStatus = 0,
OnlyStatus = 1,
}
export enum L2TransactionStatus {
Pending,
Committed,
}
export interface L2TransactionView {
inner: L2Transaction;
tx_hash: HexString;
}
export interface L2TransactionWithStatus {
transaction: L2TransactionView | null;
status: L2TransactionStatus;
}
export function formalizeEthToAddress(to_address: string | undefined | null) {
if (to_address === "0x") return DEFAULT_EMPTY_ETH_ADDRESS;
if (!to_address) return DEFAULT_EMPTY_ETH_ADDRESS;
if (typeof to_address === "string" && to_address.length !== 42)
throw new Error(`invalid ETH to_address length ${to_address.length}.`);
if (typeof to_address !== "string")
throw new Error(`invalid type of to_address ${typeof to_address}`);
return to_address;
}
export function verifyHttpUrl(_url: string) {
const url = new URL(_url);
if (url.protocol === "https:" || url.protocol === "http:") {
return true;
}
return false;
}
export function isHexString(value: any, length?: number): boolean {
if (typeof value !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
return false;
}
if (length && value.length !== 2 + 2 * length) {
return false;
}
return true;
}
export function normalizeHexValue(
value: HexNumber | bigint | number
): HexString {
if (typeof value === "number") {
let hex = "";
while (value) {
hex = HEX_CHARACTERS[value & 0xf] + hex;
value = Math.floor(value / 16);
}
if (hex.length) {
if (hex.length % 2) {
hex = "0" + hex;
}
return "0x" + hex;
}
return "0x00";
}
if (typeof value === "bigint") {
value = value.toString(16);
if (value.length % 2) {
return "0x0" + value;
}
return "0x" + value;
}
if (isHexString(value)) {
if ((<string>value).length % 2) {
value = "0x0" + (<string>value).substring(2);
}
return (<string>value).toLowerCase();
}
throw new Error(`invalid hexlify value type ${value}`);
}
export function normalizeEthTransaction(tx: InformalEthTransaction) {
if (!tx.from || typeof tx.from !== "string") {
throw new Error("missing From in Transaction!");
}
return {
from: tx.from,
to: formalizeEthToAddress(tx.to),
gas: normalizeHexValue(
tx.gas || tx.gasLimit || POLY_MAX_TRANSACTION_GAS_LIMIT
),
gasPrice: normalizeHexValue(tx.gasPrice || POLY_MIN_GAS_PRICE),
value: normalizeHexValue(tx.value || "0x00"),
data: normalizeHexValue(tx.data || "0x00"),
};
}
export function serializeAddressMapping(
addressMapping: AddressMapping
): HexString {
const _tx = NormalizeAddressMapping(addressMapping);
return new Reader(SerializeAddressMapping(_tx)).serializeJson();
}
export function deserializeAddressMapping(value: HexString): AddressMapping {
const data = new AddressMappingClass(new Reader(value));
const addresses_len =
"0x" + data.getLength().toLittleEndianUint32().toString(16);
const addresses_len_in_int = parseInt(addresses_len);
return {
length: addresses_len,
data: [...Array(addresses_len_in_int).keys()].map((index) => {
return {
eth_address: new Reader(
data.getData().indexAt(index).getEthAddress().raw()
).serializeJson(),
gw_short_address: new Reader(
data.getData().indexAt(index).getGwShortAddress().raw()
).serializeJson(),
};
}),
};
}
export function serializeRawL2TransactionWithAddressMapping(
rawL2TransactionWithAddressMapping: RawL2TransactionWithAddressMapping
): HexString {
const _tx = NormalizeRawL2TransactionWithAddressMapping(
rawL2TransactionWithAddressMapping
);
return new Reader(
SerializeRawL2TransactionWithAddressMapping(_tx)
).serializeJson();
}
export function deserializeRawL2TransactionWithAddressMapping(
value: HexString
): RawL2TransactionWithAddressMapping {
const data = new RawL2TransactionWithAddressMappingClass(new Reader(value));
const address_length =
"0x" + data.getAddresses().getLength().toLittleEndianUint32().toString(16);
const address_length_in_int = parseInt(address_length);
const raw_tx = {
from_id:
"0x" + data.getRawTx().getFromId().toLittleEndianUint32().toString(16),
to_id: "0x" + data.getRawTx().getToId().toLittleEndianUint32().toString(16),
args: new Reader(data.getRawTx().getArgs().raw()).serializeJson(),
nonce:
"0x" + data.getRawTx().getNonce().toLittleEndianUint32().toString(16),
};
const addressMapping = {
length: address_length,
data: [...Array(address_length_in_int).keys()].map((index) => {
return {
eth_address: new Reader(
data.getAddresses().getData().indexAt(index).getEthAddress().raw()
).serializeJson(),
gw_short_address: new Reader(
data.getAddresses().getData().indexAt(index).getGwShortAddress().raw()
).serializeJson(),
};
}),
};
const rawL2TransactionWithAddressMapping: RawL2TransactionWithAddressMapping =
{
raw_tx: raw_tx,
addresses: addressMapping,
extra: new Reader(data.getExtra().raw()).serializeJson(),
};
return rawL2TransactionWithAddressMapping;
}
export function serializeL2TransactionWithAddressMapping(
l2TransactionWithAddressMapping: L2TransactionWithAddressMapping
): HexString {
const _tx = NormalizeL2TransactionWithAddressMapping(
l2TransactionWithAddressMapping
);
return new Reader(
SerializeL2TransactionWithAddressMapping(_tx)
).serializeJson();
}
export function deserializeL2TransactionWithAddressMapping(
value: HexString
): L2TransactionWithAddressMapping {
const data = new L2TransactionWithAddressMappingClass(new Reader(value));
const address_length =
"0x" + data.getAddresses().getLength().toLittleEndianUint32().toString(16);
const address_length_in_int = parseInt(address_length);
const tx: L2Transaction = {
raw: {
from_id:
"0x" +
data.getTx().getRaw().getFromId().toLittleEndianUint32().toString(16),
to_id:
"0x" +
data.getTx().getRaw().getToId().toLittleEndianUint32().toString(16),
args: new Reader(data.getTx().getRaw().getArgs().raw()).serializeJson(),
nonce:
"0x" +
data.getTx().getRaw().getNonce().toLittleEndianUint32().toString(16),
},
signature: new Reader(data.getTx().getSignature().raw()).serializeJson(),
};
const addressMapping = {
length: address_length,
data: [...Array(address_length_in_int).keys()].map((index) => {
return {
eth_address: new Reader(
data.getAddresses().getData().indexAt(index).getEthAddress().raw()
).serializeJson(),
gw_short_address: new Reader(
data.getAddresses().getData().indexAt(index).getGwShortAddress().raw()
).serializeJson(),
};
}),
};
const rawL2TransactionWithAddressMapping: L2TransactionWithAddressMapping = {
tx: tx,
addresses: addressMapping,
extra: new Reader(data.getExtra().raw()).serializeJson(),
};
return rawL2TransactionWithAddressMapping;
}
export function buildL2TransactionWithAddressMapping(
tx: L2Transaction,
addressMappingItemVec: AddressMappingItem[],
abiItem?: HexString
): L2TransactionWithAddressMapping {
const addressMapping: AddressMapping = {
length: "0x" + addressMappingItemVec.length.toString(16),
data: addressMappingItemVec,
};
return {
tx: tx,
addresses: addressMapping,
extra: abiItem || EMPTY_ABI_ITEM_SERIALIZE_STR,
};
}
export function buildRawL2TransactionWithAddressMapping(
tx: RawL2Transaction,
addressMappingItemVec: AddressMappingItem[],
abiItem?: HexString
): RawL2TransactionWithAddressMapping {
const addressMapping: AddressMapping = {
length: "0x" + addressMappingItemVec.length.toString(16),
data: addressMappingItemVec,
};
return {
raw_tx: tx,
addresses: addressMapping,
extra: abiItem || EMPTY_ABI_ITEM_SERIALIZE_STR,
};
}
export function serializeL2Transaction(tx: L2Transaction): HexString {
const _tx = NormalizeL2Transaction(tx);
return new Reader(SerializeL2Transaction(_tx)).serializeJson();
}
export function serializeRawL2Transaction(tx: RawL2Transaction): HexString {
const _tx = NormalizeRawL2Transaction(tx);
return new Reader(SerializeRawL2Transaction(_tx)).serializeJson();
}
export function decodeArgs(_args: HexString) {
const args = _args.slice(2);
const args_0_7 = "0x" + args.slice(0, 14);
const args_7 = "0x" + args.slice(14, 16);
const args_8_16 = "0x" + args.slice(16, 32);
const args_16_32 = "0x" + args.slice(32, 64);
const args_32_48 = "0x" + args.slice(64, 96);
const args_48_52 = "0x" + args.slice(96, 104);
const args_data = "0x" + args.slice(104);
const header = Buffer.from(args_0_7.slice(8), "hex").toString("utf-8");
const type = args_7;
const gas_limit = "0x" + LeBytesToUInt64(args_8_16).toString(16);
const gas_price = "0x" + LeBytesToUInt128(args_16_32).toString(16);
const value = "0x" + LeBytesToUInt128(args_32_48).toString(16);
const data_length = "0x" + LeBytesToUInt32(args_48_52).toString(16);
const data = args_data;
return { header, type, gas_limit, gas_price, value, data_length, data };
}
export function 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 = UInt64ToLeBytes(BigInt(gasLimit!));
// gas price
const args_16_32 = UInt128ToLeBytes(
gasPrice === "0x" ? BigInt(0) : BigInt(gasPrice!)
);
// value
const args_32_48 = UInt128ToLeBytes(
value === "0x" ? BigInt(0) : BigInt(value)
);
const dataByteLength = Buffer.from(data.slice(2), "hex").length;
// data length
const args_48_52 = UInt32ToLeBytes(dataByteLength);
// data
const args_data = data;
let args_7 = "";
if (to === DEFAULT_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;
}
export function encodeSudtTransferArgs(
toAddress: HexString,
amount: bigint,
fee: bigint
) {
const sudtTransfer: SUDTTransfer = {
to: toAddress,
amount: "0x" + amount.toString(16),
fee: "0x" + fee.toString(16),
};
const sudtArgs: UnionType = {
type: "SUDTTransfer",
value: NormalizeSUDTTransfer(sudtTransfer),
};
const serializedSudtArgs = new Reader(
SerializeSUDTArgs(sudtArgs)
).serializeJson();
return serializedSudtArgs;
}
export function ethToCkb(
value: HexString,
decimal = DEFAULT_ETH_TO_CKB_SUDT_DECIMAL
) {
return BigInt(value) / BigInt(decimal);
}
export function ckbToEth(
value: HexString,
decimal = DEFAULT_ETH_TO_CKB_SUDT_DECIMAL
) {
return BigInt(value) * BigInt(decimal);
}
export class Godwoker {
public eth_account_lock: Omit<Script, "args"> | undefined;
public rollup_type_hash: string | undefined;
public creator_id: HexNumber | undefined;
public default_from_address: HexString | undefined;
public client: any;
public godwokenUtils: GodwokenUtils;
public queryEthAddressByShortAddress;
public 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: Response) {
return res.text();
})
.then(function (text: string) {
callback(null, text);
})
.catch(function (err: Error) {
callback(err);
});
};
this.client = jaysonBrowserClient(callServer);
this.godwokenUtils = 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.creator_id = option?.polyjuice?.creator_id;
this.default_from_address = option?.polyjuice?.default_from_address;
this.queryEthAddressByShortAddress = option?.queryEthAddressByShortAddress;
this.saveEthAddressShortAddressMapping =
option?.saveEthAddressShortAddressMapping;
}
// call init if you haven't pass rollup configs to constructor
async init(): Promise<void> {
if (!this.rollup_type_hash) {
this.rollup_type_hash = await this.getRollupTypeHash();
}
if (!this.eth_account_lock?.code_hash) {
this.eth_account_lock = {
code_hash: await this.getEthAccountLockHash(),
hash_type: "type",
};
}
if (!this.creator_id) {
this.creator_id = await this.getPolyjuiceCreatorAccountId();
}
if (!this.default_from_address) {
this.default_from_address = await this.getPolyjuiceDefaultFromAddress();
}
if (!this.godwokenUtils.rollupTypeHash) {
this.godwokenUtils = new GodwokenUtils(this.rollup_type_hash);
}
}
initSync(): Promise<void> {
const that = this;
const rollupPromise = () => {
return this.rollup_type_hash
? this.rollup_type_hash
: this.getRollupTypeHash();
};
const ethAccountPromise = () => {
return this.eth_account_lock?.code_hash
? this.eth_account_lock?.code_hash
: this.getEthAccountLockHash();
};
const creatorIdPromise = () => {
return this.creator_id
? this.creator_id
: this.getPolyjuiceCreatorAccountId();
};
const defaultFromAddressPromise = () => {
return this.default_from_address
? this.default_from_address
: this.getPolyjuiceDefaultFromAddress();
};
return Promise.all([
rollupPromise(),
ethAccountPromise(),
creatorIdPromise(),
defaultFromAddressPromise(),
])
.then(function (args) {
that.rollup_type_hash = args[0];
that.eth_account_lock = {
code_hash: args[1],
hash_type: "type",
};
that.creator_id = args[2];
that.default_from_address = args[3];
if (!that.godwokenUtils.rollupTypeHash)
that.godwokenUtils = new GodwokenUtils(that.rollup_type_hash);
return Promise.resolve();
})
.catch(function (err) {
return Promise.reject(err);
});
}
packSignature(_signature: HexString): HexString {
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;
}
async jsonRPC(
method: string,
params: any[],
_errMsgWhenNoResult?: string | null,
requireResult = RequireResult.canNotBeEmpty
): Promise<any> {
const errMsgWhenNoResult = _errMsgWhenNoResult || "";
const errWhenNoResult = new Error(
`result from jsonRPC ${method} is null or undefined. ${errMsgWhenNoResult}`
);
return new Promise((resolve, reject) => {
this.client.request(method, params, (err: any, res: any) => {
if (err) return reject(err);
if (!res) return reject(new Error("Rpc Response not found!"));
if (res.error) return reject(res.error);
if (requireResult === RequireResult.canBeEmpty)
return resolve(res.result); // here result might be non-exist
if (res.result === undefined || res.result === null)
return reject(errWhenNoResult);
return resolve(res.result);
});
});
}
computeScriptHashByEoaEthAddress(eth_address: string): HexString {
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> {
const errorWhenNoResult = `unable to fetch script from ${_script_hash}`;
return this.jsonRPC("gw_get_script", [_script_hash], errorWhenNoResult);
}
async getScriptHashByAccountId(account_id: number): Promise<HexString> {
const errorWhenNoResult = `unable to fetch account script hash from 0x${BigInt(
account_id
).toString(16)}`;
return this.jsonRPC(
"gw_get_script_hash",
[`0x${BigInt(account_id).toString(16)}`],
errorWhenNoResult
);
}
async getAccountIdByScriptHash(script_hash: string): Promise<HexNumber> {
const errorWhenNoResult = `unable to fetch account id from script hash ${script_hash}`;
return this.jsonRPC(
"gw_get_account_id_by_script_hash",
[script_hash],
errorWhenNoResult
);
}
async getAccountIdByEoaEthAddress(eth_address: string): Promise<HexNumber> {
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);
const errorWhenNoResult = `unable to fetch account id from ${eth_address}, lock_hash is ${lock_hash}`;
return this.jsonRPC(
"gw_get_account_id_by_script_hash",
[lock_hash],
errorWhenNoResult
);
}
async getScriptHashByShortAddress(
_address: string,
requireResult = RequireResult.canNotBeEmpty
): Promise<HexString> {
const errorWhenNoResult = `unable to fetch script from ${_address}`;
return this.jsonRPC(
"gw_get_script_hash_by_short_address",
[_address],
errorWhenNoResult,
requireResult
);
}
computeShortAddressByEoaEthAddress(_address: string): HexString {
const short_address = this.computeScriptHashByEoaEthAddress(_address).slice(
0,
42
);
return short_address;
}
async getShortAddressByAllTypeEthAddress(
_address: string
): Promise<ShortAddress> {
// todo: support create2 address in such case that it haven't create real contract yet.
if (_address === DEFAULT_EMPTY_ETH_ADDRESS) {
// special case: 0x0000...
// todo: right now we keep the 0x00000.., later maybe should convert to polyjuice creator short address?
return {
value: _address,
type: ShortAddressType.creatorAddress,
};
}
try {
// assume it is a contract address (thus already a short address)
const isContractAddress = await this.isShortAddressOnChain(_address);
if (isContractAddress) {
return {
value: _address,
type: ShortAddressType.contractAddress,
};
}
// script hash not exist with short address, assume it is EOA address..
const short_addr = this.computeShortAddressByEoaEthAddress(_address);
const is_eoa_exist = await this.isShortAddressOnChain(short_addr);
if (is_eoa_exist) {
return {
value: short_addr,
type: ShortAddressType.eoaAddress,
};
}
// not exist eoa address:
// remember to save the script and eoa address mapping with user-specific callback function
if (this.saveEthAddressShortAddressMapping) {
this.saveEthAddressShortAddressMapping(_address, short_addr);
}
return {
value: short_addr,
type: ShortAddressType.notExistEoaAddress,
};
} catch (error: any) {
throw new Error(error.message);
}
}
async getEthAddressByAllTypeShortAddress(
_short_address: HexString
): Promise<HexString> {
// todo: support create2 address in such case which it haven't create real contract yet.
if (_short_address === DEFAULT_EMPTY_ETH_ADDRESS) {
// special case: 0x0000...
// todo: right now we keep the 0x00000.., later maybe should convert to polyjuice creator short address?
return _short_address;
}
// first, query on-chain
const is_address_on_chain = await this.isShortAddressOnChain(
_short_address
);
if (is_address_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) {
// eoa address
return "0x" + script.args.slice(66, 106);
}
// assume it is contract address
return _short_address;
}
// not on-chain, assume 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;
}
throw Error(
`query result of eoa address ${_short_address} with ${_short_address} is not match!`
);
}
async isShortAddressOnChain(
short_address: HexString,
scriptHashCallback?: (script_hash: HexString) => void
): Promise<boolean> {
scriptHashCallback =
scriptHashCallback || function (_script_hash: HexString) {};
try {
const script_hash = await this.getScriptHashByShortAddress(
short_address,
RequireResult.canBeEmpty
);
if (script_hash) {
scriptHashCallback(script_hash);
return true;
}
// not exist on chain
return false;
} catch (error: any) {
throw new Error(error.message);
}
}
// re-compute the eth address with code_hash info to make sure
// it indeed matches with short_address
checkEthAddressIsEoa(
eth_address: string,
_target_short_address: string
): boolean {
const source_short_address =
this.computeShortAddressByEoaEthAddress(eth_address);
return (
source_short_address.toLowerCase() === _target_short_address.toLowerCase()
);
}
// default method
async defaultQueryEthAddressByShortAddress(
_short_address: string
): Promise<HexString> {
const errorWhenNoResult = `unable to fetch eth address from ${_short_address}`;
return this.jsonRPC(
"poly_getEthAddressByGodwokenShortAddress",
[_short_address],
errorWhenNoResult
);
}
async getNonce(account_id: number): Promise<HexNumber> {
const errorWhenNoResult = `unable to fetch nonce, account_id: ${account_id}}`;
return this.jsonRPC(
"gw_get_nonce",
[`0x${account_id.toString(16)}`],
errorWhenNoResult
);
}
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 = 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
): string {
return this.godwokenUtils.generateTransactionMessageToSign(
tx,
sender_script_hash,
receiver_script_hash,
is_add_prefix_in_signing_message
);
}
async generateMessageFromRawL2Transaction(
rawL2Tx: RawL2Transaction,
msg_type: SigningMessageType = SigningMessageType.withPrefix
) {
const sender_script_hash = await this.getScriptHashByAccountId(
parseInt(rawL2Tx.from_id, 16)
);
const receiver_script_hash = await this.getScriptHashByAccountId(
parseInt(rawL2Tx.to_id, 16)
);
const message = this.generateTransactionMessageToSign(
rawL2Tx,
sender_script_hash,
receiver_script_hash,
msg_type === SigningMessageType.withPrefix // with personal sign prefix in message, default is true.
);
return message;
}
async generateMessageFromEthTransaction(
tx: EthTransaction,
msg_type: SigningMessageType = SigningMessageType.withPrefix
): Promise<string> {
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,