@gatewayfm/ups-react 0.1.13
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/index.d.mts +108 -0
- package/dist/index.d.ts +108 -0
- package/dist/index.js +274 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +255 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +47 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { UPSConfig, UPSClient, WalletState, AuthState, Account, EIP1193Provider, ConnectedWallet, CreateAccountParams, CreateAccountResponse, PaymentRequirements, SettleResponse, SignedAuthorization, VerifyResponse } from '@gatewayfm/ups-sdk';
|
|
4
|
+
export { Account, PaymentRequirements, SettleResponse as PaymentResult, UPSConfig } from '@gatewayfm/ups-sdk';
|
|
5
|
+
import * as zustand from 'zustand';
|
|
6
|
+
import { UseMutationResult, UseQueryResult } from '@tanstack/react-query';
|
|
7
|
+
|
|
8
|
+
interface UPSContextValue {
|
|
9
|
+
client: UPSClient | null;
|
|
10
|
+
isInitialized: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface UPSProviderProps {
|
|
13
|
+
config: UPSConfig;
|
|
14
|
+
children: React.ReactNode;
|
|
15
|
+
}
|
|
16
|
+
declare const UPSProvider: ({ config, children }: UPSProviderProps) => react_jsx_runtime.JSX.Element;
|
|
17
|
+
declare const useUPSContext: () => UPSContextValue;
|
|
18
|
+
declare const useUPSClient: () => UPSClient;
|
|
19
|
+
|
|
20
|
+
interface UPSStore {
|
|
21
|
+
client: UPSClient | null;
|
|
22
|
+
setClient: (client: UPSClient | null) => void;
|
|
23
|
+
walletState: WalletState;
|
|
24
|
+
setWalletState: (state: WalletState) => void;
|
|
25
|
+
authState: AuthState;
|
|
26
|
+
setAuthState: (state: AuthState) => void;
|
|
27
|
+
currentAccount: Account | null;
|
|
28
|
+
setCurrentAccount: (account: Account | null) => void;
|
|
29
|
+
}
|
|
30
|
+
declare const useUPSStore: zustand.UseBoundStore<zustand.StoreApi<UPSStore>>;
|
|
31
|
+
|
|
32
|
+
interface UseWalletReturn {
|
|
33
|
+
state: WalletState;
|
|
34
|
+
address: string | null;
|
|
35
|
+
chainId: number | null;
|
|
36
|
+
isConnected: boolean;
|
|
37
|
+
connect: UseMutationResult<ConnectedWallet, Error, EIP1193Provider>;
|
|
38
|
+
disconnect: UseMutationResult<void, Error, void>;
|
|
39
|
+
signMessage: (message: string) => Promise<string>;
|
|
40
|
+
switchChain: (chainId: number) => Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
declare function useWallet(): UseWalletReturn;
|
|
43
|
+
interface UseConnectReturn {
|
|
44
|
+
connect: (provider?: EIP1193Provider) => Promise<ConnectedWallet>;
|
|
45
|
+
isPending: boolean;
|
|
46
|
+
error: Error | null;
|
|
47
|
+
}
|
|
48
|
+
declare function useConnect(): UseConnectReturn;
|
|
49
|
+
interface UseDisconnectReturn {
|
|
50
|
+
disconnect: () => Promise<void>;
|
|
51
|
+
isPending: boolean;
|
|
52
|
+
}
|
|
53
|
+
declare function useDisconnect(): UseDisconnectReturn;
|
|
54
|
+
|
|
55
|
+
interface UseAuthReturn {
|
|
56
|
+
state: AuthState;
|
|
57
|
+
isAuthenticated: boolean;
|
|
58
|
+
authenticate: UseMutationResult<void, Error, void>;
|
|
59
|
+
logout: () => void;
|
|
60
|
+
}
|
|
61
|
+
declare function useAuth(): UseAuthReturn;
|
|
62
|
+
|
|
63
|
+
declare function useAccounts(): UseQueryResult<Account[], Error>;
|
|
64
|
+
declare function useAccount(id: string): UseQueryResult<Account, Error>;
|
|
65
|
+
declare function useAccountByWallet(address: string | null): UseQueryResult<Account, Error>;
|
|
66
|
+
interface UsePredictAddressReturn {
|
|
67
|
+
predictAddress: (owner: string, salt: string) => Promise<string>;
|
|
68
|
+
isPending: boolean;
|
|
69
|
+
error: Error | null;
|
|
70
|
+
}
|
|
71
|
+
declare function usePredictAddress(): UsePredictAddressReturn;
|
|
72
|
+
interface UseCreateAccountReturn {
|
|
73
|
+
createAccount: (params: CreateAccountParams) => Promise<CreateAccountResponse>;
|
|
74
|
+
isPending: boolean;
|
|
75
|
+
error: Error | null;
|
|
76
|
+
data: CreateAccountResponse | undefined;
|
|
77
|
+
reset: () => void;
|
|
78
|
+
}
|
|
79
|
+
declare function useCreateAccount(): UseCreateAccountReturn;
|
|
80
|
+
declare function useCurrentAccount(): Account | null;
|
|
81
|
+
declare function generateSalt(): string;
|
|
82
|
+
|
|
83
|
+
interface UsePaymentParams {
|
|
84
|
+
requirements: PaymentRequirements;
|
|
85
|
+
from: string;
|
|
86
|
+
}
|
|
87
|
+
interface UsePaymentReturn {
|
|
88
|
+
pay: (params: UsePaymentParams) => Promise<SettleResponse>;
|
|
89
|
+
isPending: boolean;
|
|
90
|
+
error: Error | null;
|
|
91
|
+
data: SettleResponse | undefined;
|
|
92
|
+
reset: () => void;
|
|
93
|
+
}
|
|
94
|
+
declare function usePayment(): UsePaymentReturn;
|
|
95
|
+
interface UsePaymentVerifyReturn {
|
|
96
|
+
verify: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<VerifyResponse>;
|
|
97
|
+
isPending: boolean;
|
|
98
|
+
error: Error | null;
|
|
99
|
+
}
|
|
100
|
+
declare function usePaymentVerify(): UsePaymentVerifyReturn;
|
|
101
|
+
interface UsePaymentSettleReturn {
|
|
102
|
+
settle: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<SettleResponse>;
|
|
103
|
+
isPending: boolean;
|
|
104
|
+
error: Error | null;
|
|
105
|
+
}
|
|
106
|
+
declare function usePaymentSettle(): UsePaymentSettleReturn;
|
|
107
|
+
|
|
108
|
+
export { UPSProvider, generateSalt, useAccount, useAccountByWallet, useAccounts, useAuth, useConnect, useCreateAccount, useCurrentAccount, useDisconnect, usePayment, usePaymentSettle, usePaymentVerify, usePredictAddress, useUPSClient, useUPSContext, useUPSStore, useWallet };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { UPSConfig, UPSClient, WalletState, AuthState, Account, EIP1193Provider, ConnectedWallet, CreateAccountParams, CreateAccountResponse, PaymentRequirements, SettleResponse, SignedAuthorization, VerifyResponse } from '@gatewayfm/ups-sdk';
|
|
4
|
+
export { Account, PaymentRequirements, SettleResponse as PaymentResult, UPSConfig } from '@gatewayfm/ups-sdk';
|
|
5
|
+
import * as zustand from 'zustand';
|
|
6
|
+
import { UseMutationResult, UseQueryResult } from '@tanstack/react-query';
|
|
7
|
+
|
|
8
|
+
interface UPSContextValue {
|
|
9
|
+
client: UPSClient | null;
|
|
10
|
+
isInitialized: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface UPSProviderProps {
|
|
13
|
+
config: UPSConfig;
|
|
14
|
+
children: React.ReactNode;
|
|
15
|
+
}
|
|
16
|
+
declare const UPSProvider: ({ config, children }: UPSProviderProps) => react_jsx_runtime.JSX.Element;
|
|
17
|
+
declare const useUPSContext: () => UPSContextValue;
|
|
18
|
+
declare const useUPSClient: () => UPSClient;
|
|
19
|
+
|
|
20
|
+
interface UPSStore {
|
|
21
|
+
client: UPSClient | null;
|
|
22
|
+
setClient: (client: UPSClient | null) => void;
|
|
23
|
+
walletState: WalletState;
|
|
24
|
+
setWalletState: (state: WalletState) => void;
|
|
25
|
+
authState: AuthState;
|
|
26
|
+
setAuthState: (state: AuthState) => void;
|
|
27
|
+
currentAccount: Account | null;
|
|
28
|
+
setCurrentAccount: (account: Account | null) => void;
|
|
29
|
+
}
|
|
30
|
+
declare const useUPSStore: zustand.UseBoundStore<zustand.StoreApi<UPSStore>>;
|
|
31
|
+
|
|
32
|
+
interface UseWalletReturn {
|
|
33
|
+
state: WalletState;
|
|
34
|
+
address: string | null;
|
|
35
|
+
chainId: number | null;
|
|
36
|
+
isConnected: boolean;
|
|
37
|
+
connect: UseMutationResult<ConnectedWallet, Error, EIP1193Provider>;
|
|
38
|
+
disconnect: UseMutationResult<void, Error, void>;
|
|
39
|
+
signMessage: (message: string) => Promise<string>;
|
|
40
|
+
switchChain: (chainId: number) => Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
declare function useWallet(): UseWalletReturn;
|
|
43
|
+
interface UseConnectReturn {
|
|
44
|
+
connect: (provider?: EIP1193Provider) => Promise<ConnectedWallet>;
|
|
45
|
+
isPending: boolean;
|
|
46
|
+
error: Error | null;
|
|
47
|
+
}
|
|
48
|
+
declare function useConnect(): UseConnectReturn;
|
|
49
|
+
interface UseDisconnectReturn {
|
|
50
|
+
disconnect: () => Promise<void>;
|
|
51
|
+
isPending: boolean;
|
|
52
|
+
}
|
|
53
|
+
declare function useDisconnect(): UseDisconnectReturn;
|
|
54
|
+
|
|
55
|
+
interface UseAuthReturn {
|
|
56
|
+
state: AuthState;
|
|
57
|
+
isAuthenticated: boolean;
|
|
58
|
+
authenticate: UseMutationResult<void, Error, void>;
|
|
59
|
+
logout: () => void;
|
|
60
|
+
}
|
|
61
|
+
declare function useAuth(): UseAuthReturn;
|
|
62
|
+
|
|
63
|
+
declare function useAccounts(): UseQueryResult<Account[], Error>;
|
|
64
|
+
declare function useAccount(id: string): UseQueryResult<Account, Error>;
|
|
65
|
+
declare function useAccountByWallet(address: string | null): UseQueryResult<Account, Error>;
|
|
66
|
+
interface UsePredictAddressReturn {
|
|
67
|
+
predictAddress: (owner: string, salt: string) => Promise<string>;
|
|
68
|
+
isPending: boolean;
|
|
69
|
+
error: Error | null;
|
|
70
|
+
}
|
|
71
|
+
declare function usePredictAddress(): UsePredictAddressReturn;
|
|
72
|
+
interface UseCreateAccountReturn {
|
|
73
|
+
createAccount: (params: CreateAccountParams) => Promise<CreateAccountResponse>;
|
|
74
|
+
isPending: boolean;
|
|
75
|
+
error: Error | null;
|
|
76
|
+
data: CreateAccountResponse | undefined;
|
|
77
|
+
reset: () => void;
|
|
78
|
+
}
|
|
79
|
+
declare function useCreateAccount(): UseCreateAccountReturn;
|
|
80
|
+
declare function useCurrentAccount(): Account | null;
|
|
81
|
+
declare function generateSalt(): string;
|
|
82
|
+
|
|
83
|
+
interface UsePaymentParams {
|
|
84
|
+
requirements: PaymentRequirements;
|
|
85
|
+
from: string;
|
|
86
|
+
}
|
|
87
|
+
interface UsePaymentReturn {
|
|
88
|
+
pay: (params: UsePaymentParams) => Promise<SettleResponse>;
|
|
89
|
+
isPending: boolean;
|
|
90
|
+
error: Error | null;
|
|
91
|
+
data: SettleResponse | undefined;
|
|
92
|
+
reset: () => void;
|
|
93
|
+
}
|
|
94
|
+
declare function usePayment(): UsePaymentReturn;
|
|
95
|
+
interface UsePaymentVerifyReturn {
|
|
96
|
+
verify: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<VerifyResponse>;
|
|
97
|
+
isPending: boolean;
|
|
98
|
+
error: Error | null;
|
|
99
|
+
}
|
|
100
|
+
declare function usePaymentVerify(): UsePaymentVerifyReturn;
|
|
101
|
+
interface UsePaymentSettleReturn {
|
|
102
|
+
settle: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<SettleResponse>;
|
|
103
|
+
isPending: boolean;
|
|
104
|
+
error: Error | null;
|
|
105
|
+
}
|
|
106
|
+
declare function usePaymentSettle(): UsePaymentSettleReturn;
|
|
107
|
+
|
|
108
|
+
export { UPSProvider, generateSalt, useAccount, useAccountByWallet, useAccounts, useAuth, useConnect, useCreateAccount, useCurrentAccount, useDisconnect, usePayment, usePaymentSettle, usePaymentVerify, usePredictAddress, useUPSClient, useUPSContext, useUPSStore, useWallet };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var upsSdk = require('@gatewayfm/ups-sdk');
|
|
5
|
+
var zustand = require('zustand');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
7
|
+
var reactQuery = require('@tanstack/react-query');
|
|
8
|
+
|
|
9
|
+
// src/provider.tsx
|
|
10
|
+
var useUPSStore = zustand.create((set) => ({
|
|
11
|
+
client: null,
|
|
12
|
+
setClient: (client) => set({ client }),
|
|
13
|
+
walletState: {
|
|
14
|
+
address: null,
|
|
15
|
+
chainId: null,
|
|
16
|
+
isConnected: false,
|
|
17
|
+
provider: null
|
|
18
|
+
},
|
|
19
|
+
setWalletState: (walletState) => set({ walletState }),
|
|
20
|
+
authState: {
|
|
21
|
+
isAuthenticated: false,
|
|
22
|
+
token: null,
|
|
23
|
+
expiresAt: null,
|
|
24
|
+
address: null
|
|
25
|
+
},
|
|
26
|
+
setAuthState: (authState) => set({ authState }),
|
|
27
|
+
currentAccount: null,
|
|
28
|
+
setCurrentAccount: (currentAccount) => set({ currentAccount })
|
|
29
|
+
}));
|
|
30
|
+
var UPSContext = react.createContext({
|
|
31
|
+
client: null,
|
|
32
|
+
isInitialized: false
|
|
33
|
+
});
|
|
34
|
+
var UPSProvider = ({ config, children }) => {
|
|
35
|
+
const setClient = useUPSStore((state) => state.setClient);
|
|
36
|
+
const setWalletState = useUPSStore((state) => state.setWalletState);
|
|
37
|
+
const setAuthState = useUPSStore((state) => state.setAuthState);
|
|
38
|
+
const [isInitialized, setIsInitialized] = react.useState(false);
|
|
39
|
+
const client = react.useMemo(() => {
|
|
40
|
+
return new upsSdk.UPSClient(config);
|
|
41
|
+
}, [JSON.stringify(config)]);
|
|
42
|
+
react.useEffect(() => {
|
|
43
|
+
setClient(client);
|
|
44
|
+
const unsubWallet = client.wallet.onStateChange((state) => {
|
|
45
|
+
setWalletState(state);
|
|
46
|
+
});
|
|
47
|
+
const unsubAuth = client.auth.onStateChange((state) => {
|
|
48
|
+
setAuthState(state);
|
|
49
|
+
});
|
|
50
|
+
setWalletState(client.wallet.state);
|
|
51
|
+
setAuthState(client.auth.state);
|
|
52
|
+
setIsInitialized(true);
|
|
53
|
+
return () => {
|
|
54
|
+
unsubWallet();
|
|
55
|
+
unsubAuth();
|
|
56
|
+
client.destroy();
|
|
57
|
+
setClient(null);
|
|
58
|
+
};
|
|
59
|
+
}, [client, setClient, setWalletState, setAuthState]);
|
|
60
|
+
const value = react.useMemo(() => ({
|
|
61
|
+
client,
|
|
62
|
+
isInitialized
|
|
63
|
+
}), [client, isInitialized]);
|
|
64
|
+
return /* @__PURE__ */ jsxRuntime.jsx(UPSContext.Provider, { value, children });
|
|
65
|
+
};
|
|
66
|
+
var useUPSContext = () => react.useContext(UPSContext);
|
|
67
|
+
var useUPSClient = () => {
|
|
68
|
+
const { client } = useUPSContext();
|
|
69
|
+
if (!client) {
|
|
70
|
+
throw new Error("useUPSClient must be used within a UPSProvider");
|
|
71
|
+
}
|
|
72
|
+
return client;
|
|
73
|
+
};
|
|
74
|
+
function useWallet() {
|
|
75
|
+
const client = useUPSClient();
|
|
76
|
+
const walletState = useUPSStore((state) => state.walletState);
|
|
77
|
+
const connectMutation = reactQuery.useMutation({
|
|
78
|
+
mutationFn: async (provider) => {
|
|
79
|
+
return await client.connect(provider);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
const disconnectMutation = reactQuery.useMutation({
|
|
83
|
+
mutationFn: async () => {
|
|
84
|
+
await client.disconnect();
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
state: walletState,
|
|
89
|
+
address: walletState.address,
|
|
90
|
+
chainId: walletState.chainId,
|
|
91
|
+
isConnected: walletState.isConnected,
|
|
92
|
+
connect: connectMutation,
|
|
93
|
+
disconnect: disconnectMutation,
|
|
94
|
+
signMessage: (message) => client.wallet.signMessage(message),
|
|
95
|
+
switchChain: (chainId) => client.wallet.switchChain(chainId)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function useConnect() {
|
|
99
|
+
const { connect } = useWallet();
|
|
100
|
+
const connectFn = async (provider) => {
|
|
101
|
+
if (provider) {
|
|
102
|
+
return connect.mutateAsync(provider);
|
|
103
|
+
}
|
|
104
|
+
if (typeof window !== "undefined" && window.ethereum) {
|
|
105
|
+
return connect.mutateAsync(window.ethereum);
|
|
106
|
+
}
|
|
107
|
+
throw new Error("No EIP-1193 provider found (window.ethereum is undefined)");
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
connect: connectFn,
|
|
111
|
+
isPending: connect.isPending,
|
|
112
|
+
error: connect.error
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function useDisconnect() {
|
|
116
|
+
const { disconnect } = useWallet();
|
|
117
|
+
return {
|
|
118
|
+
disconnect: async () => {
|
|
119
|
+
await disconnect.mutateAsync();
|
|
120
|
+
},
|
|
121
|
+
isPending: disconnect.isPending
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function useAuth() {
|
|
125
|
+
const client = useUPSClient();
|
|
126
|
+
const authState = useUPSStore((state) => state.authState);
|
|
127
|
+
const authenticateMutation = reactQuery.useMutation({
|
|
128
|
+
mutationFn: async () => {
|
|
129
|
+
await client.authenticate();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
state: authState,
|
|
134
|
+
isAuthenticated: authState.isAuthenticated,
|
|
135
|
+
authenticate: authenticateMutation,
|
|
136
|
+
logout: () => client.auth.logout()
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
var accountKeys = {
|
|
140
|
+
all: ["accounts"],
|
|
141
|
+
list: () => [...accountKeys.all, "list"],
|
|
142
|
+
detail: (id) => [...accountKeys.all, "detail", id],
|
|
143
|
+
byWallet: (address) => [...accountKeys.all, "wallet", address]
|
|
144
|
+
};
|
|
145
|
+
function useAccounts() {
|
|
146
|
+
const client = useUPSClient();
|
|
147
|
+
const isAuthenticated = useUPSStore((state) => state.authState.isAuthenticated);
|
|
148
|
+
return reactQuery.useQuery({
|
|
149
|
+
queryKey: accountKeys.list(),
|
|
150
|
+
queryFn: () => client.account.list(),
|
|
151
|
+
enabled: isAuthenticated
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function useAccount(id) {
|
|
155
|
+
const client = useUPSClient();
|
|
156
|
+
const isAuthenticated = useUPSStore((state) => state.authState.isAuthenticated);
|
|
157
|
+
return reactQuery.useQuery({
|
|
158
|
+
queryKey: accountKeys.detail(id),
|
|
159
|
+
queryFn: () => client.account.get(id),
|
|
160
|
+
enabled: isAuthenticated && !!id
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
function useAccountByWallet(address) {
|
|
164
|
+
const client = useUPSClient();
|
|
165
|
+
const isAuthenticated = useUPSStore((state) => state.authState.isAuthenticated);
|
|
166
|
+
return reactQuery.useQuery({
|
|
167
|
+
queryKey: accountKeys.byWallet(address || ""),
|
|
168
|
+
queryFn: () => client.account.getByWallet(address),
|
|
169
|
+
enabled: isAuthenticated && !!address
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function usePredictAddress() {
|
|
173
|
+
const client = useUPSClient();
|
|
174
|
+
const mutation = reactQuery.useMutation({
|
|
175
|
+
mutationFn: (params) => client.account.predictAddress({ ownerAddress: params.owner, salt: params.salt })
|
|
176
|
+
});
|
|
177
|
+
return {
|
|
178
|
+
predictAddress: (owner, salt) => mutation.mutateAsync({ owner, salt }),
|
|
179
|
+
isPending: mutation.isPending,
|
|
180
|
+
error: mutation.error
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function useCreateAccount() {
|
|
184
|
+
const client = useUPSClient();
|
|
185
|
+
const queryClient = reactQuery.useQueryClient();
|
|
186
|
+
const setCurrentAccount = useUPSStore((state) => state.setCurrentAccount);
|
|
187
|
+
const mutation = reactQuery.useMutation({
|
|
188
|
+
mutationFn: (params) => client.account.create(params),
|
|
189
|
+
onSuccess: (data) => {
|
|
190
|
+
setCurrentAccount(data.account);
|
|
191
|
+
queryClient.invalidateQueries({ queryKey: accountKeys.list() });
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
createAccount: mutation.mutateAsync,
|
|
196
|
+
isPending: mutation.isPending,
|
|
197
|
+
error: mutation.error,
|
|
198
|
+
data: mutation.data,
|
|
199
|
+
reset: mutation.reset
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function useCurrentAccount() {
|
|
203
|
+
return useUPSStore((state) => state.currentAccount);
|
|
204
|
+
}
|
|
205
|
+
function generateSalt() {
|
|
206
|
+
const array = new Uint8Array(32);
|
|
207
|
+
if (typeof window !== "undefined" && window.crypto) {
|
|
208
|
+
window.crypto.getRandomValues(array);
|
|
209
|
+
} else {
|
|
210
|
+
for (let i = 0; i < 32; i++) {
|
|
211
|
+
array[i] = Math.floor(Math.random() * 256);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return "0x" + Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
215
|
+
}
|
|
216
|
+
function usePayment() {
|
|
217
|
+
const client = useUPSClient();
|
|
218
|
+
const mutation = reactQuery.useMutation({
|
|
219
|
+
mutationFn: (params) => client.payment.pay({
|
|
220
|
+
requirements: params.requirements,
|
|
221
|
+
from: params.from
|
|
222
|
+
})
|
|
223
|
+
});
|
|
224
|
+
return {
|
|
225
|
+
pay: mutation.mutateAsync,
|
|
226
|
+
isPending: mutation.isPending,
|
|
227
|
+
error: mutation.error,
|
|
228
|
+
data: mutation.data,
|
|
229
|
+
reset: mutation.reset
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function usePaymentVerify() {
|
|
233
|
+
const client = useUPSClient();
|
|
234
|
+
const mutation = reactQuery.useMutation({
|
|
235
|
+
mutationFn: (params) => client.payment.verify(params.signed, params.requirements)
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
verify: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),
|
|
239
|
+
isPending: mutation.isPending,
|
|
240
|
+
error: mutation.error
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function usePaymentSettle() {
|
|
244
|
+
const client = useUPSClient();
|
|
245
|
+
const mutation = reactQuery.useMutation({
|
|
246
|
+
mutationFn: (params) => client.payment.settle(params.signed, params.requirements)
|
|
247
|
+
});
|
|
248
|
+
return {
|
|
249
|
+
settle: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),
|
|
250
|
+
isPending: mutation.isPending,
|
|
251
|
+
error: mutation.error
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
exports.UPSProvider = UPSProvider;
|
|
256
|
+
exports.generateSalt = generateSalt;
|
|
257
|
+
exports.useAccount = useAccount;
|
|
258
|
+
exports.useAccountByWallet = useAccountByWallet;
|
|
259
|
+
exports.useAccounts = useAccounts;
|
|
260
|
+
exports.useAuth = useAuth;
|
|
261
|
+
exports.useConnect = useConnect;
|
|
262
|
+
exports.useCreateAccount = useCreateAccount;
|
|
263
|
+
exports.useCurrentAccount = useCurrentAccount;
|
|
264
|
+
exports.useDisconnect = useDisconnect;
|
|
265
|
+
exports.usePayment = usePayment;
|
|
266
|
+
exports.usePaymentSettle = usePaymentSettle;
|
|
267
|
+
exports.usePaymentVerify = usePaymentVerify;
|
|
268
|
+
exports.usePredictAddress = usePredictAddress;
|
|
269
|
+
exports.useUPSClient = useUPSClient;
|
|
270
|
+
exports.useUPSContext = useUPSContext;
|
|
271
|
+
exports.useUPSStore = useUPSStore;
|
|
272
|
+
exports.useWallet = useWallet;
|
|
273
|
+
//# sourceMappingURL=index.js.map
|
|
274
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/store.ts","../src/provider.tsx","../src/hooks/use-wallet.ts","../src/hooks/use-auth.ts","../src/hooks/use-account.ts","../src/hooks/use-payment.ts"],"names":["create","createContext","useState","useMemo","UPSClient","useEffect","jsx","useContext","useMutation","useQuery","useQueryClient"],"mappings":";;;;;;;;;AAqBO,IAAM,WAAA,GAAcA,cAAA,CAAiB,CAAC,GAAA,MAAS;AAAA,EAClD,MAAA,EAAQ,IAAA;AAAA,EACR,WAA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AAA,KACT,MAAA,CAAO,QAAQ,MAAA,CAAO,MAAA,CAAO,MAAA,EAAQ,MAAA,CAAO,YAAY;AAAA,GAC/D,CAAA;AAED,EAAA,OAAO;AAAA,IACH,MAAA,EAAQ,CAAC,MAAA,EAAQ,YAAA,KAAiB,SAAS,WAAA,CAAY,EAAE,MAAA,EAAQ,YAAA,EAAc,CAAA;AAAA,IAC/E,WAAW,QAAA,CAAS,SAAA;AAAA,IACpB,OAAO,QAAA,CAAS;AAAA,GACpB;AACJ;AAQO,SAAS,gBAAA,GAA2C;AACvD,EAAA,MAAM,SAAS,YAAA,EAAa;AAE5B,EAAA,MAAM,WAAWA,sBAAAA,CAAY;AAAA,IACzB,UAAA,EAAY,CAAC,MAAA,KACT,MAAA,CAAO,QAAQ,MAAA,CAAO,MAAA,CAAO,MAAA,EAAQ,MAAA,CAAO,YAAY;AAAA,GAC/D,CAAA;AAED,EAAA,OAAO;AAAA,IACH,MAAA,EAAQ,CAAC,MAAA,EAAQ,YAAA,KAAiB,SAAS,WAAA,CAAY,EAAE,MAAA,EAAQ,YAAA,EAAc,CAAA;AAAA,IAC/E,WAAW,QAAA,CAAS,SAAA;AAAA,IACpB,OAAO,QAAA,CAAS;AAAA,GACpB;AACJ","file":"index.js","sourcesContent":["import { create } from 'zustand';\nimport type { UPSClient, WalletState, AuthState, Account } from '@gatewayfm/ups-sdk';\n\nexport interface UPSStore {\n // Client instance\n client: UPSClient | null;\n setClient: (client: UPSClient | null) => void;\n\n // Wallet state (synced from SDK)\n walletState: WalletState;\n setWalletState: (state: WalletState) => void;\n\n // Auth state (synced from SDK)\n authState: AuthState;\n setAuthState: (state: AuthState) => void;\n\n // Current user's account (after creation)\n currentAccount: Account | null;\n setCurrentAccount: (account: Account | null) => void;\n}\n\nexport const useUPSStore = create<UPSStore>((set) => ({\n client: null,\n setClient: (client) => set({ client }),\n\n walletState: {\n address: null,\n chainId: null,\n isConnected: false,\n provider: null,\n },\n setWalletState: (walletState) => set({ walletState }),\n\n authState: {\n isAuthenticated: false,\n token: null,\n expiresAt: null,\n address: null,\n },\n setAuthState: (authState) => set({ authState }),\n\n currentAccount: null,\n setCurrentAccount: (currentAccount) => set({ currentAccount }),\n}));\n","import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';\nimport { UPSClient, type UPSConfig } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from './store';\n\ninterface UPSContextValue {\n client: UPSClient | null;\n isInitialized: boolean;\n}\n\nconst UPSContext = createContext<UPSContextValue>({\n client: null,\n isInitialized: false,\n});\n\nexport interface UPSProviderProps {\n config: UPSConfig;\n children: React.ReactNode;\n}\n\nexport const UPSProvider = ({ config, children }: UPSProviderProps) => {\n const setClient = useUPSStore((state) => state.setClient);\n const setWalletState = useUPSStore((state) => state.setWalletState);\n const setAuthState = useUPSStore((state) => state.setAuthState);\n const [isInitialized, setIsInitialized] = useState(false);\n\n // Initialize client only once (or when config deep changes, but strictly speaking config shouldn't change often)\n // We use useMemo to hold the instance.\n const client = useMemo(() => {\n return new UPSClient(config);\n }, [JSON.stringify(config)]);\n\n useEffect(() => {\n setClient(client);\n\n // Subscribe to state changes\n const unsubWallet = client.wallet.onStateChange((state) => {\n setWalletState(state);\n });\n\n const unsubAuth = client.auth.onStateChange((state) => {\n setAuthState(state);\n });\n\n // Initial sync\n setWalletState(client.wallet.state);\n setAuthState(client.auth.state);\n\n setIsInitialized(true);\n\n return () => {\n unsubWallet();\n unsubAuth();\n client.destroy();\n setClient(null);\n };\n }, [client, setClient, setWalletState, setAuthState]);\n\n const value = useMemo(() => ({\n client,\n isInitialized\n }), [client, isInitialized]);\n\n return <UPSContext.Provider value={value}>{children}</UPSContext.Provider>;\n};\n\nexport const useUPSContext = () => useContext(UPSContext);\n\nexport const useUPSClient = () => {\n const { client } = useUPSContext();\n if (!client) {\n throw new Error('useUPSClient must be used within a UPSProvider');\n }\n return client;\n};\n","import { useMutation, UseMutationResult } from '@tanstack/react-query';\nimport { type EIP1193Provider, type ConnectedWallet, type WalletState } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from '../store';\nimport { useUPSClient } from '../provider';\n\nexport interface UseWalletReturn {\n // State\n state: WalletState;\n address: string | null;\n chainId: number | null;\n isConnected: boolean;\n\n // Actions (TanStack Query mutations)\n connect: UseMutationResult<ConnectedWallet, Error, EIP1193Provider>;\n disconnect: UseMutationResult<void, Error, void>;\n\n // Direct methods\n signMessage: (message: string) => Promise<string>;\n switchChain: (chainId: number) => Promise<void>;\n}\n\nexport function useWallet(): UseWalletReturn {\n const client = useUPSClient();\n const walletState = useUPSStore((state) => state.walletState);\n\n const connectMutation = useMutation({\n mutationFn: async (provider: EIP1193Provider) => {\n return await client.connect(provider);\n },\n });\n\n const disconnectMutation = useMutation({\n mutationFn: async () => {\n await client.disconnect();\n },\n });\n\n return {\n state: walletState,\n address: walletState.address,\n chainId: walletState.chainId,\n isConnected: walletState.isConnected,\n connect: connectMutation,\n disconnect: disconnectMutation,\n signMessage: (message: string) => client.wallet.signMessage(message),\n switchChain: (chainId: number) => client.wallet.switchChain(chainId),\n };\n}\n\nexport interface UseConnectReturn {\n connect: (provider?: EIP1193Provider) => Promise<ConnectedWallet>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function useConnect(): UseConnectReturn {\n const { connect } = useWallet();\n\n const connectFn = async (provider?: EIP1193Provider) => {\n if (provider) {\n return connect.mutateAsync(provider);\n }\n\n // Default to window.ethereum\n if (typeof window !== 'undefined' && (window as unknown as { ethereum: EIP1193Provider }).ethereum) {\n return connect.mutateAsync((window as unknown as { ethereum: EIP1193Provider }).ethereum);\n }\n\n throw new Error('No EIP-1193 provider found (window.ethereum is undefined)');\n };\n\n return {\n connect: connectFn,\n isPending: connect.isPending,\n error: connect.error,\n };\n}\n\nexport interface UseDisconnectReturn {\n disconnect: () => Promise<void>;\n isPending: boolean;\n}\n\nexport function useDisconnect(): UseDisconnectReturn {\n const { disconnect } = useWallet();\n\n return {\n disconnect: async () => {\n await disconnect.mutateAsync();\n },\n isPending: disconnect.isPending,\n };\n}\n","import { useMutation, UseMutationResult } from '@tanstack/react-query';\nimport { type AuthState } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from '../store';\nimport { useUPSClient } from '../provider';\n\nexport interface UseAuthReturn {\n // State\n state: AuthState;\n isAuthenticated: boolean;\n\n // Actions\n authenticate: UseMutationResult<void, Error, void>;\n logout: () => void;\n}\n\nexport function useAuth(): UseAuthReturn {\n const client = useUPSClient();\n const authState = useUPSStore((state) => state.authState);\n\n const authenticateMutation = useMutation({\n mutationFn: async () => {\n await client.authenticate();\n },\n });\n\n return {\n state: authState,\n isAuthenticated: authState.isAuthenticated,\n authenticate: authenticateMutation,\n logout: () => client.auth.logout(),\n };\n}\n","import { useQuery, useMutation, useQueryClient, UseQueryResult } from '@tanstack/react-query';\nimport { type Account, type CreateAccountParams, type CreateAccountResponse } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from '../store';\nimport { useUPSClient } from '../provider';\n\nconst accountKeys = {\n all: ['accounts'] as const,\n list: () => [...accountKeys.all, 'list'] as const,\n detail: (id: string) => [...accountKeys.all, 'detail', id] as const,\n byWallet: (address: string) => [...accountKeys.all, 'wallet', address] as const,\n};\n\nexport function useAccounts(): UseQueryResult<Account[], Error> {\n const client = useUPSClient();\n const isAuthenticated = useUPSStore(state => state.authState.isAuthenticated);\n\n return useQuery({\n queryKey: accountKeys.list(),\n queryFn: () => client.account.list(),\n enabled: isAuthenticated,\n });\n}\n\nexport function useAccount(id: string): UseQueryResult<Account, Error> {\n const client = useUPSClient();\n const isAuthenticated = useUPSStore(state => state.authState.isAuthenticated);\n\n return useQuery({\n queryKey: accountKeys.detail(id),\n queryFn: () => client.account.get(id),\n enabled: isAuthenticated && !!id,\n });\n}\n\nexport function useAccountByWallet(address: string | null): UseQueryResult<Account, Error> {\n const client = useUPSClient();\n const isAuthenticated = useUPSStore(state => state.authState.isAuthenticated);\n\n return useQuery({\n queryKey: accountKeys.byWallet(address || ''),\n queryFn: () => client.account.getByWallet(address!),\n enabled: isAuthenticated && !!address,\n });\n}\n\nexport interface UsePredictAddressReturn {\n predictAddress: (owner: string, salt: string) => Promise<string>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function usePredictAddress(): UsePredictAddressReturn {\n const client = useUPSClient();\n const mutation = useMutation({\n mutationFn: (params: { owner: string, salt: string }) =>\n client.account.predictAddress({ ownerAddress: params.owner, salt: params.salt }),\n });\n\n return {\n predictAddress: (owner, salt) => mutation.mutateAsync({ owner, salt }),\n isPending: mutation.isPending,\n error: mutation.error,\n };\n}\n\nexport interface UseCreateAccountReturn {\n createAccount: (params: CreateAccountParams) => Promise<CreateAccountResponse>;\n isPending: boolean;\n error: Error | null;\n data: CreateAccountResponse | undefined;\n reset: () => void;\n}\n\nexport function useCreateAccount(): UseCreateAccountReturn {\n const client = useUPSClient();\n const queryClient = useQueryClient();\n const setCurrentAccount = useUPSStore(state => state.setCurrentAccount);\n\n const mutation = useMutation({\n mutationFn: (params: CreateAccountParams) => client.account.create(params),\n onSuccess: (data) => {\n setCurrentAccount(data.account);\n queryClient.invalidateQueries({ queryKey: accountKeys.list() });\n },\n });\n\n return {\n createAccount: mutation.mutateAsync,\n isPending: mutation.isPending,\n error: mutation.error,\n data: mutation.data,\n reset: mutation.reset,\n };\n}\n\nexport function useCurrentAccount(): Account | null {\n return useUPSStore(state => state.currentAccount);\n}\n\nexport function generateSalt(): string {\n const array = new Uint8Array(32);\n if (typeof window !== 'undefined' && window.crypto) {\n window.crypto.getRandomValues(array);\n } else {\n // Fallback for non-browser environments if needed (e.g. during build/test)\n for (let i = 0; i < 32; i++) {\n array[i] = Math.floor(Math.random() * 256);\n }\n }\n return '0x' + Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');\n}\n","import { useMutation } from '@tanstack/react-query';\nimport {\n type PaymentRequirements,\n type SignedAuthorization,\n type VerifyResponse,\n type SettleResponse\n} from '@gatewayfm/ups-sdk';\nimport { useUPSClient } from '../provider';\n\nexport interface UsePaymentParams {\n requirements: PaymentRequirements;\n from: string; // Payer SmartAccount address\n}\n\n// PaymentResult is SettleResponse from SDK type\nexport interface UsePaymentReturn {\n pay: (params: UsePaymentParams) => Promise<SettleResponse>;\n isPending: boolean;\n error: Error | null;\n data: SettleResponse | undefined;\n reset: () => void;\n}\n\nexport function usePayment(): UsePaymentReturn {\n const client = useUPSClient();\n\n // client.payment.pay takes { requirements, from }\n const mutation = useMutation({\n mutationFn: (params: UsePaymentParams) => client.payment.pay({\n requirements: params.requirements,\n from: params.from\n }),\n });\n\n return {\n pay: mutation.mutateAsync,\n isPending: mutation.isPending,\n error: mutation.error,\n data: mutation.data,\n reset: mutation.reset,\n };\n}\n\nexport interface UsePaymentVerifyReturn {\n verify: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<VerifyResponse>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function usePaymentVerify(): UsePaymentVerifyReturn {\n const client = useUPSClient();\n\n const mutation = useMutation({\n mutationFn: (params: { signed: SignedAuthorization, requirements: PaymentRequirements }) =>\n client.payment.verify(params.signed, params.requirements),\n });\n\n return {\n verify: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),\n isPending: mutation.isPending,\n error: mutation.error,\n };\n}\n\nexport interface UsePaymentSettleReturn {\n settle: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<SettleResponse>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function usePaymentSettle(): UsePaymentSettleReturn {\n const client = useUPSClient();\n\n const mutation = useMutation({\n mutationFn: (params: { signed: SignedAuthorization, requirements: PaymentRequirements }) =>\n client.payment.settle(params.signed, params.requirements),\n });\n\n return {\n settle: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),\n isPending: mutation.isPending,\n error: mutation.error,\n };\n}\n"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { createContext, useState, useMemo, useEffect, useContext } from 'react';
|
|
2
|
+
import { UPSClient } from '@gatewayfm/ups-sdk';
|
|
3
|
+
import { create } from 'zustand';
|
|
4
|
+
import { jsx } from 'react/jsx-runtime';
|
|
5
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
6
|
+
|
|
7
|
+
// src/provider.tsx
|
|
8
|
+
var useUPSStore = create((set) => ({
|
|
9
|
+
client: null,
|
|
10
|
+
setClient: (client) => set({ client }),
|
|
11
|
+
walletState: {
|
|
12
|
+
address: null,
|
|
13
|
+
chainId: null,
|
|
14
|
+
isConnected: false,
|
|
15
|
+
provider: null
|
|
16
|
+
},
|
|
17
|
+
setWalletState: (walletState) => set({ walletState }),
|
|
18
|
+
authState: {
|
|
19
|
+
isAuthenticated: false,
|
|
20
|
+
token: null,
|
|
21
|
+
expiresAt: null,
|
|
22
|
+
address: null
|
|
23
|
+
},
|
|
24
|
+
setAuthState: (authState) => set({ authState }),
|
|
25
|
+
currentAccount: null,
|
|
26
|
+
setCurrentAccount: (currentAccount) => set({ currentAccount })
|
|
27
|
+
}));
|
|
28
|
+
var UPSContext = createContext({
|
|
29
|
+
client: null,
|
|
30
|
+
isInitialized: false
|
|
31
|
+
});
|
|
32
|
+
var UPSProvider = ({ config, children }) => {
|
|
33
|
+
const setClient = useUPSStore((state) => state.setClient);
|
|
34
|
+
const setWalletState = useUPSStore((state) => state.setWalletState);
|
|
35
|
+
const setAuthState = useUPSStore((state) => state.setAuthState);
|
|
36
|
+
const [isInitialized, setIsInitialized] = useState(false);
|
|
37
|
+
const client = useMemo(() => {
|
|
38
|
+
return new UPSClient(config);
|
|
39
|
+
}, [JSON.stringify(config)]);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
setClient(client);
|
|
42
|
+
const unsubWallet = client.wallet.onStateChange((state) => {
|
|
43
|
+
setWalletState(state);
|
|
44
|
+
});
|
|
45
|
+
const unsubAuth = client.auth.onStateChange((state) => {
|
|
46
|
+
setAuthState(state);
|
|
47
|
+
});
|
|
48
|
+
setWalletState(client.wallet.state);
|
|
49
|
+
setAuthState(client.auth.state);
|
|
50
|
+
setIsInitialized(true);
|
|
51
|
+
return () => {
|
|
52
|
+
unsubWallet();
|
|
53
|
+
unsubAuth();
|
|
54
|
+
client.destroy();
|
|
55
|
+
setClient(null);
|
|
56
|
+
};
|
|
57
|
+
}, [client, setClient, setWalletState, setAuthState]);
|
|
58
|
+
const value = useMemo(() => ({
|
|
59
|
+
client,
|
|
60
|
+
isInitialized
|
|
61
|
+
}), [client, isInitialized]);
|
|
62
|
+
return /* @__PURE__ */ jsx(UPSContext.Provider, { value, children });
|
|
63
|
+
};
|
|
64
|
+
var useUPSContext = () => useContext(UPSContext);
|
|
65
|
+
var useUPSClient = () => {
|
|
66
|
+
const { client } = useUPSContext();
|
|
67
|
+
if (!client) {
|
|
68
|
+
throw new Error("useUPSClient must be used within a UPSProvider");
|
|
69
|
+
}
|
|
70
|
+
return client;
|
|
71
|
+
};
|
|
72
|
+
function useWallet() {
|
|
73
|
+
const client = useUPSClient();
|
|
74
|
+
const walletState = useUPSStore((state) => state.walletState);
|
|
75
|
+
const connectMutation = useMutation({
|
|
76
|
+
mutationFn: async (provider) => {
|
|
77
|
+
return await client.connect(provider);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
const disconnectMutation = useMutation({
|
|
81
|
+
mutationFn: async () => {
|
|
82
|
+
await client.disconnect();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
state: walletState,
|
|
87
|
+
address: walletState.address,
|
|
88
|
+
chainId: walletState.chainId,
|
|
89
|
+
isConnected: walletState.isConnected,
|
|
90
|
+
connect: connectMutation,
|
|
91
|
+
disconnect: disconnectMutation,
|
|
92
|
+
signMessage: (message) => client.wallet.signMessage(message),
|
|
93
|
+
switchChain: (chainId) => client.wallet.switchChain(chainId)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function useConnect() {
|
|
97
|
+
const { connect } = useWallet();
|
|
98
|
+
const connectFn = async (provider) => {
|
|
99
|
+
if (provider) {
|
|
100
|
+
return connect.mutateAsync(provider);
|
|
101
|
+
}
|
|
102
|
+
if (typeof window !== "undefined" && window.ethereum) {
|
|
103
|
+
return connect.mutateAsync(window.ethereum);
|
|
104
|
+
}
|
|
105
|
+
throw new Error("No EIP-1193 provider found (window.ethereum is undefined)");
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
connect: connectFn,
|
|
109
|
+
isPending: connect.isPending,
|
|
110
|
+
error: connect.error
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function useDisconnect() {
|
|
114
|
+
const { disconnect } = useWallet();
|
|
115
|
+
return {
|
|
116
|
+
disconnect: async () => {
|
|
117
|
+
await disconnect.mutateAsync();
|
|
118
|
+
},
|
|
119
|
+
isPending: disconnect.isPending
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function useAuth() {
|
|
123
|
+
const client = useUPSClient();
|
|
124
|
+
const authState = useUPSStore((state) => state.authState);
|
|
125
|
+
const authenticateMutation = useMutation({
|
|
126
|
+
mutationFn: async () => {
|
|
127
|
+
await client.authenticate();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return {
|
|
131
|
+
state: authState,
|
|
132
|
+
isAuthenticated: authState.isAuthenticated,
|
|
133
|
+
authenticate: authenticateMutation,
|
|
134
|
+
logout: () => client.auth.logout()
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
var accountKeys = {
|
|
138
|
+
all: ["accounts"],
|
|
139
|
+
list: () => [...accountKeys.all, "list"],
|
|
140
|
+
detail: (id) => [...accountKeys.all, "detail", id],
|
|
141
|
+
byWallet: (address) => [...accountKeys.all, "wallet", address]
|
|
142
|
+
};
|
|
143
|
+
function useAccounts() {
|
|
144
|
+
const client = useUPSClient();
|
|
145
|
+
const isAuthenticated = useUPSStore((state) => state.authState.isAuthenticated);
|
|
146
|
+
return useQuery({
|
|
147
|
+
queryKey: accountKeys.list(),
|
|
148
|
+
queryFn: () => client.account.list(),
|
|
149
|
+
enabled: isAuthenticated
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function useAccount(id) {
|
|
153
|
+
const client = useUPSClient();
|
|
154
|
+
const isAuthenticated = useUPSStore((state) => state.authState.isAuthenticated);
|
|
155
|
+
return useQuery({
|
|
156
|
+
queryKey: accountKeys.detail(id),
|
|
157
|
+
queryFn: () => client.account.get(id),
|
|
158
|
+
enabled: isAuthenticated && !!id
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function useAccountByWallet(address) {
|
|
162
|
+
const client = useUPSClient();
|
|
163
|
+
const isAuthenticated = useUPSStore((state) => state.authState.isAuthenticated);
|
|
164
|
+
return useQuery({
|
|
165
|
+
queryKey: accountKeys.byWallet(address || ""),
|
|
166
|
+
queryFn: () => client.account.getByWallet(address),
|
|
167
|
+
enabled: isAuthenticated && !!address
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
function usePredictAddress() {
|
|
171
|
+
const client = useUPSClient();
|
|
172
|
+
const mutation = useMutation({
|
|
173
|
+
mutationFn: (params) => client.account.predictAddress({ ownerAddress: params.owner, salt: params.salt })
|
|
174
|
+
});
|
|
175
|
+
return {
|
|
176
|
+
predictAddress: (owner, salt) => mutation.mutateAsync({ owner, salt }),
|
|
177
|
+
isPending: mutation.isPending,
|
|
178
|
+
error: mutation.error
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function useCreateAccount() {
|
|
182
|
+
const client = useUPSClient();
|
|
183
|
+
const queryClient = useQueryClient();
|
|
184
|
+
const setCurrentAccount = useUPSStore((state) => state.setCurrentAccount);
|
|
185
|
+
const mutation = useMutation({
|
|
186
|
+
mutationFn: (params) => client.account.create(params),
|
|
187
|
+
onSuccess: (data) => {
|
|
188
|
+
setCurrentAccount(data.account);
|
|
189
|
+
queryClient.invalidateQueries({ queryKey: accountKeys.list() });
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return {
|
|
193
|
+
createAccount: mutation.mutateAsync,
|
|
194
|
+
isPending: mutation.isPending,
|
|
195
|
+
error: mutation.error,
|
|
196
|
+
data: mutation.data,
|
|
197
|
+
reset: mutation.reset
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function useCurrentAccount() {
|
|
201
|
+
return useUPSStore((state) => state.currentAccount);
|
|
202
|
+
}
|
|
203
|
+
function generateSalt() {
|
|
204
|
+
const array = new Uint8Array(32);
|
|
205
|
+
if (typeof window !== "undefined" && window.crypto) {
|
|
206
|
+
window.crypto.getRandomValues(array);
|
|
207
|
+
} else {
|
|
208
|
+
for (let i = 0; i < 32; i++) {
|
|
209
|
+
array[i] = Math.floor(Math.random() * 256);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return "0x" + Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
213
|
+
}
|
|
214
|
+
function usePayment() {
|
|
215
|
+
const client = useUPSClient();
|
|
216
|
+
const mutation = useMutation({
|
|
217
|
+
mutationFn: (params) => client.payment.pay({
|
|
218
|
+
requirements: params.requirements,
|
|
219
|
+
from: params.from
|
|
220
|
+
})
|
|
221
|
+
});
|
|
222
|
+
return {
|
|
223
|
+
pay: mutation.mutateAsync,
|
|
224
|
+
isPending: mutation.isPending,
|
|
225
|
+
error: mutation.error,
|
|
226
|
+
data: mutation.data,
|
|
227
|
+
reset: mutation.reset
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function usePaymentVerify() {
|
|
231
|
+
const client = useUPSClient();
|
|
232
|
+
const mutation = useMutation({
|
|
233
|
+
mutationFn: (params) => client.payment.verify(params.signed, params.requirements)
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
verify: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),
|
|
237
|
+
isPending: mutation.isPending,
|
|
238
|
+
error: mutation.error
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function usePaymentSettle() {
|
|
242
|
+
const client = useUPSClient();
|
|
243
|
+
const mutation = useMutation({
|
|
244
|
+
mutationFn: (params) => client.payment.settle(params.signed, params.requirements)
|
|
245
|
+
});
|
|
246
|
+
return {
|
|
247
|
+
settle: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),
|
|
248
|
+
isPending: mutation.isPending,
|
|
249
|
+
error: mutation.error
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export { UPSProvider, generateSalt, useAccount, useAccountByWallet, useAccounts, useAuth, useConnect, useCreateAccount, useCurrentAccount, useDisconnect, usePayment, usePaymentSettle, usePaymentVerify, usePredictAddress, useUPSClient, useUPSContext, useUPSStore, useWallet };
|
|
254
|
+
//# sourceMappingURL=index.mjs.map
|
|
255
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/store.ts","../src/provider.tsx","../src/hooks/use-wallet.ts","../src/hooks/use-auth.ts","../src/hooks/use-account.ts","../src/hooks/use-payment.ts"],"names":["useMutation"],"mappings":";;;;;;;AAqBO,IAAM,WAAA,GAAc,MAAA,CAAiB,CAAC,GAAA,MAAS;AAAA,EAClD,MAAA,EAAQ,IAAA;AAAA,EACR,WAAW,CAAC,MAAA,KAAW,GAAA,CAAI,EAAE,QAAQ,CAAA;AAAA,EAErC,WAAA,EAAa;AAAA,IACT,OAAA,EAAS,IAAA;AAAA,IACT,OAAA,EAAS,IAAA;AAAA,IACT,WAAA,EAAa,KAAA;AAAA,IACb,QAAA,EAAU;AAAA,GACd;AAAA,EACA,gBAAgB,CAAC,WAAA,KAAgB,GAAA,CAAI,EAAE,aAAa,CAAA;AAAA,EAEpD,SAAA,EAAW;AAAA,IACP,eAAA,EAAiB,KAAA;AAAA,IACjB,KAAA,EAAO,IAAA;AAAA,IACP,SAAA,EAAW,IAAA;AAAA,IACX,OAAA,EAAS;AAAA,GACb;AAAA,EACA,cAAc,CAAC,SAAA,KAAc,GAAA,CAAI,EAAE,WAAW,CAAA;AAAA,EAE9C,cAAA,EAAgB,IAAA;AAAA,EAChB,mBAAmB,CAA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OAAO;AAAA,IACH,MAAA,EAAQ,CAAC,MAAA,EAAQ,YAAA,KAAiB,SAAS,WAAA,CAAY,EAAE,MAAA,EAAQ,YAAA,EAAc,CAAA;AAAA,IAC/E,WAAW,QAAA,CAAS,SAAA;AAAA,IACpB,OAAO,QAAA,CAAS;AAAA,GACpB;AACJ","file":"index.mjs","sourcesContent":["import { create } from 'zustand';\nimport type { UPSClient, WalletState, AuthState, Account } from '@gatewayfm/ups-sdk';\n\nexport interface UPSStore {\n // Client instance\n client: UPSClient | null;\n setClient: (client: UPSClient | null) => void;\n\n // Wallet state (synced from SDK)\n walletState: WalletState;\n setWalletState: (state: WalletState) => void;\n\n // Auth state (synced from SDK)\n authState: AuthState;\n setAuthState: (state: AuthState) => void;\n\n // Current user's account (after creation)\n currentAccount: Account | null;\n setCurrentAccount: (account: Account | null) => void;\n}\n\nexport const useUPSStore = create<UPSStore>((set) => ({\n client: null,\n setClient: (client) => set({ client }),\n\n walletState: {\n address: null,\n chainId: null,\n isConnected: false,\n provider: null,\n },\n setWalletState: (walletState) => set({ walletState }),\n\n authState: {\n isAuthenticated: false,\n token: null,\n expiresAt: null,\n address: null,\n },\n setAuthState: (authState) => set({ authState }),\n\n currentAccount: null,\n setCurrentAccount: (currentAccount) => set({ currentAccount }),\n}));\n","import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';\nimport { UPSClient, type UPSConfig } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from './store';\n\ninterface UPSContextValue {\n client: UPSClient | null;\n isInitialized: boolean;\n}\n\nconst UPSContext = createContext<UPSContextValue>({\n client: null,\n isInitialized: false,\n});\n\nexport interface UPSProviderProps {\n config: UPSConfig;\n children: React.ReactNode;\n}\n\nexport const UPSProvider = ({ config, children }: UPSProviderProps) => {\n const setClient = useUPSStore((state) => state.setClient);\n const setWalletState = useUPSStore((state) => state.setWalletState);\n const setAuthState = useUPSStore((state) => state.setAuthState);\n const [isInitialized, setIsInitialized] = useState(false);\n\n // Initialize client only once (or when config deep changes, but strictly speaking config shouldn't change often)\n // We use useMemo to hold the instance.\n const client = useMemo(() => {\n return new UPSClient(config);\n }, [JSON.stringify(config)]);\n\n useEffect(() => {\n setClient(client);\n\n // Subscribe to state changes\n const unsubWallet = client.wallet.onStateChange((state) => {\n setWalletState(state);\n });\n\n const unsubAuth = client.auth.onStateChange((state) => {\n setAuthState(state);\n });\n\n // Initial sync\n setWalletState(client.wallet.state);\n setAuthState(client.auth.state);\n\n setIsInitialized(true);\n\n return () => {\n unsubWallet();\n unsubAuth();\n client.destroy();\n setClient(null);\n };\n }, [client, setClient, setWalletState, setAuthState]);\n\n const value = useMemo(() => ({\n client,\n isInitialized\n }), [client, isInitialized]);\n\n return <UPSContext.Provider value={value}>{children}</UPSContext.Provider>;\n};\n\nexport const useUPSContext = () => useContext(UPSContext);\n\nexport const useUPSClient = () => {\n const { client } = useUPSContext();\n if (!client) {\n throw new Error('useUPSClient must be used within a UPSProvider');\n }\n return client;\n};\n","import { useMutation, UseMutationResult } from '@tanstack/react-query';\nimport { type EIP1193Provider, type ConnectedWallet, type WalletState } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from '../store';\nimport { useUPSClient } from '../provider';\n\nexport interface UseWalletReturn {\n // State\n state: WalletState;\n address: string | null;\n chainId: number | null;\n isConnected: boolean;\n\n // Actions (TanStack Query mutations)\n connect: UseMutationResult<ConnectedWallet, Error, EIP1193Provider>;\n disconnect: UseMutationResult<void, Error, void>;\n\n // Direct methods\n signMessage: (message: string) => Promise<string>;\n switchChain: (chainId: number) => Promise<void>;\n}\n\nexport function useWallet(): UseWalletReturn {\n const client = useUPSClient();\n const walletState = useUPSStore((state) => state.walletState);\n\n const connectMutation = useMutation({\n mutationFn: async (provider: EIP1193Provider) => {\n return await client.connect(provider);\n },\n });\n\n const disconnectMutation = useMutation({\n mutationFn: async () => {\n await client.disconnect();\n },\n });\n\n return {\n state: walletState,\n address: walletState.address,\n chainId: walletState.chainId,\n isConnected: walletState.isConnected,\n connect: connectMutation,\n disconnect: disconnectMutation,\n signMessage: (message: string) => client.wallet.signMessage(message),\n switchChain: (chainId: number) => client.wallet.switchChain(chainId),\n };\n}\n\nexport interface UseConnectReturn {\n connect: (provider?: EIP1193Provider) => Promise<ConnectedWallet>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function useConnect(): UseConnectReturn {\n const { connect } = useWallet();\n\n const connectFn = async (provider?: EIP1193Provider) => {\n if (provider) {\n return connect.mutateAsync(provider);\n }\n\n // Default to window.ethereum\n if (typeof window !== 'undefined' && (window as unknown as { ethereum: EIP1193Provider }).ethereum) {\n return connect.mutateAsync((window as unknown as { ethereum: EIP1193Provider }).ethereum);\n }\n\n throw new Error('No EIP-1193 provider found (window.ethereum is undefined)');\n };\n\n return {\n connect: connectFn,\n isPending: connect.isPending,\n error: connect.error,\n };\n}\n\nexport interface UseDisconnectReturn {\n disconnect: () => Promise<void>;\n isPending: boolean;\n}\n\nexport function useDisconnect(): UseDisconnectReturn {\n const { disconnect } = useWallet();\n\n return {\n disconnect: async () => {\n await disconnect.mutateAsync();\n },\n isPending: disconnect.isPending,\n };\n}\n","import { useMutation, UseMutationResult } from '@tanstack/react-query';\nimport { type AuthState } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from '../store';\nimport { useUPSClient } from '../provider';\n\nexport interface UseAuthReturn {\n // State\n state: AuthState;\n isAuthenticated: boolean;\n\n // Actions\n authenticate: UseMutationResult<void, Error, void>;\n logout: () => void;\n}\n\nexport function useAuth(): UseAuthReturn {\n const client = useUPSClient();\n const authState = useUPSStore((state) => state.authState);\n\n const authenticateMutation = useMutation({\n mutationFn: async () => {\n await client.authenticate();\n },\n });\n\n return {\n state: authState,\n isAuthenticated: authState.isAuthenticated,\n authenticate: authenticateMutation,\n logout: () => client.auth.logout(),\n };\n}\n","import { useQuery, useMutation, useQueryClient, UseQueryResult } from '@tanstack/react-query';\nimport { type Account, type CreateAccountParams, type CreateAccountResponse } from '@gatewayfm/ups-sdk';\nimport { useUPSStore } from '../store';\nimport { useUPSClient } from '../provider';\n\nconst accountKeys = {\n all: ['accounts'] as const,\n list: () => [...accountKeys.all, 'list'] as const,\n detail: (id: string) => [...accountKeys.all, 'detail', id] as const,\n byWallet: (address: string) => [...accountKeys.all, 'wallet', address] as const,\n};\n\nexport function useAccounts(): UseQueryResult<Account[], Error> {\n const client = useUPSClient();\n const isAuthenticated = useUPSStore(state => state.authState.isAuthenticated);\n\n return useQuery({\n queryKey: accountKeys.list(),\n queryFn: () => client.account.list(),\n enabled: isAuthenticated,\n });\n}\n\nexport function useAccount(id: string): UseQueryResult<Account, Error> {\n const client = useUPSClient();\n const isAuthenticated = useUPSStore(state => state.authState.isAuthenticated);\n\n return useQuery({\n queryKey: accountKeys.detail(id),\n queryFn: () => client.account.get(id),\n enabled: isAuthenticated && !!id,\n });\n}\n\nexport function useAccountByWallet(address: string | null): UseQueryResult<Account, Error> {\n const client = useUPSClient();\n const isAuthenticated = useUPSStore(state => state.authState.isAuthenticated);\n\n return useQuery({\n queryKey: accountKeys.byWallet(address || ''),\n queryFn: () => client.account.getByWallet(address!),\n enabled: isAuthenticated && !!address,\n });\n}\n\nexport interface UsePredictAddressReturn {\n predictAddress: (owner: string, salt: string) => Promise<string>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function usePredictAddress(): UsePredictAddressReturn {\n const client = useUPSClient();\n const mutation = useMutation({\n mutationFn: (params: { owner: string, salt: string }) =>\n client.account.predictAddress({ ownerAddress: params.owner, salt: params.salt }),\n });\n\n return {\n predictAddress: (owner, salt) => mutation.mutateAsync({ owner, salt }),\n isPending: mutation.isPending,\n error: mutation.error,\n };\n}\n\nexport interface UseCreateAccountReturn {\n createAccount: (params: CreateAccountParams) => Promise<CreateAccountResponse>;\n isPending: boolean;\n error: Error | null;\n data: CreateAccountResponse | undefined;\n reset: () => void;\n}\n\nexport function useCreateAccount(): UseCreateAccountReturn {\n const client = useUPSClient();\n const queryClient = useQueryClient();\n const setCurrentAccount = useUPSStore(state => state.setCurrentAccount);\n\n const mutation = useMutation({\n mutationFn: (params: CreateAccountParams) => client.account.create(params),\n onSuccess: (data) => {\n setCurrentAccount(data.account);\n queryClient.invalidateQueries({ queryKey: accountKeys.list() });\n },\n });\n\n return {\n createAccount: mutation.mutateAsync,\n isPending: mutation.isPending,\n error: mutation.error,\n data: mutation.data,\n reset: mutation.reset,\n };\n}\n\nexport function useCurrentAccount(): Account | null {\n return useUPSStore(state => state.currentAccount);\n}\n\nexport function generateSalt(): string {\n const array = new Uint8Array(32);\n if (typeof window !== 'undefined' && window.crypto) {\n window.crypto.getRandomValues(array);\n } else {\n // Fallback for non-browser environments if needed (e.g. during build/test)\n for (let i = 0; i < 32; i++) {\n array[i] = Math.floor(Math.random() * 256);\n }\n }\n return '0x' + Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');\n}\n","import { useMutation } from '@tanstack/react-query';\nimport {\n type PaymentRequirements,\n type SignedAuthorization,\n type VerifyResponse,\n type SettleResponse\n} from '@gatewayfm/ups-sdk';\nimport { useUPSClient } from '../provider';\n\nexport interface UsePaymentParams {\n requirements: PaymentRequirements;\n from: string; // Payer SmartAccount address\n}\n\n// PaymentResult is SettleResponse from SDK type\nexport interface UsePaymentReturn {\n pay: (params: UsePaymentParams) => Promise<SettleResponse>;\n isPending: boolean;\n error: Error | null;\n data: SettleResponse | undefined;\n reset: () => void;\n}\n\nexport function usePayment(): UsePaymentReturn {\n const client = useUPSClient();\n\n // client.payment.pay takes { requirements, from }\n const mutation = useMutation({\n mutationFn: (params: UsePaymentParams) => client.payment.pay({\n requirements: params.requirements,\n from: params.from\n }),\n });\n\n return {\n pay: mutation.mutateAsync,\n isPending: mutation.isPending,\n error: mutation.error,\n data: mutation.data,\n reset: mutation.reset,\n };\n}\n\nexport interface UsePaymentVerifyReturn {\n verify: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<VerifyResponse>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function usePaymentVerify(): UsePaymentVerifyReturn {\n const client = useUPSClient();\n\n const mutation = useMutation({\n mutationFn: (params: { signed: SignedAuthorization, requirements: PaymentRequirements }) =>\n client.payment.verify(params.signed, params.requirements),\n });\n\n return {\n verify: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),\n isPending: mutation.isPending,\n error: mutation.error,\n };\n}\n\nexport interface UsePaymentSettleReturn {\n settle: (signed: SignedAuthorization, requirements: PaymentRequirements) => Promise<SettleResponse>;\n isPending: boolean;\n error: Error | null;\n}\n\nexport function usePaymentSettle(): UsePaymentSettleReturn {\n const client = useUPSClient();\n\n const mutation = useMutation({\n mutationFn: (params: { signed: SignedAuthorization, requirements: PaymentRequirements }) =>\n client.payment.settle(params.signed, params.requirements),\n });\n\n return {\n settle: (signed, requirements) => mutation.mutateAsync({ signed, requirements }),\n isPending: mutation.isPending,\n error: mutation.error,\n };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gatewayfm/ups-react",
|
|
3
|
+
"version": "0.1.13",
|
|
4
|
+
"description": "React hooks and providers for UPS x402 SDK",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public",
|
|
10
|
+
"registry": "https://registry.npmjs.org"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/gateway-fm/ups-typescript.git"
|
|
15
|
+
},
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"zustand": "^5.0.10",
|
|
28
|
+
"@gatewayfm/ups-sdk": "0.1.13"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"react": "^19.2.4",
|
|
32
|
+
"@tanstack/react-query": "^5.0.0",
|
|
33
|
+
"@gatewayfm/ups-sdk": "0.1.13"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"react": "^19.2.4",
|
|
37
|
+
"@types/react": "^19.2.10",
|
|
38
|
+
"tsup": "^8.0.0",
|
|
39
|
+
"typescript": "^5.5.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsup",
|
|
43
|
+
"dev": "tsup --watch",
|
|
44
|
+
"lint": "eslint .",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
}
|
|
47
|
+
}
|