@1tokenfe/onetoken-tron-provider 2.2.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +51 -0
- package/README.md +1 -0
- package/dist/ProviderTron.d.ts +50 -0
- package/dist/ProviderTron.js +389 -0
- package/dist/ProviderTronBase.d.ts +8 -0
- package/dist/ProviderTronBase.js +12 -0
- package/dist/cjs/ProviderTron.js +396 -0
- package/dist/cjs/ProviderTronBase.js +15 -0
- package/dist/cjs/index.js +19 -0
- package/dist/cjs/registerTIP6963Provider.js +20 -0
- package/dist/cjs/types.js +17 -0
- package/dist/cjs/utils.js +6 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/registerTIP6963Provider.d.ts +8 -0
- package/dist/registerTIP6963Provider.js +17 -0
- package/dist/types.d.ts +56 -0
- package/dist/types.js +14 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +3 -0
- package/package.json +46 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# ONETOKEN STANDARD SOURCE LICENSE (O-SSL)
|
|
2
|
+
|
|
3
|
+
This license governs use of the accompanying software. If you use the software,
|
|
4
|
+
you accept this license. If you do not accept the license, do not use the
|
|
5
|
+
software.
|
|
6
|
+
|
|
7
|
+
## 1. Definitions
|
|
8
|
+
|
|
9
|
+
The terms "reproduce," "reproduction" and "distribution" have the same meaning
|
|
10
|
+
here as under Hong Kong copyright law.
|
|
11
|
+
|
|
12
|
+
"You" means the licensee of the software.
|
|
13
|
+
|
|
14
|
+
"Your company" means the company you worked for when you downloaded the
|
|
15
|
+
software.
|
|
16
|
+
|
|
17
|
+
"Reference use" means use of the software within your company as a reference,
|
|
18
|
+
in read only form, for the sole purposes of debugging your products,
|
|
19
|
+
maintaining your products, or enhancing the interoperability of your products
|
|
20
|
+
with the software, and specifically excludes the right to distribute the
|
|
21
|
+
software outside of your company.
|
|
22
|
+
|
|
23
|
+
"Licensed patents" means any Licensor patent claims which read directly on the
|
|
24
|
+
software as distributed by the Licensor under this license.
|
|
25
|
+
|
|
26
|
+
## 2. Grant of Rights
|
|
27
|
+
|
|
28
|
+
(A) Copyright Grant - Subject to the terms of this license, the Licensor grants
|
|
29
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free copyright
|
|
30
|
+
license to reproduce the software for reference use.
|
|
31
|
+
|
|
32
|
+
(B) Patent Grant - Subject to the terms of this license, the Licensor grants
|
|
33
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free patent license
|
|
34
|
+
under licensed patents for reference use.
|
|
35
|
+
|
|
36
|
+
## 3. Limitations
|
|
37
|
+
|
|
38
|
+
(A) No Trademark License - This license does not grant you any rights to use
|
|
39
|
+
the Licensor's name, logo, or trademarks.
|
|
40
|
+
|
|
41
|
+
(B) If you begin patent litigation against the Licensor over patents that you
|
|
42
|
+
think may apply to the software (including a cross-claim or counterclaim in
|
|
43
|
+
a lawsuit), your license to the software ends automatically.
|
|
44
|
+
|
|
45
|
+
(C) The software is licensed "as-is." You bear the risk of using it. The
|
|
46
|
+
Licensor gives no express warranties, guarantees or conditions. You may have
|
|
47
|
+
additional consumer rights under your local laws which this license cannot
|
|
48
|
+
change. To the extent permitted under your local laws, the Licensor excludes
|
|
49
|
+
the implied warranties of merchantability, fitness for a particular purpose and
|
|
50
|
+
non-infringement.This license agreement is governed by the laws of Hong Kong,
|
|
51
|
+
and any disputes related to this license agreement shall be resolved in accordance with Hong Kong law.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# cross-inpage-provider
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { TronWeb } from 'tronweb';
|
|
2
|
+
import type { UnsignedTransaction, SignedTransaction } from 'tronweb';
|
|
3
|
+
import SunWeb from 'sunweb';
|
|
4
|
+
import { IInpageProviderConfig } from '@1tokenfe/cross-inpage-provider-core';
|
|
5
|
+
import { ProviderTronBase } from './ProviderTronBase';
|
|
6
|
+
import { IProviderTron, ProviderEvents, ProviderEventsMap, ConsoleLike, Nodes, Callback, RequestArguments } from './types';
|
|
7
|
+
type OneTokenTronProviderProps = IInpageProviderConfig & {
|
|
8
|
+
timeout?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare const CONTRACT_ADDRESS: {
|
|
11
|
+
MAIN: string;
|
|
12
|
+
SIDE: string;
|
|
13
|
+
};
|
|
14
|
+
export declare const SIDE_CHAIN_ID = "41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7";
|
|
15
|
+
export declare const AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST: string[];
|
|
16
|
+
export declare const TRON_REQUEST_ACCOUNTS_LOCAL_KEY = "onetoken_tron_request_accounts_local_key";
|
|
17
|
+
export declare const TRON_REQUEST_ACCOUNTS_INTERVAL: number;
|
|
18
|
+
declare class ProviderTron extends ProviderTronBase implements IProviderTron {
|
|
19
|
+
readonly isTronLink = true;
|
|
20
|
+
tronWeb: TronWeb | null;
|
|
21
|
+
sunWeb: SunWeb | null;
|
|
22
|
+
ready: boolean;
|
|
23
|
+
private _initialized;
|
|
24
|
+
private _connected;
|
|
25
|
+
private _requestingAccounts;
|
|
26
|
+
private _defaultAddress;
|
|
27
|
+
private _accounts;
|
|
28
|
+
private _nodes;
|
|
29
|
+
private readonly _log;
|
|
30
|
+
constructor(props: OneTokenTronProviderProps);
|
|
31
|
+
private _registerTronWeb;
|
|
32
|
+
private _initialize;
|
|
33
|
+
private _registerEvents;
|
|
34
|
+
isAccountsChanged(accounts: string[]): boolean;
|
|
35
|
+
private _handleAccountsChanged;
|
|
36
|
+
private __handleDisconnected;
|
|
37
|
+
private _handleConnected;
|
|
38
|
+
private _postMessage;
|
|
39
|
+
private _dispatch;
|
|
40
|
+
isNetworkChanged(nodes: Nodes): boolean;
|
|
41
|
+
private _handleNodesChanged;
|
|
42
|
+
private _requestAccounts;
|
|
43
|
+
request<T>(args: RequestArguments): Promise<T>;
|
|
44
|
+
sign(transaction: UnsignedTransaction): Promise<SignedTransaction>;
|
|
45
|
+
signMessage(transaction: UnsignedTransaction): Promise<string>;
|
|
46
|
+
signMessageV2(message: string | Uint8Array | Array<number>): Promise<string>;
|
|
47
|
+
getNodeInfo(callback: Callback): Promise<unknown>;
|
|
48
|
+
}
|
|
49
|
+
export { ProviderTron };
|
|
50
|
+
export { IProviderTron, ProviderEvents, ProviderEventsMap, ConsoleLike, Nodes, RequestArguments, TronWeb, };
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
11
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
12
|
+
import dequal from 'fast-deep-equal';
|
|
13
|
+
import { TronWeb } from 'tronweb';
|
|
14
|
+
import SunWeb from 'sunweb';
|
|
15
|
+
import { isEmpty } from 'lodash-es';
|
|
16
|
+
import { checkWalletSwitchEnable, defineWindowProperty, } from '@1tokenfe/cross-inpage-provider-core';
|
|
17
|
+
import { getOrCreateExtInjectedJsBridge } from '@1tokenfe/extension-bridge-injected';
|
|
18
|
+
import { web3Errors } from '@1tokenfe/cross-inpage-provider-errors';
|
|
19
|
+
import { ProviderTronBase } from './ProviderTronBase';
|
|
20
|
+
import { ProviderEvents, } from './types';
|
|
21
|
+
import { isWalletEventMethodMatch } from './utils';
|
|
22
|
+
import BigNumber from 'bignumber.js';
|
|
23
|
+
export const CONTRACT_ADDRESS = {
|
|
24
|
+
MAIN: 'TL9q7aDAHYbW5KdPCwk8oJR3bCDhRwegFf',
|
|
25
|
+
SIDE: 'TGKotco6YoULzbYisTBuP6DWXDjEgJSpYz',
|
|
26
|
+
};
|
|
27
|
+
export const SIDE_CHAIN_ID = '41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7';
|
|
28
|
+
export const AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST = [
|
|
29
|
+
'https://tronscan.org',
|
|
30
|
+
'https://tronscan.io',
|
|
31
|
+
'https://app.justlend.org',
|
|
32
|
+
];
|
|
33
|
+
export const TRON_REQUEST_ACCOUNTS_LOCAL_KEY = 'onetoken_tron_request_accounts_local_key';
|
|
34
|
+
export const TRON_REQUEST_ACCOUNTS_INTERVAL = 10 * 60 * 1000; // ten minutes
|
|
35
|
+
const globalWindow = typeof window !== 'undefined' ? window : global;
|
|
36
|
+
class OneTokenTronWeb extends TronWeb {
|
|
37
|
+
constructor(props, provider) {
|
|
38
|
+
super(props);
|
|
39
|
+
this.provider = provider;
|
|
40
|
+
this.defaultAddress = {
|
|
41
|
+
hex: false,
|
|
42
|
+
base58: false,
|
|
43
|
+
};
|
|
44
|
+
this.trx.sign = (transaction) => provider.sign(transaction);
|
|
45
|
+
this.trx.signMessage = (transaction) => provider.signMessage(transaction);
|
|
46
|
+
this.trx.signMessageV2 = (message, privateKey) => provider.signMessageV2(message, privateKey);
|
|
47
|
+
this.trx.getNodeInfo = (callback) => provider.getNodeInfo(callback);
|
|
48
|
+
}
|
|
49
|
+
request(args) {
|
|
50
|
+
return this.provider.request(args);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class ProviderTron extends ProviderTronBase {
|
|
54
|
+
constructor(props) {
|
|
55
|
+
var _a;
|
|
56
|
+
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }) }));
|
|
57
|
+
this.isTronLink = true;
|
|
58
|
+
this.tronWeb = null;
|
|
59
|
+
this.sunWeb = null;
|
|
60
|
+
this.ready = false;
|
|
61
|
+
this._initialized = false;
|
|
62
|
+
this._connected = false;
|
|
63
|
+
this._requestingAccounts = false;
|
|
64
|
+
this._defaultAddress = {
|
|
65
|
+
hex: false,
|
|
66
|
+
base58: false,
|
|
67
|
+
};
|
|
68
|
+
this._accounts = [];
|
|
69
|
+
this._nodes = {
|
|
70
|
+
fullHost: '',
|
|
71
|
+
fullNode: '',
|
|
72
|
+
solidityNode: '',
|
|
73
|
+
eventServer: '',
|
|
74
|
+
};
|
|
75
|
+
this._log = (_a = props.logger) !== null && _a !== void 0 ? _a : window.console;
|
|
76
|
+
if (checkWalletSwitchEnable()) {
|
|
77
|
+
this._registerEvents();
|
|
78
|
+
void this._initialize();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
_registerTronWeb(nodes) {
|
|
82
|
+
if (isEmpty(nodes))
|
|
83
|
+
return null;
|
|
84
|
+
const tronWeb = new OneTokenTronWeb(Object.assign({}, nodes), this);
|
|
85
|
+
const tronWeb1 = new OneTokenTronWeb(Object.assign({}, nodes), this);
|
|
86
|
+
const tronWeb2 = new OneTokenTronWeb(Object.assign({}, nodes), this);
|
|
87
|
+
const sunWeb = new SunWeb(tronWeb1, tronWeb2, CONTRACT_ADDRESS.MAIN, CONTRACT_ADDRESS.SIDE, SIDE_CHAIN_ID);
|
|
88
|
+
return { tronWeb, sunWeb };
|
|
89
|
+
}
|
|
90
|
+
_initialize() {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
try {
|
|
93
|
+
const { accounts, nodes } = yield this.request({
|
|
94
|
+
method: 'tron_getProviderState',
|
|
95
|
+
});
|
|
96
|
+
const resp = this._registerTronWeb(nodes);
|
|
97
|
+
if (!resp)
|
|
98
|
+
return;
|
|
99
|
+
const { sunWeb, tronWeb } = resp;
|
|
100
|
+
if (window.tronWeb !== undefined) {
|
|
101
|
+
this._log.warn('OneToken: TronWeb is already initiated. Onetoken will overwrite the current instance');
|
|
102
|
+
}
|
|
103
|
+
if (window.sunWeb !== undefined) {
|
|
104
|
+
this._log.warn('OneToken: TronWeb is already initiated. Onetoken will overwrite the current instance');
|
|
105
|
+
}
|
|
106
|
+
this.tronWeb = tronWeb;
|
|
107
|
+
this.sunWeb = sunWeb;
|
|
108
|
+
defineWindowProperty('tronWeb', tronWeb);
|
|
109
|
+
defineWindowProperty('sunWeb', sunWeb);
|
|
110
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
111
|
+
const self = this;
|
|
112
|
+
Object.defineProperty(tronWeb, 'defaultAddress', {
|
|
113
|
+
get() {
|
|
114
|
+
var _a;
|
|
115
|
+
if (!self._connected) {
|
|
116
|
+
self._log.warn('OneToken: We recommend that DApp developers use $onetoken.tron.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
|
|
117
|
+
const origin = ((_a = globalWindow === null || globalWindow === void 0 ? void 0 : globalWindow.location) === null || _a === void 0 ? void 0 : _a.origin) || '';
|
|
118
|
+
if (origin && AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST.includes(origin)) {
|
|
119
|
+
const requestAccountsLocalStr = localStorage.getItem(TRON_REQUEST_ACCOUNTS_LOCAL_KEY);
|
|
120
|
+
const requestAccountsLocal = requestAccountsLocalStr
|
|
121
|
+
? JSON.parse(requestAccountsLocalStr)
|
|
122
|
+
: null;
|
|
123
|
+
if (requestAccountsLocal && requestAccountsLocal[origin]) {
|
|
124
|
+
const requestTimeStamp = requestAccountsLocal[origin];
|
|
125
|
+
if (new BigNumber(Date.now())
|
|
126
|
+
.minus(requestTimeStamp)
|
|
127
|
+
.isGreaterThan(TRON_REQUEST_ACCOUNTS_INTERVAL)) {
|
|
128
|
+
localStorage.setItem(TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
|
|
129
|
+
void self.request({
|
|
130
|
+
method: 'tron_requestAccounts',
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
localStorage.setItem(TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
|
|
136
|
+
void self.request({
|
|
137
|
+
method: 'tron_requestAccounts',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return self._defaultAddress;
|
|
143
|
+
},
|
|
144
|
+
set(value) {
|
|
145
|
+
self._defaultAddress = value;
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
this._handleAccountsChanged(accounts);
|
|
149
|
+
this._dispatch('tronLink#initialized');
|
|
150
|
+
this._initialized = true;
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
this._log.error('OneToken: Failed to get initial state. Please report this bug.', error);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
_registerEvents() {
|
|
158
|
+
window.addEventListener('onetoken_bridge_disconnect', () => {
|
|
159
|
+
this.__handleDisconnected();
|
|
160
|
+
});
|
|
161
|
+
this.on(ProviderEvents.MESSAGE_LOW_LEVEL, (payload) => {
|
|
162
|
+
const { method } = payload;
|
|
163
|
+
if (isWalletEventMethodMatch(method, ProviderEvents.ACCOUNTS_CHANGED)) {
|
|
164
|
+
this._handleAccountsChanged(payload.params);
|
|
165
|
+
}
|
|
166
|
+
if (isWalletEventMethodMatch(method, ProviderEvents.NODES_CHANGED)) {
|
|
167
|
+
if (this._initialized) {
|
|
168
|
+
this._handleNodesChanged(payload.params);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
void this._initialize();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
isAccountsChanged(accounts) {
|
|
177
|
+
return !dequal(this._accounts, accounts);
|
|
178
|
+
}
|
|
179
|
+
_handleAccountsChanged(accounts) {
|
|
180
|
+
let _accounts = accounts;
|
|
181
|
+
if (!Array.isArray(accounts)) {
|
|
182
|
+
this._log.error('Onetoken: Received invalid accounts parameter. Please report this bug.', accounts);
|
|
183
|
+
_accounts = [];
|
|
184
|
+
}
|
|
185
|
+
for (const account of _accounts) {
|
|
186
|
+
if (typeof account !== 'string') {
|
|
187
|
+
this._log.error('Onetoken: Received non-string account. Please report this bug.', accounts);
|
|
188
|
+
_accounts = [];
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (this.isAccountsChanged(_accounts)) {
|
|
193
|
+
this._accounts = _accounts;
|
|
194
|
+
const address = _accounts[0];
|
|
195
|
+
const tronWeb = this.tronWeb;
|
|
196
|
+
if (!tronWeb) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (tronWeb.isAddress(address)) {
|
|
200
|
+
tronWeb.setAddress(address);
|
|
201
|
+
tronWeb.ready = true;
|
|
202
|
+
this.ready = true;
|
|
203
|
+
this._handleConnected();
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
tronWeb.defaultAddress = {
|
|
207
|
+
hex: false,
|
|
208
|
+
base58: false,
|
|
209
|
+
};
|
|
210
|
+
tronWeb.ready = false;
|
|
211
|
+
this.ready = false;
|
|
212
|
+
this.__handleDisconnected();
|
|
213
|
+
}
|
|
214
|
+
if (this._initialized) {
|
|
215
|
+
this._postMessage(ProviderEvents.SET_ACCOUNT, {
|
|
216
|
+
address,
|
|
217
|
+
});
|
|
218
|
+
this._postMessage(ProviderEvents.ACCOUNTS_CHANGED, {
|
|
219
|
+
address,
|
|
220
|
+
});
|
|
221
|
+
this.emit(ProviderEvents.ACCOUNTS_CHANGED, [address]);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
__handleDisconnected() {
|
|
226
|
+
if (this._connected) {
|
|
227
|
+
this._connected = false;
|
|
228
|
+
this._postMessage(ProviderEvents.DISCONNECT);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
_handleConnected() {
|
|
232
|
+
if (!this._connected) {
|
|
233
|
+
this._connected = true;
|
|
234
|
+
this._postMessage(ProviderEvents.CONNECT);
|
|
235
|
+
this._postMessage(ProviderEvents.ACCEPT_WEB);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
_postMessage(action, data) {
|
|
239
|
+
window.postMessage({
|
|
240
|
+
message: {
|
|
241
|
+
action,
|
|
242
|
+
data,
|
|
243
|
+
},
|
|
244
|
+
isTronLink: true,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
_dispatch(event) {
|
|
248
|
+
window.dispatchEvent(new Event(event));
|
|
249
|
+
}
|
|
250
|
+
isNetworkChanged(nodes) {
|
|
251
|
+
return !dequal(nodes, this._nodes);
|
|
252
|
+
}
|
|
253
|
+
_handleNodesChanged({ nodes, chainId }) {
|
|
254
|
+
var _a, _b, _c, _d, _e, _f;
|
|
255
|
+
if (isEmpty(nodes))
|
|
256
|
+
return;
|
|
257
|
+
if (this.isNetworkChanged(nodes)) {
|
|
258
|
+
this._nodes = nodes;
|
|
259
|
+
(_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.setFullNode((_b = nodes.fullNode) !== null && _b !== void 0 ? _b : nodes.fullHost);
|
|
260
|
+
(_c = this.tronWeb) === null || _c === void 0 ? void 0 : _c.setSolidityNode((_d = nodes.solidityNode) !== null && _d !== void 0 ? _d : nodes.fullHost);
|
|
261
|
+
(_e = this.tronWeb) === null || _e === void 0 ? void 0 : _e.setEventServer((_f = nodes.eventServer) !== null && _f !== void 0 ? _f : nodes.fullHost);
|
|
262
|
+
this._postMessage(ProviderEvents.NODES_CHANGED, Object.assign({}, nodes));
|
|
263
|
+
this._postMessage(ProviderEvents.SET_NODE, Object.assign(Object.assign({}, nodes), { node: {
|
|
264
|
+
chainId,
|
|
265
|
+
chain: '_',
|
|
266
|
+
} }));
|
|
267
|
+
this.emit(ProviderEvents.CHAIN_CHANGED, chainId);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
_requestAccounts(args) {
|
|
271
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
272
|
+
if (this._requestingAccounts) {
|
|
273
|
+
return {
|
|
274
|
+
code: 4001,
|
|
275
|
+
message: 'in the request queue',
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
this._requestingAccounts = true;
|
|
279
|
+
try {
|
|
280
|
+
const accounts = (yield this.bridgeRequest(args));
|
|
281
|
+
this._handleAccountsChanged(accounts);
|
|
282
|
+
this._requestingAccounts = false;
|
|
283
|
+
if (accounts.length > 0) {
|
|
284
|
+
return {
|
|
285
|
+
code: 200,
|
|
286
|
+
message: 'ok',
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
return {
|
|
290
|
+
code: 4000,
|
|
291
|
+
message: 'user rejected',
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
catch (e) {
|
|
295
|
+
this._requestingAccounts = false;
|
|
296
|
+
return {
|
|
297
|
+
code: 4000,
|
|
298
|
+
message: 'user rejected',
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
request(args) {
|
|
304
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
305
|
+
const { method, params } = args;
|
|
306
|
+
if (!method || typeof method !== 'string' || method.length === 0) {
|
|
307
|
+
throw web3Errors.rpc.methodNotFound();
|
|
308
|
+
}
|
|
309
|
+
if (params !== undefined &&
|
|
310
|
+
!Array.isArray(params) &&
|
|
311
|
+
(typeof params !== 'object' || params === null)) {
|
|
312
|
+
throw web3Errors.rpc.invalidParams();
|
|
313
|
+
}
|
|
314
|
+
if (method === 'tron_requestAccounts') {
|
|
315
|
+
const result = yield this._requestAccounts(args);
|
|
316
|
+
this._postMessage(ProviderEvents.TAB_REPLY, result);
|
|
317
|
+
return result;
|
|
318
|
+
}
|
|
319
|
+
const resp = yield this.bridgeRequest(args);
|
|
320
|
+
return resp;
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
sign(transaction) {
|
|
324
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
325
|
+
var _a, _b;
|
|
326
|
+
if ((_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.utils.isString(transaction)) {
|
|
327
|
+
// @ts-ignore
|
|
328
|
+
if (!((_b = this.tronWeb) === null || _b === void 0 ? void 0 : _b.utils.isHex(transaction))) {
|
|
329
|
+
throw new Error('Expected hex message input');
|
|
330
|
+
}
|
|
331
|
+
return this.request({
|
|
332
|
+
method: 'signMessageV1',
|
|
333
|
+
params: transaction,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
return this.request({
|
|
337
|
+
method: 'tron_signTransaction',
|
|
338
|
+
params: transaction,
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
signMessage(transaction) {
|
|
343
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
344
|
+
let messageStr;
|
|
345
|
+
if (typeof transaction === 'string') {
|
|
346
|
+
messageStr = transaction;
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
throw new Error('Expected hex message input');
|
|
350
|
+
}
|
|
351
|
+
return this.request({
|
|
352
|
+
method: 'signMessageV1',
|
|
353
|
+
params: [messageStr],
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
signMessageV2(message) {
|
|
358
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
+
var _a, _b, _c;
|
|
360
|
+
let messageStr;
|
|
361
|
+
if (typeof message === 'string') {
|
|
362
|
+
const bytes = (_c = (_b = (_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.utils) === null || _b === void 0 ? void 0 : _b.ethersUtils) === null || _c === void 0 ? void 0 : _c.toUtf8Bytes(message);
|
|
363
|
+
if (!bytes) {
|
|
364
|
+
throw new Error('Expected message input');
|
|
365
|
+
}
|
|
366
|
+
messageStr = Buffer.from(bytes).toString('hex');
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
messageStr = Buffer.from(message).toString('hex');
|
|
370
|
+
}
|
|
371
|
+
return this.request({
|
|
372
|
+
method: 'signMessageV2',
|
|
373
|
+
params: [messageStr],
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
getNodeInfo(callback) {
|
|
378
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
379
|
+
const info = yield this.request({
|
|
380
|
+
method: 'tron_getNodeInfo',
|
|
381
|
+
});
|
|
382
|
+
if (!callback)
|
|
383
|
+
return info;
|
|
384
|
+
callback(null, info);
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
export { ProviderTron };
|
|
389
|
+
export { ProviderEvents, TronWeb, };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
|
|
2
|
+
import { ProviderBase, IInpageProviderConfig } from '@1tokenfe/cross-inpage-provider-core';
|
|
3
|
+
declare class ProviderTronBase extends ProviderBase {
|
|
4
|
+
constructor(props: IInpageProviderConfig);
|
|
5
|
+
protected readonly providerName = IInjectedProviderNames.tron;
|
|
6
|
+
request(data: unknown): Promise<unknown>;
|
|
7
|
+
}
|
|
8
|
+
export { ProviderTronBase };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
|
|
2
|
+
import { ProviderBase } from '@1tokenfe/cross-inpage-provider-core';
|
|
3
|
+
class ProviderTronBase extends ProviderBase {
|
|
4
|
+
constructor(props) {
|
|
5
|
+
super(props);
|
|
6
|
+
this.providerName = IInjectedProviderNames.tron;
|
|
7
|
+
}
|
|
8
|
+
request(data) {
|
|
9
|
+
return this.bridgeRequest(data);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export { ProviderTronBase };
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.TronWeb = exports.ProviderEvents = exports.ProviderTron = exports.TRON_REQUEST_ACCOUNTS_INTERVAL = exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY = exports.AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST = exports.SIDE_CHAIN_ID = exports.CONTRACT_ADDRESS = void 0;
|
|
16
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
17
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
18
|
+
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
|
|
19
|
+
const tronweb_1 = require("tronweb");
|
|
20
|
+
Object.defineProperty(exports, "TronWeb", { enumerable: true, get: function () { return tronweb_1.TronWeb; } });
|
|
21
|
+
const sunweb_1 = __importDefault(require("sunweb"));
|
|
22
|
+
const lodash_es_1 = require("lodash-es");
|
|
23
|
+
const cross_inpage_provider_core_1 = require("@1tokenfe/cross-inpage-provider-core");
|
|
24
|
+
const extension_bridge_injected_1 = require("@1tokenfe/extension-bridge-injected");
|
|
25
|
+
const cross_inpage_provider_errors_1 = require("@1tokenfe/cross-inpage-provider-errors");
|
|
26
|
+
const ProviderTronBase_1 = require("./ProviderTronBase");
|
|
27
|
+
const types_1 = require("./types");
|
|
28
|
+
Object.defineProperty(exports, "ProviderEvents", { enumerable: true, get: function () { return types_1.ProviderEvents; } });
|
|
29
|
+
const utils_1 = require("./utils");
|
|
30
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
31
|
+
exports.CONTRACT_ADDRESS = {
|
|
32
|
+
MAIN: 'TL9q7aDAHYbW5KdPCwk8oJR3bCDhRwegFf',
|
|
33
|
+
SIDE: 'TGKotco6YoULzbYisTBuP6DWXDjEgJSpYz',
|
|
34
|
+
};
|
|
35
|
+
exports.SIDE_CHAIN_ID = '41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7';
|
|
36
|
+
exports.AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST = [
|
|
37
|
+
'https://tronscan.org',
|
|
38
|
+
'https://tronscan.io',
|
|
39
|
+
'https://app.justlend.org',
|
|
40
|
+
];
|
|
41
|
+
exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY = 'onetoken_tron_request_accounts_local_key';
|
|
42
|
+
exports.TRON_REQUEST_ACCOUNTS_INTERVAL = 10 * 60 * 1000; // ten minutes
|
|
43
|
+
const globalWindow = typeof window !== 'undefined' ? window : global;
|
|
44
|
+
class OneTokenTronWeb extends tronweb_1.TronWeb {
|
|
45
|
+
constructor(props, provider) {
|
|
46
|
+
super(props);
|
|
47
|
+
this.provider = provider;
|
|
48
|
+
this.defaultAddress = {
|
|
49
|
+
hex: false,
|
|
50
|
+
base58: false,
|
|
51
|
+
};
|
|
52
|
+
this.trx.sign = (transaction) => provider.sign(transaction);
|
|
53
|
+
this.trx.signMessage = (transaction) => provider.signMessage(transaction);
|
|
54
|
+
this.trx.signMessageV2 = (message, privateKey) => provider.signMessageV2(message, privateKey);
|
|
55
|
+
this.trx.getNodeInfo = (callback) => provider.getNodeInfo(callback);
|
|
56
|
+
}
|
|
57
|
+
request(args) {
|
|
58
|
+
return this.provider.request(args);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
|
|
62
|
+
constructor(props) {
|
|
63
|
+
var _a;
|
|
64
|
+
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || (0, extension_bridge_injected_1.getOrCreateExtInjectedJsBridge)({ timeout: props.timeout }) }));
|
|
65
|
+
this.isTronLink = true;
|
|
66
|
+
this.tronWeb = null;
|
|
67
|
+
this.sunWeb = null;
|
|
68
|
+
this.ready = false;
|
|
69
|
+
this._initialized = false;
|
|
70
|
+
this._connected = false;
|
|
71
|
+
this._requestingAccounts = false;
|
|
72
|
+
this._defaultAddress = {
|
|
73
|
+
hex: false,
|
|
74
|
+
base58: false,
|
|
75
|
+
};
|
|
76
|
+
this._accounts = [];
|
|
77
|
+
this._nodes = {
|
|
78
|
+
fullHost: '',
|
|
79
|
+
fullNode: '',
|
|
80
|
+
solidityNode: '',
|
|
81
|
+
eventServer: '',
|
|
82
|
+
};
|
|
83
|
+
this._log = (_a = props.logger) !== null && _a !== void 0 ? _a : window.console;
|
|
84
|
+
if ((0, cross_inpage_provider_core_1.checkWalletSwitchEnable)()) {
|
|
85
|
+
this._registerEvents();
|
|
86
|
+
void this._initialize();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
_registerTronWeb(nodes) {
|
|
90
|
+
if ((0, lodash_es_1.isEmpty)(nodes))
|
|
91
|
+
return null;
|
|
92
|
+
const tronWeb = new OneTokenTronWeb(Object.assign({}, nodes), this);
|
|
93
|
+
const tronWeb1 = new OneTokenTronWeb(Object.assign({}, nodes), this);
|
|
94
|
+
const tronWeb2 = new OneTokenTronWeb(Object.assign({}, nodes), this);
|
|
95
|
+
const sunWeb = new sunweb_1.default(tronWeb1, tronWeb2, exports.CONTRACT_ADDRESS.MAIN, exports.CONTRACT_ADDRESS.SIDE, exports.SIDE_CHAIN_ID);
|
|
96
|
+
return { tronWeb, sunWeb };
|
|
97
|
+
}
|
|
98
|
+
_initialize() {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
try {
|
|
101
|
+
const { accounts, nodes } = yield this.request({
|
|
102
|
+
method: 'tron_getProviderState',
|
|
103
|
+
});
|
|
104
|
+
const resp = this._registerTronWeb(nodes);
|
|
105
|
+
if (!resp)
|
|
106
|
+
return;
|
|
107
|
+
const { sunWeb, tronWeb } = resp;
|
|
108
|
+
if (window.tronWeb !== undefined) {
|
|
109
|
+
this._log.warn('OneToken: TronWeb is already initiated. Onetoken will overwrite the current instance');
|
|
110
|
+
}
|
|
111
|
+
if (window.sunWeb !== undefined) {
|
|
112
|
+
this._log.warn('OneToken: TronWeb is already initiated. Onetoken will overwrite the current instance');
|
|
113
|
+
}
|
|
114
|
+
this.tronWeb = tronWeb;
|
|
115
|
+
this.sunWeb = sunWeb;
|
|
116
|
+
(0, cross_inpage_provider_core_1.defineWindowProperty)('tronWeb', tronWeb);
|
|
117
|
+
(0, cross_inpage_provider_core_1.defineWindowProperty)('sunWeb', sunWeb);
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
119
|
+
const self = this;
|
|
120
|
+
Object.defineProperty(tronWeb, 'defaultAddress', {
|
|
121
|
+
get() {
|
|
122
|
+
var _a;
|
|
123
|
+
if (!self._connected) {
|
|
124
|
+
self._log.warn('OneToken: We recommend that DApp developers use $onetoken.tron.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
|
|
125
|
+
const origin = ((_a = globalWindow === null || globalWindow === void 0 ? void 0 : globalWindow.location) === null || _a === void 0 ? void 0 : _a.origin) || '';
|
|
126
|
+
if (origin && exports.AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST.includes(origin)) {
|
|
127
|
+
const requestAccountsLocalStr = localStorage.getItem(exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY);
|
|
128
|
+
const requestAccountsLocal = requestAccountsLocalStr
|
|
129
|
+
? JSON.parse(requestAccountsLocalStr)
|
|
130
|
+
: null;
|
|
131
|
+
if (requestAccountsLocal && requestAccountsLocal[origin]) {
|
|
132
|
+
const requestTimeStamp = requestAccountsLocal[origin];
|
|
133
|
+
if (new bignumber_js_1.default(Date.now())
|
|
134
|
+
.minus(requestTimeStamp)
|
|
135
|
+
.isGreaterThan(exports.TRON_REQUEST_ACCOUNTS_INTERVAL)) {
|
|
136
|
+
localStorage.setItem(exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
|
|
137
|
+
void self.request({
|
|
138
|
+
method: 'tron_requestAccounts',
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
localStorage.setItem(exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
|
|
144
|
+
void self.request({
|
|
145
|
+
method: 'tron_requestAccounts',
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return self._defaultAddress;
|
|
151
|
+
},
|
|
152
|
+
set(value) {
|
|
153
|
+
self._defaultAddress = value;
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
this._handleAccountsChanged(accounts);
|
|
157
|
+
this._dispatch('tronLink#initialized');
|
|
158
|
+
this._initialized = true;
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
this._log.error('OneToken: Failed to get initial state. Please report this bug.', error);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
_registerEvents() {
|
|
166
|
+
window.addEventListener('onetoken_bridge_disconnect', () => {
|
|
167
|
+
this.__handleDisconnected();
|
|
168
|
+
});
|
|
169
|
+
this.on(types_1.ProviderEvents.MESSAGE_LOW_LEVEL, (payload) => {
|
|
170
|
+
const { method } = payload;
|
|
171
|
+
if ((0, utils_1.isWalletEventMethodMatch)(method, types_1.ProviderEvents.ACCOUNTS_CHANGED)) {
|
|
172
|
+
this._handleAccountsChanged(payload.params);
|
|
173
|
+
}
|
|
174
|
+
if ((0, utils_1.isWalletEventMethodMatch)(method, types_1.ProviderEvents.NODES_CHANGED)) {
|
|
175
|
+
if (this._initialized) {
|
|
176
|
+
this._handleNodesChanged(payload.params);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
void this._initialize();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
isAccountsChanged(accounts) {
|
|
185
|
+
return !(0, fast_deep_equal_1.default)(this._accounts, accounts);
|
|
186
|
+
}
|
|
187
|
+
_handleAccountsChanged(accounts) {
|
|
188
|
+
let _accounts = accounts;
|
|
189
|
+
if (!Array.isArray(accounts)) {
|
|
190
|
+
this._log.error('Onetoken: Received invalid accounts parameter. Please report this bug.', accounts);
|
|
191
|
+
_accounts = [];
|
|
192
|
+
}
|
|
193
|
+
for (const account of _accounts) {
|
|
194
|
+
if (typeof account !== 'string') {
|
|
195
|
+
this._log.error('Onetoken: Received non-string account. Please report this bug.', accounts);
|
|
196
|
+
_accounts = [];
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (this.isAccountsChanged(_accounts)) {
|
|
201
|
+
this._accounts = _accounts;
|
|
202
|
+
const address = _accounts[0];
|
|
203
|
+
const tronWeb = this.tronWeb;
|
|
204
|
+
if (!tronWeb) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (tronWeb.isAddress(address)) {
|
|
208
|
+
tronWeb.setAddress(address);
|
|
209
|
+
tronWeb.ready = true;
|
|
210
|
+
this.ready = true;
|
|
211
|
+
this._handleConnected();
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
tronWeb.defaultAddress = {
|
|
215
|
+
hex: false,
|
|
216
|
+
base58: false,
|
|
217
|
+
};
|
|
218
|
+
tronWeb.ready = false;
|
|
219
|
+
this.ready = false;
|
|
220
|
+
this.__handleDisconnected();
|
|
221
|
+
}
|
|
222
|
+
if (this._initialized) {
|
|
223
|
+
this._postMessage(types_1.ProviderEvents.SET_ACCOUNT, {
|
|
224
|
+
address,
|
|
225
|
+
});
|
|
226
|
+
this._postMessage(types_1.ProviderEvents.ACCOUNTS_CHANGED, {
|
|
227
|
+
address,
|
|
228
|
+
});
|
|
229
|
+
this.emit(types_1.ProviderEvents.ACCOUNTS_CHANGED, [address]);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
__handleDisconnected() {
|
|
234
|
+
if (this._connected) {
|
|
235
|
+
this._connected = false;
|
|
236
|
+
this._postMessage(types_1.ProviderEvents.DISCONNECT);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
_handleConnected() {
|
|
240
|
+
if (!this._connected) {
|
|
241
|
+
this._connected = true;
|
|
242
|
+
this._postMessage(types_1.ProviderEvents.CONNECT);
|
|
243
|
+
this._postMessage(types_1.ProviderEvents.ACCEPT_WEB);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
_postMessage(action, data) {
|
|
247
|
+
window.postMessage({
|
|
248
|
+
message: {
|
|
249
|
+
action,
|
|
250
|
+
data,
|
|
251
|
+
},
|
|
252
|
+
isTronLink: true,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
_dispatch(event) {
|
|
256
|
+
window.dispatchEvent(new Event(event));
|
|
257
|
+
}
|
|
258
|
+
isNetworkChanged(nodes) {
|
|
259
|
+
return !(0, fast_deep_equal_1.default)(nodes, this._nodes);
|
|
260
|
+
}
|
|
261
|
+
_handleNodesChanged({ nodes, chainId }) {
|
|
262
|
+
var _a, _b, _c, _d, _e, _f;
|
|
263
|
+
if ((0, lodash_es_1.isEmpty)(nodes))
|
|
264
|
+
return;
|
|
265
|
+
if (this.isNetworkChanged(nodes)) {
|
|
266
|
+
this._nodes = nodes;
|
|
267
|
+
(_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.setFullNode((_b = nodes.fullNode) !== null && _b !== void 0 ? _b : nodes.fullHost);
|
|
268
|
+
(_c = this.tronWeb) === null || _c === void 0 ? void 0 : _c.setSolidityNode((_d = nodes.solidityNode) !== null && _d !== void 0 ? _d : nodes.fullHost);
|
|
269
|
+
(_e = this.tronWeb) === null || _e === void 0 ? void 0 : _e.setEventServer((_f = nodes.eventServer) !== null && _f !== void 0 ? _f : nodes.fullHost);
|
|
270
|
+
this._postMessage(types_1.ProviderEvents.NODES_CHANGED, Object.assign({}, nodes));
|
|
271
|
+
this._postMessage(types_1.ProviderEvents.SET_NODE, Object.assign(Object.assign({}, nodes), { node: {
|
|
272
|
+
chainId,
|
|
273
|
+
chain: '_',
|
|
274
|
+
} }));
|
|
275
|
+
this.emit(types_1.ProviderEvents.CHAIN_CHANGED, chainId);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
_requestAccounts(args) {
|
|
279
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
280
|
+
if (this._requestingAccounts) {
|
|
281
|
+
return {
|
|
282
|
+
code: 4001,
|
|
283
|
+
message: 'in the request queue',
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
this._requestingAccounts = true;
|
|
287
|
+
try {
|
|
288
|
+
const accounts = (yield this.bridgeRequest(args));
|
|
289
|
+
this._handleAccountsChanged(accounts);
|
|
290
|
+
this._requestingAccounts = false;
|
|
291
|
+
if (accounts.length > 0) {
|
|
292
|
+
return {
|
|
293
|
+
code: 200,
|
|
294
|
+
message: 'ok',
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
code: 4000,
|
|
299
|
+
message: 'user rejected',
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
catch (e) {
|
|
303
|
+
this._requestingAccounts = false;
|
|
304
|
+
return {
|
|
305
|
+
code: 4000,
|
|
306
|
+
message: 'user rejected',
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
request(args) {
|
|
312
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
313
|
+
const { method, params } = args;
|
|
314
|
+
if (!method || typeof method !== 'string' || method.length === 0) {
|
|
315
|
+
throw cross_inpage_provider_errors_1.web3Errors.rpc.methodNotFound();
|
|
316
|
+
}
|
|
317
|
+
if (params !== undefined &&
|
|
318
|
+
!Array.isArray(params) &&
|
|
319
|
+
(typeof params !== 'object' || params === null)) {
|
|
320
|
+
throw cross_inpage_provider_errors_1.web3Errors.rpc.invalidParams();
|
|
321
|
+
}
|
|
322
|
+
if (method === 'tron_requestAccounts') {
|
|
323
|
+
const result = yield this._requestAccounts(args);
|
|
324
|
+
this._postMessage(types_1.ProviderEvents.TAB_REPLY, result);
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
const resp = yield this.bridgeRequest(args);
|
|
328
|
+
return resp;
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
sign(transaction) {
|
|
332
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
333
|
+
var _a, _b;
|
|
334
|
+
if ((_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.utils.isString(transaction)) {
|
|
335
|
+
// @ts-ignore
|
|
336
|
+
if (!((_b = this.tronWeb) === null || _b === void 0 ? void 0 : _b.utils.isHex(transaction))) {
|
|
337
|
+
throw new Error('Expected hex message input');
|
|
338
|
+
}
|
|
339
|
+
return this.request({
|
|
340
|
+
method: 'signMessageV1',
|
|
341
|
+
params: transaction,
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
return this.request({
|
|
345
|
+
method: 'tron_signTransaction',
|
|
346
|
+
params: transaction,
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
signMessage(transaction) {
|
|
351
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
352
|
+
let messageStr;
|
|
353
|
+
if (typeof transaction === 'string') {
|
|
354
|
+
messageStr = transaction;
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
throw new Error('Expected hex message input');
|
|
358
|
+
}
|
|
359
|
+
return this.request({
|
|
360
|
+
method: 'signMessageV1',
|
|
361
|
+
params: [messageStr],
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
signMessageV2(message) {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
var _a, _b, _c;
|
|
368
|
+
let messageStr;
|
|
369
|
+
if (typeof message === 'string') {
|
|
370
|
+
const bytes = (_c = (_b = (_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.utils) === null || _b === void 0 ? void 0 : _b.ethersUtils) === null || _c === void 0 ? void 0 : _c.toUtf8Bytes(message);
|
|
371
|
+
if (!bytes) {
|
|
372
|
+
throw new Error('Expected message input');
|
|
373
|
+
}
|
|
374
|
+
messageStr = Buffer.from(bytes).toString('hex');
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
messageStr = Buffer.from(message).toString('hex');
|
|
378
|
+
}
|
|
379
|
+
return this.request({
|
|
380
|
+
method: 'signMessageV2',
|
|
381
|
+
params: [messageStr],
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
getNodeInfo(callback) {
|
|
386
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
387
|
+
const info = yield this.request({
|
|
388
|
+
method: 'tron_getNodeInfo',
|
|
389
|
+
});
|
|
390
|
+
if (!callback)
|
|
391
|
+
return info;
|
|
392
|
+
callback(null, info);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
exports.ProviderTron = ProviderTron;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProviderTronBase = void 0;
|
|
4
|
+
const cross_inpage_provider_types_1 = require("@1tokenfe/cross-inpage-provider-types");
|
|
5
|
+
const cross_inpage_provider_core_1 = require("@1tokenfe/cross-inpage-provider-core");
|
|
6
|
+
class ProviderTronBase extends cross_inpage_provider_core_1.ProviderBase {
|
|
7
|
+
constructor(props) {
|
|
8
|
+
super(props);
|
|
9
|
+
this.providerName = cross_inpage_provider_types_1.IInjectedProviderNames.tron;
|
|
10
|
+
}
|
|
11
|
+
request(data) {
|
|
12
|
+
return this.bridgeRequest(data);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.ProviderTronBase = ProviderTronBase;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./ProviderTronBase"), exports);
|
|
18
|
+
__exportStar(require("./ProviderTron"), exports);
|
|
19
|
+
__exportStar(require("./registerTIP6963Provider"), exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerTIP6963Provider = registerTIP6963Provider;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
function registerTIP6963Provider({ uuid = (0, uuid_1.v4)(), name = 'OneToken', rdns = 'so.onetoken.app.wallet', image, provider, }) {
|
|
6
|
+
// TIP-6963: https://github.com/tronprotocol/tips/issues/737
|
|
7
|
+
const info = {
|
|
8
|
+
uuid,
|
|
9
|
+
name,
|
|
10
|
+
icon: image,
|
|
11
|
+
rdns,
|
|
12
|
+
};
|
|
13
|
+
function announceProvider() {
|
|
14
|
+
window.dispatchEvent(new CustomEvent('TIP6963:announceProvider', {
|
|
15
|
+
detail: { info, provider: provider },
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
window.addEventListener('TIP6963:requestProvider', announceProvider);
|
|
19
|
+
announceProvider();
|
|
20
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProviderEvents = void 0;
|
|
4
|
+
var ProviderEvents;
|
|
5
|
+
(function (ProviderEvents) {
|
|
6
|
+
ProviderEvents["TAB_REPLY"] = "tabReply";
|
|
7
|
+
ProviderEvents["CONNECT"] = "connect";
|
|
8
|
+
ProviderEvents["DISCONNECT"] = "disconnect";
|
|
9
|
+
ProviderEvents["ACCOUNTS_CHANGED"] = "accountsChanged";
|
|
10
|
+
ProviderEvents["SET_ACCOUNT"] = "setAccount";
|
|
11
|
+
ProviderEvents["SET_NODE"] = "setNode";
|
|
12
|
+
ProviderEvents["NODES_CHANGED"] = "nodesChanged";
|
|
13
|
+
ProviderEvents["MESSAGE"] = "message";
|
|
14
|
+
ProviderEvents["MESSAGE_LOW_LEVEL"] = "message_low_level";
|
|
15
|
+
ProviderEvents["CHAIN_CHANGED"] = "chainChanged";
|
|
16
|
+
ProviderEvents["ACCEPT_WEB"] = "acceptWeb";
|
|
17
|
+
})(ProviderEvents || (exports.ProviderEvents = ProviderEvents = {}));
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { v4 as uuidV4 } from 'uuid';
|
|
2
|
+
export function registerTIP6963Provider({ uuid = uuidV4(), name = 'OneToken', rdns = 'so.onetoken.app.wallet', image, provider, }) {
|
|
3
|
+
// TIP-6963: https://github.com/tronprotocol/tips/issues/737
|
|
4
|
+
const info = {
|
|
5
|
+
uuid,
|
|
6
|
+
name,
|
|
7
|
+
icon: image,
|
|
8
|
+
rdns,
|
|
9
|
+
};
|
|
10
|
+
function announceProvider() {
|
|
11
|
+
window.dispatchEvent(new CustomEvent('TIP6963:announceProvider', {
|
|
12
|
+
detail: { info, provider: provider },
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
window.addEventListener('TIP6963:requestProvider', announceProvider);
|
|
16
|
+
announceProvider();
|
|
17
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { IJsonRpcRequest } from '@1tokenfe/cross-inpage-provider-types';
|
|
2
|
+
import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb';
|
|
3
|
+
import { ProviderTronBase } from './ProviderTronBase';
|
|
4
|
+
export declare enum ProviderEvents {
|
|
5
|
+
TAB_REPLY = "tabReply",
|
|
6
|
+
CONNECT = "connect",
|
|
7
|
+
DISCONNECT = "disconnect",
|
|
8
|
+
ACCOUNTS_CHANGED = "accountsChanged",
|
|
9
|
+
SET_ACCOUNT = "setAccount",
|
|
10
|
+
SET_NODE = "setNode",
|
|
11
|
+
NODES_CHANGED = "nodesChanged",
|
|
12
|
+
MESSAGE = "message",
|
|
13
|
+
MESSAGE_LOW_LEVEL = "message_low_level",
|
|
14
|
+
CHAIN_CHANGED = "chainChanged",
|
|
15
|
+
ACCEPT_WEB = "acceptWeb"
|
|
16
|
+
}
|
|
17
|
+
export interface Nodes {
|
|
18
|
+
fullHost: string;
|
|
19
|
+
fullNode: string;
|
|
20
|
+
solidityNode: string;
|
|
21
|
+
eventServer: string;
|
|
22
|
+
}
|
|
23
|
+
export interface Network {
|
|
24
|
+
chainId?: string;
|
|
25
|
+
networkId?: string;
|
|
26
|
+
}
|
|
27
|
+
export type ConsoleLike = Pick<Console, 'log' | 'warn' | 'error' | 'debug' | 'info' | 'trace'>;
|
|
28
|
+
export interface ProviderEventsMap {
|
|
29
|
+
[ProviderEvents.CONNECT]: (network: Network) => void;
|
|
30
|
+
[ProviderEvents.DISCONNECT]: () => void;
|
|
31
|
+
[ProviderEvents.ACCOUNTS_CHANGED]: (accounts: string[]) => void;
|
|
32
|
+
[ProviderEvents.NODES_CHANGED]: (nodes: Nodes) => void;
|
|
33
|
+
[ProviderEvents.MESSAGE_LOW_LEVEL]: (payload: IJsonRpcRequest) => void;
|
|
34
|
+
[ProviderEvents.MESSAGE]: (message: string) => void;
|
|
35
|
+
}
|
|
36
|
+
export interface RequestArguments {
|
|
37
|
+
id?: number | string;
|
|
38
|
+
method: string;
|
|
39
|
+
params?: unknown[] | Record<string, unknown>;
|
|
40
|
+
}
|
|
41
|
+
export type Callback = false | ((err: Error | null, info: any) => any);
|
|
42
|
+
export interface IProviderTron extends ProviderTronBase {
|
|
43
|
+
readonly isTronLink: true;
|
|
44
|
+
tronWeb: TronWeb | null;
|
|
45
|
+
sunWeb: any;
|
|
46
|
+
ready: boolean;
|
|
47
|
+
request<T>(args: RequestArguments): Promise<T>;
|
|
48
|
+
sign(transaction: UnsignedTransaction): Promise<SignedTransaction>;
|
|
49
|
+
signMessage(transaction: UnsignedTransaction): Promise<string>;
|
|
50
|
+
signMessageV2(message: string | Uint8Array | Array<number>, privateKey?: string | false): Promise<string>;
|
|
51
|
+
getNodeInfo(callback?: Callback): Promise<any>;
|
|
52
|
+
}
|
|
53
|
+
export interface requestAccountsResponse {
|
|
54
|
+
code: 200 | 4000 | 4001;
|
|
55
|
+
message: string;
|
|
56
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export var ProviderEvents;
|
|
2
|
+
(function (ProviderEvents) {
|
|
3
|
+
ProviderEvents["TAB_REPLY"] = "tabReply";
|
|
4
|
+
ProviderEvents["CONNECT"] = "connect";
|
|
5
|
+
ProviderEvents["DISCONNECT"] = "disconnect";
|
|
6
|
+
ProviderEvents["ACCOUNTS_CHANGED"] = "accountsChanged";
|
|
7
|
+
ProviderEvents["SET_ACCOUNT"] = "setAccount";
|
|
8
|
+
ProviderEvents["SET_NODE"] = "setNode";
|
|
9
|
+
ProviderEvents["NODES_CHANGED"] = "nodesChanged";
|
|
10
|
+
ProviderEvents["MESSAGE"] = "message";
|
|
11
|
+
ProviderEvents["MESSAGE_LOW_LEVEL"] = "message_low_level";
|
|
12
|
+
ProviderEvents["CHAIN_CHANGED"] = "chainChanged";
|
|
13
|
+
ProviderEvents["ACCEPT_WEB"] = "acceptWeb";
|
|
14
|
+
})(ProviderEvents || (ProviderEvents = {}));
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isWalletEventMethodMatch(method: string, name: string): boolean;
|
package/dist/utils.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1tokenfe/onetoken-tron-provider",
|
|
3
|
+
"version": "2.2.46",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"cross-inpage-provider"
|
|
6
|
+
],
|
|
7
|
+
"author": "dev-fe@onetoken.so",
|
|
8
|
+
"repository": "https://github.com/OneTokenHQ/cross-inpage-provider",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/*"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.js"
|
|
20
|
+
},
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"main": "./dist/cjs/index.js",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prebuild": "rm -rf dist",
|
|
26
|
+
"build": "tsc && tsc --project tsconfig.cjs.json",
|
|
27
|
+
"start": "tsc --watch"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@1tokenfe/cross-inpage-provider-core": "2.2.46",
|
|
31
|
+
"@1tokenfe/cross-inpage-provider-errors": "2.2.46",
|
|
32
|
+
"@1tokenfe/cross-inpage-provider-types": "2.2.46",
|
|
33
|
+
"@1tokenfe/extension-bridge-injected": "2.2.46",
|
|
34
|
+
"@noble/secp256k1": "1.7.1",
|
|
35
|
+
"lodash-es": "^4.17.21",
|
|
36
|
+
"querystring": "^0.2.1",
|
|
37
|
+
"sunweb": "^1.0.7",
|
|
38
|
+
"tronweb": "^6.0.1",
|
|
39
|
+
"uuid": "^8.3.2"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/lodash-es": "^4.17.12",
|
|
43
|
+
"@types/uuid": "^8.3.2"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "dadd71e7e07ab7aaf87961aecc01255586848e16"
|
|
46
|
+
}
|