@phantom/react-sdk 0.3.9 → 1.0.0-beta.0
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 +401 -246
- package/dist/index.d.ts +62 -16
- package/dist/index.js +254 -81
- package/dist/index.mjs +249 -76
- package/package.json +4 -2
package/dist/index.mjs
CHANGED
|
@@ -75,9 +75,9 @@ function PhantomProvider({ children, config, debugConfig }) {
|
|
|
75
75
|
useEffect(() => {
|
|
76
76
|
if (!sdk)
|
|
77
77
|
return;
|
|
78
|
-
const initialize =
|
|
78
|
+
const initialize = () => {
|
|
79
79
|
try {
|
|
80
|
-
const available =
|
|
80
|
+
const available = BrowserSDK.isPhantomInstalled();
|
|
81
81
|
setIsPhantomAvailable(available);
|
|
82
82
|
} catch (err) {
|
|
83
83
|
console.error("Error checking Phantom extension:", err);
|
|
@@ -178,76 +178,6 @@ function useDisconnect() {
|
|
|
178
178
|
};
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
// src/hooks/useSignMessage.ts
|
|
182
|
-
import { useCallback as useCallback3, useState as useState3 } from "react";
|
|
183
|
-
function useSignMessage() {
|
|
184
|
-
const { sdk } = usePhantom();
|
|
185
|
-
const [isSigning, setIsSigning] = useState3(false);
|
|
186
|
-
const [error, setError] = useState3(null);
|
|
187
|
-
const signMessage = useCallback3(
|
|
188
|
-
async (params) => {
|
|
189
|
-
if (!sdk) {
|
|
190
|
-
throw new Error("SDK not initialized");
|
|
191
|
-
}
|
|
192
|
-
if (!sdk.isConnected()) {
|
|
193
|
-
throw new Error("Wallet not connected");
|
|
194
|
-
}
|
|
195
|
-
setIsSigning(true);
|
|
196
|
-
setError(null);
|
|
197
|
-
try {
|
|
198
|
-
const signature = await sdk.signMessage(params);
|
|
199
|
-
return signature;
|
|
200
|
-
} catch (err) {
|
|
201
|
-
setError(err);
|
|
202
|
-
throw err;
|
|
203
|
-
} finally {
|
|
204
|
-
setIsSigning(false);
|
|
205
|
-
}
|
|
206
|
-
},
|
|
207
|
-
[sdk]
|
|
208
|
-
);
|
|
209
|
-
return {
|
|
210
|
-
signMessage,
|
|
211
|
-
isSigning,
|
|
212
|
-
error
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// src/hooks/useSignAndSendTransaction.ts
|
|
217
|
-
import { useCallback as useCallback4, useState as useState4 } from "react";
|
|
218
|
-
function useSignAndSendTransaction() {
|
|
219
|
-
const { sdk } = usePhantom();
|
|
220
|
-
const [isSigning, setIsSigning] = useState4(false);
|
|
221
|
-
const [error, setError] = useState4(null);
|
|
222
|
-
const signAndSendTransaction = useCallback4(
|
|
223
|
-
async (params) => {
|
|
224
|
-
if (!sdk) {
|
|
225
|
-
throw new Error("SDK not initialized");
|
|
226
|
-
}
|
|
227
|
-
if (!sdk.isConnected()) {
|
|
228
|
-
throw new Error("Wallet not connected");
|
|
229
|
-
}
|
|
230
|
-
setIsSigning(true);
|
|
231
|
-
setError(null);
|
|
232
|
-
try {
|
|
233
|
-
const result = await sdk.signAndSendTransaction(params);
|
|
234
|
-
return result;
|
|
235
|
-
} catch (err) {
|
|
236
|
-
setError(err);
|
|
237
|
-
throw err;
|
|
238
|
-
} finally {
|
|
239
|
-
setIsSigning(false);
|
|
240
|
-
}
|
|
241
|
-
},
|
|
242
|
-
[sdk]
|
|
243
|
-
);
|
|
244
|
-
return {
|
|
245
|
-
signAndSendTransaction,
|
|
246
|
-
isSigning,
|
|
247
|
-
error
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
|
|
251
181
|
// src/hooks/useAccounts.ts
|
|
252
182
|
function useAccounts() {
|
|
253
183
|
const { addresses, isConnected } = usePhantom();
|
|
@@ -256,6 +186,7 @@ function useAccounts() {
|
|
|
256
186
|
|
|
257
187
|
// src/hooks/useIsExtensionInstalled.ts
|
|
258
188
|
import * as React from "react";
|
|
189
|
+
import { BrowserSDK as BrowserSDK2 } from "@phantom/browser-sdk";
|
|
259
190
|
var cachedIsInstalled = null;
|
|
260
191
|
function useIsExtensionInstalled() {
|
|
261
192
|
const { sdk } = usePhantom();
|
|
@@ -271,10 +202,10 @@ function useIsExtensionInstalled() {
|
|
|
271
202
|
setIsLoading(false);
|
|
272
203
|
return;
|
|
273
204
|
}
|
|
274
|
-
const checkExtension =
|
|
205
|
+
const checkExtension = () => {
|
|
275
206
|
try {
|
|
276
207
|
setIsLoading(true);
|
|
277
|
-
const result =
|
|
208
|
+
const result = BrowserSDK2.isPhantomInstalled();
|
|
278
209
|
cachedIsInstalled = result;
|
|
279
210
|
setIsInstalled(result);
|
|
280
211
|
} catch (error) {
|
|
@@ -289,6 +220,247 @@ function useIsExtensionInstalled() {
|
|
|
289
220
|
return { isLoading, isInstalled };
|
|
290
221
|
}
|
|
291
222
|
|
|
223
|
+
// src/hooks/useAutoConfirm.ts
|
|
224
|
+
import { useCallback as useCallback3, useState as useState4, useEffect as useEffect3 } from "react";
|
|
225
|
+
function useAutoConfirm() {
|
|
226
|
+
const { sdk, currentProviderType } = usePhantom();
|
|
227
|
+
const [status, setStatus] = useState4(null);
|
|
228
|
+
const [supportedChains, setSupportedChains] = useState4(null);
|
|
229
|
+
const [isLoading, setIsLoading] = useState4(false);
|
|
230
|
+
const [error, setError] = useState4(null);
|
|
231
|
+
const isInjected = currentProviderType === "injected";
|
|
232
|
+
const enable = useCallback3(
|
|
233
|
+
async (params) => {
|
|
234
|
+
if (!sdk) {
|
|
235
|
+
throw new Error("SDK not initialized");
|
|
236
|
+
}
|
|
237
|
+
if (!isInjected) {
|
|
238
|
+
throw new Error("Auto-confirm is only available for injected (extension) providers");
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
setIsLoading(true);
|
|
242
|
+
setError(null);
|
|
243
|
+
const result = await sdk.enableAutoConfirm(params);
|
|
244
|
+
setStatus(result);
|
|
245
|
+
return result;
|
|
246
|
+
} catch (err) {
|
|
247
|
+
const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
|
|
248
|
+
setError(error2);
|
|
249
|
+
throw error2;
|
|
250
|
+
} finally {
|
|
251
|
+
setIsLoading(false);
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
[sdk, isInjected]
|
|
255
|
+
);
|
|
256
|
+
const disable = useCallback3(
|
|
257
|
+
async () => {
|
|
258
|
+
if (!sdk) {
|
|
259
|
+
throw new Error("SDK not initialized");
|
|
260
|
+
}
|
|
261
|
+
if (!isInjected) {
|
|
262
|
+
throw new Error("Auto-confirm is only available for injected (extension) providers");
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
setIsLoading(true);
|
|
266
|
+
setError(null);
|
|
267
|
+
await sdk.disableAutoConfirm();
|
|
268
|
+
const newStatus = await sdk.getAutoConfirmStatus();
|
|
269
|
+
setStatus(newStatus);
|
|
270
|
+
} catch (err) {
|
|
271
|
+
const error2 = err instanceof Error ? err : new Error("Unknown error occurred");
|
|
272
|
+
setError(error2);
|
|
273
|
+
throw error2;
|
|
274
|
+
} finally {
|
|
275
|
+
setIsLoading(false);
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
[sdk, isInjected]
|
|
279
|
+
);
|
|
280
|
+
const refetch = useCallback3(
|
|
281
|
+
async () => {
|
|
282
|
+
if (!sdk || !isInjected) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
setIsLoading(true);
|
|
287
|
+
setError(null);
|
|
288
|
+
const [statusResult, supportedResult] = await Promise.all([
|
|
289
|
+
sdk.getAutoConfirmStatus(),
|
|
290
|
+
sdk.getSupportedAutoConfirmChains()
|
|
291
|
+
]);
|
|
292
|
+
setStatus(statusResult);
|
|
293
|
+
setSupportedChains(supportedResult);
|
|
294
|
+
} catch (err) {
|
|
295
|
+
const error2 = err instanceof Error ? err : new Error("Failed to fetch auto-confirm data");
|
|
296
|
+
setError(error2);
|
|
297
|
+
} finally {
|
|
298
|
+
setIsLoading(false);
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
[sdk, isInjected]
|
|
302
|
+
);
|
|
303
|
+
useEffect3(() => {
|
|
304
|
+
if (sdk && isInjected) {
|
|
305
|
+
refetch();
|
|
306
|
+
} else {
|
|
307
|
+
setStatus(null);
|
|
308
|
+
setSupportedChains(null);
|
|
309
|
+
setError(null);
|
|
310
|
+
}
|
|
311
|
+
}, [sdk, isInjected, refetch]);
|
|
312
|
+
return {
|
|
313
|
+
enable,
|
|
314
|
+
disable,
|
|
315
|
+
status,
|
|
316
|
+
supportedChains,
|
|
317
|
+
isLoading,
|
|
318
|
+
error,
|
|
319
|
+
refetch
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// src/hooks/useSolana.ts
|
|
324
|
+
import { useCallback as useCallback4, useMemo as useMemo2 } from "react";
|
|
325
|
+
function useSolana() {
|
|
326
|
+
const { sdk, isConnected } = usePhantom();
|
|
327
|
+
const solanaChain = useMemo2(() => {
|
|
328
|
+
if (!sdk || !isConnected)
|
|
329
|
+
return null;
|
|
330
|
+
try {
|
|
331
|
+
return sdk.solana;
|
|
332
|
+
} catch {
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}, [sdk, isConnected]);
|
|
336
|
+
const signMessage = useCallback4(async (message) => {
|
|
337
|
+
if (!solanaChain)
|
|
338
|
+
throw new Error("Solana chain not available. Ensure SDK is connected.");
|
|
339
|
+
return solanaChain.signMessage(message);
|
|
340
|
+
}, [solanaChain]);
|
|
341
|
+
const signTransaction = useCallback4(async (transaction) => {
|
|
342
|
+
if (!solanaChain)
|
|
343
|
+
throw new Error("Solana chain not available. Ensure SDK is connected.");
|
|
344
|
+
return solanaChain.signTransaction(transaction);
|
|
345
|
+
}, [solanaChain]);
|
|
346
|
+
const signAndSendTransaction = useCallback4(async (transaction) => {
|
|
347
|
+
if (!solanaChain)
|
|
348
|
+
throw new Error("Solana chain not available. Ensure SDK is connected.");
|
|
349
|
+
return solanaChain.signAndSendTransaction(transaction);
|
|
350
|
+
}, [solanaChain]);
|
|
351
|
+
const connect = useCallback4(async (options) => {
|
|
352
|
+
if (!solanaChain)
|
|
353
|
+
throw new Error("Solana chain not available. Ensure SDK is connected.");
|
|
354
|
+
return solanaChain.connect(options);
|
|
355
|
+
}, [solanaChain]);
|
|
356
|
+
const disconnect = useCallback4(async () => {
|
|
357
|
+
if (!solanaChain)
|
|
358
|
+
throw new Error("Solana chain not available. Ensure SDK is connected.");
|
|
359
|
+
return solanaChain.disconnect();
|
|
360
|
+
}, [solanaChain]);
|
|
361
|
+
const switchNetwork = useCallback4(async (network) => {
|
|
362
|
+
if (!solanaChain)
|
|
363
|
+
throw new Error("Solana chain not available. Ensure SDK is connected.");
|
|
364
|
+
return solanaChain.switchNetwork(network);
|
|
365
|
+
}, [solanaChain]);
|
|
366
|
+
const getPublicKey = useCallback4(async () => {
|
|
367
|
+
if (!solanaChain)
|
|
368
|
+
return null;
|
|
369
|
+
return solanaChain.getPublicKey();
|
|
370
|
+
}, [solanaChain]);
|
|
371
|
+
return {
|
|
372
|
+
// Chain instance for advanced usage
|
|
373
|
+
solana: solanaChain,
|
|
374
|
+
// Convenient methods
|
|
375
|
+
signMessage,
|
|
376
|
+
signTransaction,
|
|
377
|
+
signAndSendTransaction,
|
|
378
|
+
connect,
|
|
379
|
+
disconnect,
|
|
380
|
+
switchNetwork,
|
|
381
|
+
getPublicKey,
|
|
382
|
+
// State
|
|
383
|
+
isAvailable: !!solanaChain,
|
|
384
|
+
isConnected: solanaChain?.isConnected() ?? false
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// src/hooks/useEthereum.ts
|
|
389
|
+
import { useCallback as useCallback5, useMemo as useMemo3 } from "react";
|
|
390
|
+
function useEthereum() {
|
|
391
|
+
const { sdk, isConnected } = usePhantom();
|
|
392
|
+
const ethereumChain = useMemo3(() => {
|
|
393
|
+
if (!sdk || !isConnected)
|
|
394
|
+
return null;
|
|
395
|
+
try {
|
|
396
|
+
return sdk.ethereum;
|
|
397
|
+
} catch {
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
}, [sdk, isConnected]);
|
|
401
|
+
const request = useCallback5(async (args) => {
|
|
402
|
+
if (!ethereumChain)
|
|
403
|
+
throw new Error("Ethereum chain not available. Ensure SDK is connected.");
|
|
404
|
+
return ethereumChain.request(args);
|
|
405
|
+
}, [ethereumChain]);
|
|
406
|
+
const signPersonalMessage = useCallback5(async (message, address) => {
|
|
407
|
+
return request({
|
|
408
|
+
method: "personal_sign",
|
|
409
|
+
params: [message, address]
|
|
410
|
+
});
|
|
411
|
+
}, [request]);
|
|
412
|
+
const sendTransaction = useCallback5(async (transaction) => {
|
|
413
|
+
if (!ethereumChain)
|
|
414
|
+
throw new Error("Ethereum chain not available. Ensure SDK is connected.");
|
|
415
|
+
return ethereumChain.sendTransaction(transaction);
|
|
416
|
+
}, [ethereumChain]);
|
|
417
|
+
const switchChain = useCallback5(async (chainId) => {
|
|
418
|
+
if (!ethereumChain)
|
|
419
|
+
throw new Error("Ethereum chain not available. Ensure SDK is connected.");
|
|
420
|
+
return ethereumChain.switchChain(chainId);
|
|
421
|
+
}, [ethereumChain]);
|
|
422
|
+
const getChainId = useCallback5(async () => {
|
|
423
|
+
if (!ethereumChain)
|
|
424
|
+
throw new Error("Ethereum chain not available. Ensure SDK is connected.");
|
|
425
|
+
return ethereumChain.getChainId();
|
|
426
|
+
}, [ethereumChain]);
|
|
427
|
+
const getAccounts = useCallback5(async () => {
|
|
428
|
+
if (!ethereumChain)
|
|
429
|
+
throw new Error("Ethereum chain not available. Ensure SDK is connected.");
|
|
430
|
+
return ethereumChain.getAccounts();
|
|
431
|
+
}, [ethereumChain]);
|
|
432
|
+
const signMessage = useCallback5(async (message) => {
|
|
433
|
+
return request({
|
|
434
|
+
method: "eth_sign",
|
|
435
|
+
params: [await getAccounts().then((accounts) => accounts[0]), message]
|
|
436
|
+
});
|
|
437
|
+
}, [request, getAccounts]);
|
|
438
|
+
const signTypedData = useCallback5(async (typedData) => {
|
|
439
|
+
const accounts = await getAccounts();
|
|
440
|
+
return request({
|
|
441
|
+
method: "eth_signTypedData_v4",
|
|
442
|
+
params: [accounts[0], JSON.stringify(typedData)]
|
|
443
|
+
});
|
|
444
|
+
}, [request, getAccounts]);
|
|
445
|
+
return {
|
|
446
|
+
// Chain instance for advanced usage
|
|
447
|
+
ethereum: ethereumChain,
|
|
448
|
+
// Standard EIP-1193 interface
|
|
449
|
+
request,
|
|
450
|
+
// Convenient methods
|
|
451
|
+
signPersonalMessage,
|
|
452
|
+
signMessage,
|
|
453
|
+
signTypedData,
|
|
454
|
+
sendTransaction,
|
|
455
|
+
switchChain,
|
|
456
|
+
getChainId,
|
|
457
|
+
getAccounts,
|
|
458
|
+
// State
|
|
459
|
+
isAvailable: !!ethereumChain,
|
|
460
|
+
isConnected: ethereumChain?.isConnected() ?? false
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
292
464
|
// src/index.ts
|
|
293
465
|
import { NetworkId, AddressType, DebugLevel, debug } from "@phantom/browser-sdk";
|
|
294
466
|
export {
|
|
@@ -298,10 +470,11 @@ export {
|
|
|
298
470
|
PhantomProvider,
|
|
299
471
|
debug,
|
|
300
472
|
useAccounts,
|
|
473
|
+
useAutoConfirm,
|
|
301
474
|
useConnect,
|
|
302
475
|
useDisconnect,
|
|
476
|
+
useEthereum,
|
|
303
477
|
useIsExtensionInstalled,
|
|
304
478
|
usePhantom,
|
|
305
|
-
|
|
306
|
-
useSignMessage
|
|
479
|
+
useSolana
|
|
307
480
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phantom/react-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
"prettier": "prettier --write \"src/**/*.{ts,tsx}\""
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@phantom/browser-sdk": "^0.
|
|
29
|
+
"@phantom/browser-sdk": "^1.0.0-beta.0",
|
|
30
|
+
"@phantom/chains": "^1.0.0-beta.0",
|
|
31
|
+
"@phantom/constants": "^1.0.0-beta.0"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|
|
32
34
|
"@testing-library/dom": "^10.4.0",
|