@onekeyfe/onekey-sui-provider 1.1.7

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.
@@ -0,0 +1,168 @@
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
+ import { getOrCreateExtInjectedJsBridge } from '@onekeyfe/extension-bridge-injected';
11
+ import { ProviderSuiBase } from './ProviderSuiBase';
12
+ import { web3Errors } from '@onekeyfe/cross-inpage-provider-errors';
13
+ import { ALL_PERMISSION_TYPES } from './types';
14
+ const PROVIDER_EVENTS = {
15
+ 'connect': 'connect',
16
+ 'disconnect': 'disconnect',
17
+ 'accountChanged': 'accountChanged',
18
+ 'networkChange': 'networkChange',
19
+ 'message_low_level': 'message_low_level',
20
+ };
21
+ function isWalletEventMethodMatch({ method, name }) {
22
+ return method === `wallet_events_${name}`;
23
+ }
24
+ class ProviderSui extends ProviderSuiBase {
25
+ constructor(props) {
26
+ super(Object.assign(Object.assign({}, props), { bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }) }));
27
+ this._account = null;
28
+ this._registerEvents();
29
+ }
30
+ _registerEvents() {
31
+ window.addEventListener('onekey_bridge_disconnect', () => {
32
+ this._handleDisconnected();
33
+ });
34
+ this.on(PROVIDER_EVENTS.message_low_level, (payload) => {
35
+ const { method, params } = payload;
36
+ if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.accountChanged })) {
37
+ this._handleAccountChange(params);
38
+ }
39
+ if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.networkChange })) {
40
+ this._handleNetworkChange(params);
41
+ }
42
+ });
43
+ }
44
+ _callBridge(params) {
45
+ return this.bridgeRequest(params);
46
+ }
47
+ _handleConnected(account, options = { emit: true }) {
48
+ this._account = account;
49
+ if (options.emit && this.isConnectionStatusChanged('connected')) {
50
+ this.connectionStatus = 'connected';
51
+ const address = account !== null && account !== void 0 ? account : null;
52
+ this.emit('connect', address);
53
+ this.emit('accountChanged', address);
54
+ }
55
+ }
56
+ _handleDisconnected(options = { emit: true }) {
57
+ this._account = null;
58
+ if (options.emit && this.isConnectionStatusChanged('disconnected')) {
59
+ this.connectionStatus = 'disconnected';
60
+ this.emit('disconnect');
61
+ this.emit('accountChanged', null);
62
+ }
63
+ }
64
+ isAccountsChanged(account) {
65
+ return account !== this._account;
66
+ }
67
+ // trigger by bridge account change event
68
+ _handleAccountChange(payload) {
69
+ const account = payload;
70
+ if (this.isAccountsChanged(account)) {
71
+ this.emit('accountChanged', account || null);
72
+ }
73
+ if (!account) {
74
+ this._handleDisconnected();
75
+ return;
76
+ }
77
+ this._handleConnected(account, { emit: false });
78
+ }
79
+ isNetworkChanged(network) {
80
+ return this._network === undefined || network !== this._network;
81
+ }
82
+ _handleNetworkChange(payload) {
83
+ const network = payload;
84
+ if (this.isNetworkChanged(network)) {
85
+ this.emit('networkChange', network || null);
86
+ }
87
+ this._network = network;
88
+ }
89
+ hasPermissions(permissions = ALL_PERMISSION_TYPES) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ return this._callBridge({
92
+ method: 'hasPermissions',
93
+ params: permissions,
94
+ });
95
+ });
96
+ }
97
+ requestPermissions(permissions = ALL_PERMISSION_TYPES) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ return this._callBridge({
100
+ method: 'requestPermissions',
101
+ params: permissions,
102
+ });
103
+ });
104
+ }
105
+ disconnect() {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ yield this._callBridge({
108
+ method: 'disconnect',
109
+ params: void 0,
110
+ });
111
+ this._handleDisconnected();
112
+ });
113
+ }
114
+ getAccounts() {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ const accounts = yield this._callBridge({
117
+ method: 'getAccounts',
118
+ params: undefined,
119
+ });
120
+ if (accounts.length === 0) {
121
+ this._handleDisconnected();
122
+ throw web3Errors.provider.unauthorized();
123
+ }
124
+ this._handleConnected(accounts[0]);
125
+ return accounts;
126
+ });
127
+ }
128
+ signAndExecuteTransaction(transaction) {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ return this._callBridge({
131
+ method: 'signAndExecuteTransaction',
132
+ params: transaction,
133
+ });
134
+ });
135
+ }
136
+ executeMoveCall(transaction) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ return this._callBridge({
139
+ method: 'executeMoveCall',
140
+ params: transaction,
141
+ });
142
+ });
143
+ }
144
+ executeSerializedMoveCall(transaction) {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ return this._callBridge({
147
+ method: 'executeSerializedMoveCall',
148
+ params: transaction,
149
+ });
150
+ });
151
+ }
152
+ isConnected() {
153
+ return this._account !== null;
154
+ }
155
+ onNetworkChange(listener) {
156
+ return super.on(PROVIDER_EVENTS.networkChange, listener);
157
+ }
158
+ onAccountChange(listener) {
159
+ return super.on(PROVIDER_EVENTS.accountChanged, listener);
160
+ }
161
+ on(event, listener) {
162
+ return super.on(event, listener);
163
+ }
164
+ emit(event, ...args) {
165
+ return super.emit(event, ...args);
166
+ }
167
+ }
168
+ export { ProviderSui };
@@ -0,0 +1,8 @@
1
+ import { IInjectedProviderNames } from '@onekeyfe/cross-inpage-provider-types';
2
+ import { ProviderBase, IInpageProviderConfig } from '@onekeyfe/cross-inpage-provider-core';
3
+ declare class ProviderSuiBase extends ProviderBase {
4
+ constructor(props: IInpageProviderConfig);
5
+ protected providerName: IInjectedProviderNames;
6
+ request(data: unknown): Promise<unknown>;
7
+ }
8
+ export { ProviderSuiBase };
@@ -0,0 +1,12 @@
1
+ import { IInjectedProviderNames } from '@onekeyfe/cross-inpage-provider-types';
2
+ import { ProviderBase } from '@onekeyfe/cross-inpage-provider-core';
3
+ class ProviderSuiBase extends ProviderBase {
4
+ constructor(props) {
5
+ super(props);
6
+ this.providerName = IInjectedProviderNames.sui;
7
+ }
8
+ request(data) {
9
+ return this.bridgeRequest(data);
10
+ }
11
+ }
12
+ export { ProviderSuiBase };
@@ -0,0 +1,171 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ProviderSui = void 0;
13
+ const extension_bridge_injected_1 = require("@onekeyfe/extension-bridge-injected");
14
+ const ProviderSuiBase_1 = require("./ProviderSuiBase");
15
+ const cross_inpage_provider_errors_1 = require("@onekeyfe/cross-inpage-provider-errors");
16
+ const types_1 = require("./types");
17
+ const PROVIDER_EVENTS = {
18
+ 'connect': 'connect',
19
+ 'disconnect': 'disconnect',
20
+ 'accountChanged': 'accountChanged',
21
+ 'networkChange': 'networkChange',
22
+ 'message_low_level': 'message_low_level',
23
+ };
24
+ function isWalletEventMethodMatch({ method, name }) {
25
+ return method === `wallet_events_${name}`;
26
+ }
27
+ class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
28
+ constructor(props) {
29
+ super(Object.assign(Object.assign({}, props), { bridge: props.bridge || (0, extension_bridge_injected_1.getOrCreateExtInjectedJsBridge)({ timeout: props.timeout }) }));
30
+ this._account = null;
31
+ this._registerEvents();
32
+ }
33
+ _registerEvents() {
34
+ window.addEventListener('onekey_bridge_disconnect', () => {
35
+ this._handleDisconnected();
36
+ });
37
+ this.on(PROVIDER_EVENTS.message_low_level, (payload) => {
38
+ const { method, params } = payload;
39
+ if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.accountChanged })) {
40
+ this._handleAccountChange(params);
41
+ }
42
+ if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.networkChange })) {
43
+ this._handleNetworkChange(params);
44
+ }
45
+ });
46
+ }
47
+ _callBridge(params) {
48
+ return this.bridgeRequest(params);
49
+ }
50
+ _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);
56
+ this.emit('accountChanged', address);
57
+ }
58
+ }
59
+ _handleDisconnected(options = { emit: true }) {
60
+ this._account = null;
61
+ if (options.emit && this.isConnectionStatusChanged('disconnected')) {
62
+ this.connectionStatus = 'disconnected';
63
+ this.emit('disconnect');
64
+ this.emit('accountChanged', null);
65
+ }
66
+ }
67
+ isAccountsChanged(account) {
68
+ return account !== this._account;
69
+ }
70
+ // trigger by bridge account change event
71
+ _handleAccountChange(payload) {
72
+ const account = payload;
73
+ if (this.isAccountsChanged(account)) {
74
+ this.emit('accountChanged', account || null);
75
+ }
76
+ if (!account) {
77
+ this._handleDisconnected();
78
+ return;
79
+ }
80
+ this._handleConnected(account, { emit: false });
81
+ }
82
+ isNetworkChanged(network) {
83
+ return this._network === undefined || network !== this._network;
84
+ }
85
+ _handleNetworkChange(payload) {
86
+ const network = payload;
87
+ if (this.isNetworkChanged(network)) {
88
+ this.emit('networkChange', network || null);
89
+ }
90
+ this._network = network;
91
+ }
92
+ hasPermissions(permissions = types_1.ALL_PERMISSION_TYPES) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ return this._callBridge({
95
+ method: 'hasPermissions',
96
+ params: permissions,
97
+ });
98
+ });
99
+ }
100
+ requestPermissions(permissions = types_1.ALL_PERMISSION_TYPES) {
101
+ return __awaiter(this, void 0, void 0, function* () {
102
+ return this._callBridge({
103
+ method: 'requestPermissions',
104
+ params: permissions,
105
+ });
106
+ });
107
+ }
108
+ disconnect() {
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ yield this._callBridge({
111
+ method: 'disconnect',
112
+ params: void 0,
113
+ });
114
+ this._handleDisconnected();
115
+ });
116
+ }
117
+ getAccounts() {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ const accounts = yield this._callBridge({
120
+ method: 'getAccounts',
121
+ params: undefined,
122
+ });
123
+ if (accounts.length === 0) {
124
+ this._handleDisconnected();
125
+ throw cross_inpage_provider_errors_1.web3Errors.provider.unauthorized();
126
+ }
127
+ this._handleConnected(accounts[0]);
128
+ return accounts;
129
+ });
130
+ }
131
+ signAndExecuteTransaction(transaction) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ return this._callBridge({
134
+ method: 'signAndExecuteTransaction',
135
+ params: transaction,
136
+ });
137
+ });
138
+ }
139
+ executeMoveCall(transaction) {
140
+ return __awaiter(this, void 0, void 0, function* () {
141
+ return this._callBridge({
142
+ method: 'executeMoveCall',
143
+ params: transaction,
144
+ });
145
+ });
146
+ }
147
+ executeSerializedMoveCall(transaction) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ return this._callBridge({
150
+ method: 'executeSerializedMoveCall',
151
+ params: transaction,
152
+ });
153
+ });
154
+ }
155
+ isConnected() {
156
+ return this._account !== null;
157
+ }
158
+ onNetworkChange(listener) {
159
+ return super.on(PROVIDER_EVENTS.networkChange, listener);
160
+ }
161
+ onAccountChange(listener) {
162
+ return super.on(PROVIDER_EVENTS.accountChanged, listener);
163
+ }
164
+ on(event, listener) {
165
+ return super.on(event, listener);
166
+ }
167
+ emit(event, ...args) {
168
+ return super.emit(event, ...args);
169
+ }
170
+ }
171
+ exports.ProviderSui = ProviderSui;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProviderSuiBase = void 0;
4
+ const cross_inpage_provider_types_1 = require("@onekeyfe/cross-inpage-provider-types");
5
+ const cross_inpage_provider_core_1 = require("@onekeyfe/cross-inpage-provider-core");
6
+ class ProviderSuiBase extends cross_inpage_provider_core_1.ProviderBase {
7
+ constructor(props) {
8
+ super(props);
9
+ this.providerName = cross_inpage_provider_types_1.IInjectedProviderNames.sui;
10
+ }
11
+ request(data) {
12
+ return this.bridgeRequest(data);
13
+ }
14
+ }
15
+ exports.ProviderSuiBase = ProviderSuiBase;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./OnekeySuiProvider"), exports);
14
+ __exportStar(require("./ProviderSuiBase"), exports);
15
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ALL_PERMISSION_TYPES = void 0;
4
+ exports.ALL_PERMISSION_TYPES = [
5
+ 'viewAccount',
6
+ 'suggestTransactions',
7
+ ];
@@ -0,0 +1,3 @@
1
+ export * from './OnekeySuiProvider';
2
+ export * from './ProviderSuiBase';
3
+ export * from './types';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './OnekeySuiProvider';
2
+ export * from './ProviderSuiBase';
3
+ export * from './types';
@@ -0,0 +1,4 @@
1
+ export declare type WireStringified<T> = T extends Array<infer P> ? Array<WireStringified<P>> : T extends object ? {
2
+ [K in keyof T]: WireStringified<T[K]>;
3
+ } : T;
4
+ export declare type ResolvePromise<T> = T extends Promise<infer P> ? P : T;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export declare const ALL_PERMISSION_TYPES: readonly ["viewAccount", "suggestTransactions"];
2
+ declare type AllPermissionsType = typeof ALL_PERMISSION_TYPES;
3
+ export declare type PermissionType = AllPermissionsType[number];
4
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1,4 @@
1
+ export const ALL_PERMISSION_TYPES = [
2
+ 'viewAccount',
3
+ 'suggestTransactions',
4
+ ];
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@onekeyfe/onekey-sui-provider",
3
+ "version": "1.1.7",
4
+ "keywords": [
5
+ "cross-inpage-provider"
6
+ ],
7
+ "author": "dev-fe@onekey.so",
8
+ "repository": "https://github.com/OneKeyHQ/cross-inpage-provider",
9
+ "license": "Apache-2.0",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "type": "module",
14
+ "files": [
15
+ "dist/*"
16
+ ],
17
+ "exports": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/cjs/index.js"
21
+ },
22
+ "types": "./dist/index.d.ts",
23
+ "module": "./dist/index.js",
24
+ "main": "./dist/cjs/index.js",
25
+ "scripts": {
26
+ "prebuild": "rm -rf dist",
27
+ "build": "tsc && tsc --project tsconfig.cjs.json",
28
+ "start": "tsc --watch"
29
+ },
30
+ "dependencies": {
31
+ "@mysten/sui.js": "^0.17.1",
32
+ "@onekeyfe/cross-inpage-provider-core": "1.1.7",
33
+ "@onekeyfe/cross-inpage-provider-errors": "1.1.7",
34
+ "@onekeyfe/cross-inpage-provider-types": "1.1.7",
35
+ "@onekeyfe/extension-bridge-injected": "1.1.7",
36
+ "eth-rpc-errors": "^4.0.3"
37
+ },
38
+ "gitHead": "caaa1fd03f388765f3d8b1bf8f013c2dfbba7a1b"
39
+ }