@onekeyfe/onekey-sui-provider 2.1.8 → 2.1.10

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.
@@ -14,7 +14,10 @@ declare const PROVIDER_EVENTS: {
14
14
  type SuiProviderEventsMap = {
15
15
  [PROVIDER_EVENTS.connect]: (account: string) => void;
16
16
  [PROVIDER_EVENTS.disconnect]: () => void;
17
- [PROVIDER_EVENTS.accountChanged]: (account: string | null) => void;
17
+ [PROVIDER_EVENTS.accountChanged]: (account: {
18
+ address: string;
19
+ publicKey: string;
20
+ } | null) => void;
18
21
  [PROVIDER_EVENTS.networkChange]: (name: string | null) => void;
19
22
  [PROVIDER_EVENTS.message_low_level]: (payload: IJsonRpcRequest) => void;
20
23
  };
@@ -11,6 +11,7 @@ import { bytesToHex } from '@noble/hashes/utils';
11
11
  import { getOrCreateExtInjectedJsBridge } from '@onekeyfe/extension-bridge-injected';
12
12
  import { ProviderSuiBase } from './ProviderSuiBase';
13
13
  import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
14
+ import { TransactionBlock } from '@mysten/sui.js/transactions';
14
15
  import { ALL_PERMISSION_TYPES } from './types';
15
16
  const PROVIDER_EVENTS = {
16
17
  'connect': 'connect',
@@ -48,18 +49,15 @@ class ProviderSui extends ProviderSuiBase {
48
49
  return this.bridgeRequest(params);
49
50
  }
50
51
  _handleConnected(account, options = { emit: true }) {
51
- this._account = account;
52
- if (options.emit && this.isConnectionStatusChanged('connected')) {
53
- this.connectionStatus = 'connected';
54
- const address = account !== null && account !== void 0 ? account : null;
55
- this.emit('connect', address.address);
56
- this.emit('accountChanged', address.address);
52
+ var _a;
53
+ if (options.emit) {
54
+ this.emit('connect', (_a = account === null || account === void 0 ? void 0 : account.address) !== null && _a !== void 0 ? _a : null);
55
+ this.emit('accountChanged', account ? { address: account === null || account === void 0 ? void 0 : account.address, publicKey: account === null || account === void 0 ? void 0 : account.publicKey } : null);
57
56
  }
58
57
  }
59
58
  _handleDisconnected(options = { emit: true }) {
60
59
  this._account = null;
61
- if (options.emit && this.isConnectionStatusChanged('disconnected')) {
62
- this.connectionStatus = 'disconnected';
60
+ if (options.emit) {
63
61
  this.emit('disconnect');
64
62
  this.emit('accountChanged', null);
65
63
  }
@@ -70,15 +68,14 @@ class ProviderSui extends ProviderSuiBase {
70
68
  }
71
69
  // trigger by bridge account change event
72
70
  _handleAccountChange(payload) {
73
- const account = payload;
74
- if (this.isAccountsChanged(account)) {
75
- this.emit('accountChanged', (account === null || account === void 0 ? void 0 : account.address) || null);
76
- }
77
- if (!account) {
71
+ if (!payload) {
78
72
  this._handleDisconnected();
79
73
  return;
80
74
  }
81
- this._handleConnected(account, { emit: false });
75
+ if (this.isAccountsChanged(payload)) {
76
+ this._handleConnected(payload);
77
+ }
78
+ this._account = payload;
82
79
  }
83
80
  isNetworkChanged(network) {
84
81
  return this._network === undefined || network !== this._network;
@@ -92,7 +89,7 @@ class ProviderSui extends ProviderSuiBase {
92
89
  }
93
90
  hasPermissions(permissions = ALL_PERMISSION_TYPES) {
94
91
  return __awaiter(this, void 0, void 0, function* () {
95
- return this._callBridge({
92
+ return yield this._callBridge({
96
93
  method: 'hasPermissions',
97
94
  params: permissions,
98
95
  });
@@ -100,7 +97,7 @@ class ProviderSui extends ProviderSuiBase {
100
97
  }
101
98
  requestPermissions(permissions = ALL_PERMISSION_TYPES) {
102
99
  return __awaiter(this, void 0, void 0, function* () {
103
- return this._callBridge({
100
+ return yield this._callBridge({
104
101
  method: 'requestPermissions',
105
102
  params: permissions,
106
103
  });
@@ -125,7 +122,6 @@ class ProviderSui extends ProviderSuiBase {
125
122
  this._handleDisconnected();
126
123
  throw web3Errors.provider.unauthorized();
127
124
  }
128
- this._handleConnected(accounts[0]);
129
125
  return accounts;
130
126
  });
131
127
  }
@@ -141,7 +137,10 @@ class ProviderSui extends ProviderSuiBase {
141
137
  return __awaiter(this, void 0, void 0, function* () {
142
138
  return this._callBridge({
143
139
  method: 'signAndExecuteTransactionBlock',
144
- params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
140
+ params: Object.assign(Object.assign({}, input), {
141
+ // https://github.com/MystenLabs/sui/blob/ace69fa8404eb704b504082d324ebc355a3d2948/sdk/typescript/src/transactions/object.ts#L6-L17
142
+ // With a few more objects, other wallets have steps for tojson.
143
+ transactionBlock: TransactionBlock.from(input.transactionBlock.serialize()), walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
145
144
  });
146
145
  });
147
146
  }
@@ -149,7 +148,7 @@ class ProviderSui extends ProviderSuiBase {
149
148
  return __awaiter(this, void 0, void 0, function* () {
150
149
  return this._callBridge({
151
150
  method: 'signTransactionBlock',
152
- params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
151
+ params: Object.assign(Object.assign({}, input), { transactionBlock: TransactionBlock.from(input.transactionBlock.serialize()), walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
153
152
  });
154
153
  });
155
154
  }
@@ -77,7 +77,6 @@ class OnekeySuiStandardWallet {
77
77
  return () => this._events.off(event, listener);
78
78
  };
79
79
  this.$connected = () => __awaiter(this, void 0, void 0, function* () {
80
- const activeChain = yield this.getActiveChain();
81
80
  if (!(yield this.$hasPermissions(['viewAccount']))) {
82
81
  return;
83
82
  }
@@ -88,19 +87,7 @@ class OnekeySuiStandardWallet {
88
87
  return { accounts: this.accounts };
89
88
  }
90
89
  if (account) {
91
- this._account = new ReadonlyWalletAccount({
92
- address: account.address,
93
- publicKey: hexToBytes(account.publicKey),
94
- chains: activeChain ? [activeChain] : [],
95
- features: [
96
- Feature.STANDARD__CONNECT,
97
- Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK,
98
- Feature.SUI__SIGN_TRANSACTION_BLOCK,
99
- Feature.SUI__SIGN_MESSAGE,
100
- Feature.SUI__SIGN_PERSONAL_MESSAGE,
101
- ],
102
- });
103
- this._events.emit('change', { accounts: this.accounts });
90
+ yield this.handleAccountSwitch(account);
104
91
  return { accounts: this.accounts };
105
92
  }
106
93
  });
@@ -128,6 +115,33 @@ class OnekeySuiStandardWallet {
128
115
  this.$signPersonalMessage = (input) => __awaiter(this, void 0, void 0, function* () {
129
116
  return this.provider.signPersonalMessage(input);
130
117
  });
118
+ this.handleAccountSwitch = (payload) => __awaiter(this, void 0, void 0, function* () {
119
+ const { address, publicKey } = payload;
120
+ const activateChain = yield this.getActiveChain();
121
+ this._account = new ReadonlyWalletAccount({
122
+ address: address,
123
+ publicKey: hexToBytes(publicKey),
124
+ chains: activateChain ? [activateChain] : [],
125
+ features: [
126
+ Feature.STANDARD__CONNECT,
127
+ Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK,
128
+ Feature.SUI__SIGN_TRANSACTION_BLOCK,
129
+ Feature.SUI__SIGN_MESSAGE,
130
+ Feature.SUI__SIGN_PERSONAL_MESSAGE,
131
+ ],
132
+ });
133
+ this._events.emit('change', {
134
+ accounts: this.accounts,
135
+ chains: activateChain ? [activateChain] : [],
136
+ });
137
+ });
138
+ this.handleNetworkSwitch = (payload) => {
139
+ const { network } = payload;
140
+ this._events.emit('change', {
141
+ accounts: this.accounts,
142
+ chains: [network],
143
+ });
144
+ };
131
145
  this.provider = provider;
132
146
  this._events = mitt();
133
147
  this._account = null;
@@ -143,15 +157,16 @@ class OnekeySuiStandardWallet {
143
157
  }
144
158
  subscribeEventFromBackend() {
145
159
  this.provider.onNetworkChange((network) => {
146
- if (!network)
160
+ if (!network) {
147
161
  return;
148
- return this.handleNetworkSwitch({ network });
162
+ }
163
+ this.handleNetworkSwitch({ network: network });
149
164
  });
150
- }
151
- handleNetworkSwitch(payload) {
152
- const { network } = payload;
153
- this._events.emit('change', {
154
- chains: [network],
165
+ this.provider.onAccountChange((account) => {
166
+ if (!account) {
167
+ return;
168
+ }
169
+ void this.handleAccountSwitch(account);
155
170
  });
156
171
  }
157
172
  }
@@ -14,6 +14,7 @@ const utils_1 = require("@noble/hashes/utils");
14
14
  const extension_bridge_injected_1 = require("@onekeyfe/extension-bridge-injected");
15
15
  const ProviderSuiBase_1 = require("./ProviderSuiBase");
16
16
  const cross_inpage_provider_errors_1 = require("@onekeyfe/cross-inpage-provider-errors");
17
+ const transactions_1 = require("@mysten/sui.js/transactions");
17
18
  const types_1 = require("./types");
18
19
  const PROVIDER_EVENTS = {
19
20
  'connect': 'connect',
@@ -51,18 +52,15 @@ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
51
52
  return this.bridgeRequest(params);
52
53
  }
53
54
  _handleConnected(account, options = { emit: true }) {
54
- this._account = account;
55
- if (options.emit && this.isConnectionStatusChanged('connected')) {
56
- this.connectionStatus = 'connected';
57
- const address = account !== null && account !== void 0 ? account : null;
58
- this.emit('connect', address.address);
59
- this.emit('accountChanged', address.address);
55
+ var _a;
56
+ if (options.emit) {
57
+ this.emit('connect', (_a = account === null || account === void 0 ? void 0 : account.address) !== null && _a !== void 0 ? _a : null);
58
+ this.emit('accountChanged', account ? { address: account === null || account === void 0 ? void 0 : account.address, publicKey: account === null || account === void 0 ? void 0 : account.publicKey } : null);
60
59
  }
61
60
  }
62
61
  _handleDisconnected(options = { emit: true }) {
63
62
  this._account = null;
64
- if (options.emit && this.isConnectionStatusChanged('disconnected')) {
65
- this.connectionStatus = 'disconnected';
63
+ if (options.emit) {
66
64
  this.emit('disconnect');
67
65
  this.emit('accountChanged', null);
68
66
  }
@@ -73,15 +71,14 @@ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
73
71
  }
74
72
  // trigger by bridge account change event
75
73
  _handleAccountChange(payload) {
76
- const account = payload;
77
- if (this.isAccountsChanged(account)) {
78
- this.emit('accountChanged', (account === null || account === void 0 ? void 0 : account.address) || null);
79
- }
80
- if (!account) {
74
+ if (!payload) {
81
75
  this._handleDisconnected();
82
76
  return;
83
77
  }
84
- this._handleConnected(account, { emit: false });
78
+ if (this.isAccountsChanged(payload)) {
79
+ this._handleConnected(payload);
80
+ }
81
+ this._account = payload;
85
82
  }
86
83
  isNetworkChanged(network) {
87
84
  return this._network === undefined || network !== this._network;
@@ -95,7 +92,7 @@ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
95
92
  }
96
93
  hasPermissions(permissions = types_1.ALL_PERMISSION_TYPES) {
97
94
  return __awaiter(this, void 0, void 0, function* () {
98
- return this._callBridge({
95
+ return yield this._callBridge({
99
96
  method: 'hasPermissions',
100
97
  params: permissions,
101
98
  });
@@ -103,7 +100,7 @@ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
103
100
  }
104
101
  requestPermissions(permissions = types_1.ALL_PERMISSION_TYPES) {
105
102
  return __awaiter(this, void 0, void 0, function* () {
106
- return this._callBridge({
103
+ return yield this._callBridge({
107
104
  method: 'requestPermissions',
108
105
  params: permissions,
109
106
  });
@@ -128,7 +125,6 @@ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
128
125
  this._handleDisconnected();
129
126
  throw cross_inpage_provider_errors_1.web3Errors.provider.unauthorized();
130
127
  }
131
- this._handleConnected(accounts[0]);
132
128
  return accounts;
133
129
  });
134
130
  }
@@ -144,7 +140,10 @@ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
144
140
  return __awaiter(this, void 0, void 0, function* () {
145
141
  return this._callBridge({
146
142
  method: 'signAndExecuteTransactionBlock',
147
- params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
143
+ params: Object.assign(Object.assign({}, input), {
144
+ // https://github.com/MystenLabs/sui/blob/ace69fa8404eb704b504082d324ebc355a3d2948/sdk/typescript/src/transactions/object.ts#L6-L17
145
+ // With a few more objects, other wallets have steps for tojson.
146
+ transactionBlock: transactions_1.TransactionBlock.from(input.transactionBlock.serialize()), walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
148
147
  });
149
148
  });
150
149
  }
@@ -152,7 +151,7 @@ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
152
151
  return __awaiter(this, void 0, void 0, function* () {
153
152
  return this._callBridge({
154
153
  method: 'signTransactionBlock',
155
- params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
154
+ params: Object.assign(Object.assign({}, input), { transactionBlock: transactions_1.TransactionBlock.from(input.transactionBlock.serialize()), walletSerialize: JSON.stringify(input.account), blockSerialize: input.transactionBlock.serialize() }),
156
155
  });
157
156
  });
158
157
  }
@@ -83,7 +83,6 @@ class OnekeySuiStandardWallet {
83
83
  return () => this._events.off(event, listener);
84
84
  };
85
85
  this.$connected = () => __awaiter(this, void 0, void 0, function* () {
86
- const activeChain = yield this.getActiveChain();
87
86
  if (!(yield this.$hasPermissions(['viewAccount']))) {
88
87
  return;
89
88
  }
@@ -94,19 +93,7 @@ class OnekeySuiStandardWallet {
94
93
  return { accounts: this.accounts };
95
94
  }
96
95
  if (account) {
97
- this._account = new wallet_standard_1.ReadonlyWalletAccount({
98
- address: account.address,
99
- publicKey: (0, utils_1.hexToBytes)(account.publicKey),
100
- chains: activeChain ? [activeChain] : [],
101
- features: [
102
- Feature.STANDARD__CONNECT,
103
- Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK,
104
- Feature.SUI__SIGN_TRANSACTION_BLOCK,
105
- Feature.SUI__SIGN_MESSAGE,
106
- Feature.SUI__SIGN_PERSONAL_MESSAGE,
107
- ],
108
- });
109
- this._events.emit('change', { accounts: this.accounts });
96
+ yield this.handleAccountSwitch(account);
110
97
  return { accounts: this.accounts };
111
98
  }
112
99
  });
@@ -134,6 +121,33 @@ class OnekeySuiStandardWallet {
134
121
  this.$signPersonalMessage = (input) => __awaiter(this, void 0, void 0, function* () {
135
122
  return this.provider.signPersonalMessage(input);
136
123
  });
124
+ this.handleAccountSwitch = (payload) => __awaiter(this, void 0, void 0, function* () {
125
+ const { address, publicKey } = payload;
126
+ const activateChain = yield this.getActiveChain();
127
+ this._account = new wallet_standard_1.ReadonlyWalletAccount({
128
+ address: address,
129
+ publicKey: (0, utils_1.hexToBytes)(publicKey),
130
+ chains: activateChain ? [activateChain] : [],
131
+ features: [
132
+ Feature.STANDARD__CONNECT,
133
+ Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK,
134
+ Feature.SUI__SIGN_TRANSACTION_BLOCK,
135
+ Feature.SUI__SIGN_MESSAGE,
136
+ Feature.SUI__SIGN_PERSONAL_MESSAGE,
137
+ ],
138
+ });
139
+ this._events.emit('change', {
140
+ accounts: this.accounts,
141
+ chains: activateChain ? [activateChain] : [],
142
+ });
143
+ });
144
+ this.handleNetworkSwitch = (payload) => {
145
+ const { network } = payload;
146
+ this._events.emit('change', {
147
+ accounts: this.accounts,
148
+ chains: [network],
149
+ });
150
+ };
137
151
  this.provider = provider;
138
152
  this._events = (0, mitt_1.default)();
139
153
  this._account = null;
@@ -149,15 +163,16 @@ class OnekeySuiStandardWallet {
149
163
  }
150
164
  subscribeEventFromBackend() {
151
165
  this.provider.onNetworkChange((network) => {
152
- if (!network)
166
+ if (!network) {
153
167
  return;
154
- return this.handleNetworkSwitch({ network });
168
+ }
169
+ this.handleNetworkSwitch({ network: network });
155
170
  });
156
- }
157
- handleNetworkSwitch(payload) {
158
- const { network } = payload;
159
- this._events.emit('change', {
160
- chains: [network],
171
+ this.provider.onAccountChange((account) => {
172
+ if (!account) {
173
+ return;
174
+ }
175
+ void this.handleAccountSwitch(account);
161
176
  });
162
177
  }
163
178
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/onekey-sui-provider",
3
- "version": "2.1.8",
3
+ "version": "2.1.10",
4
4
  "keywords": [
5
5
  "cross-inpage-provider"
6
6
  ],
@@ -29,12 +29,12 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@mysten/wallet-standard": "^0.7.2",
32
- "@onekeyfe/cross-inpage-provider-core": "2.1.8",
33
- "@onekeyfe/cross-inpage-provider-errors": "2.1.8",
34
- "@onekeyfe/cross-inpage-provider-types": "2.1.8",
35
- "@onekeyfe/extension-bridge-injected": "2.1.8",
32
+ "@onekeyfe/cross-inpage-provider-core": "2.1.10",
33
+ "@onekeyfe/cross-inpage-provider-errors": "2.1.10",
34
+ "@onekeyfe/cross-inpage-provider-types": "2.1.10",
35
+ "@onekeyfe/extension-bridge-injected": "2.1.10",
36
36
  "eth-rpc-errors": "^4.0.3",
37
37
  "mitt": "^3.0.0"
38
38
  },
39
- "gitHead": "40fd9b618f62b24870523282fa47da5c2ca33d66"
39
+ "gitHead": "6c2afa3ac5ea2f9e0464c2a5480db3e4c38ff94e"
40
40
  }