@1tokenfe/onetoken-sui-provider 2.2.46
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.
- package/LICENSE.md +51 -0
- package/README.md +1 -0
- package/dist/OnetokenSuiProvider.d.ts +107 -0
- package/dist/OnetokenSuiProvider.js +217 -0
- package/dist/OnetokenSuiStandardWallet.d.ts +3 -0
- package/dist/OnetokenSuiStandardWallet.js +198 -0
- package/dist/ProviderSuiBase.d.ts +9 -0
- package/dist/ProviderSuiBase.js +12 -0
- package/dist/cjs/OnetokenSuiProvider.js +220 -0
- package/dist/cjs/OnetokenSuiStandardWallet.js +204 -0
- package/dist/cjs/ProviderSuiBase.js +15 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/type-utils.js +2 -0
- package/dist/cjs/types.js +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/type-utils.d.ts +4 -0
- package/dist/type-utils.js +1 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.js +1 -0
- package/package.json +39 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# ONETOKEN STANDARD SOURCE LICENSE (O-SSL)
|
|
2
|
+
|
|
3
|
+
This license governs use of the accompanying software. If you use the software,
|
|
4
|
+
you accept this license. If you do not accept the license, do not use the
|
|
5
|
+
software.
|
|
6
|
+
|
|
7
|
+
## 1. Definitions
|
|
8
|
+
|
|
9
|
+
The terms "reproduce," "reproduction" and "distribution" have the same meaning
|
|
10
|
+
here as under Hong Kong copyright law.
|
|
11
|
+
|
|
12
|
+
"You" means the licensee of the software.
|
|
13
|
+
|
|
14
|
+
"Your company" means the company you worked for when you downloaded the
|
|
15
|
+
software.
|
|
16
|
+
|
|
17
|
+
"Reference use" means use of the software within your company as a reference,
|
|
18
|
+
in read only form, for the sole purposes of debugging your products,
|
|
19
|
+
maintaining your products, or enhancing the interoperability of your products
|
|
20
|
+
with the software, and specifically excludes the right to distribute the
|
|
21
|
+
software outside of your company.
|
|
22
|
+
|
|
23
|
+
"Licensed patents" means any Licensor patent claims which read directly on the
|
|
24
|
+
software as distributed by the Licensor under this license.
|
|
25
|
+
|
|
26
|
+
## 2. Grant of Rights
|
|
27
|
+
|
|
28
|
+
(A) Copyright Grant - Subject to the terms of this license, the Licensor grants
|
|
29
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free copyright
|
|
30
|
+
license to reproduce the software for reference use.
|
|
31
|
+
|
|
32
|
+
(B) Patent Grant - Subject to the terms of this license, the Licensor grants
|
|
33
|
+
you a non-transferable, non-exclusive, worldwide, royalty-free patent license
|
|
34
|
+
under licensed patents for reference use.
|
|
35
|
+
|
|
36
|
+
## 3. Limitations
|
|
37
|
+
|
|
38
|
+
(A) No Trademark License - This license does not grant you any rights to use
|
|
39
|
+
the Licensor's name, logo, or trademarks.
|
|
40
|
+
|
|
41
|
+
(B) If you begin patent litigation against the Licensor over patents that you
|
|
42
|
+
think may apply to the software (including a cross-claim or counterclaim in
|
|
43
|
+
a lawsuit), your license to the software ends automatically.
|
|
44
|
+
|
|
45
|
+
(C) The software is licensed "as-is." You bear the risk of using it. The
|
|
46
|
+
Licensor gives no express warranties, guarantees or conditions. You may have
|
|
47
|
+
additional consumer rights under your local laws which this license cannot
|
|
48
|
+
change. To the extent permitted under your local laws, the Licensor excludes
|
|
49
|
+
the implied warranties of merchantability, fitness for a particular purpose and
|
|
50
|
+
non-infringement.This license agreement is governed by the laws of Hong Kong,
|
|
51
|
+
and any disputes related to this license agreement shall be resolved in accordance with Hong Kong law.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# cross-inpage-provider
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { IInpageProviderConfig } from '@1tokenfe/cross-inpage-provider-core';
|
|
2
|
+
import type { IJsonRpcRequest } from '@1tokenfe/cross-inpage-provider-types';
|
|
3
|
+
import { ProviderSuiBase } from './ProviderSuiBase';
|
|
4
|
+
import type { AccountInfo, PermissionType } from './types';
|
|
5
|
+
import type { IdentifierString, SignedTransaction, SuiSignAndExecuteTransactionBlockInput, SuiSignAndExecuteTransactionBlockOutput, SuiSignAndExecuteTransactionInput, SuiSignAndExecuteTransactionOutput, SuiSignMessageInput, SuiSignMessageOutput, SuiSignPersonalMessageInput, SuiSignPersonalMessageOutput, SuiSignTransactionBlockInput, SuiSignTransactionBlockOutput, SuiSignTransactionInput } from '@mysten/wallet-standard';
|
|
6
|
+
declare const PROVIDER_EVENTS: {
|
|
7
|
+
readonly connect: "connect";
|
|
8
|
+
readonly disconnect: "disconnect";
|
|
9
|
+
readonly accountChanged: "accountChanged";
|
|
10
|
+
readonly networkChange: "networkChange";
|
|
11
|
+
readonly message_low_level: "message_low_level";
|
|
12
|
+
};
|
|
13
|
+
type SuiProviderEventsMap = {
|
|
14
|
+
[PROVIDER_EVENTS.connect]: (account: string) => void;
|
|
15
|
+
[PROVIDER_EVENTS.disconnect]: () => void;
|
|
16
|
+
[PROVIDER_EVENTS.accountChanged]: (account: {
|
|
17
|
+
address: string;
|
|
18
|
+
publicKey: string;
|
|
19
|
+
} | null) => void;
|
|
20
|
+
[PROVIDER_EVENTS.networkChange]: (name: string | null) => void;
|
|
21
|
+
[PROVIDER_EVENTS.message_low_level]: (payload: IJsonRpcRequest) => void;
|
|
22
|
+
};
|
|
23
|
+
type SignAndExecuteTransactionBlockInput = Omit<SuiSignAndExecuteTransactionBlockInput, 'transactionBlock'> & {
|
|
24
|
+
blockSerialize: string;
|
|
25
|
+
walletSerialize: string;
|
|
26
|
+
};
|
|
27
|
+
type SignTransactionBlockInput = Omit<SuiSignTransactionBlockInput, 'transactionBlock'> & {
|
|
28
|
+
blockSerialize: string;
|
|
29
|
+
walletSerialize: string;
|
|
30
|
+
};
|
|
31
|
+
type SignMessageInput = SuiSignMessageInput & {
|
|
32
|
+
messageSerialize: string;
|
|
33
|
+
walletSerialize: string;
|
|
34
|
+
};
|
|
35
|
+
type SignPersonalMessageInput = SuiSignPersonalMessageInput & {
|
|
36
|
+
messageSerialize: string;
|
|
37
|
+
walletSerialize: string;
|
|
38
|
+
};
|
|
39
|
+
type OneTokenSuiSignTransactionInput = Omit<SuiSignTransactionInput, 'transaction' | 'signal'> & {
|
|
40
|
+
transaction: string;
|
|
41
|
+
};
|
|
42
|
+
type OneTokenSuiSignAndExecuteTransactionInput = Omit<SuiSignAndExecuteTransactionInput, 'transaction' | 'signal'> & {
|
|
43
|
+
transaction: string;
|
|
44
|
+
};
|
|
45
|
+
export type SuiRequest = {
|
|
46
|
+
'hasPermissions': (permissions: readonly PermissionType[]) => Promise<boolean>;
|
|
47
|
+
'requestPermissions': (permissions: readonly PermissionType[]) => Promise<boolean>;
|
|
48
|
+
'disconnect': () => Promise<void>;
|
|
49
|
+
'getActiveChain': () => Promise<IdentifierString | undefined>;
|
|
50
|
+
'getAccounts': () => Promise<AccountInfo[]>;
|
|
51
|
+
'signAndExecuteTransactionBlock': (input: SignAndExecuteTransactionBlockInput) => Promise<SuiSignAndExecuteTransactionBlockOutput>;
|
|
52
|
+
'signTransactionBlock': (input: SignTransactionBlockInput) => Promise<SuiSignTransactionBlockOutput>;
|
|
53
|
+
'signMessage': (input: SignMessageInput) => Promise<SuiSignMessageOutput>;
|
|
54
|
+
'signPersonalMessage': (input: SignPersonalMessageInput) => Promise<SuiSignPersonalMessageOutput>;
|
|
55
|
+
'signTransaction': (input: OneTokenSuiSignTransactionInput) => Promise<SignedTransaction>;
|
|
56
|
+
'signAndExecuteTransaction': (input: OneTokenSuiSignAndExecuteTransactionInput) => Promise<SuiSignAndExecuteTransactionOutput>;
|
|
57
|
+
};
|
|
58
|
+
export type PROVIDER_EVENTS_STRINGS = keyof typeof PROVIDER_EVENTS;
|
|
59
|
+
export interface IProviderSui {
|
|
60
|
+
hasPermissions(permissions: readonly PermissionType[]): Promise<boolean>;
|
|
61
|
+
requestPermissions(permissions: readonly PermissionType[]): Promise<boolean>;
|
|
62
|
+
/**
|
|
63
|
+
* Disconnect wallet
|
|
64
|
+
*/
|
|
65
|
+
disconnect(): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Connect wallet, and get wallet info
|
|
68
|
+
* @emits `connect` on success
|
|
69
|
+
*/
|
|
70
|
+
getAccounts(): Promise<AccountInfo[]>;
|
|
71
|
+
}
|
|
72
|
+
export type OneTokenSuiProviderProps = IInpageProviderConfig & {
|
|
73
|
+
timeout?: number;
|
|
74
|
+
};
|
|
75
|
+
declare class ProviderSui extends ProviderSuiBase implements IProviderSui {
|
|
76
|
+
protected _account: AccountInfo | null;
|
|
77
|
+
constructor(props: OneTokenSuiProviderProps);
|
|
78
|
+
private _registerEvents;
|
|
79
|
+
private _callBridge;
|
|
80
|
+
private _handleConnected;
|
|
81
|
+
private _handleDisconnected;
|
|
82
|
+
isAccountsChanged(account: AccountInfo | undefined): boolean;
|
|
83
|
+
private _handleAccountChange;
|
|
84
|
+
private _network;
|
|
85
|
+
isNetworkChanged(network: string): boolean;
|
|
86
|
+
private _handleNetworkChange;
|
|
87
|
+
hasPermissions(permissions?: readonly PermissionType[]): Promise<boolean>;
|
|
88
|
+
requestPermissions(permissions?: readonly PermissionType[]): Promise<boolean>;
|
|
89
|
+
disconnect(): Promise<void>;
|
|
90
|
+
getAccounts(): Promise<{
|
|
91
|
+
address: string;
|
|
92
|
+
publicKey: string;
|
|
93
|
+
}[]>;
|
|
94
|
+
getActiveChain(): Promise<`${string}:${string}` | undefined>;
|
|
95
|
+
signAndExecuteTransactionBlock(input: SuiSignAndExecuteTransactionBlockInput): Promise<SuiSignAndExecuteTransactionBlockOutput>;
|
|
96
|
+
signTransactionBlock(input: SuiSignTransactionBlockInput): Promise<SuiSignTransactionBlockOutput>;
|
|
97
|
+
signMessage(input: SuiSignMessageInput): Promise<SuiSignMessageOutput>;
|
|
98
|
+
signPersonalMessage(input: SuiSignPersonalMessageInput): Promise<SuiSignPersonalMessageOutput>;
|
|
99
|
+
signTransaction(input: SuiSignTransactionInput): Promise<SignedTransaction>;
|
|
100
|
+
signAndExecuteTransaction(input: SuiSignAndExecuteTransactionInput): Promise<SuiSignAndExecuteTransactionOutput>;
|
|
101
|
+
isConnected(): boolean;
|
|
102
|
+
onNetworkChange(listener: SuiProviderEventsMap['networkChange']): this;
|
|
103
|
+
onAccountChange(listener: SuiProviderEventsMap['accountChanged']): this;
|
|
104
|
+
on<E extends keyof SuiProviderEventsMap>(event: E, listener: SuiProviderEventsMap[E]): this;
|
|
105
|
+
emit<E extends keyof SuiProviderEventsMap>(event: E, ...args: Parameters<SuiProviderEventsMap[E]>): boolean;
|
|
106
|
+
}
|
|
107
|
+
export { ProviderSui };
|
|
@@ -0,0 +1,217 @@
|
|
|
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 { bytesToHex } from '@1tokenfe/cross-inpage-provider-core';
|
|
11
|
+
import { web3Errors } from '@1tokenfe/cross-inpage-provider-errors';
|
|
12
|
+
import { getOrCreateExtInjectedJsBridge } from '@1tokenfe/extension-bridge-injected';
|
|
13
|
+
import { ProviderSuiBase } from './ProviderSuiBase';
|
|
14
|
+
import { ALL_PERMISSION_TYPES } from './types';
|
|
15
|
+
const PROVIDER_EVENTS = {
|
|
16
|
+
'connect': 'connect',
|
|
17
|
+
'disconnect': 'disconnect',
|
|
18
|
+
'accountChanged': 'accountChanged',
|
|
19
|
+
'networkChange': 'networkChange',
|
|
20
|
+
'message_low_level': 'message_low_level',
|
|
21
|
+
};
|
|
22
|
+
function isWalletEventMethodMatch({ method, name }) {
|
|
23
|
+
return method === `wallet_events_${name}`;
|
|
24
|
+
}
|
|
25
|
+
class ProviderSui extends ProviderSuiBase {
|
|
26
|
+
constructor(props) {
|
|
27
|
+
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }) }));
|
|
28
|
+
this._account = null;
|
|
29
|
+
this._registerEvents();
|
|
30
|
+
}
|
|
31
|
+
_registerEvents() {
|
|
32
|
+
window.addEventListener('onetoken_bridge_disconnect', () => {
|
|
33
|
+
this._handleDisconnected();
|
|
34
|
+
});
|
|
35
|
+
this.on(PROVIDER_EVENTS.message_low_level, (payload) => {
|
|
36
|
+
if (!payload)
|
|
37
|
+
return;
|
|
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
|
+
var _a;
|
|
52
|
+
if (options.emit) {
|
|
53
|
+
this.emit('connect', (_a = account === null || account === void 0 ? void 0 : account.address) !== null && _a !== void 0 ? _a : null);
|
|
54
|
+
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);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
_handleDisconnected(options = { emit: true }) {
|
|
58
|
+
this._account = null;
|
|
59
|
+
if (options.emit) {
|
|
60
|
+
this.emit('disconnect');
|
|
61
|
+
this.emit('accountChanged', null);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
isAccountsChanged(account) {
|
|
65
|
+
var _a;
|
|
66
|
+
return (account === null || account === void 0 ? void 0 : account.address) !== ((_a = this._account) === null || _a === void 0 ? void 0 : _a.address);
|
|
67
|
+
}
|
|
68
|
+
// trigger by bridge account change event
|
|
69
|
+
_handleAccountChange(payload) {
|
|
70
|
+
if (!payload) {
|
|
71
|
+
this._handleDisconnected();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (this.isAccountsChanged(payload)) {
|
|
75
|
+
this._handleConnected(payload);
|
|
76
|
+
}
|
|
77
|
+
this._account = payload;
|
|
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() {
|
|
90
|
+
return __awaiter(this, arguments, void 0, function* (permissions = ALL_PERMISSION_TYPES) {
|
|
91
|
+
return yield this._callBridge({
|
|
92
|
+
method: 'hasPermissions',
|
|
93
|
+
params: permissions,
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
requestPermissions() {
|
|
98
|
+
return __awaiter(this, arguments, void 0, function* (permissions = ALL_PERMISSION_TYPES) {
|
|
99
|
+
return yield 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
|
+
return accounts;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
getActiveChain() {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
return this._callBridge({
|
|
130
|
+
method: 'getActiveChain',
|
|
131
|
+
params: undefined,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
signAndExecuteTransactionBlock(input) {
|
|
136
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
+
return this._callBridge({
|
|
138
|
+
method: 'signAndExecuteTransactionBlock',
|
|
139
|
+
params: {
|
|
140
|
+
account: input.account,
|
|
141
|
+
chain: input.chain,
|
|
142
|
+
walletSerialize: JSON.stringify(input.account),
|
|
143
|
+
blockSerialize: input.transactionBlock.serialize(),
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
signTransactionBlock(input) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
return this._callBridge({
|
|
151
|
+
method: 'signTransactionBlock',
|
|
152
|
+
params: {
|
|
153
|
+
account: input.account,
|
|
154
|
+
chain: input.chain,
|
|
155
|
+
walletSerialize: JSON.stringify(input.account),
|
|
156
|
+
blockSerialize: input.transactionBlock.serialize(),
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
signMessage(input) {
|
|
162
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
return this._callBridge({
|
|
164
|
+
method: 'signMessage',
|
|
165
|
+
params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), messageSerialize: bytesToHex(input.message) }),
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
signPersonalMessage(input) {
|
|
170
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
+
return this._callBridge({
|
|
172
|
+
method: 'signPersonalMessage',
|
|
173
|
+
params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), messageSerialize: bytesToHex(input.message) }),
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
signTransaction(input) {
|
|
178
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
+
return this._callBridge({
|
|
180
|
+
method: 'signTransaction',
|
|
181
|
+
params: {
|
|
182
|
+
transaction: yield input.transaction.toJSON(),
|
|
183
|
+
account: input.account,
|
|
184
|
+
chain: input.chain,
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
signAndExecuteTransaction(input) {
|
|
190
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
191
|
+
return this._callBridge({
|
|
192
|
+
method: 'signAndExecuteTransaction',
|
|
193
|
+
params: {
|
|
194
|
+
transaction: yield input.transaction.toJSON(),
|
|
195
|
+
account: input.account,
|
|
196
|
+
chain: input.chain,
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
isConnected() {
|
|
202
|
+
return this._account !== null;
|
|
203
|
+
}
|
|
204
|
+
onNetworkChange(listener) {
|
|
205
|
+
return super.on(PROVIDER_EVENTS.networkChange, listener);
|
|
206
|
+
}
|
|
207
|
+
onAccountChange(listener) {
|
|
208
|
+
return super.on(PROVIDER_EVENTS.accountChanged, listener);
|
|
209
|
+
}
|
|
210
|
+
on(event, listener) {
|
|
211
|
+
return super.on(event, listener);
|
|
212
|
+
}
|
|
213
|
+
emit(event, ...args) {
|
|
214
|
+
return super.emit(event, ...args);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
export { ProviderSui };
|
|
@@ -0,0 +1,198 @@
|
|
|
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 { ReadonlyWalletAccount, SUI_DEVNET_CHAIN, SUI_TESTNET_CHAIN, registerWallet, } from '@mysten/wallet-standard';
|
|
11
|
+
import mitt from 'mitt';
|
|
12
|
+
import { hexToBytes } from '@1tokenfe/cross-inpage-provider-core';
|
|
13
|
+
import { ALL_PERMISSION_TYPES } from './types';
|
|
14
|
+
var Feature;
|
|
15
|
+
(function (Feature) {
|
|
16
|
+
Feature["STANDARD__CONNECT"] = "standard:connect";
|
|
17
|
+
Feature["STANDARD__DISCONNECT"] = "standard:disconnect";
|
|
18
|
+
Feature["STANDARD__EVENTS"] = "standard:events";
|
|
19
|
+
Feature["SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK"] = "sui:signAndExecuteTransactionBlock";
|
|
20
|
+
Feature["SUI__SIGN_TRANSACTION_BLOCK"] = "sui:signTransactionBlock";
|
|
21
|
+
Feature["SUI__SIGN_MESSAGE"] = "sui:signMessage";
|
|
22
|
+
Feature["SUI__SIGN_PERSONAL_MESSAGE"] = "sui:signPersonalMessage";
|
|
23
|
+
Feature["SUI__SIGN_AND_EXECUTE_TRANSACTION"] = "sui:signAndExecuteTransaction";
|
|
24
|
+
Feature["SUI__SIGN_TRANSACTION"] = "sui:signTransaction";
|
|
25
|
+
})(Feature || (Feature = {}));
|
|
26
|
+
class OnetokenSuiStandardWallet {
|
|
27
|
+
get name() {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
return (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : this._name;
|
|
30
|
+
}
|
|
31
|
+
get icon() {
|
|
32
|
+
var _a;
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
|
|
34
|
+
return (((_a = this.options) === null || _a === void 0 ? void 0 : _a.logo) || '');
|
|
35
|
+
}
|
|
36
|
+
get chains() {
|
|
37
|
+
return [SUI_DEVNET_CHAIN, SUI_TESTNET_CHAIN];
|
|
38
|
+
}
|
|
39
|
+
get accounts() {
|
|
40
|
+
return this._account ? [this._account] : [];
|
|
41
|
+
}
|
|
42
|
+
get features() {
|
|
43
|
+
return {
|
|
44
|
+
[Feature.STANDARD__CONNECT]: {
|
|
45
|
+
version: '1.0.0',
|
|
46
|
+
connect: this.$connect,
|
|
47
|
+
},
|
|
48
|
+
[Feature.STANDARD__DISCONNECT]: {
|
|
49
|
+
version: '1.0.0',
|
|
50
|
+
disconnect: this.$disconnect,
|
|
51
|
+
},
|
|
52
|
+
[Feature.STANDARD__EVENTS]: {
|
|
53
|
+
version: '1.0.0',
|
|
54
|
+
on: this.$on,
|
|
55
|
+
},
|
|
56
|
+
[Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK]: {
|
|
57
|
+
version: '1.0.0',
|
|
58
|
+
signAndExecuteTransactionBlock: this.$signAndExecuteTransactionBlock,
|
|
59
|
+
},
|
|
60
|
+
[Feature.SUI__SIGN_TRANSACTION_BLOCK]: {
|
|
61
|
+
version: '1.0.0',
|
|
62
|
+
signTransactionBlock: this.$signTransactionBlock,
|
|
63
|
+
},
|
|
64
|
+
[Feature.SUI__SIGN_MESSAGE]: {
|
|
65
|
+
version: '1.0.0',
|
|
66
|
+
signMessage: this.$signMessage,
|
|
67
|
+
},
|
|
68
|
+
[Feature.SUI__SIGN_PERSONAL_MESSAGE]: {
|
|
69
|
+
version: '1.1.0',
|
|
70
|
+
signPersonalMessage: this.$signPersonalMessage,
|
|
71
|
+
},
|
|
72
|
+
[Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION]: {
|
|
73
|
+
version: '2.0.0',
|
|
74
|
+
signAndExecuteTransaction: this.$signAndExecuteTransaction,
|
|
75
|
+
},
|
|
76
|
+
[Feature.SUI__SIGN_TRANSACTION]: {
|
|
77
|
+
version: '2.0.0',
|
|
78
|
+
signTransaction: this.$signTransaction,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
constructor(provider, options) {
|
|
83
|
+
this.version = '1.0.0';
|
|
84
|
+
this._name = 'OneToken Wallet';
|
|
85
|
+
this.$on = (event, listener) => {
|
|
86
|
+
this._events.on(event, listener);
|
|
87
|
+
return () => this._events.off(event, listener);
|
|
88
|
+
};
|
|
89
|
+
this.$connected = () => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
if (!(yield this.$hasPermissions(['viewAccount']))) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const accounts = yield this.provider.getAccounts();
|
|
94
|
+
const [account] = accounts;
|
|
95
|
+
const activateAccount = this._account;
|
|
96
|
+
if (activateAccount && activateAccount.address === account.address) {
|
|
97
|
+
return { accounts: this.accounts };
|
|
98
|
+
}
|
|
99
|
+
if (account) {
|
|
100
|
+
yield this.handleAccountSwitch(account);
|
|
101
|
+
return { accounts: this.accounts };
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
this.$connect = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
if (!(input === null || input === void 0 ? void 0 : input.silent)) {
|
|
106
|
+
yield this.provider.requestPermissions();
|
|
107
|
+
}
|
|
108
|
+
yield this.$connected();
|
|
109
|
+
return { accounts: this.accounts };
|
|
110
|
+
});
|
|
111
|
+
this.$disconnect = () => __awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
yield this.provider.disconnect();
|
|
113
|
+
this._account = null;
|
|
114
|
+
this._events.all.clear();
|
|
115
|
+
});
|
|
116
|
+
this.$signAndExecuteTransactionBlock = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
return this.provider.signAndExecuteTransactionBlock(input);
|
|
118
|
+
});
|
|
119
|
+
this.$signTransactionBlock = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
return this.provider.signTransactionBlock(input);
|
|
121
|
+
});
|
|
122
|
+
this.$signMessage = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
return this.provider.signMessage(input);
|
|
124
|
+
});
|
|
125
|
+
this.$signPersonalMessage = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
return this.provider.signPersonalMessage(input);
|
|
127
|
+
});
|
|
128
|
+
this.$signAndExecuteTransaction = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
return this.provider.signAndExecuteTransaction(input);
|
|
130
|
+
});
|
|
131
|
+
this.$signTransaction = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
return this.provider.signTransaction(input);
|
|
133
|
+
});
|
|
134
|
+
this.handleAccountSwitch = (payload) => __awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
const { address, publicKey } = payload;
|
|
136
|
+
const activateChain = yield this.getActiveChain();
|
|
137
|
+
this._account = new ReadonlyWalletAccount({
|
|
138
|
+
address: address,
|
|
139
|
+
publicKey: hexToBytes(publicKey),
|
|
140
|
+
chains: activateChain ? [activateChain] : [],
|
|
141
|
+
features: [
|
|
142
|
+
Feature.STANDARD__CONNECT,
|
|
143
|
+
Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK,
|
|
144
|
+
Feature.SUI__SIGN_TRANSACTION_BLOCK,
|
|
145
|
+
Feature.SUI__SIGN_MESSAGE,
|
|
146
|
+
Feature.SUI__SIGN_PERSONAL_MESSAGE,
|
|
147
|
+
Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION,
|
|
148
|
+
Feature.SUI__SIGN_TRANSACTION,
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
this._events.emit('change', {
|
|
152
|
+
accounts: this.accounts,
|
|
153
|
+
chains: activateChain ? [activateChain] : [],
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
this.handleNetworkSwitch = (payload) => {
|
|
157
|
+
const { network } = payload;
|
|
158
|
+
this._events.emit('change', {
|
|
159
|
+
accounts: this.accounts,
|
|
160
|
+
chains: [network],
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
this.provider = provider;
|
|
164
|
+
this._events = mitt();
|
|
165
|
+
this._account = null;
|
|
166
|
+
this.options = options;
|
|
167
|
+
this.subscribeEventFromBackend();
|
|
168
|
+
void this.$connected();
|
|
169
|
+
}
|
|
170
|
+
getActiveChain() {
|
|
171
|
+
return this.provider.getActiveChain();
|
|
172
|
+
}
|
|
173
|
+
$hasPermissions(permissions = ALL_PERMISSION_TYPES) {
|
|
174
|
+
return this.provider.hasPermissions(permissions);
|
|
175
|
+
}
|
|
176
|
+
subscribeEventFromBackend() {
|
|
177
|
+
this.provider.onNetworkChange((network) => {
|
|
178
|
+
if (!network) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this.handleNetworkSwitch({ network: network });
|
|
182
|
+
});
|
|
183
|
+
this.provider.onAccountChange((account) => {
|
|
184
|
+
if (!account) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
void this.handleAccountSwitch(account);
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
export function registerSuiWallet(provider, options) {
|
|
192
|
+
try {
|
|
193
|
+
registerWallet(new OnetokenSuiStandardWallet(provider, options));
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
console.error(error);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { IInpageProviderConfig } from '@1tokenfe/cross-inpage-provider-core';
|
|
2
|
+
import { ProviderBase } from '@1tokenfe/cross-inpage-provider-core';
|
|
3
|
+
import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
|
|
4
|
+
declare class ProviderSuiBase extends ProviderBase {
|
|
5
|
+
constructor(props: IInpageProviderConfig);
|
|
6
|
+
protected providerName: IInjectedProviderNames;
|
|
7
|
+
request(data: unknown): Promise<unknown>;
|
|
8
|
+
}
|
|
9
|
+
export { ProviderSuiBase };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ProviderBase } from '@1tokenfe/cross-inpage-provider-core';
|
|
2
|
+
import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
|
|
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,220 @@
|
|
|
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 cross_inpage_provider_core_1 = require("@1tokenfe/cross-inpage-provider-core");
|
|
14
|
+
const cross_inpage_provider_errors_1 = require("@1tokenfe/cross-inpage-provider-errors");
|
|
15
|
+
const extension_bridge_injected_1 = require("@1tokenfe/extension-bridge-injected");
|
|
16
|
+
const ProviderSuiBase_1 = require("./ProviderSuiBase");
|
|
17
|
+
const types_1 = require("./types");
|
|
18
|
+
const PROVIDER_EVENTS = {
|
|
19
|
+
'connect': 'connect',
|
|
20
|
+
'disconnect': 'disconnect',
|
|
21
|
+
'accountChanged': 'accountChanged',
|
|
22
|
+
'networkChange': 'networkChange',
|
|
23
|
+
'message_low_level': 'message_low_level',
|
|
24
|
+
};
|
|
25
|
+
function isWalletEventMethodMatch({ method, name }) {
|
|
26
|
+
return method === `wallet_events_${name}`;
|
|
27
|
+
}
|
|
28
|
+
class ProviderSui extends ProviderSuiBase_1.ProviderSuiBase {
|
|
29
|
+
constructor(props) {
|
|
30
|
+
super(Object.assign(Object.assign({}, props), { bridge: props.bridge || (0, extension_bridge_injected_1.getOrCreateExtInjectedJsBridge)({ timeout: props.timeout }) }));
|
|
31
|
+
this._account = null;
|
|
32
|
+
this._registerEvents();
|
|
33
|
+
}
|
|
34
|
+
_registerEvents() {
|
|
35
|
+
window.addEventListener('onetoken_bridge_disconnect', () => {
|
|
36
|
+
this._handleDisconnected();
|
|
37
|
+
});
|
|
38
|
+
this.on(PROVIDER_EVENTS.message_low_level, (payload) => {
|
|
39
|
+
if (!payload)
|
|
40
|
+
return;
|
|
41
|
+
const { method, params } = payload;
|
|
42
|
+
if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.accountChanged })) {
|
|
43
|
+
this._handleAccountChange(params);
|
|
44
|
+
}
|
|
45
|
+
if (isWalletEventMethodMatch({ method, name: PROVIDER_EVENTS.networkChange })) {
|
|
46
|
+
this._handleNetworkChange(params);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
_callBridge(params) {
|
|
51
|
+
return this.bridgeRequest(params);
|
|
52
|
+
}
|
|
53
|
+
_handleConnected(account, options = { emit: true }) {
|
|
54
|
+
var _a;
|
|
55
|
+
if (options.emit) {
|
|
56
|
+
this.emit('connect', (_a = account === null || account === void 0 ? void 0 : account.address) !== null && _a !== void 0 ? _a : null);
|
|
57
|
+
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);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
_handleDisconnected(options = { emit: true }) {
|
|
61
|
+
this._account = null;
|
|
62
|
+
if (options.emit) {
|
|
63
|
+
this.emit('disconnect');
|
|
64
|
+
this.emit('accountChanged', null);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
isAccountsChanged(account) {
|
|
68
|
+
var _a;
|
|
69
|
+
return (account === null || account === void 0 ? void 0 : account.address) !== ((_a = this._account) === null || _a === void 0 ? void 0 : _a.address);
|
|
70
|
+
}
|
|
71
|
+
// trigger by bridge account change event
|
|
72
|
+
_handleAccountChange(payload) {
|
|
73
|
+
if (!payload) {
|
|
74
|
+
this._handleDisconnected();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (this.isAccountsChanged(payload)) {
|
|
78
|
+
this._handleConnected(payload);
|
|
79
|
+
}
|
|
80
|
+
this._account = payload;
|
|
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() {
|
|
93
|
+
return __awaiter(this, arguments, void 0, function* (permissions = types_1.ALL_PERMISSION_TYPES) {
|
|
94
|
+
return yield this._callBridge({
|
|
95
|
+
method: 'hasPermissions',
|
|
96
|
+
params: permissions,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
requestPermissions() {
|
|
101
|
+
return __awaiter(this, arguments, void 0, function* (permissions = types_1.ALL_PERMISSION_TYPES) {
|
|
102
|
+
return yield 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
|
+
return accounts;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
getActiveChain() {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
return this._callBridge({
|
|
133
|
+
method: 'getActiveChain',
|
|
134
|
+
params: undefined,
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
signAndExecuteTransactionBlock(input) {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
return this._callBridge({
|
|
141
|
+
method: 'signAndExecuteTransactionBlock',
|
|
142
|
+
params: {
|
|
143
|
+
account: input.account,
|
|
144
|
+
chain: input.chain,
|
|
145
|
+
walletSerialize: JSON.stringify(input.account),
|
|
146
|
+
blockSerialize: input.transactionBlock.serialize(),
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
signTransactionBlock(input) {
|
|
152
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
return this._callBridge({
|
|
154
|
+
method: 'signTransactionBlock',
|
|
155
|
+
params: {
|
|
156
|
+
account: input.account,
|
|
157
|
+
chain: input.chain,
|
|
158
|
+
walletSerialize: JSON.stringify(input.account),
|
|
159
|
+
blockSerialize: input.transactionBlock.serialize(),
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
signMessage(input) {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
166
|
+
return this._callBridge({
|
|
167
|
+
method: 'signMessage',
|
|
168
|
+
params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), messageSerialize: (0, cross_inpage_provider_core_1.bytesToHex)(input.message) }),
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
signPersonalMessage(input) {
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
return this._callBridge({
|
|
175
|
+
method: 'signPersonalMessage',
|
|
176
|
+
params: Object.assign(Object.assign({}, input), { walletSerialize: JSON.stringify(input.account), messageSerialize: (0, cross_inpage_provider_core_1.bytesToHex)(input.message) }),
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
signTransaction(input) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
return this._callBridge({
|
|
183
|
+
method: 'signTransaction',
|
|
184
|
+
params: {
|
|
185
|
+
transaction: yield input.transaction.toJSON(),
|
|
186
|
+
account: input.account,
|
|
187
|
+
chain: input.chain,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
signAndExecuteTransaction(input) {
|
|
193
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
194
|
+
return this._callBridge({
|
|
195
|
+
method: 'signAndExecuteTransaction',
|
|
196
|
+
params: {
|
|
197
|
+
transaction: yield input.transaction.toJSON(),
|
|
198
|
+
account: input.account,
|
|
199
|
+
chain: input.chain,
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
isConnected() {
|
|
205
|
+
return this._account !== null;
|
|
206
|
+
}
|
|
207
|
+
onNetworkChange(listener) {
|
|
208
|
+
return super.on(PROVIDER_EVENTS.networkChange, listener);
|
|
209
|
+
}
|
|
210
|
+
onAccountChange(listener) {
|
|
211
|
+
return super.on(PROVIDER_EVENTS.accountChanged, listener);
|
|
212
|
+
}
|
|
213
|
+
on(event, listener) {
|
|
214
|
+
return super.on(event, listener);
|
|
215
|
+
}
|
|
216
|
+
emit(event, ...args) {
|
|
217
|
+
return super.emit(event, ...args);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
exports.ProviderSui = ProviderSui;
|
|
@@ -0,0 +1,204 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.registerSuiWallet = registerSuiWallet;
|
|
16
|
+
const wallet_standard_1 = require("@mysten/wallet-standard");
|
|
17
|
+
const mitt_1 = __importDefault(require("mitt"));
|
|
18
|
+
const cross_inpage_provider_core_1 = require("@1tokenfe/cross-inpage-provider-core");
|
|
19
|
+
const types_1 = require("./types");
|
|
20
|
+
var Feature;
|
|
21
|
+
(function (Feature) {
|
|
22
|
+
Feature["STANDARD__CONNECT"] = "standard:connect";
|
|
23
|
+
Feature["STANDARD__DISCONNECT"] = "standard:disconnect";
|
|
24
|
+
Feature["STANDARD__EVENTS"] = "standard:events";
|
|
25
|
+
Feature["SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK"] = "sui:signAndExecuteTransactionBlock";
|
|
26
|
+
Feature["SUI__SIGN_TRANSACTION_BLOCK"] = "sui:signTransactionBlock";
|
|
27
|
+
Feature["SUI__SIGN_MESSAGE"] = "sui:signMessage";
|
|
28
|
+
Feature["SUI__SIGN_PERSONAL_MESSAGE"] = "sui:signPersonalMessage";
|
|
29
|
+
Feature["SUI__SIGN_AND_EXECUTE_TRANSACTION"] = "sui:signAndExecuteTransaction";
|
|
30
|
+
Feature["SUI__SIGN_TRANSACTION"] = "sui:signTransaction";
|
|
31
|
+
})(Feature || (Feature = {}));
|
|
32
|
+
class OnetokenSuiStandardWallet {
|
|
33
|
+
get name() {
|
|
34
|
+
var _a, _b;
|
|
35
|
+
return (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : this._name;
|
|
36
|
+
}
|
|
37
|
+
get icon() {
|
|
38
|
+
var _a;
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any
|
|
40
|
+
return (((_a = this.options) === null || _a === void 0 ? void 0 : _a.logo) || '');
|
|
41
|
+
}
|
|
42
|
+
get chains() {
|
|
43
|
+
return [wallet_standard_1.SUI_DEVNET_CHAIN, wallet_standard_1.SUI_TESTNET_CHAIN];
|
|
44
|
+
}
|
|
45
|
+
get accounts() {
|
|
46
|
+
return this._account ? [this._account] : [];
|
|
47
|
+
}
|
|
48
|
+
get features() {
|
|
49
|
+
return {
|
|
50
|
+
[Feature.STANDARD__CONNECT]: {
|
|
51
|
+
version: '1.0.0',
|
|
52
|
+
connect: this.$connect,
|
|
53
|
+
},
|
|
54
|
+
[Feature.STANDARD__DISCONNECT]: {
|
|
55
|
+
version: '1.0.0',
|
|
56
|
+
disconnect: this.$disconnect,
|
|
57
|
+
},
|
|
58
|
+
[Feature.STANDARD__EVENTS]: {
|
|
59
|
+
version: '1.0.0',
|
|
60
|
+
on: this.$on,
|
|
61
|
+
},
|
|
62
|
+
[Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK]: {
|
|
63
|
+
version: '1.0.0',
|
|
64
|
+
signAndExecuteTransactionBlock: this.$signAndExecuteTransactionBlock,
|
|
65
|
+
},
|
|
66
|
+
[Feature.SUI__SIGN_TRANSACTION_BLOCK]: {
|
|
67
|
+
version: '1.0.0',
|
|
68
|
+
signTransactionBlock: this.$signTransactionBlock,
|
|
69
|
+
},
|
|
70
|
+
[Feature.SUI__SIGN_MESSAGE]: {
|
|
71
|
+
version: '1.0.0',
|
|
72
|
+
signMessage: this.$signMessage,
|
|
73
|
+
},
|
|
74
|
+
[Feature.SUI__SIGN_PERSONAL_MESSAGE]: {
|
|
75
|
+
version: '1.1.0',
|
|
76
|
+
signPersonalMessage: this.$signPersonalMessage,
|
|
77
|
+
},
|
|
78
|
+
[Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION]: {
|
|
79
|
+
version: '2.0.0',
|
|
80
|
+
signAndExecuteTransaction: this.$signAndExecuteTransaction,
|
|
81
|
+
},
|
|
82
|
+
[Feature.SUI__SIGN_TRANSACTION]: {
|
|
83
|
+
version: '2.0.0',
|
|
84
|
+
signTransaction: this.$signTransaction,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
constructor(provider, options) {
|
|
89
|
+
this.version = '1.0.0';
|
|
90
|
+
this._name = 'OneToken Wallet';
|
|
91
|
+
this.$on = (event, listener) => {
|
|
92
|
+
this._events.on(event, listener);
|
|
93
|
+
return () => this._events.off(event, listener);
|
|
94
|
+
};
|
|
95
|
+
this.$connected = () => __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
if (!(yield this.$hasPermissions(['viewAccount']))) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const accounts = yield this.provider.getAccounts();
|
|
100
|
+
const [account] = accounts;
|
|
101
|
+
const activateAccount = this._account;
|
|
102
|
+
if (activateAccount && activateAccount.address === account.address) {
|
|
103
|
+
return { accounts: this.accounts };
|
|
104
|
+
}
|
|
105
|
+
if (account) {
|
|
106
|
+
yield this.handleAccountSwitch(account);
|
|
107
|
+
return { accounts: this.accounts };
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
this.$connect = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
if (!(input === null || input === void 0 ? void 0 : input.silent)) {
|
|
112
|
+
yield this.provider.requestPermissions();
|
|
113
|
+
}
|
|
114
|
+
yield this.$connected();
|
|
115
|
+
return { accounts: this.accounts };
|
|
116
|
+
});
|
|
117
|
+
this.$disconnect = () => __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
yield this.provider.disconnect();
|
|
119
|
+
this._account = null;
|
|
120
|
+
this._events.all.clear();
|
|
121
|
+
});
|
|
122
|
+
this.$signAndExecuteTransactionBlock = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
return this.provider.signAndExecuteTransactionBlock(input);
|
|
124
|
+
});
|
|
125
|
+
this.$signTransactionBlock = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
return this.provider.signTransactionBlock(input);
|
|
127
|
+
});
|
|
128
|
+
this.$signMessage = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
return this.provider.signMessage(input);
|
|
130
|
+
});
|
|
131
|
+
this.$signPersonalMessage = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
return this.provider.signPersonalMessage(input);
|
|
133
|
+
});
|
|
134
|
+
this.$signAndExecuteTransaction = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
return this.provider.signAndExecuteTransaction(input);
|
|
136
|
+
});
|
|
137
|
+
this.$signTransaction = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
return this.provider.signTransaction(input);
|
|
139
|
+
});
|
|
140
|
+
this.handleAccountSwitch = (payload) => __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
const { address, publicKey } = payload;
|
|
142
|
+
const activateChain = yield this.getActiveChain();
|
|
143
|
+
this._account = new wallet_standard_1.ReadonlyWalletAccount({
|
|
144
|
+
address: address,
|
|
145
|
+
publicKey: (0, cross_inpage_provider_core_1.hexToBytes)(publicKey),
|
|
146
|
+
chains: activateChain ? [activateChain] : [],
|
|
147
|
+
features: [
|
|
148
|
+
Feature.STANDARD__CONNECT,
|
|
149
|
+
Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION_BLOCK,
|
|
150
|
+
Feature.SUI__SIGN_TRANSACTION_BLOCK,
|
|
151
|
+
Feature.SUI__SIGN_MESSAGE,
|
|
152
|
+
Feature.SUI__SIGN_PERSONAL_MESSAGE,
|
|
153
|
+
Feature.SUI__SIGN_AND_EXECUTE_TRANSACTION,
|
|
154
|
+
Feature.SUI__SIGN_TRANSACTION,
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
this._events.emit('change', {
|
|
158
|
+
accounts: this.accounts,
|
|
159
|
+
chains: activateChain ? [activateChain] : [],
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
this.handleNetworkSwitch = (payload) => {
|
|
163
|
+
const { network } = payload;
|
|
164
|
+
this._events.emit('change', {
|
|
165
|
+
accounts: this.accounts,
|
|
166
|
+
chains: [network],
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
this.provider = provider;
|
|
170
|
+
this._events = (0, mitt_1.default)();
|
|
171
|
+
this._account = null;
|
|
172
|
+
this.options = options;
|
|
173
|
+
this.subscribeEventFromBackend();
|
|
174
|
+
void this.$connected();
|
|
175
|
+
}
|
|
176
|
+
getActiveChain() {
|
|
177
|
+
return this.provider.getActiveChain();
|
|
178
|
+
}
|
|
179
|
+
$hasPermissions(permissions = types_1.ALL_PERMISSION_TYPES) {
|
|
180
|
+
return this.provider.hasPermissions(permissions);
|
|
181
|
+
}
|
|
182
|
+
subscribeEventFromBackend() {
|
|
183
|
+
this.provider.onNetworkChange((network) => {
|
|
184
|
+
if (!network) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.handleNetworkSwitch({ network: network });
|
|
188
|
+
});
|
|
189
|
+
this.provider.onAccountChange((account) => {
|
|
190
|
+
if (!account) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
void this.handleAccountSwitch(account);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function registerSuiWallet(provider, options) {
|
|
198
|
+
try {
|
|
199
|
+
(0, wallet_standard_1.registerWallet)(new OnetokenSuiStandardWallet(provider, options));
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
console.error(error);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProviderSuiBase = void 0;
|
|
4
|
+
const cross_inpage_provider_core_1 = require("@1tokenfe/cross-inpage-provider-core");
|
|
5
|
+
const cross_inpage_provider_types_1 = require("@1tokenfe/cross-inpage-provider-types");
|
|
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,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.registerSuiWallet = void 0;
|
|
18
|
+
__exportStar(require("./OnetokenSuiProvider"), exports);
|
|
19
|
+
__exportStar(require("./ProviderSuiBase"), exports);
|
|
20
|
+
var OnetokenSuiStandardWallet_1 = require("./OnetokenSuiStandardWallet");
|
|
21
|
+
Object.defineProperty(exports, "registerSuiWallet", { enumerable: true, get: function () { return OnetokenSuiStandardWallet_1.registerSuiWallet; } });
|
|
22
|
+
__exportStar(require("./types"), exports);
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { SuiChain } from '@mysten/wallet-standard';
|
|
2
|
+
export declare const ALL_PERMISSION_TYPES: readonly ["viewAccount", "suggestTransactions"];
|
|
3
|
+
type AllPermissionsType = typeof ALL_PERMISSION_TYPES;
|
|
4
|
+
export type PermissionType = AllPermissionsType[number];
|
|
5
|
+
export type WalletInfo = {
|
|
6
|
+
name?: string;
|
|
7
|
+
logo: string;
|
|
8
|
+
};
|
|
9
|
+
export type SuiChainType = SuiChain;
|
|
10
|
+
export interface AccountInfo {
|
|
11
|
+
address: string;
|
|
12
|
+
publicKey: string;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ALL_PERMISSION_TYPES = ['viewAccount', 'suggestTransactions'];
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1tokenfe/onetoken-sui-provider",
|
|
3
|
+
"version": "2.2.46",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"cross-inpage-provider"
|
|
6
|
+
],
|
|
7
|
+
"author": "dev-fe@onetoken.so",
|
|
8
|
+
"repository": "https://github.com/OneTokenHQ/cross-inpage-provider",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/*"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.js"
|
|
20
|
+
},
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"module": "./dist/index.js",
|
|
23
|
+
"main": "./dist/cjs/index.js",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prebuild": "rm -rf dist",
|
|
26
|
+
"build": "tsc && tsc --project tsconfig.cjs.json",
|
|
27
|
+
"start": "tsc --watch"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@1tokenfe/cross-inpage-provider-core": "2.2.46",
|
|
31
|
+
"@1tokenfe/cross-inpage-provider-errors": "2.2.46",
|
|
32
|
+
"@1tokenfe/cross-inpage-provider-types": "2.2.46",
|
|
33
|
+
"@1tokenfe/extension-bridge-injected": "2.2.46",
|
|
34
|
+
"@mysten/wallet-standard": "^0.14.0",
|
|
35
|
+
"eth-rpc-errors": "^4.0.3",
|
|
36
|
+
"mitt": "^3.0.0"
|
|
37
|
+
},
|
|
38
|
+
"gitHead": "dadd71e7e07ab7aaf87961aecc01255586848e16"
|
|
39
|
+
}
|