@chipi-stack/chipi-react 14.4.0 → 14.6.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/dist/hooks.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
2
2
  import { createContext, useMemo, useCallback, useState, useEffect, useRef, useContext } from 'react';
3
- import { decryptPrivateKey, encryptPrivateKey } from '@chipi-stack/backend';
3
+ import { decryptPrivateKey, encryptPrivateKey, buildJwtAppleSubEnvelopeFromTokenResponse, buildWebAuthnEnvelopeFromAssertion, buildEip191EnvelopeFromSignature, buildEd25519Envelope, buildStarkEnvelope, OP_SET_THRESHOLD, OP_ROTATE_OWNER, OP_REMOVE_OWNER, OP_ADD_OWNER, ROLE_GUARDIAN, ROLE_OWNER, DEFAULT_OP_EXPIRY_SECONDS, TIMELOCK_SECONDS, secondsUntilExecutable, isReadyToExecute, buildFinalizeRecoveryCall, buildCancelRecoveryCall, buildInitiateRecoveryCall, buildCancelPendingOpCall, buildExecuteSetThresholdCall, buildProposeSetThresholdCall, buildExecuteRotateOwnerCall, buildProposeRotateOwnerCall, buildExecuteRemoveOwnerCall, buildProposeRemoveOwnerCall, buildExecuteAddOwnerCall, buildProposeAddOwnerCall, assertValidV2Snip12Envelope, buildThresholdEnvelope, MIN_THRESHOLD_OWNERS, estimateThresholdGasOverhead, parseThresholdEnvelope } from '@chipi-stack/backend';
4
4
  import { createWalletPasskey } from '@chipi-stack/chipi-passkey';
5
5
  import 'react/jsx-runtime';
6
6
  import { ChipiApiError } from '@chipi-stack/shared';
@@ -664,6 +664,25 @@ function useUpdateWalletEncryption() {
664
664
  reset: mutation.reset
665
665
  };
666
666
  }
667
+ function useMigrateWalletToShhh() {
668
+ const { chipiSDK } = useChipiContext();
669
+ const mutation = useMutation({
670
+ mutationFn: async (input) => {
671
+ const { bearerToken, ...rest } = input;
672
+ return chipiSDK.wallets.migrateToShhh(rest, bearerToken);
673
+ }
674
+ });
675
+ return {
676
+ migrate: mutation.mutate,
677
+ migrateAsync: mutation.mutateAsync,
678
+ data: mutation.data,
679
+ isLoading: mutation.isPending,
680
+ isError: mutation.isError,
681
+ error: mutation.error,
682
+ isSuccess: mutation.isSuccess,
683
+ reset: mutation.reset
684
+ };
685
+ }
667
686
  function useTransfer(config) {
668
687
  const { chipiSDK, passkeyAdapter } = useChipiContext();
669
688
  const mutation = useMutation({
@@ -1549,7 +1568,146 @@ function useX402Payment(config) {
1549
1568
  paymentCount
1550
1569
  };
1551
1570
  }
1571
+ function useShhhSigner() {
1572
+ return useMemo(
1573
+ () => ({
1574
+ signWithStark: (params) => buildStarkEnvelope({
1575
+ privateKey: params.privateKey,
1576
+ messageHash: params.messageHash
1577
+ }),
1578
+ signWithPhantom: (params) => buildEd25519Envelope({
1579
+ privateKey: params.privateKey,
1580
+ messageHash: params.messageHash
1581
+ }),
1582
+ signWithMetaMask: (params) => buildEip191EnvelopeFromSignature({
1583
+ signatureHex: params.signatureHex,
1584
+ pubkey: params.pubkey
1585
+ }),
1586
+ signWithPasskey: (params) => buildWebAuthnEnvelopeFromAssertion({
1587
+ authenticatorData: params.authenticatorData,
1588
+ clientDataJSON: params.clientDataJSON,
1589
+ signatureDer: params.signatureDer,
1590
+ pubkey: params.pubkey,
1591
+ messageHash: params.messageHash,
1592
+ yParity: params.yParity ?? 0n
1593
+ }),
1594
+ signWithAppleIdToken: (params) => buildJwtAppleSubEnvelopeFromTokenResponse({
1595
+ idToken: params.idToken,
1596
+ pubkey: params.pubkey,
1597
+ messageHash: params.messageHash
1598
+ })
1599
+ }),
1600
+ []
1601
+ );
1602
+ }
1603
+ function useGuardianRecovery() {
1604
+ return useMemo(
1605
+ () => ({
1606
+ buildProposeAddOwner: (args) => buildProposeAddOwnerCall(args),
1607
+ buildExecuteAddOwner: (args) => buildExecuteAddOwnerCall(args),
1608
+ buildProposeRemoveOwner: (args) => buildProposeRemoveOwnerCall(args),
1609
+ buildExecuteRemoveOwner: (args) => buildExecuteRemoveOwnerCall(args),
1610
+ buildProposeRotateOwner: (args) => buildProposeRotateOwnerCall(args),
1611
+ buildExecuteRotateOwner: (args) => buildExecuteRotateOwnerCall(args),
1612
+ buildProposeSetThreshold: (args) => buildProposeSetThresholdCall(args),
1613
+ buildExecuteSetThreshold: (args) => buildExecuteSetThresholdCall(args),
1614
+ buildCancelPendingOp: (args) => buildCancelPendingOpCall(args),
1615
+ buildInitiateRecovery: (args) => buildInitiateRecoveryCall(args),
1616
+ buildCancelRecovery: (args) => buildCancelRecoveryCall(args),
1617
+ buildFinalizeRecovery: (args) => buildFinalizeRecoveryCall(args),
1618
+ isReady: (validAfterSecs, nowSecs) => isReadyToExecute(validAfterSecs, nowSecs ?? Math.floor(Date.now() / 1e3)),
1619
+ secondsRemaining: (validAfterSecs, nowSecs) => secondsUntilExecutable(validAfterSecs, nowSecs ?? Math.floor(Date.now() / 1e3)),
1620
+ TIMELOCK_SECONDS,
1621
+ DEFAULT_OP_EXPIRY_SECONDS,
1622
+ ROLE_OWNER,
1623
+ ROLE_GUARDIAN,
1624
+ OP_ADD_OWNER,
1625
+ OP_REMOVE_OWNER,
1626
+ OP_ROTATE_OWNER,
1627
+ OP_SET_THRESHOLD
1628
+ }),
1629
+ []
1630
+ );
1631
+ }
1632
+ function useThresholdSign(opts) {
1633
+ const { threshold } = opts;
1634
+ const [collected, setCollected] = useState([]);
1635
+ const [signedOwnerIds, setSignedOwnerIds] = useState([]);
1636
+ const [error, setError] = useState(null);
1637
+ const signedOwnerIdsRef = useRef([]);
1638
+ const addEnvelope = useCallback((envelope) => {
1639
+ let ownerId;
1640
+ try {
1641
+ ownerId = assertValidV2Snip12Envelope(envelope);
1642
+ } catch (e) {
1643
+ const err = e instanceof Error ? e : new Error(String(e));
1644
+ setError(err);
1645
+ return false;
1646
+ }
1647
+ if (signedOwnerIdsRef.current.some((id) => id === ownerId)) {
1648
+ const err = new Error(
1649
+ `THRESH: owner_id 0x${ownerId.toString(16)} has already signed; use removeEnvelope first if you want to replace their envelope.`
1650
+ );
1651
+ setError(err);
1652
+ return false;
1653
+ }
1654
+ signedOwnerIdsRef.current = [...signedOwnerIdsRef.current, ownerId];
1655
+ setCollected((prev) => [...prev, envelope]);
1656
+ setSignedOwnerIds(signedOwnerIdsRef.current);
1657
+ setError(null);
1658
+ return true;
1659
+ }, []);
1660
+ const removeEnvelope = useCallback((ownerId) => {
1661
+ const matchIndex = signedOwnerIdsRef.current.findIndex((id) => id === ownerId);
1662
+ if (matchIndex < 0) return;
1663
+ signedOwnerIdsRef.current = signedOwnerIdsRef.current.filter((id) => id !== ownerId);
1664
+ setCollected((prev) => prev.filter((_, i) => i !== matchIndex));
1665
+ setSignedOwnerIds(signedOwnerIdsRef.current);
1666
+ setError(null);
1667
+ }, []);
1668
+ const reset = useCallback(() => {
1669
+ signedOwnerIdsRef.current = [];
1670
+ setCollected([]);
1671
+ setSignedOwnerIds([]);
1672
+ setError(null);
1673
+ }, []);
1674
+ const finalize = useCallback(() => {
1675
+ if (collected.length < threshold) {
1676
+ throw new Error(
1677
+ `THRESH: cannot finalize \u2014 need ${threshold} envelopes, have ${collected.length}. Wait for more owners to sign.`
1678
+ );
1679
+ }
1680
+ return buildThresholdEnvelope({ innerEnvelopes: collected });
1681
+ }, [collected, threshold]);
1682
+ const state = useMemo(
1683
+ () => ({
1684
+ threshold,
1685
+ collected,
1686
+ signedOwnerIds,
1687
+ isReady: collected.length >= threshold,
1688
+ error
1689
+ }),
1690
+ [threshold, collected, signedOwnerIds, error]
1691
+ );
1692
+ const stateless = useMemo(
1693
+ () => ({
1694
+ assemble: (args) => buildThresholdEnvelope(args),
1695
+ parse: (args) => parseThresholdEnvelope(args.envelope),
1696
+ estimateGasOverhead: (args) => estimateThresholdGasOverhead(args.owners),
1697
+ MIN_THRESHOLD_OWNERS
1698
+ }),
1699
+ []
1700
+ );
1701
+ return {
1702
+ ...stateless,
1703
+ state,
1704
+ addEnvelope,
1705
+ removeEnvelope,
1706
+ reset,
1707
+ finalize
1708
+ };
1709
+ }
1552
1710
 
1553
- export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useSyncOnChainTransfers, useTransfer, useUpdateWalletEncryption, useX402Payment };
1711
+ export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useGuardianRecovery, useMigrateWalletToPasskey, useMigrateWalletToShhh, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useShhhSigner, useSyncOnChainTransfers, useThresholdSign, useTransfer, useUpdateWalletEncryption, useX402Payment };
1554
1712
  //# sourceMappingURL=hooks.mjs.map
1555
1713
  //# sourceMappingURL=hooks.mjs.map