@onekeyfe/onekey-tron-provider 1.1.21 → 1.1.22

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,14 +1,20 @@
1
1
  import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb';
2
+ import SunWeb from 'sunweb';
2
3
  import { IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
3
4
  import { ProviderTronBase } from './ProviderTronBase';
4
5
  import { IProviderTron, ProviderEvents, ProviderEventsMap, ConsoleLike, Nodes, Callback, RequestArguments } from './types';
5
6
  declare type OneKeyTronProviderProps = IInpageProviderConfig & {
6
7
  timeout?: number;
7
8
  };
9
+ export declare const CONTRACT_ADDRESS: {
10
+ MAIN: string;
11
+ SIDE: string;
12
+ };
13
+ export declare const SIDE_CHAIN_ID = "41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7";
8
14
  declare class ProviderTron extends ProviderTronBase implements IProviderTron {
9
15
  readonly isTronLink = true;
10
16
  tronWeb: TronWeb | null;
11
- sunWeb: {};
17
+ sunWeb: SunWeb | null;
12
18
  ready: boolean;
13
19
  private _initialized;
14
20
  private _connected;
@@ -9,12 +9,19 @@ 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 SunWeb from 'sunweb';
12
13
  import { isEmpty } from 'lodash';
14
+ import { checkWalletSwitchEnable, defineWindowProperty, } from '@onekeyfe/cross-inpage-provider-core';
13
15
  import { getOrCreateExtInjectedJsBridge } from '@onekeyfe/extension-bridge-injected';
14
16
  import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
15
17
  import { ProviderTronBase } from './ProviderTronBase';
16
18
  import { ProviderEvents, } from './types';
17
19
  import { isWalletEventMethodMatch } from './utils';
20
+ export const CONTRACT_ADDRESS = {
21
+ MAIN: 'TL9q7aDAHYbW5KdPCwk8oJR3bCDhRwegFf',
22
+ SIDE: 'TGKotco6YoULzbYisTBuP6DWXDjEgJSpYz',
23
+ };
24
+ export const SIDE_CHAIN_ID = '41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7';
18
25
  class OneKeyTronWeb extends TronWeb {
19
26
  constructor(props, provider) {
20
27
  super(props);
@@ -36,7 +43,7 @@ class ProviderTron extends ProviderTronBase {
36
43
  super(Object.assign(Object.assign({}, props), { bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }) }));
37
44
  this.isTronLink = true;
38
45
  this.tronWeb = null;
39
- this.sunWeb = {};
46
+ this.sunWeb = null;
40
47
  this.ready = false;
41
48
  this._initialized = false;
42
49
  this._connected = false;
@@ -53,31 +60,19 @@ class ProviderTron extends ProviderTronBase {
53
60
  eventServer: '',
54
61
  };
55
62
  this._log = (_a = props.logger) !== null && _a !== void 0 ? _a : window.console;
56
- this._registerEvents();
57
- void this._initialize();
63
+ if (checkWalletSwitchEnable('tronLink')) {
64
+ this._registerEvents();
65
+ void this._initialize();
66
+ }
58
67
  }
59
68
  _registerTronWeb(nodes) {
60
69
  if (isEmpty(nodes))
61
70
  return null;
62
71
  const tronWeb = new OneKeyTronWeb(Object.assign({}, nodes), this);
63
- tronWeb.getFullnodeVersion();
64
- // eslint-disable-next-line @typescript-eslint/no-this-alias
65
- const self = this;
66
- Object.defineProperty(tronWeb, 'defaultAddress', {
67
- get() {
68
- if (!self._connected) {
69
- self._log.warn('OneKey: We recommend that DApp developers use tronLink.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
70
- void self.request({
71
- method: 'tron_requestAccounts',
72
- });
73
- }
74
- return self._defaultAddress;
75
- },
76
- set(value) {
77
- self._defaultAddress = value;
78
- },
79
- });
80
- return tronWeb;
72
+ const tronWeb1 = new OneKeyTronWeb(Object.assign({}, nodes), this);
73
+ const tronWeb2 = new OneKeyTronWeb(Object.assign({}, nodes), this);
74
+ const sunWeb = new SunWeb(tronWeb1, tronWeb2, CONTRACT_ADDRESS.MAIN, CONTRACT_ADDRESS.SIDE, SIDE_CHAIN_ID);
75
+ return { tronWeb, sunWeb };
81
76
  }
82
77
  _initialize() {
83
78
  return __awaiter(this, void 0, void 0, function* () {
@@ -85,18 +80,39 @@ class ProviderTron extends ProviderTronBase {
85
80
  const { accounts, nodes } = yield this.request({
86
81
  method: 'tron_getProviderState',
87
82
  });
88
- const tronWeb = this._registerTronWeb(nodes);
89
- if (!tronWeb)
83
+ const resp = this._registerTronWeb(nodes);
84
+ if (!resp)
90
85
  return;
86
+ const { sunWeb, tronWeb } = resp;
91
87
  if (window.tronWeb !== undefined) {
92
88
  this._log.warn('OneKey: TronWeb is already initiated. Onekey will overwrite the current instance');
93
89
  }
94
- window.tronWeb = this.tronWeb = tronWeb;
95
- // some DApp also check if the sunWeb object exists before requesting accounts
96
- window.sunWeb = {};
97
- this._initialized = true;
90
+ if (window.sunWeb !== undefined) {
91
+ this._log.warn('OneKey: TronWeb is already initiated. Onekey will overwrite the current instance');
92
+ }
93
+ this.tronWeb = tronWeb;
94
+ this.sunWeb = sunWeb;
95
+ defineWindowProperty('tronWeb', tronWeb);
96
+ defineWindowProperty('sunWeb', sunWeb);
97
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
98
+ const self = this;
99
+ Object.defineProperty(tronWeb, 'defaultAddress', {
100
+ get() {
101
+ if (!self._connected) {
102
+ self._log.warn('OneKey: We recommend that DApp developers use tronLink.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
103
+ void self.request({
104
+ method: 'tron_requestAccounts',
105
+ });
106
+ }
107
+ return self._defaultAddress;
108
+ },
109
+ set(value) {
110
+ self._defaultAddress = value;
111
+ },
112
+ });
98
113
  this._handleAccountsChanged(accounts);
99
114
  this._dispatch('tronLink#initialized');
115
+ this._initialized = true;
100
116
  }
101
117
  catch (error) {
102
118
  this._log.error('OneKey: Failed to get initial state. Please report this bug.', error);
@@ -141,29 +157,32 @@ class ProviderTron extends ProviderTronBase {
141
157
  if (this.isAccountsChanged(_accounts)) {
142
158
  this._accounts = _accounts;
143
159
  const address = _accounts[0];
160
+ const tronWeb = this.tronWeb;
161
+ if (!tronWeb) {
162
+ return;
163
+ }
164
+ if (tronWeb.isAddress(address)) {
165
+ tronWeb.setAddress(address);
166
+ tronWeb.ready = true;
167
+ this.ready = true;
168
+ this._handleConnected();
169
+ }
170
+ else {
171
+ tronWeb.defaultAddress = {
172
+ hex: false,
173
+ base58: false,
174
+ };
175
+ tronWeb.ready = false;
176
+ this.ready = false;
177
+ this.__handleDisconnected();
178
+ }
144
179
  if (this._initialized) {
145
- this._postMessage(ProviderEvents.ACCOUNTS_CHANGED, {
180
+ this._postMessage(ProviderEvents.SET_ACCOUNT, {
146
181
  address,
147
182
  });
148
- this._postMessage(ProviderEvents.SET_ACCOUNT, {
183
+ this._postMessage(ProviderEvents.ACCOUNTS_CHANGED, {
149
184
  address,
150
185
  });
151
- const tronWeb = this.tronWeb;
152
- if (tronWeb.isAddress(address)) {
153
- tronWeb.setAddress(address);
154
- tronWeb.ready = true;
155
- this.ready = true;
156
- this._handleConnected();
157
- }
158
- else {
159
- tronWeb.defaultAddress = {
160
- hex: false,
161
- base58: false,
162
- };
163
- tronWeb.ready = false;
164
- this.ready = false;
165
- this.__handleDisconnected();
166
- }
167
186
  }
168
187
  }
169
188
  }
@@ -12,17 +12,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.TronWeb = exports.ProviderEvents = exports.ProviderTron = void 0;
15
+ exports.TronWeb = exports.ProviderEvents = exports.ProviderTron = exports.SIDE_CHAIN_ID = exports.CONTRACT_ADDRESS = 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 sunweb_1 = __importDefault(require("sunweb"));
19
20
  const lodash_1 = require("lodash");
21
+ const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
20
22
  const extension_bridge_injected_1 = require("@onekeyfe/extension-bridge-injected");
21
23
  const cross_inpage_provider_errors_1 = require("@onekeyfe/cross-inpage-provider-errors");
22
24
  const ProviderTronBase_1 = require("./ProviderTronBase");
23
25
  const types_1 = require("./types");
24
26
  Object.defineProperty(exports, "ProviderEvents", { enumerable: true, get: function () { return types_1.ProviderEvents; } });
25
27
  const utils_1 = require("./utils");
28
+ exports.CONTRACT_ADDRESS = {
29
+ MAIN: 'TL9q7aDAHYbW5KdPCwk8oJR3bCDhRwegFf',
30
+ SIDE: 'TGKotco6YoULzbYisTBuP6DWXDjEgJSpYz',
31
+ };
32
+ exports.SIDE_CHAIN_ID = '41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7';
26
33
  class OneKeyTronWeb extends tronweb_1.default {
27
34
  constructor(props, provider) {
28
35
  super(props);
@@ -44,7 +51,7 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
44
51
  super(Object.assign(Object.assign({}, props), { bridge: props.bridge || (0, extension_bridge_injected_1.getOrCreateExtInjectedJsBridge)({ timeout: props.timeout }) }));
45
52
  this.isTronLink = true;
46
53
  this.tronWeb = null;
47
- this.sunWeb = {};
54
+ this.sunWeb = null;
48
55
  this.ready = false;
49
56
  this._initialized = false;
50
57
  this._connected = false;
@@ -61,31 +68,19 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
61
68
  eventServer: '',
62
69
  };
63
70
  this._log = (_a = props.logger) !== null && _a !== void 0 ? _a : window.console;
64
- this._registerEvents();
65
- void this._initialize();
71
+ if ((0, cross_inpage_provider_core_1.checkWalletSwitchEnable)('tronLink')) {
72
+ this._registerEvents();
73
+ void this._initialize();
74
+ }
66
75
  }
67
76
  _registerTronWeb(nodes) {
68
77
  if ((0, lodash_1.isEmpty)(nodes))
69
78
  return null;
70
79
  const tronWeb = new OneKeyTronWeb(Object.assign({}, nodes), this);
71
- tronWeb.getFullnodeVersion();
72
- // eslint-disable-next-line @typescript-eslint/no-this-alias
73
- const self = this;
74
- Object.defineProperty(tronWeb, 'defaultAddress', {
75
- get() {
76
- if (!self._connected) {
77
- self._log.warn('OneKey: We recommend that DApp developers use tronLink.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
78
- void self.request({
79
- method: 'tron_requestAccounts',
80
- });
81
- }
82
- return self._defaultAddress;
83
- },
84
- set(value) {
85
- self._defaultAddress = value;
86
- },
87
- });
88
- return tronWeb;
80
+ const tronWeb1 = new OneKeyTronWeb(Object.assign({}, nodes), this);
81
+ const tronWeb2 = new OneKeyTronWeb(Object.assign({}, nodes), this);
82
+ const sunWeb = new sunweb_1.default(tronWeb1, tronWeb2, exports.CONTRACT_ADDRESS.MAIN, exports.CONTRACT_ADDRESS.SIDE, exports.SIDE_CHAIN_ID);
83
+ return { tronWeb, sunWeb };
89
84
  }
90
85
  _initialize() {
91
86
  return __awaiter(this, void 0, void 0, function* () {
@@ -93,18 +88,39 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
93
88
  const { accounts, nodes } = yield this.request({
94
89
  method: 'tron_getProviderState',
95
90
  });
96
- const tronWeb = this._registerTronWeb(nodes);
97
- if (!tronWeb)
91
+ const resp = this._registerTronWeb(nodes);
92
+ if (!resp)
98
93
  return;
94
+ const { sunWeb, tronWeb } = resp;
99
95
  if (window.tronWeb !== undefined) {
100
96
  this._log.warn('OneKey: TronWeb is already initiated. Onekey will overwrite the current instance');
101
97
  }
102
- window.tronWeb = this.tronWeb = tronWeb;
103
- // some DApp also check if the sunWeb object exists before requesting accounts
104
- window.sunWeb = {};
105
- this._initialized = true;
98
+ if (window.sunWeb !== undefined) {
99
+ this._log.warn('OneKey: TronWeb is already initiated. Onekey will overwrite the current instance');
100
+ }
101
+ this.tronWeb = tronWeb;
102
+ this.sunWeb = sunWeb;
103
+ (0, cross_inpage_provider_core_1.defineWindowProperty)('tronWeb', tronWeb);
104
+ (0, cross_inpage_provider_core_1.defineWindowProperty)('sunWeb', sunWeb);
105
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
106
+ const self = this;
107
+ Object.defineProperty(tronWeb, 'defaultAddress', {
108
+ get() {
109
+ if (!self._connected) {
110
+ self._log.warn('OneKey: We recommend that DApp developers use tronLink.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
111
+ void self.request({
112
+ method: 'tron_requestAccounts',
113
+ });
114
+ }
115
+ return self._defaultAddress;
116
+ },
117
+ set(value) {
118
+ self._defaultAddress = value;
119
+ },
120
+ });
106
121
  this._handleAccountsChanged(accounts);
107
122
  this._dispatch('tronLink#initialized');
123
+ this._initialized = true;
108
124
  }
109
125
  catch (error) {
110
126
  this._log.error('OneKey: Failed to get initial state. Please report this bug.', error);
@@ -149,29 +165,32 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
149
165
  if (this.isAccountsChanged(_accounts)) {
150
166
  this._accounts = _accounts;
151
167
  const address = _accounts[0];
168
+ const tronWeb = this.tronWeb;
169
+ if (!tronWeb) {
170
+ return;
171
+ }
172
+ if (tronWeb.isAddress(address)) {
173
+ tronWeb.setAddress(address);
174
+ tronWeb.ready = true;
175
+ this.ready = true;
176
+ this._handleConnected();
177
+ }
178
+ else {
179
+ tronWeb.defaultAddress = {
180
+ hex: false,
181
+ base58: false,
182
+ };
183
+ tronWeb.ready = false;
184
+ this.ready = false;
185
+ this.__handleDisconnected();
186
+ }
152
187
  if (this._initialized) {
153
- this._postMessage(types_1.ProviderEvents.ACCOUNTS_CHANGED, {
188
+ this._postMessage(types_1.ProviderEvents.SET_ACCOUNT, {
154
189
  address,
155
190
  });
156
- this._postMessage(types_1.ProviderEvents.SET_ACCOUNT, {
191
+ this._postMessage(types_1.ProviderEvents.ACCOUNTS_CHANGED, {
157
192
  address,
158
193
  });
159
- const tronWeb = this.tronWeb;
160
- if (tronWeb.isAddress(address)) {
161
- tronWeb.setAddress(address);
162
- tronWeb.ready = true;
163
- this.ready = true;
164
- this._handleConnected();
165
- }
166
- else {
167
- tronWeb.defaultAddress = {
168
- hex: false,
169
- base58: false,
170
- };
171
- tronWeb.ready = false;
172
- this.ready = false;
173
- this.__handleDisconnected();
174
- }
175
194
  }
176
195
  }
177
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/onekey-tron-provider",
3
- "version": "1.1.21",
3
+ "version": "1.1.22",
4
4
  "keywords": [
5
5
  "cross-inpage-provider"
6
6
  ],
@@ -28,12 +28,13 @@
28
28
  "start": "tsc --watch"
29
29
  },
30
30
  "dependencies": {
31
- "@onekeyfe/cross-inpage-provider-core": "1.1.21",
32
- "@onekeyfe/cross-inpage-provider-errors": "1.1.21",
33
- "@onekeyfe/cross-inpage-provider-types": "1.1.21",
34
- "@onekeyfe/extension-bridge-injected": "1.1.21",
31
+ "@onekeyfe/cross-inpage-provider-core": "1.1.22",
32
+ "@onekeyfe/cross-inpage-provider-errors": "1.1.22",
33
+ "@onekeyfe/cross-inpage-provider-types": "1.1.22",
34
+ "@onekeyfe/extension-bridge-injected": "1.1.22",
35
35
  "querystring": "^0.2.1",
36
+ "sunweb": "^1.0.7",
36
37
  "tronweb": "^4.4.0"
37
38
  },
38
- "gitHead": "1a4b0188d8795ae0a3fed605fa53c056b6966b6a"
39
+ "gitHead": "605e8793b6ec2251f0f0c39cf13381495a1be500"
39
40
  }