-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyfill.ts
167 lines (147 loc) · 5.13 KB
/
polyfill.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
// @ts-nocheck
import { Tendermint37Client } from "@cosmjs/tendermint-rpc";
import _BigInt from "big-integer";
import { chainInfos } from "@/constants/chainInfo";
import { getWalletByNetworkFromStorage } from "@/helper";
import Keplr from "@/libs/keplr";
import Metamask from "@/libs/metamask";
import Bitcoin from "@/libs/bitcoin";
export const polyfill = () => {
// polyfill
Tendermint37Client.detectVersion = () => {};
Tendermint37Client.prototype.status = function () {
const chainInfo = chainInfos.find(
(chain) => chain.networkType === "cosmos" && chain.rpc === this.client.url
);
return {
nodeInfo: {
network: chainInfo.chainId,
version: "",
},
};
};
// inject global
window.TronWeb = require("tronweb");
window.Networks = require("@oraichain/ethereum-multicall").Networks;
// enable Keplr
const walletType = getWalletByNetworkFromStorage();
window.Keplr = new Keplr(walletType?.cosmos);
window.ethereumDapp = window.ethereum;
window.Bitcoin = new Bitcoin();
window.Metamask = new Metamask(window.tronWeb);
window.React = require("react");
window.Buffer = require("buffer").Buffer;
// window.process = require('process/browser');
// extend formatToJson
Intl.DateTimeFormat.prototype.formatToJson = function (date: Date) {
const _this = this as Intl.DateTimeFormat;
return Object.fromEntries(
_this
.formatToParts(date)
.filter((item) => item.type !== "literal")
.map((item) => [item.type, item.value])
) as Record<Intl.DateTimeFormatPartTypes, string>;
};
if (typeof BigInt === "undefined") {
(window as any)._BigInt = _BigInt;
class MyBigInt {
value: bigint;
constructor(value: MyBigInt | number | string) {
if (value instanceof MyBigInt) {
return value;
} else {
if (typeof value === "string" && value.startsWith("0x")) {
this.value = _BigInt(value.substring(2), 16);
} else {
this.value = _BigInt(value);
}
}
// Proxy method calls to _BigInt if possible
return new Proxy(this, {
get(obj, field) {
if (field in obj) return obj[field];
if (typeof obj.value !== "bigint" && field in obj.value)
return obj.value[field].bind(obj.value);
return undefined;
},
});
}
valueOf() {
return this.value.valueOf();
}
equals(b) {
if (typeof this.value === "bigint") {
return this.value === b.value;
} else if (b instanceof MyBigInt) {
return this.value.equals(_BigInt(b.value));
} else {
return this.value.equals(_BigInt(b));
}
}
toString() {
return this.value.toString();
}
_toUint8ArrayNative(littleEndian = false, elements = 8) {
const arr = new ArrayBuffer(elements);
const view = new DataView(arr);
view.setBigUint64(0, this.value, littleEndian);
return new Uint8Array(arr);
}
_toUint8Array(littleEndian = false, elements = 8) {
const arr = new ArrayBuffer(elements);
const uint8arr = new Uint8Array(arr);
const intarr = this.value.toArray(2 ** 8).value;
if (littleEndian) uint8arr.set(intarr.reverse(), 0);
else uint8arr.set(intarr, elements - intarr.length);
return uint8arr;
}
toUint8Array(littleEndian = false, elements = 8) {
if (typeof this.value === "bigint") {
return this._toUint8ArrayNative(littleEndian, elements);
} else {
return this._toUint8Array(littleEndian, elements);
}
}
/**
* Get MyBigInt from a uint8 array in specified endianess.
* Uses native MyBigInt if the environment supports it and detectSupport is true.
*
* @param {Uint8Array} uint8arr
* @param {boolean} littleEndian use little endian byte order, default is false
* @param {boolean} detectSupport auto-detect support for native MyBigInt, default is true
*/
static fromUint8Array(
uint8arr: Uint8Array,
littleEndian: boolean = false,
detectSupport: boolean = true
) {
if (supportsNative && detectSupport) {
const view = new DataView(uint8arr.buffer);
return new MyBigInt(view.getBigUint64(0, littleEndian));
}
let array;
if (littleEndian) {
array = Array.from(uint8arr).reverse();
} else {
array = Array.from(uint8arr);
}
return new MyBigInt(_BigInt.fromArray(array, 2 ** 8));
}
}
var _old = MyBigInt;
MyBigInt = function (...args) {
return new _old(...args);
};
(window as any).BigInt = MyBigInt;
}
// polyfill abort timeout for some old browser not support.
if (!("timeout" in AbortSignal)) {
AbortSignal.timeout = function (delay: number) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), delay);
// Allow Node.js processes to exit early if only the timeout is running
timeoutId?.unref?.();
return controller.signal;
};
}
};