@onekeyfe/onekey-tron-provider 1.1.5 → 1.1.6

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.
@@ -1,7 +1,7 @@
1
1
  import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb';
2
2
  import { IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
3
3
  import { ProviderTronBase } from './ProviderTronBase';
4
- import { IProviderTron, ProviderEvents, ProviderEventsMap, ConsoleLike, Nodes, RequestArguments } from './types';
4
+ import { IProviderTron, ProviderEvents, ProviderEventsMap, ConsoleLike, Nodes, Callback, RequestArguments } from './types';
5
5
  declare type OneKeyTronProviderProps = IInpageProviderConfig & {
6
6
  timeout?: number;
7
7
  };
@@ -30,6 +30,7 @@ declare class ProviderTron extends ProviderTronBase implements IProviderTron {
30
30
  private _requestAccounts;
31
31
  request<T>(args: RequestArguments): Promise<T>;
32
32
  sign(transaction: UnsignedTransaction): Promise<SignedTransaction>;
33
+ getNodeInfo(callback: Callback): Promise<unknown>;
33
34
  }
34
35
  export { ProviderTron };
35
36
  export { IProviderTron, ProviderEvents, ProviderEventsMap, ConsoleLike, Nodes, RequestArguments, TronWeb, };
@@ -9,11 +9,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import dequal from 'fast-deep-equal';
11
11
  import TronWeb from 'tronweb';
12
+ import { isEmpty } from 'lodash';
12
13
  import { getOrCreateExtInjectedJsBridge } from '@onekeyfe/extension-bridge-injected';
13
14
  import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
14
15
  import { ProviderTronBase } from './ProviderTronBase';
15
16
  import { ProviderEvents, } from './types';
16
17
  import { isWalletEventMethodMatch } from './utils';
18
+ class OneKeyTronWeb extends TronWeb {
19
+ constructor(props, provider) {
20
+ super(props);
21
+ this.provider = provider;
22
+ this.defaultAddress = {
23
+ hex: false,
24
+ base58: false,
25
+ };
26
+ this.trx.sign = (transaction) => provider.sign(transaction);
27
+ this.trx.getNodeInfo = (callback) => provider.getNodeInfo(callback);
28
+ }
29
+ request(args) {
30
+ return this.provider.request(args);
31
+ }
32
+ }
17
33
  class ProviderTron extends ProviderTronBase {
18
34
  constructor(props) {
19
35
  var _a;
@@ -37,16 +53,14 @@ class ProviderTron extends ProviderTronBase {
37
53
  eventServer: '',
38
54
  };
39
55
  this._log = (_a = props.logger) !== null && _a !== void 0 ? _a : window.console;
56
+ this._registerEvents();
40
57
  void this._initialize();
41
58
  }
42
59
  _registerTronWeb(nodes) {
43
- const tronWeb = new TronWeb(Object.assign({}, nodes));
44
- tronWeb.trx.sign = (transaction) => this.sign(transaction);
45
- tronWeb.request = (args) => this.request(args);
46
- tronWeb.defaultAddress = {
47
- hex: false,
48
- base58: false,
49
- };
60
+ if (isEmpty(nodes))
61
+ return null;
62
+ const tronWeb = new OneKeyTronWeb(Object.assign({}, nodes), this);
63
+ tronWeb.getFullnodeVersion();
50
64
  // eslint-disable-next-line @typescript-eslint/no-this-alias
51
65
  const self = this;
52
66
  Object.defineProperty(tronWeb, 'defaultAddress', {
@@ -72,6 +86,8 @@ class ProviderTron extends ProviderTronBase {
72
86
  method: 'tron_getProviderState',
73
87
  });
74
88
  const tronWeb = this._registerTronWeb(nodes);
89
+ if (!tronWeb)
90
+ return;
75
91
  if (window.tronWeb !== undefined) {
76
92
  this._log.warn('OneKey: TronWeb is already initiated. Onekey will overwrite the current instance');
77
93
  }
@@ -79,8 +95,7 @@ class ProviderTron extends ProviderTronBase {
79
95
  // some DApp also check if the sunWeb object exists before requesting accounts
80
96
  window.sunWeb = {};
81
97
  this._initialized = true;
82
- this._registerEvents(tronWeb);
83
- this._handleAccountsChanged(accounts, tronWeb);
98
+ this._handleAccountsChanged(accounts);
84
99
  this._dispatch('tronLink#initialized');
85
100
  }
86
101
  catch (error) {
@@ -88,21 +103,26 @@ class ProviderTron extends ProviderTronBase {
88
103
  }
89
104
  });
90
105
  }
91
- _registerEvents(tronWeb) {
106
+ _registerEvents() {
92
107
  window.addEventListener('onekey_bridge_disconnect', () => {
93
108
  this.__handleDisconnected();
94
109
  });
95
110
  this.on(ProviderEvents.MESSAGE_LOW_LEVEL, (payload) => {
96
111
  const { method } = payload;
97
112
  if (isWalletEventMethodMatch(method, ProviderEvents.ACCOUNTS_CHANGED)) {
98
- this._handleAccountsChanged(payload.params, tronWeb);
113
+ this._handleAccountsChanged(payload.params);
99
114
  }
100
115
  if (isWalletEventMethodMatch(method, ProviderEvents.NODES_CHANGED)) {
101
- this._handleNodesChanged(payload.params, tronWeb);
116
+ if (this._initialized) {
117
+ this._handleNodesChanged(payload.params);
118
+ }
119
+ else {
120
+ void this._initialize();
121
+ }
102
122
  }
103
123
  });
104
124
  }
105
- _handleAccountsChanged(accounts, tronWeb) {
125
+ _handleAccountsChanged(accounts) {
106
126
  let _accounts = accounts;
107
127
  if (!Array.isArray(accounts)) {
108
128
  this._log.error('Onekey: Received invalid accounts parameter. Please report this bug.', accounts);
@@ -125,6 +145,7 @@ class ProviderTron extends ProviderTronBase {
125
145
  this._postMessage(ProviderEvents.SET_ACCOUNT, {
126
146
  address,
127
147
  });
148
+ const tronWeb = this.tronWeb;
128
149
  if (tronWeb.isAddress(address)) {
129
150
  tronWeb.setAddress(address);
130
151
  tronWeb.ready = true;
@@ -167,13 +188,15 @@ class ProviderTron extends ProviderTronBase {
167
188
  _dispatch(event) {
168
189
  window.dispatchEvent(new Event(event));
169
190
  }
170
- _handleNodesChanged(nodes, tronWeb) {
171
- var _a, _b, _c;
191
+ _handleNodesChanged(nodes) {
192
+ var _a, _b, _c, _d, _e, _f;
193
+ if (isEmpty(nodes))
194
+ return;
172
195
  if (!dequal(nodes, this._nodes)) {
173
196
  this._nodes = nodes;
174
- tronWeb.setFullNode((_a = nodes.fullNode) !== null && _a !== void 0 ? _a : nodes.fullHost);
175
- tronWeb.setSolidityNode((_b = nodes.solidityNode) !== null && _b !== void 0 ? _b : nodes.fullHost);
176
- tronWeb.setEventServer((_c = nodes.eventServer) !== null && _c !== void 0 ? _c : nodes.fullHost);
197
+ (_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.setFullNode((_b = nodes.fullNode) !== null && _b !== void 0 ? _b : nodes.fullHost);
198
+ (_c = this.tronWeb) === null || _c === void 0 ? void 0 : _c.setSolidityNode((_d = nodes.solidityNode) !== null && _d !== void 0 ? _d : nodes.fullHost);
199
+ (_e = this.tronWeb) === null || _e === void 0 ? void 0 : _e.setEventServer((_f = nodes.eventServer) !== null && _f !== void 0 ? _f : nodes.fullHost);
177
200
  this._postMessage(ProviderEvents.NODES_CHANGED, Object.assign({}, nodes));
178
201
  }
179
202
  }
@@ -186,19 +209,28 @@ class ProviderTron extends ProviderTronBase {
186
209
  };
187
210
  }
188
211
  this._requestingAccounts = true;
189
- const accounts = (yield this.bridgeRequest(args));
190
- this._handleAccountsChanged(accounts, this.tronWeb);
191
- this._requestingAccounts = false;
192
- if (accounts.length > 0) {
212
+ try {
213
+ const accounts = (yield this.bridgeRequest(args));
214
+ this._handleAccountsChanged(accounts);
215
+ this._requestingAccounts = false;
216
+ if (accounts.length > 0) {
217
+ return {
218
+ code: 200,
219
+ message: 'ok',
220
+ };
221
+ }
193
222
  return {
194
- code: 200,
195
- message: 'ok',
223
+ code: 4000,
224
+ message: 'user rejected',
225
+ };
226
+ }
227
+ catch (e) {
228
+ this._requestingAccounts = false;
229
+ return {
230
+ code: 4000,
231
+ message: 'user rejected',
196
232
  };
197
233
  }
198
- return {
199
- code: 4000,
200
- message: 'user rejected',
201
- };
202
234
  });
203
235
  }
204
236
  request(args) {
@@ -229,6 +261,16 @@ class ProviderTron extends ProviderTronBase {
229
261
  });
230
262
  });
231
263
  }
264
+ getNodeInfo(callback) {
265
+ return __awaiter(this, void 0, void 0, function* () {
266
+ const info = yield this.request({
267
+ method: 'tron_getNodeInfo',
268
+ });
269
+ if (!callback)
270
+ return info;
271
+ callback(null, info);
272
+ });
273
+ }
232
274
  }
233
275
  export { ProviderTron };
234
276
  export { ProviderEvents, TronWeb, };
@@ -16,12 +16,28 @@ exports.TronWeb = exports.ProviderEvents = exports.ProviderTron = void 0;
16
16
  const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
17
17
  const tronweb_1 = __importDefault(require("tronweb"));
18
18
  exports.TronWeb = tronweb_1.default;
19
+ const lodash_1 = require("lodash");
19
20
  const extension_bridge_injected_1 = require("@onekeyfe/extension-bridge-injected");
20
21
  const cross_inpage_provider_errors_1 = require("@onekeyfe/cross-inpage-provider-errors");
21
22
  const ProviderTronBase_1 = require("./ProviderTronBase");
22
23
  const types_1 = require("./types");
23
24
  Object.defineProperty(exports, "ProviderEvents", { enumerable: true, get: function () { return types_1.ProviderEvents; } });
24
25
  const utils_1 = require("./utils");
26
+ class OneKeyTronWeb extends tronweb_1.default {
27
+ constructor(props, provider) {
28
+ super(props);
29
+ this.provider = provider;
30
+ this.defaultAddress = {
31
+ hex: false,
32
+ base58: false,
33
+ };
34
+ this.trx.sign = (transaction) => provider.sign(transaction);
35
+ this.trx.getNodeInfo = (callback) => provider.getNodeInfo(callback);
36
+ }
37
+ request(args) {
38
+ return this.provider.request(args);
39
+ }
40
+ }
25
41
  class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
26
42
  constructor(props) {
27
43
  var _a;
@@ -45,16 +61,14 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
45
61
  eventServer: '',
46
62
  };
47
63
  this._log = (_a = props.logger) !== null && _a !== void 0 ? _a : window.console;
64
+ this._registerEvents();
48
65
  void this._initialize();
49
66
  }
50
67
  _registerTronWeb(nodes) {
51
- const tronWeb = new tronweb_1.default(Object.assign({}, nodes));
52
- tronWeb.trx.sign = (transaction) => this.sign(transaction);
53
- tronWeb.request = (args) => this.request(args);
54
- tronWeb.defaultAddress = {
55
- hex: false,
56
- base58: false,
57
- };
68
+ if ((0, lodash_1.isEmpty)(nodes))
69
+ return null;
70
+ const tronWeb = new OneKeyTronWeb(Object.assign({}, nodes), this);
71
+ tronWeb.getFullnodeVersion();
58
72
  // eslint-disable-next-line @typescript-eslint/no-this-alias
59
73
  const self = this;
60
74
  Object.defineProperty(tronWeb, 'defaultAddress', {
@@ -80,6 +94,8 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
80
94
  method: 'tron_getProviderState',
81
95
  });
82
96
  const tronWeb = this._registerTronWeb(nodes);
97
+ if (!tronWeb)
98
+ return;
83
99
  if (window.tronWeb !== undefined) {
84
100
  this._log.warn('OneKey: TronWeb is already initiated. Onekey will overwrite the current instance');
85
101
  }
@@ -87,8 +103,7 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
87
103
  // some DApp also check if the sunWeb object exists before requesting accounts
88
104
  window.sunWeb = {};
89
105
  this._initialized = true;
90
- this._registerEvents(tronWeb);
91
- this._handleAccountsChanged(accounts, tronWeb);
106
+ this._handleAccountsChanged(accounts);
92
107
  this._dispatch('tronLink#initialized');
93
108
  }
94
109
  catch (error) {
@@ -96,21 +111,26 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
96
111
  }
97
112
  });
98
113
  }
99
- _registerEvents(tronWeb) {
114
+ _registerEvents() {
100
115
  window.addEventListener('onekey_bridge_disconnect', () => {
101
116
  this.__handleDisconnected();
102
117
  });
103
118
  this.on(types_1.ProviderEvents.MESSAGE_LOW_LEVEL, (payload) => {
104
119
  const { method } = payload;
105
120
  if ((0, utils_1.isWalletEventMethodMatch)(method, types_1.ProviderEvents.ACCOUNTS_CHANGED)) {
106
- this._handleAccountsChanged(payload.params, tronWeb);
121
+ this._handleAccountsChanged(payload.params);
107
122
  }
108
123
  if ((0, utils_1.isWalletEventMethodMatch)(method, types_1.ProviderEvents.NODES_CHANGED)) {
109
- this._handleNodesChanged(payload.params, tronWeb);
124
+ if (this._initialized) {
125
+ this._handleNodesChanged(payload.params);
126
+ }
127
+ else {
128
+ void this._initialize();
129
+ }
110
130
  }
111
131
  });
112
132
  }
113
- _handleAccountsChanged(accounts, tronWeb) {
133
+ _handleAccountsChanged(accounts) {
114
134
  let _accounts = accounts;
115
135
  if (!Array.isArray(accounts)) {
116
136
  this._log.error('Onekey: Received invalid accounts parameter. Please report this bug.', accounts);
@@ -133,6 +153,7 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
133
153
  this._postMessage(types_1.ProviderEvents.SET_ACCOUNT, {
134
154
  address,
135
155
  });
156
+ const tronWeb = this.tronWeb;
136
157
  if (tronWeb.isAddress(address)) {
137
158
  tronWeb.setAddress(address);
138
159
  tronWeb.ready = true;
@@ -175,13 +196,15 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
175
196
  _dispatch(event) {
176
197
  window.dispatchEvent(new Event(event));
177
198
  }
178
- _handleNodesChanged(nodes, tronWeb) {
179
- var _a, _b, _c;
199
+ _handleNodesChanged(nodes) {
200
+ var _a, _b, _c, _d, _e, _f;
201
+ if ((0, lodash_1.isEmpty)(nodes))
202
+ return;
180
203
  if (!(0, fast_deep_equal_1.default)(nodes, this._nodes)) {
181
204
  this._nodes = nodes;
182
- tronWeb.setFullNode((_a = nodes.fullNode) !== null && _a !== void 0 ? _a : nodes.fullHost);
183
- tronWeb.setSolidityNode((_b = nodes.solidityNode) !== null && _b !== void 0 ? _b : nodes.fullHost);
184
- tronWeb.setEventServer((_c = nodes.eventServer) !== null && _c !== void 0 ? _c : nodes.fullHost);
205
+ (_a = this.tronWeb) === null || _a === void 0 ? void 0 : _a.setFullNode((_b = nodes.fullNode) !== null && _b !== void 0 ? _b : nodes.fullHost);
206
+ (_c = this.tronWeb) === null || _c === void 0 ? void 0 : _c.setSolidityNode((_d = nodes.solidityNode) !== null && _d !== void 0 ? _d : nodes.fullHost);
207
+ (_e = this.tronWeb) === null || _e === void 0 ? void 0 : _e.setEventServer((_f = nodes.eventServer) !== null && _f !== void 0 ? _f : nodes.fullHost);
185
208
  this._postMessage(types_1.ProviderEvents.NODES_CHANGED, Object.assign({}, nodes));
186
209
  }
187
210
  }
@@ -194,19 +217,28 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
194
217
  };
195
218
  }
196
219
  this._requestingAccounts = true;
197
- const accounts = (yield this.bridgeRequest(args));
198
- this._handleAccountsChanged(accounts, this.tronWeb);
199
- this._requestingAccounts = false;
200
- if (accounts.length > 0) {
220
+ try {
221
+ const accounts = (yield this.bridgeRequest(args));
222
+ this._handleAccountsChanged(accounts);
223
+ this._requestingAccounts = false;
224
+ if (accounts.length > 0) {
225
+ return {
226
+ code: 200,
227
+ message: 'ok',
228
+ };
229
+ }
201
230
  return {
202
- code: 200,
203
- message: 'ok',
231
+ code: 4000,
232
+ message: 'user rejected',
233
+ };
234
+ }
235
+ catch (e) {
236
+ this._requestingAccounts = false;
237
+ return {
238
+ code: 4000,
239
+ message: 'user rejected',
204
240
  };
205
241
  }
206
- return {
207
- code: 4000,
208
- message: 'user rejected',
209
- };
210
242
  });
211
243
  }
212
244
  request(args) {
@@ -237,5 +269,15 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
237
269
  });
238
270
  });
239
271
  }
272
+ getNodeInfo(callback) {
273
+ return __awaiter(this, void 0, void 0, function* () {
274
+ const info = yield this.request({
275
+ method: 'tron_getNodeInfo',
276
+ });
277
+ if (!callback)
278
+ return info;
279
+ callback(null, info);
280
+ });
281
+ }
240
282
  }
241
283
  exports.ProviderTron = ProviderTron;
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IJsonRpcRequest } from '@onekeyfe/cross-inpage-provider-types';
2
- import TronWeb from 'tronweb';
2
+ import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb';
3
3
  import { ProviderTronBase } from './ProviderTronBase';
4
4
  export declare enum ProviderEvents {
5
5
  TAB_REPLY = "tabReply",
@@ -36,12 +36,15 @@ export interface RequestArguments {
36
36
  method: string;
37
37
  params?: unknown[] | Record<string, unknown>;
38
38
  }
39
+ export declare type Callback = false | ((err: Error | null, info: any) => any);
39
40
  export interface IProviderTron extends ProviderTronBase {
40
41
  readonly isTronLink: true;
41
42
  tronWeb: TronWeb | null;
42
43
  sunWeb: any;
43
44
  ready: boolean;
44
45
  request<T>(args: RequestArguments): Promise<T>;
46
+ sign(transaction: UnsignedTransaction): Promise<SignedTransaction>;
47
+ getNodeInfo(callback?: Callback): Promise<any>;
45
48
  }
46
49
  export interface requestAccountsResponse {
47
50
  code: 200 | 4000 | 4001;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/onekey-tron-provider",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "keywords": [
5
5
  "cross-inpage-provider"
6
6
  ],
@@ -28,12 +28,12 @@
28
28
  "start": "tsc --watch"
29
29
  },
30
30
  "dependencies": {
31
- "@onekeyfe/cross-inpage-provider-core": "1.1.5",
32
- "@onekeyfe/cross-inpage-provider-errors": "1.1.5",
33
- "@onekeyfe/cross-inpage-provider-types": "1.1.5",
34
- "@onekeyfe/extension-bridge-injected": "1.1.5",
31
+ "@onekeyfe/cross-inpage-provider-core": "1.1.6",
32
+ "@onekeyfe/cross-inpage-provider-errors": "1.1.6",
33
+ "@onekeyfe/cross-inpage-provider-types": "1.1.6",
34
+ "@onekeyfe/extension-bridge-injected": "1.1.6",
35
35
  "querystring": "^0.2.1",
36
36
  "tronweb": "^4.4.0"
37
37
  },
38
- "gitHead": "548bcfe561ad682347e2551c5f7b3893b5d19f74"
38
+ "gitHead": "74a047c11aa3bbc20e3b3cc0dac84eea9447083c"
39
39
  }