@chipi-stack/chipi-react 14.1.1 → 14.2.1
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/dist/hooks.d.mts +9 -1
- package/dist/hooks.d.ts +9 -1
- package/dist/hooks.js +106 -1
- package/dist/hooks.js.map +1 -1
- package/dist/hooks.mjs +106 -1
- package/dist/hooks.mjs.map +1 -1
- package/dist/index.js +106 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +106 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/hooks.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
|
|
1
|
+
import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, SpendingPolicyConfig, SpendingPolicyData, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
|
|
2
2
|
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
3
3
|
import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
|
|
4
4
|
|
|
@@ -363,11 +363,19 @@ interface UseChipiSessionReturn {
|
|
|
363
363
|
clearSession: () => void;
|
|
364
364
|
/** Refresh the on-chain session status */
|
|
365
365
|
refetchStatus: () => Promise<void>;
|
|
366
|
+
/** Set a spending policy for the current session + token pair */
|
|
367
|
+
setSpendingPolicy: (config: Omit<SpendingPolicyConfig, "sessionPublicKey">) => Promise<string>;
|
|
368
|
+
/** Query the spending policy for a token (read-only, no gas) */
|
|
369
|
+
getSpendingPolicy: (token: string) => Promise<SpendingPolicyData>;
|
|
370
|
+
/** Remove a spending policy for a token */
|
|
371
|
+
removeSpendingPolicy: (token: string) => Promise<string>;
|
|
366
372
|
isCreating: boolean;
|
|
367
373
|
isRegistering: boolean;
|
|
368
374
|
isRevoking: boolean;
|
|
369
375
|
isExecuting: boolean;
|
|
370
376
|
isLoadingStatus: boolean;
|
|
377
|
+
isSettingSpendingPolicy: boolean;
|
|
378
|
+
isRemovingSpendingPolicy: boolean;
|
|
371
379
|
error: Error | null;
|
|
372
380
|
}
|
|
373
381
|
/**
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
|
|
1
|
+
import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, SpendingPolicyConfig, SpendingPolicyData, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
|
|
2
2
|
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
3
3
|
import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
|
|
4
4
|
|
|
@@ -363,11 +363,19 @@ interface UseChipiSessionReturn {
|
|
|
363
363
|
clearSession: () => void;
|
|
364
364
|
/** Refresh the on-chain session status */
|
|
365
365
|
refetchStatus: () => Promise<void>;
|
|
366
|
+
/** Set a spending policy for the current session + token pair */
|
|
367
|
+
setSpendingPolicy: (config: Omit<SpendingPolicyConfig, "sessionPublicKey">) => Promise<string>;
|
|
368
|
+
/** Query the spending policy for a token (read-only, no gas) */
|
|
369
|
+
getSpendingPolicy: (token: string) => Promise<SpendingPolicyData>;
|
|
370
|
+
/** Remove a spending policy for a token */
|
|
371
|
+
removeSpendingPolicy: (token: string) => Promise<string>;
|
|
366
372
|
isCreating: boolean;
|
|
367
373
|
isRegistering: boolean;
|
|
368
374
|
isRevoking: boolean;
|
|
369
375
|
isExecuting: boolean;
|
|
370
376
|
isLoadingStatus: boolean;
|
|
377
|
+
isSettingSpendingPolicy: boolean;
|
|
378
|
+
isRemovingSpendingPolicy: boolean;
|
|
371
379
|
error: Error | null;
|
|
372
380
|
}
|
|
373
381
|
/**
|
package/dist/hooks.js
CHANGED
|
@@ -402,6 +402,61 @@ function useChipiSession(config) {
|
|
|
402
402
|
setError(err);
|
|
403
403
|
}
|
|
404
404
|
});
|
|
405
|
+
const setSpendingPolicyMutation = reactQuery.useMutation({
|
|
406
|
+
mutationFn: async (policyConfig) => {
|
|
407
|
+
if (!wallet) throw new Error("Wallet is required");
|
|
408
|
+
if (!localSession)
|
|
409
|
+
throw new Error("No session to set policy on. Create and register a session first.");
|
|
410
|
+
if (!encryptKey) throw new Error("Encryption key (PIN) is required");
|
|
411
|
+
if (!supportsSession) throw new Error("Wallet does not support sessions");
|
|
412
|
+
const bearerToken = await getBearerToken();
|
|
413
|
+
if (!bearerToken) throw new Error("Bearer token is required");
|
|
414
|
+
const txHash = await chipiSDK.sessions.setSpendingPolicy(
|
|
415
|
+
{
|
|
416
|
+
encryptKey,
|
|
417
|
+
wallet,
|
|
418
|
+
spendingPolicyConfig: {
|
|
419
|
+
sessionPublicKey: localSession.publicKey,
|
|
420
|
+
...policyConfig
|
|
421
|
+
}
|
|
422
|
+
},
|
|
423
|
+
bearerToken
|
|
424
|
+
);
|
|
425
|
+
return txHash;
|
|
426
|
+
},
|
|
427
|
+
onSuccess: () => {
|
|
428
|
+
setError(null);
|
|
429
|
+
},
|
|
430
|
+
onError: (err) => {
|
|
431
|
+
setError(err);
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
const removeSpendingPolicyMutation = reactQuery.useMutation({
|
|
435
|
+
mutationFn: async (token) => {
|
|
436
|
+
if (!wallet) throw new Error("Wallet is required");
|
|
437
|
+
if (!localSession) throw new Error("No session to remove policy from");
|
|
438
|
+
if (!encryptKey) throw new Error("Encryption key (PIN) is required");
|
|
439
|
+
if (!supportsSession) throw new Error("Wallet does not support sessions");
|
|
440
|
+
const bearerToken = await getBearerToken();
|
|
441
|
+
if (!bearerToken) throw new Error("Bearer token is required");
|
|
442
|
+
const txHash = await chipiSDK.sessions.removeSpendingPolicy(
|
|
443
|
+
{
|
|
444
|
+
encryptKey,
|
|
445
|
+
wallet,
|
|
446
|
+
sessionPublicKey: localSession.publicKey,
|
|
447
|
+
token
|
|
448
|
+
},
|
|
449
|
+
bearerToken
|
|
450
|
+
);
|
|
451
|
+
return txHash;
|
|
452
|
+
},
|
|
453
|
+
onSuccess: () => {
|
|
454
|
+
setError(null);
|
|
455
|
+
},
|
|
456
|
+
onError: (err) => {
|
|
457
|
+
setError(err);
|
|
458
|
+
}
|
|
459
|
+
});
|
|
405
460
|
const createSession = react.useCallback(
|
|
406
461
|
async (params) => {
|
|
407
462
|
return createSessionMutation.mutateAsync(params ?? {});
|
|
@@ -430,14 +485,40 @@ function useChipiSession(config) {
|
|
|
430
485
|
const refetchStatus = react.useCallback(async () => {
|
|
431
486
|
await sessionStatusQuery.refetch();
|
|
432
487
|
}, [sessionStatusQuery]);
|
|
488
|
+
const setSpendingPolicy = react.useCallback(
|
|
489
|
+
async (policyConfig) => {
|
|
490
|
+
return setSpendingPolicyMutation.mutateAsync(policyConfig);
|
|
491
|
+
},
|
|
492
|
+
[setSpendingPolicyMutation]
|
|
493
|
+
);
|
|
494
|
+
const getSpendingPolicy = react.useCallback(
|
|
495
|
+
async (token) => {
|
|
496
|
+
if (!wallet?.publicKey) throw new Error("Wallet is required");
|
|
497
|
+
if (!localSession?.publicKey) throw new Error("No session to query policy for");
|
|
498
|
+
return chipiSDK.sessions.getSpendingPolicy({
|
|
499
|
+
walletAddress: wallet.publicKey,
|
|
500
|
+
sessionPublicKey: localSession.publicKey,
|
|
501
|
+
token
|
|
502
|
+
});
|
|
503
|
+
},
|
|
504
|
+
[chipiSDK, wallet, localSession]
|
|
505
|
+
);
|
|
506
|
+
const removeSpendingPolicy = react.useCallback(
|
|
507
|
+
async (token) => {
|
|
508
|
+
return removeSpendingPolicyMutation.mutateAsync(token);
|
|
509
|
+
},
|
|
510
|
+
[removeSpendingPolicyMutation]
|
|
511
|
+
);
|
|
433
512
|
const combinedError = react.useMemo(() => {
|
|
434
|
-
return error || createSessionMutation.error || registerSessionMutation.error || revokeSessionMutation.error || executeWithSessionMutation.error || sessionStatusQuery.error || null;
|
|
513
|
+
return error || createSessionMutation.error || registerSessionMutation.error || revokeSessionMutation.error || executeWithSessionMutation.error || setSpendingPolicyMutation.error || removeSpendingPolicyMutation.error || sessionStatusQuery.error || null;
|
|
435
514
|
}, [
|
|
436
515
|
error,
|
|
437
516
|
createSessionMutation.error,
|
|
438
517
|
registerSessionMutation.error,
|
|
439
518
|
revokeSessionMutation.error,
|
|
440
519
|
executeWithSessionMutation.error,
|
|
520
|
+
setSpendingPolicyMutation.error,
|
|
521
|
+
removeSpendingPolicyMutation.error,
|
|
441
522
|
sessionStatusQuery.error
|
|
442
523
|
]);
|
|
443
524
|
return {
|
|
@@ -456,18 +537,25 @@ function useChipiSession(config) {
|
|
|
456
537
|
executeWithSession,
|
|
457
538
|
clearSession,
|
|
458
539
|
refetchStatus,
|
|
540
|
+
// Spending Policy Actions
|
|
541
|
+
setSpendingPolicy,
|
|
542
|
+
getSpendingPolicy,
|
|
543
|
+
removeSpendingPolicy,
|
|
459
544
|
// Loading states
|
|
460
545
|
isCreating: createSessionMutation.isPending,
|
|
461
546
|
isRegistering: registerSessionMutation.isPending,
|
|
462
547
|
isRevoking: revokeSessionMutation.isPending,
|
|
463
548
|
isExecuting: executeWithSessionMutation.isPending,
|
|
464
549
|
isLoadingStatus: sessionStatusQuery.isLoading,
|
|
550
|
+
isSettingSpendingPolicy: setSpendingPolicyMutation.isPending,
|
|
551
|
+
isRemovingSpendingPolicy: removeSpendingPolicyMutation.isPending,
|
|
465
552
|
// Errors
|
|
466
553
|
error: combinedError
|
|
467
554
|
};
|
|
468
555
|
}
|
|
469
556
|
function useMigrateWalletToPasskey() {
|
|
470
557
|
const { chipiSDK } = useChipiContext();
|
|
558
|
+
const queryClient = reactQuery.useQueryClient();
|
|
471
559
|
const mutation = reactQuery.useMutation({
|
|
472
560
|
mutationFn: async (input) => {
|
|
473
561
|
const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
|
|
@@ -489,10 +577,27 @@ function useMigrateWalletToPasskey() {
|
|
|
489
577
|
decryptedPrivateKey,
|
|
490
578
|
passkeyResult.encryptKey
|
|
491
579
|
);
|
|
580
|
+
const updateResult = await chipiSDK.wallets.updateWalletEncryption(
|
|
581
|
+
{
|
|
582
|
+
externalUserId,
|
|
583
|
+
newEncryptedPrivateKey,
|
|
584
|
+
publicKey: wallet.publicKey
|
|
585
|
+
},
|
|
586
|
+
bearerToken
|
|
587
|
+
);
|
|
588
|
+
if (updateResult?.success === false) {
|
|
589
|
+
throw new Error("Backend rejected wallet encryption update");
|
|
590
|
+
}
|
|
492
591
|
const updatedWallet = {
|
|
493
592
|
...wallet,
|
|
494
593
|
encryptedPrivateKey: newEncryptedPrivateKey
|
|
495
594
|
};
|
|
595
|
+
queryClient.invalidateQueries({
|
|
596
|
+
queryKey: ["chipi-wallet", externalUserId]
|
|
597
|
+
});
|
|
598
|
+
queryClient.invalidateQueries({
|
|
599
|
+
queryKey: ["wallet", externalUserId]
|
|
600
|
+
});
|
|
496
601
|
return {
|
|
497
602
|
success: true,
|
|
498
603
|
wallet: updatedWallet,
|