@1tokenfe/onetoken-solana-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.
Files changed (36) hide show
  1. package/LICENSE.md +51 -0
  2. package/README.md +1 -0
  3. package/dist/ProviderSolana.d.ts +156 -0
  4. package/dist/ProviderSolana.js +264 -0
  5. package/dist/ProviderSolanaBase.d.ts +8 -0
  6. package/dist/ProviderSolanaBase.js +12 -0
  7. package/dist/cjs/ProviderSolana.js +270 -0
  8. package/dist/cjs/ProviderSolanaBase.js +15 -0
  9. package/dist/cjs/index.js +19 -0
  10. package/dist/cjs/type-utils.js +2 -0
  11. package/dist/cjs/utils.js +55 -0
  12. package/dist/cjs/wallet-standard/account.js +58 -0
  13. package/dist/cjs/wallet-standard/index.js +7 -0
  14. package/dist/cjs/wallet-standard/register.js +82 -0
  15. package/dist/cjs/wallet-standard/solana.js +17 -0
  16. package/dist/cjs/wallet-standard/types.js +2 -0
  17. package/dist/cjs/wallet-standard/wallet.js +237 -0
  18. package/dist/index.d.ts +3 -0
  19. package/dist/index.js +3 -0
  20. package/dist/type-utils.d.ts +5 -0
  21. package/dist/type-utils.js +1 -0
  22. package/dist/utils.d.ts +15 -0
  23. package/dist/utils.js +43 -0
  24. package/dist/wallet-standard/account.d.ts +11 -0
  25. package/dist/wallet-standard/account.js +54 -0
  26. package/dist/wallet-standard/index.d.ts +3 -0
  27. package/dist/wallet-standard/index.js +2 -0
  28. package/dist/wallet-standard/register.d.ts +7 -0
  29. package/dist/wallet-standard/register.js +77 -0
  30. package/dist/wallet-standard/solana.d.ts +8 -0
  31. package/dist/wallet-standard/solana.js +13 -0
  32. package/dist/wallet-standard/types.d.ts +6 -0
  33. package/dist/wallet-standard/types.js +1 -0
  34. package/dist/wallet-standard/wallet.d.ts +22 -0
  35. package/dist/wallet-standard/wallet.js +230 -0
  36. package/package.json +44 -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,156 @@
1
+ import { PublicKey, VersionedTransaction } from '@solana/web3.js';
2
+ import type { SendOptions, Transaction } from '@solana/web3.js';
3
+ import { IInpageProviderConfig } from '@1tokenfe/cross-inpage-provider-core';
4
+ import { IJsonRpcRequest } from '@1tokenfe/cross-inpage-provider-types';
5
+ import { ProviderSolanaBase } from './ProviderSolanaBase';
6
+ import type * as TypeUtils from './type-utils';
7
+ export type DisplayEncoding = 'utf8' | 'hex';
8
+ export type ConnectOptions = {
9
+ onlyIfTrusted?: boolean;
10
+ };
11
+ export type SolanaRequest = {
12
+ 'connect': (params: ConnectOptions | undefined) => Promise<{
13
+ publicKey: PublicKey;
14
+ }>;
15
+ 'disconnect': () => Promise<void>;
16
+ 'signMessage': (params: {
17
+ message: string;
18
+ display?: DisplayEncoding;
19
+ }) => Promise<{
20
+ signature: string;
21
+ publicKey: string;
22
+ }>;
23
+ 'solSignOffchainMessage': (params: {
24
+ message: string;
25
+ version?: number;
26
+ }) => Promise<{
27
+ signature: string;
28
+ publicKey: string;
29
+ }>;
30
+ 'signTransaction': (params: {
31
+ message: string;
32
+ }) => Promise<Transaction>;
33
+ 'signAllTransactions': (params: {
34
+ message: string[];
35
+ }) => Promise<Transaction[]>;
36
+ 'signAndSendTransaction': (params: {
37
+ message: string;
38
+ options?: SendOptions;
39
+ }) => Promise<{
40
+ signature: string;
41
+ publicKey: string;
42
+ }>;
43
+ };
44
+ export type JsBridgeRequest = {
45
+ [K in keyof SolanaRequest]: (params: Parameters<SolanaRequest[K]>[0]) => Promise<TypeUtils.WireStringified<TypeUtils.ResolvePromise<ReturnType<SolanaRequest[K]>>>>;
46
+ };
47
+ declare const PROVIDER_EVENTS: {
48
+ readonly connect: "connect";
49
+ readonly disconnect: "disconnect";
50
+ readonly accountChanged: "accountChanged";
51
+ readonly message_low_level: "message_low_level";
52
+ };
53
+ type SolanaProviderEventsMap = {
54
+ [PROVIDER_EVENTS.connect]: (publicKey: PublicKey) => void;
55
+ [PROVIDER_EVENTS.disconnect]: () => void;
56
+ [PROVIDER_EVENTS.accountChanged]: (publicKey: PublicKey | null) => void;
57
+ [PROVIDER_EVENTS.message_low_level]: (payload: IJsonRpcRequest) => void;
58
+ };
59
+ type SolanaAccountInfo = {
60
+ publicKey: string;
61
+ };
62
+ interface IProviderSolana extends ProviderSolanaBase {
63
+ readonly isPhantom: true;
64
+ readonly isSolflare: true;
65
+ isConnected: boolean;
66
+ publicKey: PublicKey | null;
67
+ /**
68
+ * Connect wallet, and get wallet public key
69
+ * @param {Object} options - Connection options
70
+ * @param {string} options.onlyIfTrusted - Only connect when user have connected before, otherwise would throw an error
71
+ * @emits `connect` on success
72
+ */
73
+ connect(options?: ConnectOptions): Promise<{
74
+ publicKey: PublicKey;
75
+ }>;
76
+ /**
77
+ * Disconnect wallet
78
+ */
79
+ disconnect(): Promise<void>;
80
+ /**
81
+ * @deprecated
82
+ * Sign multiple transactions
83
+ * @returns Transaction[]
84
+ */
85
+ signAllTransactions(transactions: (Transaction | VersionedTransaction)[]): Promise<(Transaction | VersionedTransaction)[]>;
86
+ /**
87
+ * @deprecated
88
+ * Sign one transaction
89
+ * @returns Transaction
90
+ */
91
+ signTransaction(transaction: Transaction | VersionedTransaction): Promise<Transaction | VersionedTransaction>;
92
+ /**
93
+ * Sign and send a transaction
94
+ * @returns {Object} Signature and public key
95
+ */
96
+ signAndSendTransaction(transaction: Transaction | VersionedTransaction, options?: SendOptions): Promise<{
97
+ publicKey: string;
98
+ signature: string;
99
+ }>;
100
+ /** Sign a message
101
+ * @param message - The message to be signed.
102
+ * @param {string} [display='utf8'] - Specify how the message should be displayed. (default: 'uft8')
103
+ */
104
+ signMessage(message: Uint8Array, display?: DisplayEncoding): Promise<{
105
+ signature: Uint8Array;
106
+ publicKey: PublicKey;
107
+ }>;
108
+ }
109
+ type OneTokenSolanaProviderProps = IInpageProviderConfig & {
110
+ timeout?: number;
111
+ };
112
+ declare class ProviderSolana extends ProviderSolanaBase implements IProviderSolana {
113
+ readonly isPhantom = true;
114
+ readonly isSolflare = true;
115
+ private _publicKey;
116
+ get publicKey(): PublicKey | null;
117
+ get isConnected(): boolean;
118
+ constructor(props: OneTokenSolanaProviderProps);
119
+ private _registerEvents;
120
+ private _callBridge;
121
+ private postMessage;
122
+ connect(options?: ConnectOptions): Promise<{
123
+ publicKey: PublicKey;
124
+ }>;
125
+ private _handleConnected;
126
+ disconnect(): Promise<void>;
127
+ private _handleDisconnected;
128
+ isAccountsChanged(account: SolanaAccountInfo | undefined): boolean;
129
+ private _handleAccountChange;
130
+ signAndSendTransaction(transaction: Transaction | VersionedTransaction, options?: Partial<SendOptions>): Promise<{
131
+ publicKey: string;
132
+ signature: string;
133
+ }>;
134
+ private _handleSignAndSendTransaction;
135
+ signTransaction(transaction: Transaction | VersionedTransaction): Promise<Transaction | VersionedTransaction>;
136
+ private _handleSignTransaction;
137
+ signAllTransactions(transactions: (Transaction | VersionedTransaction)[]): Promise<(Transaction | VersionedTransaction)[]>;
138
+ private _handleSignAllTransactions;
139
+ signMessage(message: Uint8Array, display?: DisplayEncoding): Promise<{
140
+ signature: Uint8Array;
141
+ publicKey: PublicKey;
142
+ }>;
143
+ solSignOffchainMessage(message: Uint8Array, version?: number): Promise<{
144
+ signature: Uint8Array;
145
+ publicKey: PublicKey;
146
+ }>;
147
+ private _handleSignMessage;
148
+ request<T extends keyof SolanaRequest>(method: T, params: Parameters<SolanaRequest[T]>[0]): ReturnType<SolanaRequest[T]>;
149
+ request<T extends keyof SolanaRequest>(payload: {
150
+ method: T;
151
+ params: Parameters<SolanaRequest[T]>[0];
152
+ }): ReturnType<SolanaRequest[T]>;
153
+ on<E extends keyof SolanaProviderEventsMap>(event: E, listener: SolanaProviderEventsMap[E]): this;
154
+ emit<E extends keyof SolanaProviderEventsMap>(event: E, ...args: Parameters<SolanaProviderEventsMap[E]>): boolean;
155
+ }
156
+ export { ProviderSolana };
@@ -0,0 +1,264 @@
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 { PublicKey } from '@solana/web3.js';
11
+ import { getOrCreateExtInjectedJsBridge } from '@1tokenfe/extension-bridge-injected';
12
+ import base58 from 'bs58';
13
+ import { ProviderSolanaBase } from './ProviderSolanaBase';
14
+ import { decodeSignedTransaction, encodeTransaction, isWalletEventMethodMatch } from './utils';
15
+ const PROVIDER_EVENTS = {
16
+ 'connect': 'connect',
17
+ 'disconnect': 'disconnect',
18
+ 'accountChanged': 'accountChanged',
19
+ 'message_low_level': 'message_low_level',
20
+ };
21
+ class ProviderSolana extends ProviderSolanaBase {
22
+ get publicKey() {
23
+ return this._publicKey;
24
+ }
25
+ get isConnected() {
26
+ return this._publicKey !== null;
27
+ }
28
+ constructor(props) {
29
+ super(Object.assign(Object.assign({}, props), { bridge: props.bridge || getOrCreateExtInjectedJsBridge({ timeout: props.timeout }) }));
30
+ this.isPhantom = true;
31
+ this.isSolflare = true;
32
+ this._publicKey = null;
33
+ this._registerEvents = this._registerEvents.bind(this);
34
+ this._callBridge = this._callBridge.bind(this);
35
+ this._handleAccountChange = this._handleAccountChange.bind(this);
36
+ this._handleConnected = this._handleConnected.bind(this);
37
+ this._handleDisconnected = this._handleDisconnected.bind(this);
38
+ this._handleSignAndSendTransaction = this._handleSignAndSendTransaction.bind(this);
39
+ this._handleSignTransaction = this._handleSignTransaction.bind(this);
40
+ this._handleSignAllTransactions = this._handleSignAllTransactions.bind(this);
41
+ this._handleSignMessage = this._handleSignMessage.bind(this);
42
+ this.request = this.request.bind(this);
43
+ this.connect = this.connect.bind(this);
44
+ this.disconnect = this.disconnect.bind(this);
45
+ this.signAndSendTransaction = this.signAndSendTransaction.bind(this);
46
+ this.signTransaction = this.signTransaction.bind(this);
47
+ this.signAllTransactions = this.signAllTransactions.bind(this);
48
+ this.signMessage = this.signMessage.bind(this);
49
+ this.isAccountsChanged = this.isAccountsChanged.bind(this);
50
+ this.bridgeRequest = this.bridgeRequest.bind(this);
51
+ this._registerEvents();
52
+ }
53
+ _registerEvents() {
54
+ window.addEventListener('onetoken_bridge_disconnect', () => {
55
+ this._handleDisconnected();
56
+ });
57
+ this.on(PROVIDER_EVENTS.message_low_level, (payload) => {
58
+ const { method, params } = payload;
59
+ if (isWalletEventMethodMatch(method, PROVIDER_EVENTS.accountChanged)) {
60
+ this._handleAccountChange(params);
61
+ }
62
+ });
63
+ }
64
+ _callBridge(params) {
65
+ return this.bridgeRequest(params);
66
+ }
67
+ postMessage(param) {
68
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
69
+ return this._callBridge(param);
70
+ }
71
+ connect(options) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ if (this.publicKey) {
74
+ return { publicKey: this.publicKey };
75
+ }
76
+ // TODO: pass options to connect
77
+ const result = yield this._callBridge({
78
+ method: 'connect',
79
+ params: options,
80
+ });
81
+ const publicKey = new PublicKey(result.publicKey);
82
+ this._handleConnected(publicKey, { emit: true });
83
+ return { publicKey };
84
+ });
85
+ }
86
+ _handleConnected(publicKey, options = { emit: true }) {
87
+ this._publicKey = publicKey;
88
+ if (options.emit && this.isConnectionStatusChanged('connected')) {
89
+ this.connectionStatus = 'connected';
90
+ this.emit('connect', publicKey);
91
+ this.emit('accountChanged', publicKey);
92
+ }
93
+ }
94
+ disconnect() {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ yield this._callBridge({
97
+ method: 'disconnect',
98
+ params: void 0,
99
+ });
100
+ this._handleDisconnected();
101
+ });
102
+ }
103
+ _handleDisconnected(options = { emit: true }) {
104
+ this._publicKey = null;
105
+ if (options.emit && this.isConnectionStatusChanged('disconnected')) {
106
+ this.connectionStatus = 'disconnected';
107
+ this.emit('disconnect');
108
+ this.emit('accountChanged', null);
109
+ }
110
+ }
111
+ isAccountsChanged(account) {
112
+ var _a;
113
+ return (account === null || account === void 0 ? void 0 : account.publicKey) !== ((_a = this._publicKey) === null || _a === void 0 ? void 0 : _a.toBase58());
114
+ }
115
+ // trigger by bridge account change event
116
+ _handleAccountChange(payload) {
117
+ const account = payload.accounts[0];
118
+ let publicKey;
119
+ try {
120
+ publicKey = new PublicKey(account.publicKey);
121
+ }
122
+ catch (error) {
123
+ // noop
124
+ }
125
+ if (this.isAccountsChanged(account)) {
126
+ this.emit('accountChanged', publicKey || null);
127
+ }
128
+ if (!account) {
129
+ this._handleDisconnected();
130
+ return;
131
+ }
132
+ if (publicKey) {
133
+ this._handleConnected(publicKey, { emit: false });
134
+ }
135
+ }
136
+ signAndSendTransaction(transaction, options) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ return this._handleSignAndSendTransaction({
139
+ message: encodeTransaction(transaction),
140
+ options,
141
+ });
142
+ });
143
+ }
144
+ _handleSignAndSendTransaction(params) {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ const result = yield this._callBridge({
147
+ method: 'signAndSendTransaction',
148
+ params,
149
+ });
150
+ return result;
151
+ });
152
+ }
153
+ signTransaction(transaction) {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ const hasVersionedTx = 'version' in transaction;
156
+ return this._handleSignTransaction({
157
+ message: encodeTransaction(transaction),
158
+ }, {
159
+ onlyVersionedTx: hasVersionedTx,
160
+ });
161
+ });
162
+ }
163
+ _handleSignTransaction(params, options) {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ const { onlyVersionedTx } = options || {};
166
+ const result = yield this._callBridge({
167
+ method: 'signTransaction',
168
+ params,
169
+ });
170
+ return decodeSignedTransaction({
171
+ message: result,
172
+ onlyVersionedTx,
173
+ });
174
+ });
175
+ }
176
+ signAllTransactions(transactions) {
177
+ return __awaiter(this, void 0, void 0, function* () {
178
+ return this._handleSignAllTransactions({
179
+ message: transactions.map(encodeTransaction),
180
+ });
181
+ });
182
+ }
183
+ _handleSignAllTransactions(params) {
184
+ return __awaiter(this, void 0, void 0, function* () {
185
+ const result = yield this._callBridge({
186
+ method: 'signAllTransactions',
187
+ params,
188
+ });
189
+ return result.map((message) => decodeSignedTransaction({ message }));
190
+ });
191
+ }
192
+ signMessage(message, display) {
193
+ return __awaiter(this, void 0, void 0, function* () {
194
+ return this._handleSignMessage({ message, display });
195
+ });
196
+ }
197
+ solSignOffchainMessage(message, version) {
198
+ return __awaiter(this, void 0, void 0, function* () {
199
+ const result = yield this._callBridge({
200
+ method: 'solSignOffchainMessage',
201
+ params: {
202
+ message: typeof message === 'string' ? message : base58.encode(message),
203
+ version,
204
+ },
205
+ });
206
+ return {
207
+ signature: base58.decode(result.signature),
208
+ publicKey: new PublicKey(result.publicKey),
209
+ };
210
+ });
211
+ }
212
+ _handleSignMessage(params) {
213
+ return __awaiter(this, void 0, void 0, function* () {
214
+ const { message, display } = params;
215
+ const result = yield this._callBridge({
216
+ method: 'signMessage',
217
+ params: {
218
+ message: typeof message === 'string' ? message : base58.encode(message),
219
+ display,
220
+ },
221
+ });
222
+ return {
223
+ signature: base58.decode(result.signature),
224
+ publicKey: new PublicKey(result.publicKey),
225
+ };
226
+ });
227
+ }
228
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
229
+ request(...args) {
230
+ let method;
231
+ let params;
232
+ if (typeof args[0] === 'string') {
233
+ method = args[0];
234
+ params = args[1];
235
+ }
236
+ else {
237
+ const payload = args[0];
238
+ method = payload.method;
239
+ params = payload.params;
240
+ }
241
+ switch (method) {
242
+ case 'connect':
243
+ return this.connect(params);
244
+ case 'disconnect':
245
+ return this.disconnect();
246
+ case 'signTransaction':
247
+ return this._handleSignTransaction(params);
248
+ case 'signAllTransactions':
249
+ return this._handleSignAllTransactions(params);
250
+ case 'signMessage':
251
+ return this._handleSignMessage(params);
252
+ case 'signAndSendTransaction':
253
+ return this._handleSignAndSendTransaction(params);
254
+ }
255
+ return this._callBridge({ method, params });
256
+ }
257
+ on(event, listener) {
258
+ return super.on(event, listener);
259
+ }
260
+ emit(event, ...args) {
261
+ return super.emit(event, ...args);
262
+ }
263
+ }
264
+ export { ProviderSolana };
@@ -0,0 +1,8 @@
1
+ import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
2
+ import { ProviderBase, IInpageProviderConfig } from '@1tokenfe/cross-inpage-provider-core';
3
+ declare class ProviderSolanaBase extends ProviderBase {
4
+ constructor(props: IInpageProviderConfig);
5
+ protected providerName: IInjectedProviderNames;
6
+ request(data: unknown): Promise<unknown>;
7
+ }
8
+ export { ProviderSolanaBase };
@@ -0,0 +1,12 @@
1
+ import { IInjectedProviderNames } from '@1tokenfe/cross-inpage-provider-types';
2
+ import { ProviderBase } from '@1tokenfe/cross-inpage-provider-core';
3
+ class ProviderSolanaBase extends ProviderBase {
4
+ constructor(props) {
5
+ super(props);
6
+ this.providerName = IInjectedProviderNames.solana;
7
+ }
8
+ request(data) {
9
+ return this.bridgeRequest(data);
10
+ }
11
+ }
12
+ export { ProviderSolanaBase };