@phantom/browser-sdk 0.0.5 → 0.0.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.
- package/README.md +2 -12
- package/dist/chunk-GV6AIHPN.mjs +18 -0
- package/dist/index.mjs +2 -0
- package/dist/solana/index.d.ts +25 -32
- package/dist/solana/index.js +428 -98
- package/dist/solana/index.mjs +420 -98
- package/package.json +13 -5
package/README.md
CHANGED
|
@@ -34,14 +34,6 @@ const phantom = createPhantom({
|
|
|
34
34
|
// Now you can use the Solana-specific methods
|
|
35
35
|
async function connectAndSign() {
|
|
36
36
|
try {
|
|
37
|
-
// Get the Solana provider (Phantom wallet instance)
|
|
38
|
-
const provider = phantom.solana.getProvider();
|
|
39
|
-
|
|
40
|
-
if (!provider) {
|
|
41
|
-
console.error("Phantom wallet not found. Please install Phantom.");
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
37
|
// Attempt to connect to the wallet
|
|
46
38
|
const connectionResult = await phantom.solana.connect();
|
|
47
39
|
console.log("Connection Result:", connectionResult.address);
|
|
@@ -68,14 +60,12 @@ connectAndSign();
|
|
|
68
60
|
|
|
69
61
|
Once the `phantom.solana` object is initialized, you can access the following methods:
|
|
70
62
|
|
|
71
|
-
- `getProvider(): PhantomSolanaProvider | null`
|
|
72
|
-
- Retrieves the Phantom Solana provider instance.
|
|
73
63
|
- `connect(opts?: { onlyIfTrusted?: boolean }): Promise<string>`
|
|
74
64
|
- Connects to the Phantom wallet. Optionally, `onlyIfTrusted` can be set to true to only connect if the dApp is already trusted.
|
|
75
65
|
- `disconnect(): Promise<void>`
|
|
76
66
|
- Disconnects from the Phantom wallet.
|
|
77
|
-
- `getAccount():
|
|
78
|
-
- Gets the current connected
|
|
67
|
+
- `getAccount(): Promise<string | undefined>`
|
|
68
|
+
- Gets the current connected address
|
|
79
69
|
- `signIn(): Promise<SignInResult>`
|
|
80
70
|
- Initiates a sign-in request to the wallet.
|
|
81
71
|
- `signMessage(message: Uint8Array | string, display?: 'utf8' | 'hex'): Promise<SignedMessage>`
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __accessCheck = (obj, member, msg) => {
|
|
2
|
+
if (!member.has(obj))
|
|
3
|
+
throw TypeError("Cannot " + msg);
|
|
4
|
+
};
|
|
5
|
+
var __privateAdd = (obj, member, value) => {
|
|
6
|
+
if (member.has(obj))
|
|
7
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
8
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
9
|
+
};
|
|
10
|
+
var __privateMethod = (obj, member, method) => {
|
|
11
|
+
__accessCheck(obj, member, "access private method");
|
|
12
|
+
return method;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
__privateAdd,
|
|
17
|
+
__privateMethod
|
|
18
|
+
};
|
package/dist/index.mjs
CHANGED
package/dist/solana/index.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { ChainPlugin } from '../index.js';
|
|
2
2
|
import { Transaction as Transaction$1 } from '@solana/kit';
|
|
3
|
+
import { VersionedTransaction as VersionedTransaction$1 } from '@solana/web3.js';
|
|
4
|
+
|
|
5
|
+
declare function connect(): Promise<string | undefined>;
|
|
6
|
+
|
|
7
|
+
declare function disconnect(): Promise<void>;
|
|
3
8
|
|
|
4
9
|
type Transaction = {
|
|
5
10
|
message: Uint8Array;
|
|
@@ -71,16 +76,22 @@ interface PhantomSolanaProvider {
|
|
|
71
76
|
off: (event: "connect" | "disconnect" | "accountChanged", handler: (publicKey?: PublicKey) => void) => void;
|
|
72
77
|
}
|
|
73
78
|
|
|
79
|
+
type ConnectCallback = (publicKey: string) => void;
|
|
80
|
+
type DisconnectCallback = () => void;
|
|
81
|
+
type AccountChangedCallback = (publicKey: string) => void;
|
|
82
|
+
type PhantomEventCallback = ConnectCallback | DisconnectCallback | AccountChangedCallback;
|
|
83
|
+
|
|
84
|
+
declare function getAccount(): Promise<string | undefined>;
|
|
85
|
+
|
|
74
86
|
/**
|
|
75
|
-
* Signs a
|
|
76
|
-
* @param
|
|
77
|
-
* @
|
|
78
|
-
* @returns A promise that resolves with the signature and public key.
|
|
87
|
+
* Signs and sends a transaction using the Phantom provider.
|
|
88
|
+
* @param transaction The transaction to sign and send.
|
|
89
|
+
* @returns A promise that resolves with the transaction signature and optionally the public key.
|
|
79
90
|
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
80
91
|
*/
|
|
81
|
-
declare function
|
|
82
|
-
signature:
|
|
83
|
-
address
|
|
92
|
+
declare function signAndSendTransaction(transaction: Transaction$1 | VersionedTransaction$1): Promise<{
|
|
93
|
+
signature: string;
|
|
94
|
+
address?: string;
|
|
84
95
|
}>;
|
|
85
96
|
|
|
86
97
|
/**
|
|
@@ -96,36 +107,18 @@ declare function signIn(signInData: SolanaSignInData): Promise<{
|
|
|
96
107
|
}>;
|
|
97
108
|
|
|
98
109
|
/**
|
|
99
|
-
* Signs
|
|
100
|
-
* @param
|
|
101
|
-
* @
|
|
110
|
+
* Signs a message using the Phantom provider.
|
|
111
|
+
* @param message The message to sign (as a Uint8Array).
|
|
112
|
+
* @param display The display encoding for the message (optional, defaults to utf8).
|
|
113
|
+
* @returns A promise that resolves with the signature and public key.
|
|
102
114
|
* @throws Error if Phantom provider is not found or if the operation fails.
|
|
103
115
|
*/
|
|
104
|
-
declare function
|
|
105
|
-
signature:
|
|
106
|
-
address?: string;
|
|
107
|
-
}>;
|
|
108
|
-
|
|
109
|
-
declare function connect(): Promise<string | undefined>;
|
|
110
|
-
|
|
111
|
-
declare function disconnect(): Promise<void>;
|
|
112
|
-
|
|
113
|
-
type ConnectCallback = (publicKey: string) => void;
|
|
114
|
-
type DisconnectCallback = () => void;
|
|
115
|
-
type AccountChangedCallback = (publicKey: string) => void;
|
|
116
|
-
type PhantomEventCallback = ConnectCallback | DisconnectCallback | AccountChangedCallback;
|
|
117
|
-
|
|
118
|
-
type GetAccountResult = {
|
|
119
|
-
status: "connected";
|
|
116
|
+
declare function signMessage(message: Uint8Array, display?: DisplayEncoding): Promise<{
|
|
117
|
+
signature: Uint8Array;
|
|
120
118
|
address: string;
|
|
121
|
-
}
|
|
122
|
-
status: "disconnected";
|
|
123
|
-
address: null;
|
|
124
|
-
};
|
|
125
|
-
declare function getAccount(): GetAccountResult;
|
|
119
|
+
}>;
|
|
126
120
|
|
|
127
121
|
type Solana = {
|
|
128
|
-
getProvider: () => PhantomSolanaProvider | null;
|
|
129
122
|
connect: typeof connect;
|
|
130
123
|
disconnect: typeof disconnect;
|
|
131
124
|
getAccount: typeof getAccount;
|
package/dist/solana/index.js
CHANGED
|
@@ -16,6 +16,19 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __accessCheck = (obj, member, msg) => {
|
|
20
|
+
if (!member.has(obj))
|
|
21
|
+
throw TypeError("Cannot " + msg);
|
|
22
|
+
};
|
|
23
|
+
var __privateAdd = (obj, member, value) => {
|
|
24
|
+
if (member.has(obj))
|
|
25
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
26
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
27
|
+
};
|
|
28
|
+
var __privateMethod = (obj, member, method) => {
|
|
29
|
+
__accessCheck(obj, member, "access private method");
|
|
30
|
+
return method;
|
|
31
|
+
};
|
|
19
32
|
|
|
20
33
|
// src/solana/index.ts
|
|
21
34
|
var solana_exports = {};
|
|
@@ -24,9 +37,368 @@ __export(solana_exports, {
|
|
|
24
37
|
});
|
|
25
38
|
module.exports = __toCommonJS(solana_exports);
|
|
26
39
|
|
|
27
|
-
// src/solana/
|
|
28
|
-
|
|
29
|
-
|
|
40
|
+
// src/solana/utils/transactionToVersionedTransaction.ts
|
|
41
|
+
var import_transactions = require("@solana/transactions");
|
|
42
|
+
function transactionToVersionedTransaction(transaction) {
|
|
43
|
+
const serialized = (0, import_transactions.getTransactionEncoder)().encode(transaction);
|
|
44
|
+
const fakeVersioned = {
|
|
45
|
+
serialize() {
|
|
46
|
+
return new Uint8Array(serialized);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return fakeVersioned;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/solana/adapters/injected.ts
|
|
53
|
+
var import_compat = require("@solana/compat");
|
|
54
|
+
var MAX_RETRIES = 4;
|
|
55
|
+
var BASE_DELAY = 100;
|
|
56
|
+
var _getProvider, getProvider_fn;
|
|
57
|
+
var InjectedSolanaAdapter = class {
|
|
58
|
+
constructor() {
|
|
59
|
+
__privateAdd(this, _getProvider);
|
|
60
|
+
}
|
|
61
|
+
load() {
|
|
62
|
+
let retryCount = 0;
|
|
63
|
+
const scheduleRetry = (resolve, reject) => {
|
|
64
|
+
const delay = BASE_DELAY * Math.pow(2, Math.min(retryCount, 5));
|
|
65
|
+
setTimeout(() => {
|
|
66
|
+
if (__privateMethod(this, _getProvider, getProvider_fn).call(this)) {
|
|
67
|
+
resolve();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
retryCount++;
|
|
71
|
+
if (retryCount >= MAX_RETRIES) {
|
|
72
|
+
reject();
|
|
73
|
+
} else {
|
|
74
|
+
scheduleRetry(resolve, reject);
|
|
75
|
+
}
|
|
76
|
+
}, delay);
|
|
77
|
+
};
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
scheduleRetry(() => resolve(this), reject);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
get isConnected() {
|
|
83
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
84
|
+
return provider?.isConnected && provider.publicKey ? true : false;
|
|
85
|
+
}
|
|
86
|
+
async connect({ onlyIfTrusted }) {
|
|
87
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
88
|
+
if (!provider) {
|
|
89
|
+
throw new Error("Phantom provider not found.");
|
|
90
|
+
}
|
|
91
|
+
if (provider.isConnected && provider.publicKey) {
|
|
92
|
+
return this.getAccount() ?? void 0;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const result = await provider.connect({ onlyIfTrusted });
|
|
96
|
+
return result.publicKey.toString();
|
|
97
|
+
} catch (_) {
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async disconnect() {
|
|
102
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
103
|
+
if (!provider) {
|
|
104
|
+
throw new Error("Phantom provider not found.");
|
|
105
|
+
}
|
|
106
|
+
await provider.disconnect();
|
|
107
|
+
}
|
|
108
|
+
async getAccount() {
|
|
109
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
110
|
+
if (provider && provider.isConnected && provider.publicKey) {
|
|
111
|
+
return Promise.resolve(provider.publicKey.toString());
|
|
112
|
+
}
|
|
113
|
+
return Promise.resolve(void 0);
|
|
114
|
+
}
|
|
115
|
+
async signMessage(message, display) {
|
|
116
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
117
|
+
if (!provider) {
|
|
118
|
+
throw new Error("Phantom provider not found.");
|
|
119
|
+
}
|
|
120
|
+
if (!provider.isConnected) {
|
|
121
|
+
throw new Error("Provider is not connected.");
|
|
122
|
+
}
|
|
123
|
+
const result = await provider.signMessage(message, display);
|
|
124
|
+
return {
|
|
125
|
+
signature: result.signature,
|
|
126
|
+
address: result.publicKey.toString()
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
async signIn(signInData) {
|
|
130
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
131
|
+
if (!provider) {
|
|
132
|
+
throw new Error("Phantom provider not found.");
|
|
133
|
+
}
|
|
134
|
+
const result = await provider.signIn(signInData);
|
|
135
|
+
return {
|
|
136
|
+
address: result.address.toString(),
|
|
137
|
+
signature: result.signature,
|
|
138
|
+
signedMessage: result.signedMessage
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
async signAndSendTransaction(transaction) {
|
|
142
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
143
|
+
if (!provider) {
|
|
144
|
+
throw new Error("Phantom provider not found.");
|
|
145
|
+
}
|
|
146
|
+
if (!provider.isConnected) {
|
|
147
|
+
throw new Error("Provider is not connected.");
|
|
148
|
+
}
|
|
149
|
+
const versionedTransaction = transactionToVersionedTransaction(transaction);
|
|
150
|
+
const result = await provider.signAndSendTransaction(versionedTransaction);
|
|
151
|
+
return {
|
|
152
|
+
signature: result.signature,
|
|
153
|
+
address: result.publicKey
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async signTransaction(transaction) {
|
|
157
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
158
|
+
if (!provider) {
|
|
159
|
+
throw new Error("Phantom provider not found.");
|
|
160
|
+
}
|
|
161
|
+
if (!provider.isConnected) {
|
|
162
|
+
throw new Error("Provider is not connected.");
|
|
163
|
+
}
|
|
164
|
+
const versionedTransaction = transactionToVersionedTransaction(transaction);
|
|
165
|
+
const result = await provider.signTransaction(versionedTransaction);
|
|
166
|
+
const responseTransaction = (0, import_compat.fromVersionedTransaction)(result);
|
|
167
|
+
return responseTransaction;
|
|
168
|
+
}
|
|
169
|
+
async signAllTransactions(transactions) {
|
|
170
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
171
|
+
if (!provider) {
|
|
172
|
+
throw new Error("Phantom provider not found.");
|
|
173
|
+
}
|
|
174
|
+
if (!provider.isConnected) {
|
|
175
|
+
throw new Error("Provider is not connected.");
|
|
176
|
+
}
|
|
177
|
+
const versionedTransactions = transactions.map(
|
|
178
|
+
(transaction) => transactionToVersionedTransaction(transaction)
|
|
179
|
+
);
|
|
180
|
+
const result = await provider.signAllTransactions(versionedTransactions);
|
|
181
|
+
const responseTransactions = result.map((transaction) => (0, import_compat.fromVersionedTransaction)(transaction));
|
|
182
|
+
return responseTransactions;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
_getProvider = new WeakSet();
|
|
186
|
+
getProvider_fn = function() {
|
|
187
|
+
return window?.phantom?.solana;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/solana/adapters/kms.ts
|
|
191
|
+
var API_URL = "https://api.phantom.app/v1/wallet";
|
|
192
|
+
var _getJwtToken, getJwtToken_fn;
|
|
193
|
+
var KmsSolanaAdapter = class {
|
|
194
|
+
constructor() {
|
|
195
|
+
__privateAdd(this, _getJwtToken);
|
|
196
|
+
}
|
|
197
|
+
load() {
|
|
198
|
+
return Promise.resolve(this);
|
|
199
|
+
}
|
|
200
|
+
get isConnected() {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
async connect({ onlyIfTrusted }) {
|
|
204
|
+
return fetch(`${API_URL}/connect`, {
|
|
205
|
+
method: "POST",
|
|
206
|
+
headers: {
|
|
207
|
+
"Content-Type": "application/json",
|
|
208
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
209
|
+
},
|
|
210
|
+
body: JSON.stringify({
|
|
211
|
+
onlyIfTrusted
|
|
212
|
+
})
|
|
213
|
+
}).then((res) => res.json()).then((data) => data.publicKey);
|
|
214
|
+
}
|
|
215
|
+
async disconnect() {
|
|
216
|
+
const response = await fetch(`${API_URL}/disconnect`, {
|
|
217
|
+
method: "POST",
|
|
218
|
+
headers: {
|
|
219
|
+
"Content-Type": "application/json",
|
|
220
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
221
|
+
}
|
|
222
|
+
}).then((res) => res.json()).then((data) => data.disconnected);
|
|
223
|
+
if (!response) {
|
|
224
|
+
throw new Error("Failed to disconnect wallet.");
|
|
225
|
+
}
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
async getAccount() {
|
|
229
|
+
return fetch(`${API_URL}/account`, {
|
|
230
|
+
method: "GET",
|
|
231
|
+
headers: {
|
|
232
|
+
"Content-Type": "application/json",
|
|
233
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
234
|
+
}
|
|
235
|
+
}).then((res) => res.json()).then((data) => data.publicKey);
|
|
236
|
+
}
|
|
237
|
+
async signMessage(message, display) {
|
|
238
|
+
return fetch(`${API_URL}/sign-message`, {
|
|
239
|
+
method: "POST",
|
|
240
|
+
headers: {
|
|
241
|
+
"Content-Type": "application/json",
|
|
242
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
243
|
+
},
|
|
244
|
+
body: JSON.stringify({
|
|
245
|
+
message,
|
|
246
|
+
display
|
|
247
|
+
})
|
|
248
|
+
}).then((res) => res.json()).then((data) => {
|
|
249
|
+
return {
|
|
250
|
+
signature: new Uint8Array(Buffer.from(data.signature, "base64")),
|
|
251
|
+
address: data.publicKey
|
|
252
|
+
};
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
async signIn(signInData) {
|
|
256
|
+
return fetch(`${API_URL}/sign-in`, {
|
|
257
|
+
method: "POST",
|
|
258
|
+
headers: {
|
|
259
|
+
"Content-Type": "application/json",
|
|
260
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
261
|
+
},
|
|
262
|
+
body: JSON.stringify({
|
|
263
|
+
signInData
|
|
264
|
+
})
|
|
265
|
+
}).then((res) => res.json()).then((data) => {
|
|
266
|
+
return {
|
|
267
|
+
address: data.address,
|
|
268
|
+
signature: new Uint8Array(Buffer.from(data.signature, "base64")),
|
|
269
|
+
signedMessage: new Uint8Array(Buffer.from(data.signedMessage, "base64"))
|
|
270
|
+
};
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
async signAndSendTransaction(transaction) {
|
|
274
|
+
return fetch(`${API_URL}/sign-and-send-transaction`, {
|
|
275
|
+
method: "POST",
|
|
276
|
+
headers: {
|
|
277
|
+
"Content-Type": "application/json",
|
|
278
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
279
|
+
},
|
|
280
|
+
body: JSON.stringify({
|
|
281
|
+
transaction
|
|
282
|
+
})
|
|
283
|
+
}).then((res) => res.json()).then((data) => {
|
|
284
|
+
return {
|
|
285
|
+
signature: data.signature,
|
|
286
|
+
address: data.publicKey
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
async signTransaction(transaction) {
|
|
291
|
+
return fetch(`${API_URL}/sign-transaction`, {
|
|
292
|
+
method: "POST",
|
|
293
|
+
headers: {
|
|
294
|
+
"Content-Type": "application/json",
|
|
295
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
296
|
+
},
|
|
297
|
+
body: JSON.stringify({
|
|
298
|
+
transaction
|
|
299
|
+
})
|
|
300
|
+
}).then((res) => res.json()).then((data) => {
|
|
301
|
+
return data;
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
async signAllTransactions(transactions) {
|
|
305
|
+
return fetch(`${API_URL}/sign-all-transactions`, {
|
|
306
|
+
method: "POST",
|
|
307
|
+
headers: {
|
|
308
|
+
"Content-Type": "application/json",
|
|
309
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
310
|
+
},
|
|
311
|
+
body: JSON.stringify({
|
|
312
|
+
transactions
|
|
313
|
+
})
|
|
314
|
+
}).then((res) => res.json()).then((data) => {
|
|
315
|
+
return data;
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
_getJwtToken = new WeakSet();
|
|
320
|
+
getJwtToken_fn = function() {
|
|
321
|
+
return localStorage.getItem("phantom-solana-kms-jwt");
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
// src/solana/adapters/deeplinks.ts
|
|
325
|
+
var DeepLinkSolanaAdapter = class {
|
|
326
|
+
load() {
|
|
327
|
+
return Promise.resolve(this);
|
|
328
|
+
}
|
|
329
|
+
get isConnected() {
|
|
330
|
+
return true;
|
|
331
|
+
}
|
|
332
|
+
async connect({ onlyIfTrusted }) {
|
|
333
|
+
const deeplink = `phantom://connect?onlyIfTrusted=${onlyIfTrusted}`;
|
|
334
|
+
window.location.href = deeplink;
|
|
335
|
+
return Promise.resolve(void 0);
|
|
336
|
+
}
|
|
337
|
+
async disconnect() {
|
|
338
|
+
const deeplink = `phantom://disconnect`;
|
|
339
|
+
window.location.href = deeplink;
|
|
340
|
+
return Promise.resolve();
|
|
341
|
+
}
|
|
342
|
+
async getAccount() {
|
|
343
|
+
const deeplink = `phantom://account`;
|
|
344
|
+
window.location.href = deeplink;
|
|
345
|
+
return Promise.resolve(void 0);
|
|
346
|
+
}
|
|
347
|
+
async signMessage(message, display) {
|
|
348
|
+
const messageEncoded = Buffer.from(message).toString("base64");
|
|
349
|
+
const deeplink = `phantom://sign-message?message=${messageEncoded}&display=${display}`;
|
|
350
|
+
window.location.href = deeplink;
|
|
351
|
+
return Promise.resolve({
|
|
352
|
+
signature: new Uint8Array(),
|
|
353
|
+
address: ""
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
async signIn(signInData) {
|
|
357
|
+
const deeplink = `phantom://sign-in?signInData=${encodeURIComponent(JSON.stringify(signInData))}`;
|
|
358
|
+
window.location.href = deeplink;
|
|
359
|
+
return Promise.resolve({
|
|
360
|
+
address: "",
|
|
361
|
+
signature: new Uint8Array(),
|
|
362
|
+
signedMessage: new Uint8Array()
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
async signAndSendTransaction(transaction) {
|
|
366
|
+
const deeplink = `phantom://sign-and-send-transaction?transaction=${transaction}`;
|
|
367
|
+
window.location.href = deeplink;
|
|
368
|
+
return Promise.resolve({
|
|
369
|
+
signature: "",
|
|
370
|
+
address: ""
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
async signTransaction(transaction) {
|
|
374
|
+
const deeplink = `phantom://sign-transaction?transaction=${transaction}`;
|
|
375
|
+
window.location.href = deeplink;
|
|
376
|
+
return Promise.resolve(transaction);
|
|
377
|
+
}
|
|
378
|
+
async signAllTransactions(transactions) {
|
|
379
|
+
const deeplink = `phantom://sign-all-transactions?transactions=${transactions}`;
|
|
380
|
+
window.location.href = deeplink;
|
|
381
|
+
return Promise.resolve(transactions);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// src/solana/getAdapter.ts
|
|
386
|
+
async function getAdapter(type = "injected") {
|
|
387
|
+
if (type === "injected") {
|
|
388
|
+
const adapter = new InjectedSolanaAdapter();
|
|
389
|
+
try {
|
|
390
|
+
await adapter.load();
|
|
391
|
+
return adapter;
|
|
392
|
+
} catch (error) {
|
|
393
|
+
throw new Error("Phantom provider not found.");
|
|
394
|
+
}
|
|
395
|
+
} else if (type === "kms") {
|
|
396
|
+
return new KmsSolanaAdapter();
|
|
397
|
+
} else if (type === "deeplink") {
|
|
398
|
+
return new DeepLinkSolanaAdapter();
|
|
399
|
+
} else {
|
|
400
|
+
throw new Error("Invalid adapter type.");
|
|
401
|
+
}
|
|
30
402
|
}
|
|
31
403
|
|
|
32
404
|
// src/solana/eventListeners.ts
|
|
@@ -64,136 +436,94 @@ function triggerEvent(event, ...args) {
|
|
|
64
436
|
|
|
65
437
|
// src/solana/connect.ts
|
|
66
438
|
async function connect() {
|
|
67
|
-
const
|
|
68
|
-
if (!
|
|
439
|
+
const adapter = await getAdapter();
|
|
440
|
+
if (!adapter) {
|
|
69
441
|
throw new Error("Phantom provider not found.");
|
|
70
442
|
}
|
|
71
|
-
if (
|
|
72
|
-
return
|
|
443
|
+
if (adapter.isConnected) {
|
|
444
|
+
return adapter.getAccount();
|
|
73
445
|
}
|
|
74
446
|
try {
|
|
75
|
-
const
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return publicKeyStr;
|
|
447
|
+
const address = await adapter.connect({ onlyIfTrusted: true });
|
|
448
|
+
if (address) {
|
|
449
|
+
triggerEvent("connect", address);
|
|
450
|
+
return address;
|
|
80
451
|
}
|
|
81
452
|
} catch (error) {
|
|
82
453
|
}
|
|
83
454
|
try {
|
|
84
|
-
const
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return publicKeyStr;
|
|
455
|
+
const address = await adapter.connect({ onlyIfTrusted: false });
|
|
456
|
+
if (address) {
|
|
457
|
+
triggerEvent("connect", address);
|
|
458
|
+
return address;
|
|
89
459
|
}
|
|
90
460
|
} catch (error) {
|
|
91
461
|
}
|
|
92
462
|
throw new Error("Failed to connect to Phantom.");
|
|
93
463
|
}
|
|
94
464
|
|
|
95
|
-
// src/solana/
|
|
96
|
-
async function
|
|
97
|
-
const
|
|
98
|
-
if (!
|
|
99
|
-
throw new Error("Phantom provider not found.");
|
|
100
|
-
}
|
|
101
|
-
if (!provider.isConnected) {
|
|
102
|
-
await connect();
|
|
103
|
-
}
|
|
104
|
-
if (!provider.signMessage) {
|
|
105
|
-
throw new Error("The connected provider does not support signMessage.");
|
|
106
|
-
}
|
|
107
|
-
if (!provider.isConnected) {
|
|
108
|
-
throw new Error("Provider is not connected even after attempting to connect.");
|
|
109
|
-
}
|
|
110
|
-
const result = await provider.signMessage(message, display);
|
|
111
|
-
return {
|
|
112
|
-
signature: result.signature,
|
|
113
|
-
address: result.publicKey.toString()
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// src/solana/signIn.ts
|
|
118
|
-
async function signIn(signInData) {
|
|
119
|
-
const provider = getProvider();
|
|
120
|
-
if (!provider) {
|
|
465
|
+
// src/solana/disconnect.ts
|
|
466
|
+
async function disconnect() {
|
|
467
|
+
const adapter = await getAdapter();
|
|
468
|
+
if (!adapter) {
|
|
121
469
|
throw new Error("Phantom provider not found.");
|
|
122
470
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
const result = await provider.signIn(signInData);
|
|
127
|
-
return {
|
|
128
|
-
address: result.address.toString(),
|
|
129
|
-
signature: result.signature,
|
|
130
|
-
signedMessage: result.signedMessage
|
|
131
|
-
};
|
|
471
|
+
await adapter.disconnect();
|
|
472
|
+
triggerEvent("disconnect");
|
|
132
473
|
}
|
|
133
474
|
|
|
134
|
-
// src/solana/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const fakeVersioned = {
|
|
139
|
-
serialize() {
|
|
140
|
-
return new Uint8Array(serialized);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
return fakeVersioned;
|
|
475
|
+
// src/solana/getAccount.ts
|
|
476
|
+
async function getAccount() {
|
|
477
|
+
const adapter = await getAdapter();
|
|
478
|
+
return adapter.getAccount();
|
|
144
479
|
}
|
|
145
480
|
|
|
146
481
|
// src/solana/signAndSendTransaction.ts
|
|
482
|
+
var import_compat2 = require("@solana/compat");
|
|
147
483
|
async function signAndSendTransaction(transaction) {
|
|
148
|
-
const
|
|
149
|
-
if (!
|
|
150
|
-
throw new Error("
|
|
151
|
-
}
|
|
152
|
-
if (!provider.isConnected) {
|
|
153
|
-
await connect();
|
|
484
|
+
const adapter = await getAdapter();
|
|
485
|
+
if (!adapter) {
|
|
486
|
+
throw new Error("Adapter not found.");
|
|
154
487
|
}
|
|
155
|
-
if (!
|
|
156
|
-
|
|
488
|
+
if (!adapter.isConnected) {
|
|
489
|
+
await adapter.connect({ onlyIfTrusted: false });
|
|
157
490
|
}
|
|
158
|
-
|
|
159
|
-
|
|
491
|
+
let kitTransaction;
|
|
492
|
+
if (transaction.constructor.name === "VersionedTransaction") {
|
|
493
|
+
kitTransaction = (0, import_compat2.fromVersionedTransaction)(transaction);
|
|
494
|
+
} else {
|
|
495
|
+
kitTransaction = transaction;
|
|
160
496
|
}
|
|
161
|
-
|
|
162
|
-
const result = await provider.signAndSendTransaction(versionedTransaction);
|
|
163
|
-
return {
|
|
164
|
-
signature: result.signature,
|
|
165
|
-
address: result.publicKey
|
|
166
|
-
};
|
|
497
|
+
return adapter.signAndSendTransaction(kitTransaction);
|
|
167
498
|
}
|
|
168
499
|
|
|
169
|
-
// src/solana/
|
|
170
|
-
async function
|
|
171
|
-
const
|
|
172
|
-
if (!
|
|
173
|
-
throw new Error("
|
|
500
|
+
// src/solana/signIn.ts
|
|
501
|
+
async function signIn(signInData) {
|
|
502
|
+
const adapter = await getAdapter();
|
|
503
|
+
if (!adapter) {
|
|
504
|
+
throw new Error("Adapter not found.");
|
|
174
505
|
}
|
|
175
|
-
await
|
|
176
|
-
|
|
506
|
+
const result = await adapter.signIn(signInData);
|
|
507
|
+
if (result.address) {
|
|
508
|
+
triggerEvent("connect", result.address);
|
|
509
|
+
}
|
|
510
|
+
return result;
|
|
177
511
|
}
|
|
178
512
|
|
|
179
|
-
// src/solana/
|
|
180
|
-
function
|
|
181
|
-
const
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
status: "connected",
|
|
185
|
-
address: provider.publicKey.toString()
|
|
186
|
-
};
|
|
513
|
+
// src/solana/signMessage.ts
|
|
514
|
+
async function signMessage(message, display) {
|
|
515
|
+
const adapter = await getAdapter();
|
|
516
|
+
if (!adapter) {
|
|
517
|
+
throw new Error("Adapter not found.");
|
|
187
518
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
519
|
+
if (!adapter.isConnected) {
|
|
520
|
+
await adapter.connect({ onlyIfTrusted: false });
|
|
521
|
+
}
|
|
522
|
+
return adapter.signMessage(message, display);
|
|
192
523
|
}
|
|
193
524
|
|
|
194
525
|
// src/solana/plugin.ts
|
|
195
526
|
var solana = {
|
|
196
|
-
getProvider,
|
|
197
527
|
connect,
|
|
198
528
|
disconnect,
|
|
199
529
|
getAccount,
|
package/dist/solana/index.mjs
CHANGED
|
@@ -1,6 +1,370 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
__privateAdd,
|
|
3
|
+
__privateMethod
|
|
4
|
+
} from "../chunk-GV6AIHPN.mjs";
|
|
5
|
+
|
|
6
|
+
// src/solana/utils/transactionToVersionedTransaction.ts
|
|
7
|
+
import { getTransactionEncoder } from "@solana/transactions";
|
|
8
|
+
function transactionToVersionedTransaction(transaction) {
|
|
9
|
+
const serialized = getTransactionEncoder().encode(transaction);
|
|
10
|
+
const fakeVersioned = {
|
|
11
|
+
serialize() {
|
|
12
|
+
return new Uint8Array(serialized);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
return fakeVersioned;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/solana/adapters/injected.ts
|
|
19
|
+
import { fromVersionedTransaction } from "@solana/compat";
|
|
20
|
+
var MAX_RETRIES = 4;
|
|
21
|
+
var BASE_DELAY = 100;
|
|
22
|
+
var _getProvider, getProvider_fn;
|
|
23
|
+
var InjectedSolanaAdapter = class {
|
|
24
|
+
constructor() {
|
|
25
|
+
__privateAdd(this, _getProvider);
|
|
26
|
+
}
|
|
27
|
+
load() {
|
|
28
|
+
let retryCount = 0;
|
|
29
|
+
const scheduleRetry = (resolve, reject) => {
|
|
30
|
+
const delay = BASE_DELAY * Math.pow(2, Math.min(retryCount, 5));
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
if (__privateMethod(this, _getProvider, getProvider_fn).call(this)) {
|
|
33
|
+
resolve();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
retryCount++;
|
|
37
|
+
if (retryCount >= MAX_RETRIES) {
|
|
38
|
+
reject();
|
|
39
|
+
} else {
|
|
40
|
+
scheduleRetry(resolve, reject);
|
|
41
|
+
}
|
|
42
|
+
}, delay);
|
|
43
|
+
};
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
scheduleRetry(() => resolve(this), reject);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
get isConnected() {
|
|
49
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
50
|
+
return provider?.isConnected && provider.publicKey ? true : false;
|
|
51
|
+
}
|
|
52
|
+
async connect({ onlyIfTrusted }) {
|
|
53
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
54
|
+
if (!provider) {
|
|
55
|
+
throw new Error("Phantom provider not found.");
|
|
56
|
+
}
|
|
57
|
+
if (provider.isConnected && provider.publicKey) {
|
|
58
|
+
return this.getAccount() ?? void 0;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const result = await provider.connect({ onlyIfTrusted });
|
|
62
|
+
return result.publicKey.toString();
|
|
63
|
+
} catch (_) {
|
|
64
|
+
return void 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async disconnect() {
|
|
68
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
69
|
+
if (!provider) {
|
|
70
|
+
throw new Error("Phantom provider not found.");
|
|
71
|
+
}
|
|
72
|
+
await provider.disconnect();
|
|
73
|
+
}
|
|
74
|
+
async getAccount() {
|
|
75
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
76
|
+
if (provider && provider.isConnected && provider.publicKey) {
|
|
77
|
+
return Promise.resolve(provider.publicKey.toString());
|
|
78
|
+
}
|
|
79
|
+
return Promise.resolve(void 0);
|
|
80
|
+
}
|
|
81
|
+
async signMessage(message, display) {
|
|
82
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
83
|
+
if (!provider) {
|
|
84
|
+
throw new Error("Phantom provider not found.");
|
|
85
|
+
}
|
|
86
|
+
if (!provider.isConnected) {
|
|
87
|
+
throw new Error("Provider is not connected.");
|
|
88
|
+
}
|
|
89
|
+
const result = await provider.signMessage(message, display);
|
|
90
|
+
return {
|
|
91
|
+
signature: result.signature,
|
|
92
|
+
address: result.publicKey.toString()
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
async signIn(signInData) {
|
|
96
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
97
|
+
if (!provider) {
|
|
98
|
+
throw new Error("Phantom provider not found.");
|
|
99
|
+
}
|
|
100
|
+
const result = await provider.signIn(signInData);
|
|
101
|
+
return {
|
|
102
|
+
address: result.address.toString(),
|
|
103
|
+
signature: result.signature,
|
|
104
|
+
signedMessage: result.signedMessage
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async signAndSendTransaction(transaction) {
|
|
108
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
109
|
+
if (!provider) {
|
|
110
|
+
throw new Error("Phantom provider not found.");
|
|
111
|
+
}
|
|
112
|
+
if (!provider.isConnected) {
|
|
113
|
+
throw new Error("Provider is not connected.");
|
|
114
|
+
}
|
|
115
|
+
const versionedTransaction = transactionToVersionedTransaction(transaction);
|
|
116
|
+
const result = await provider.signAndSendTransaction(versionedTransaction);
|
|
117
|
+
return {
|
|
118
|
+
signature: result.signature,
|
|
119
|
+
address: result.publicKey
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
async signTransaction(transaction) {
|
|
123
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
124
|
+
if (!provider) {
|
|
125
|
+
throw new Error("Phantom provider not found.");
|
|
126
|
+
}
|
|
127
|
+
if (!provider.isConnected) {
|
|
128
|
+
throw new Error("Provider is not connected.");
|
|
129
|
+
}
|
|
130
|
+
const versionedTransaction = transactionToVersionedTransaction(transaction);
|
|
131
|
+
const result = await provider.signTransaction(versionedTransaction);
|
|
132
|
+
const responseTransaction = fromVersionedTransaction(result);
|
|
133
|
+
return responseTransaction;
|
|
134
|
+
}
|
|
135
|
+
async signAllTransactions(transactions) {
|
|
136
|
+
const provider = __privateMethod(this, _getProvider, getProvider_fn).call(this);
|
|
137
|
+
if (!provider) {
|
|
138
|
+
throw new Error("Phantom provider not found.");
|
|
139
|
+
}
|
|
140
|
+
if (!provider.isConnected) {
|
|
141
|
+
throw new Error("Provider is not connected.");
|
|
142
|
+
}
|
|
143
|
+
const versionedTransactions = transactions.map(
|
|
144
|
+
(transaction) => transactionToVersionedTransaction(transaction)
|
|
145
|
+
);
|
|
146
|
+
const result = await provider.signAllTransactions(versionedTransactions);
|
|
147
|
+
const responseTransactions = result.map((transaction) => fromVersionedTransaction(transaction));
|
|
148
|
+
return responseTransactions;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
_getProvider = new WeakSet();
|
|
152
|
+
getProvider_fn = function() {
|
|
153
|
+
return window?.phantom?.solana;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/solana/adapters/kms.ts
|
|
157
|
+
var API_URL = "https://api.phantom.app/v1/wallet";
|
|
158
|
+
var _getJwtToken, getJwtToken_fn;
|
|
159
|
+
var KmsSolanaAdapter = class {
|
|
160
|
+
constructor() {
|
|
161
|
+
__privateAdd(this, _getJwtToken);
|
|
162
|
+
}
|
|
163
|
+
load() {
|
|
164
|
+
return Promise.resolve(this);
|
|
165
|
+
}
|
|
166
|
+
get isConnected() {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
async connect({ onlyIfTrusted }) {
|
|
170
|
+
return fetch(`${API_URL}/connect`, {
|
|
171
|
+
method: "POST",
|
|
172
|
+
headers: {
|
|
173
|
+
"Content-Type": "application/json",
|
|
174
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
175
|
+
},
|
|
176
|
+
body: JSON.stringify({
|
|
177
|
+
onlyIfTrusted
|
|
178
|
+
})
|
|
179
|
+
}).then((res) => res.json()).then((data) => data.publicKey);
|
|
180
|
+
}
|
|
181
|
+
async disconnect() {
|
|
182
|
+
const response = await fetch(`${API_URL}/disconnect`, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
headers: {
|
|
185
|
+
"Content-Type": "application/json",
|
|
186
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
187
|
+
}
|
|
188
|
+
}).then((res) => res.json()).then((data) => data.disconnected);
|
|
189
|
+
if (!response) {
|
|
190
|
+
throw new Error("Failed to disconnect wallet.");
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
async getAccount() {
|
|
195
|
+
return fetch(`${API_URL}/account`, {
|
|
196
|
+
method: "GET",
|
|
197
|
+
headers: {
|
|
198
|
+
"Content-Type": "application/json",
|
|
199
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
200
|
+
}
|
|
201
|
+
}).then((res) => res.json()).then((data) => data.publicKey);
|
|
202
|
+
}
|
|
203
|
+
async signMessage(message, display) {
|
|
204
|
+
return fetch(`${API_URL}/sign-message`, {
|
|
205
|
+
method: "POST",
|
|
206
|
+
headers: {
|
|
207
|
+
"Content-Type": "application/json",
|
|
208
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
209
|
+
},
|
|
210
|
+
body: JSON.stringify({
|
|
211
|
+
message,
|
|
212
|
+
display
|
|
213
|
+
})
|
|
214
|
+
}).then((res) => res.json()).then((data) => {
|
|
215
|
+
return {
|
|
216
|
+
signature: new Uint8Array(Buffer.from(data.signature, "base64")),
|
|
217
|
+
address: data.publicKey
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
async signIn(signInData) {
|
|
222
|
+
return fetch(`${API_URL}/sign-in`, {
|
|
223
|
+
method: "POST",
|
|
224
|
+
headers: {
|
|
225
|
+
"Content-Type": "application/json",
|
|
226
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
227
|
+
},
|
|
228
|
+
body: JSON.stringify({
|
|
229
|
+
signInData
|
|
230
|
+
})
|
|
231
|
+
}).then((res) => res.json()).then((data) => {
|
|
232
|
+
return {
|
|
233
|
+
address: data.address,
|
|
234
|
+
signature: new Uint8Array(Buffer.from(data.signature, "base64")),
|
|
235
|
+
signedMessage: new Uint8Array(Buffer.from(data.signedMessage, "base64"))
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
async signAndSendTransaction(transaction) {
|
|
240
|
+
return fetch(`${API_URL}/sign-and-send-transaction`, {
|
|
241
|
+
method: "POST",
|
|
242
|
+
headers: {
|
|
243
|
+
"Content-Type": "application/json",
|
|
244
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
245
|
+
},
|
|
246
|
+
body: JSON.stringify({
|
|
247
|
+
transaction
|
|
248
|
+
})
|
|
249
|
+
}).then((res) => res.json()).then((data) => {
|
|
250
|
+
return {
|
|
251
|
+
signature: data.signature,
|
|
252
|
+
address: data.publicKey
|
|
253
|
+
};
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
async signTransaction(transaction) {
|
|
257
|
+
return fetch(`${API_URL}/sign-transaction`, {
|
|
258
|
+
method: "POST",
|
|
259
|
+
headers: {
|
|
260
|
+
"Content-Type": "application/json",
|
|
261
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
262
|
+
},
|
|
263
|
+
body: JSON.stringify({
|
|
264
|
+
transaction
|
|
265
|
+
})
|
|
266
|
+
}).then((res) => res.json()).then((data) => {
|
|
267
|
+
return data;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
async signAllTransactions(transactions) {
|
|
271
|
+
return fetch(`${API_URL}/sign-all-transactions`, {
|
|
272
|
+
method: "POST",
|
|
273
|
+
headers: {
|
|
274
|
+
"Content-Type": "application/json",
|
|
275
|
+
Authorization: `Bearer ${__privateMethod(this, _getJwtToken, getJwtToken_fn).call(this)}`
|
|
276
|
+
},
|
|
277
|
+
body: JSON.stringify({
|
|
278
|
+
transactions
|
|
279
|
+
})
|
|
280
|
+
}).then((res) => res.json()).then((data) => {
|
|
281
|
+
return data;
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
_getJwtToken = new WeakSet();
|
|
286
|
+
getJwtToken_fn = function() {
|
|
287
|
+
return localStorage.getItem("phantom-solana-kms-jwt");
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// src/solana/adapters/deeplinks.ts
|
|
291
|
+
var DeepLinkSolanaAdapter = class {
|
|
292
|
+
load() {
|
|
293
|
+
return Promise.resolve(this);
|
|
294
|
+
}
|
|
295
|
+
get isConnected() {
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
async connect({ onlyIfTrusted }) {
|
|
299
|
+
const deeplink = `phantom://connect?onlyIfTrusted=${onlyIfTrusted}`;
|
|
300
|
+
window.location.href = deeplink;
|
|
301
|
+
return Promise.resolve(void 0);
|
|
302
|
+
}
|
|
303
|
+
async disconnect() {
|
|
304
|
+
const deeplink = `phantom://disconnect`;
|
|
305
|
+
window.location.href = deeplink;
|
|
306
|
+
return Promise.resolve();
|
|
307
|
+
}
|
|
308
|
+
async getAccount() {
|
|
309
|
+
const deeplink = `phantom://account`;
|
|
310
|
+
window.location.href = deeplink;
|
|
311
|
+
return Promise.resolve(void 0);
|
|
312
|
+
}
|
|
313
|
+
async signMessage(message, display) {
|
|
314
|
+
const messageEncoded = Buffer.from(message).toString("base64");
|
|
315
|
+
const deeplink = `phantom://sign-message?message=${messageEncoded}&display=${display}`;
|
|
316
|
+
window.location.href = deeplink;
|
|
317
|
+
return Promise.resolve({
|
|
318
|
+
signature: new Uint8Array(),
|
|
319
|
+
address: ""
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
async signIn(signInData) {
|
|
323
|
+
const deeplink = `phantom://sign-in?signInData=${encodeURIComponent(JSON.stringify(signInData))}`;
|
|
324
|
+
window.location.href = deeplink;
|
|
325
|
+
return Promise.resolve({
|
|
326
|
+
address: "",
|
|
327
|
+
signature: new Uint8Array(),
|
|
328
|
+
signedMessage: new Uint8Array()
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
async signAndSendTransaction(transaction) {
|
|
332
|
+
const deeplink = `phantom://sign-and-send-transaction?transaction=${transaction}`;
|
|
333
|
+
window.location.href = deeplink;
|
|
334
|
+
return Promise.resolve({
|
|
335
|
+
signature: "",
|
|
336
|
+
address: ""
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
async signTransaction(transaction) {
|
|
340
|
+
const deeplink = `phantom://sign-transaction?transaction=${transaction}`;
|
|
341
|
+
window.location.href = deeplink;
|
|
342
|
+
return Promise.resolve(transaction);
|
|
343
|
+
}
|
|
344
|
+
async signAllTransactions(transactions) {
|
|
345
|
+
const deeplink = `phantom://sign-all-transactions?transactions=${transactions}`;
|
|
346
|
+
window.location.href = deeplink;
|
|
347
|
+
return Promise.resolve(transactions);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
// src/solana/getAdapter.ts
|
|
352
|
+
async function getAdapter(type = "injected") {
|
|
353
|
+
if (type === "injected") {
|
|
354
|
+
const adapter = new InjectedSolanaAdapter();
|
|
355
|
+
try {
|
|
356
|
+
await adapter.load();
|
|
357
|
+
return adapter;
|
|
358
|
+
} catch (error) {
|
|
359
|
+
throw new Error("Phantom provider not found.");
|
|
360
|
+
}
|
|
361
|
+
} else if (type === "kms") {
|
|
362
|
+
return new KmsSolanaAdapter();
|
|
363
|
+
} else if (type === "deeplink") {
|
|
364
|
+
return new DeepLinkSolanaAdapter();
|
|
365
|
+
} else {
|
|
366
|
+
throw new Error("Invalid adapter type.");
|
|
367
|
+
}
|
|
4
368
|
}
|
|
5
369
|
|
|
6
370
|
// src/solana/eventListeners.ts
|
|
@@ -38,136 +402,94 @@ function triggerEvent(event, ...args) {
|
|
|
38
402
|
|
|
39
403
|
// src/solana/connect.ts
|
|
40
404
|
async function connect() {
|
|
41
|
-
const
|
|
42
|
-
if (!
|
|
405
|
+
const adapter = await getAdapter();
|
|
406
|
+
if (!adapter) {
|
|
43
407
|
throw new Error("Phantom provider not found.");
|
|
44
408
|
}
|
|
45
|
-
if (
|
|
46
|
-
return
|
|
409
|
+
if (adapter.isConnected) {
|
|
410
|
+
return adapter.getAccount();
|
|
47
411
|
}
|
|
48
412
|
try {
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return publicKeyStr;
|
|
413
|
+
const address = await adapter.connect({ onlyIfTrusted: true });
|
|
414
|
+
if (address) {
|
|
415
|
+
triggerEvent("connect", address);
|
|
416
|
+
return address;
|
|
54
417
|
}
|
|
55
418
|
} catch (error) {
|
|
56
419
|
}
|
|
57
420
|
try {
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return publicKeyStr;
|
|
421
|
+
const address = await adapter.connect({ onlyIfTrusted: false });
|
|
422
|
+
if (address) {
|
|
423
|
+
triggerEvent("connect", address);
|
|
424
|
+
return address;
|
|
63
425
|
}
|
|
64
426
|
} catch (error) {
|
|
65
427
|
}
|
|
66
428
|
throw new Error("Failed to connect to Phantom.");
|
|
67
429
|
}
|
|
68
430
|
|
|
69
|
-
// src/solana/
|
|
70
|
-
async function
|
|
71
|
-
const
|
|
72
|
-
if (!
|
|
73
|
-
throw new Error("Phantom provider not found.");
|
|
74
|
-
}
|
|
75
|
-
if (!provider.isConnected) {
|
|
76
|
-
await connect();
|
|
77
|
-
}
|
|
78
|
-
if (!provider.signMessage) {
|
|
79
|
-
throw new Error("The connected provider does not support signMessage.");
|
|
80
|
-
}
|
|
81
|
-
if (!provider.isConnected) {
|
|
82
|
-
throw new Error("Provider is not connected even after attempting to connect.");
|
|
83
|
-
}
|
|
84
|
-
const result = await provider.signMessage(message, display);
|
|
85
|
-
return {
|
|
86
|
-
signature: result.signature,
|
|
87
|
-
address: result.publicKey.toString()
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// src/solana/signIn.ts
|
|
92
|
-
async function signIn(signInData) {
|
|
93
|
-
const provider = getProvider();
|
|
94
|
-
if (!provider) {
|
|
431
|
+
// src/solana/disconnect.ts
|
|
432
|
+
async function disconnect() {
|
|
433
|
+
const adapter = await getAdapter();
|
|
434
|
+
if (!adapter) {
|
|
95
435
|
throw new Error("Phantom provider not found.");
|
|
96
436
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
const result = await provider.signIn(signInData);
|
|
101
|
-
return {
|
|
102
|
-
address: result.address.toString(),
|
|
103
|
-
signature: result.signature,
|
|
104
|
-
signedMessage: result.signedMessage
|
|
105
|
-
};
|
|
437
|
+
await adapter.disconnect();
|
|
438
|
+
triggerEvent("disconnect");
|
|
106
439
|
}
|
|
107
440
|
|
|
108
|
-
// src/solana/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const fakeVersioned = {
|
|
113
|
-
serialize() {
|
|
114
|
-
return new Uint8Array(serialized);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
return fakeVersioned;
|
|
441
|
+
// src/solana/getAccount.ts
|
|
442
|
+
async function getAccount() {
|
|
443
|
+
const adapter = await getAdapter();
|
|
444
|
+
return adapter.getAccount();
|
|
118
445
|
}
|
|
119
446
|
|
|
120
447
|
// src/solana/signAndSendTransaction.ts
|
|
448
|
+
import { fromVersionedTransaction as fromVersionedTransaction2 } from "@solana/compat";
|
|
121
449
|
async function signAndSendTransaction(transaction) {
|
|
122
|
-
const
|
|
123
|
-
if (!
|
|
124
|
-
throw new Error("
|
|
125
|
-
}
|
|
126
|
-
if (!provider.isConnected) {
|
|
127
|
-
await connect();
|
|
450
|
+
const adapter = await getAdapter();
|
|
451
|
+
if (!adapter) {
|
|
452
|
+
throw new Error("Adapter not found.");
|
|
128
453
|
}
|
|
129
|
-
if (!
|
|
130
|
-
|
|
454
|
+
if (!adapter.isConnected) {
|
|
455
|
+
await adapter.connect({ onlyIfTrusted: false });
|
|
131
456
|
}
|
|
132
|
-
|
|
133
|
-
|
|
457
|
+
let kitTransaction;
|
|
458
|
+
if (transaction.constructor.name === "VersionedTransaction") {
|
|
459
|
+
kitTransaction = fromVersionedTransaction2(transaction);
|
|
460
|
+
} else {
|
|
461
|
+
kitTransaction = transaction;
|
|
134
462
|
}
|
|
135
|
-
|
|
136
|
-
const result = await provider.signAndSendTransaction(versionedTransaction);
|
|
137
|
-
return {
|
|
138
|
-
signature: result.signature,
|
|
139
|
-
address: result.publicKey
|
|
140
|
-
};
|
|
463
|
+
return adapter.signAndSendTransaction(kitTransaction);
|
|
141
464
|
}
|
|
142
465
|
|
|
143
|
-
// src/solana/
|
|
144
|
-
async function
|
|
145
|
-
const
|
|
146
|
-
if (!
|
|
147
|
-
throw new Error("
|
|
466
|
+
// src/solana/signIn.ts
|
|
467
|
+
async function signIn(signInData) {
|
|
468
|
+
const adapter = await getAdapter();
|
|
469
|
+
if (!adapter) {
|
|
470
|
+
throw new Error("Adapter not found.");
|
|
148
471
|
}
|
|
149
|
-
await
|
|
150
|
-
|
|
472
|
+
const result = await adapter.signIn(signInData);
|
|
473
|
+
if (result.address) {
|
|
474
|
+
triggerEvent("connect", result.address);
|
|
475
|
+
}
|
|
476
|
+
return result;
|
|
151
477
|
}
|
|
152
478
|
|
|
153
|
-
// src/solana/
|
|
154
|
-
function
|
|
155
|
-
const
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
status: "connected",
|
|
159
|
-
address: provider.publicKey.toString()
|
|
160
|
-
};
|
|
479
|
+
// src/solana/signMessage.ts
|
|
480
|
+
async function signMessage(message, display) {
|
|
481
|
+
const adapter = await getAdapter();
|
|
482
|
+
if (!adapter) {
|
|
483
|
+
throw new Error("Adapter not found.");
|
|
161
484
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
485
|
+
if (!adapter.isConnected) {
|
|
486
|
+
await adapter.connect({ onlyIfTrusted: false });
|
|
487
|
+
}
|
|
488
|
+
return adapter.signMessage(message, display);
|
|
166
489
|
}
|
|
167
490
|
|
|
168
491
|
// src/solana/plugin.ts
|
|
169
492
|
var solana = {
|
|
170
|
-
getProvider,
|
|
171
493
|
connect,
|
|
172
494
|
disconnect,
|
|
173
495
|
getAccount,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phantom/browser-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,15 +21,20 @@
|
|
|
21
21
|
],
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"scripts": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
24
|
+
"?pack-release": "When https://github.com/changesets/changesets/issues/432 has a solution we can remove this trick",
|
|
25
|
+
"pack-release": "rimraf ./_release && yarn pack && mkdir ./_release && tar zxvf ./package.tgz --directory ./_release && rm ./package.tgz",
|
|
26
|
+
"build": "rimraf ./dist && tsup src/index.ts src/solana/index.ts --format cjs,esm --dts",
|
|
27
|
+
"build:watch": "rimraf ./dist && tsup src/index.ts src/solana/index.ts --format cjs,esm --dts --watch",
|
|
28
|
+
"dev": "rimraf ./dist && tsup src/index.ts src/solana/index.ts --format cjs,esm --dts --watch",
|
|
27
29
|
"lint": "tsc --noEmit && eslint --cache . --ext .ts,.tsx",
|
|
28
30
|
"test": "jest"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"@solana/web3.js": "^1.98.2",
|
|
34
|
+
"@types/jest": "^29.5.14",
|
|
32
35
|
"eslint": "8.53.0",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"rimraf": "^6.0.1",
|
|
33
38
|
"tsup": "^6.7.0",
|
|
34
39
|
"typescript": "^5.0.4"
|
|
35
40
|
},
|
|
@@ -37,5 +42,8 @@
|
|
|
37
42
|
"@solana/compat": "2.1.1",
|
|
38
43
|
"@solana/kit": "^2.1.1",
|
|
39
44
|
"@solana/transactions": "^2.1.1"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"directory": "_release/package"
|
|
40
48
|
}
|
|
41
|
-
}
|
|
49
|
+
}
|