@onekeyfe/onekey-tron-provider 2.1.0 → 2.1.2

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,4 +1,4 @@
1
- import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb';
1
+ import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb/dist/TronWeb';
2
2
  import SunWeb from 'sunweb';
3
3
  import { IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
4
4
  import { ProviderTronBase } from './ProviderTronBase';
@@ -11,6 +11,9 @@ export declare const CONTRACT_ADDRESS: {
11
11
  SIDE: string;
12
12
  };
13
13
  export declare const SIDE_CHAIN_ID = "41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7";
14
+ export declare const AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST: string[];
15
+ export declare const TRON_REQUEST_ACCOUNTS_LOCAL_KEY = "onekey_tron_request_accounts_local_key";
16
+ export declare const TRON_REQUEST_ACCOUNTS_INTERVAL: number;
14
17
  declare class ProviderTron extends ProviderTronBase implements IProviderTron {
15
18
  readonly isTronLink = true;
16
19
  tronWeb: TronWeb | null;
@@ -7,8 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
11
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
10
12
  import dequal from 'fast-deep-equal';
11
- import TronWeb from 'tronweb';
13
+ import TronWeb from 'tronweb/dist/TronWeb';
12
14
  import SunWeb from 'sunweb';
13
15
  import { isEmpty } from 'lodash';
14
16
  import { checkWalletSwitchEnable, defineWindowProperty, } from '@onekeyfe/cross-inpage-provider-core';
@@ -17,11 +19,19 @@ import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
17
19
  import { ProviderTronBase } from './ProviderTronBase';
18
20
  import { ProviderEvents, } from './types';
19
21
  import { isWalletEventMethodMatch } from './utils';
22
+ import BigNumber from 'bignumber.js';
20
23
  export const CONTRACT_ADDRESS = {
21
24
  MAIN: 'TL9q7aDAHYbW5KdPCwk8oJR3bCDhRwegFf',
22
25
  SIDE: 'TGKotco6YoULzbYisTBuP6DWXDjEgJSpYz',
23
26
  };
24
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
+ ];
32
+ export const TRON_REQUEST_ACCOUNTS_LOCAL_KEY = 'onekey_tron_request_accounts_local_key';
33
+ export const TRON_REQUEST_ACCOUNTS_INTERVAL = 10 * 60 * 1000; // ten minutes
34
+ const globalWindow = typeof window !== 'undefined' ? window : global;
25
35
  class OneKeyTronWeb extends TronWeb {
26
36
  constructor(props, provider) {
27
37
  super(props);
@@ -98,8 +108,33 @@ class ProviderTron extends ProviderTronBase {
98
108
  const self = this;
99
109
  Object.defineProperty(tronWeb, 'defaultAddress', {
100
110
  get() {
111
+ var _a;
101
112
  if (!self._connected) {
102
113
  self._log.warn('OneKey: We recommend that DApp developers use $onekey.tron.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
114
+ const origin = ((_a = globalWindow === null || globalWindow === void 0 ? void 0 : globalWindow.location) === null || _a === void 0 ? void 0 : _a.origin) || '';
115
+ if (origin && AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST.includes(origin)) {
116
+ const requestAccountsLocalStr = localStorage.getItem(TRON_REQUEST_ACCOUNTS_LOCAL_KEY);
117
+ const requestAccountsLocal = requestAccountsLocalStr
118
+ ? JSON.parse(requestAccountsLocalStr)
119
+ : null;
120
+ if (requestAccountsLocal && requestAccountsLocal[origin]) {
121
+ const requestTimeStamp = requestAccountsLocal[origin];
122
+ if (new BigNumber(Date.now())
123
+ .minus(requestTimeStamp)
124
+ .isGreaterThan(TRON_REQUEST_ACCOUNTS_INTERVAL)) {
125
+ localStorage.setItem(TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
126
+ void self.request({
127
+ method: 'tron_requestAccounts',
128
+ });
129
+ }
130
+ }
131
+ else {
132
+ localStorage.setItem(TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
133
+ void self.request({
134
+ method: 'tron_requestAccounts',
135
+ });
136
+ }
137
+ }
103
138
  }
104
139
  return self._defaultAddress;
105
140
  },
@@ -193,6 +228,7 @@ class ProviderTron extends ProviderTronBase {
193
228
  if (!this._connected) {
194
229
  this._connected = true;
195
230
  this._postMessage(ProviderEvents.CONNECT);
231
+ this._postMessage(ProviderEvents.ACCEPT_WEB);
196
232
  }
197
233
  }
198
234
  _postMessage(action, data) {
@@ -12,10 +12,12 @@ 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 = exports.SIDE_CHAIN_ID = exports.CONTRACT_ADDRESS = void 0;
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 */
16
18
  const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
17
- const tronweb_1 = __importDefault(require("tronweb"));
18
- exports.TronWeb = tronweb_1.default;
19
+ const TronWeb_1 = __importDefault(require("tronweb/dist/TronWeb"));
20
+ exports.TronWeb = TronWeb_1.default;
19
21
  const sunweb_1 = __importDefault(require("sunweb"));
20
22
  const lodash_1 = require("lodash");
21
23
  const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
@@ -25,12 +27,20 @@ const ProviderTronBase_1 = require("./ProviderTronBase");
25
27
  const types_1 = require("./types");
26
28
  Object.defineProperty(exports, "ProviderEvents", { enumerable: true, get: function () { return types_1.ProviderEvents; } });
27
29
  const utils_1 = require("./utils");
30
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
28
31
  exports.CONTRACT_ADDRESS = {
29
32
  MAIN: 'TL9q7aDAHYbW5KdPCwk8oJR3bCDhRwegFf',
30
33
  SIDE: 'TGKotco6YoULzbYisTBuP6DWXDjEgJSpYz',
31
34
  };
32
35
  exports.SIDE_CHAIN_ID = '41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7';
33
- class OneKeyTronWeb extends tronweb_1.default {
36
+ exports.AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST = [
37
+ 'https://tronscan.org',
38
+ 'https://tronscan.io',
39
+ ];
40
+ exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY = 'onekey_tron_request_accounts_local_key';
41
+ exports.TRON_REQUEST_ACCOUNTS_INTERVAL = 10 * 60 * 1000; // ten minutes
42
+ const globalWindow = typeof window !== 'undefined' ? window : global;
43
+ class OneKeyTronWeb extends TronWeb_1.default {
34
44
  constructor(props, provider) {
35
45
  super(props);
36
46
  this.provider = provider;
@@ -106,8 +116,33 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
106
116
  const self = this;
107
117
  Object.defineProperty(tronWeb, 'defaultAddress', {
108
118
  get() {
119
+ var _a;
109
120
  if (!self._connected) {
110
121
  self._log.warn('OneKey: We recommend that DApp developers use $onekey.tron.request({method: "tron_requestAccounts"}) to request users’ account information at the earliest time possible in order to get a complete TronWeb injection.');
122
+ const origin = ((_a = globalWindow === null || globalWindow === void 0 ? void 0 : globalWindow.location) === null || _a === void 0 ? void 0 : _a.origin) || '';
123
+ if (origin && exports.AUTO_REQUEST_ACCOUNTS_ORIGIN_WHITE_LIST.includes(origin)) {
124
+ const requestAccountsLocalStr = localStorage.getItem(exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY);
125
+ const requestAccountsLocal = requestAccountsLocalStr
126
+ ? JSON.parse(requestAccountsLocalStr)
127
+ : null;
128
+ if (requestAccountsLocal && requestAccountsLocal[origin]) {
129
+ const requestTimeStamp = requestAccountsLocal[origin];
130
+ if (new bignumber_js_1.default(Date.now())
131
+ .minus(requestTimeStamp)
132
+ .isGreaterThan(exports.TRON_REQUEST_ACCOUNTS_INTERVAL)) {
133
+ localStorage.setItem(exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
134
+ void self.request({
135
+ method: 'tron_requestAccounts',
136
+ });
137
+ }
138
+ }
139
+ else {
140
+ localStorage.setItem(exports.TRON_REQUEST_ACCOUNTS_LOCAL_KEY, JSON.stringify(Object.assign(Object.assign({}, requestAccountsLocal), { [origin]: Date.now() })));
141
+ void self.request({
142
+ method: 'tron_requestAccounts',
143
+ });
144
+ }
145
+ }
111
146
  }
112
147
  return self._defaultAddress;
113
148
  },
@@ -201,6 +236,7 @@ class ProviderTron extends ProviderTronBase_1.ProviderTronBase {
201
236
  if (!this._connected) {
202
237
  this._connected = true;
203
238
  this._postMessage(types_1.ProviderEvents.CONNECT);
239
+ this._postMessage(types_1.ProviderEvents.ACCEPT_WEB);
204
240
  }
205
241
  }
206
242
  _postMessage(action, data) {
package/dist/cjs/types.js CHANGED
@@ -13,4 +13,5 @@ var ProviderEvents;
13
13
  ProviderEvents["MESSAGE"] = "message";
14
14
  ProviderEvents["MESSAGE_LOW_LEVEL"] = "message_low_level";
15
15
  ProviderEvents["CHAIN_CHANGED"] = "chainChanged";
16
+ ProviderEvents["ACCEPT_WEB"] = "acceptWeb";
16
17
  })(ProviderEvents = exports.ProviderEvents || (exports.ProviderEvents = {}));
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IJsonRpcRequest } from '@onekeyfe/cross-inpage-provider-types';
2
- import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb';
2
+ import TronWeb, { UnsignedTransaction, SignedTransaction } from 'tronweb/dist/TronWeb';
3
3
  import { ProviderTronBase } from './ProviderTronBase';
4
4
  export declare enum ProviderEvents {
5
5
  TAB_REPLY = "tabReply",
@@ -11,7 +11,8 @@ export declare enum ProviderEvents {
11
11
  NODES_CHANGED = "nodesChanged",
12
12
  MESSAGE = "message",
13
13
  MESSAGE_LOW_LEVEL = "message_low_level",
14
- CHAIN_CHANGED = "chainChanged"
14
+ CHAIN_CHANGED = "chainChanged",
15
+ ACCEPT_WEB = "acceptWeb"
15
16
  }
16
17
  export interface Nodes {
17
18
  fullHost: string;
package/dist/types.js CHANGED
@@ -10,4 +10,5 @@ export var ProviderEvents;
10
10
  ProviderEvents["MESSAGE"] = "message";
11
11
  ProviderEvents["MESSAGE_LOW_LEVEL"] = "message_low_level";
12
12
  ProviderEvents["CHAIN_CHANGED"] = "chainChanged";
13
+ ProviderEvents["ACCEPT_WEB"] = "acceptWeb";
13
14
  })(ProviderEvents || (ProviderEvents = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/onekey-tron-provider",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "keywords": [
5
5
  "cross-inpage-provider"
6
6
  ],
@@ -29,13 +29,13 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@noble/secp256k1": "1.7.1",
32
- "@onekeyfe/cross-inpage-provider-core": "2.1.0",
33
- "@onekeyfe/cross-inpage-provider-errors": "2.1.0",
34
- "@onekeyfe/cross-inpage-provider-types": "2.1.0",
35
- "@onekeyfe/extension-bridge-injected": "2.1.0",
32
+ "@onekeyfe/cross-inpage-provider-core": "2.1.2",
33
+ "@onekeyfe/cross-inpage-provider-errors": "2.1.2",
34
+ "@onekeyfe/cross-inpage-provider-types": "2.1.2",
35
+ "@onekeyfe/extension-bridge-injected": "2.1.2",
36
36
  "querystring": "^0.2.1",
37
37
  "sunweb": "^1.0.7",
38
38
  "tronweb": "^5.3.2"
39
39
  },
40
- "gitHead": "6c27b10b87edbd6d6c839566276c7f6f9cb4940c"
40
+ "gitHead": "18c5992b3eb11029f807d9488d2bb8a378e9b503"
41
41
  }