@explorins/pers-sdk-react-native 1.3.2 → 1.5.2
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 +276 -123
- package/dist/hooks/index.d.ts +10 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useAnalytics.d.ts +96 -0
- package/dist/hooks/useAnalytics.d.ts.map +1 -0
- package/dist/hooks/useAnalytics.js +73 -0
- package/dist/hooks/useAuth.d.ts +52 -7
- package/dist/hooks/useAuth.d.ts.map +1 -1
- package/dist/hooks/useAuth.js +250 -3
- package/dist/hooks/useBusiness.d.ts +45 -3
- package/dist/hooks/useBusiness.d.ts.map +1 -1
- package/dist/hooks/useBusiness.js +182 -28
- package/dist/hooks/useCampaigns.d.ts +11 -4
- package/dist/hooks/useCampaigns.d.ts.map +1 -1
- package/dist/hooks/useCampaigns.js +101 -39
- package/dist/hooks/useDonations.d.ts +33 -0
- package/dist/hooks/useDonations.d.ts.map +1 -0
- package/dist/hooks/useDonations.js +62 -0
- package/dist/hooks/useFiles.d.ts +50 -0
- package/dist/hooks/useFiles.d.ts.map +1 -0
- package/dist/hooks/useFiles.js +140 -0
- package/dist/hooks/usePurchases.d.ts +9 -0
- package/dist/hooks/usePurchases.d.ts.map +1 -0
- package/dist/hooks/usePurchases.js +56 -0
- package/dist/hooks/useRedemptions.d.ts +10 -5
- package/dist/hooks/useRedemptions.d.ts.map +1 -1
- package/dist/hooks/useRedemptions.js +118 -66
- package/dist/hooks/useTenants.d.ts +10 -0
- package/dist/hooks/useTenants.d.ts.map +1 -0
- package/dist/hooks/useTenants.js +68 -0
- package/dist/hooks/useTokens.d.ts +36 -2
- package/dist/hooks/useTokens.d.ts.map +1 -1
- package/dist/hooks/useTokens.js +180 -19
- package/dist/hooks/useTransactionSigner.d.ts +73 -0
- package/dist/hooks/useTransactionSigner.d.ts.map +1 -0
- package/dist/hooks/useTransactionSigner.js +216 -0
- package/dist/hooks/useTransactions.d.ts +45 -3
- package/dist/hooks/useTransactions.d.ts.map +1 -1
- package/dist/hooks/useTransactions.js +149 -39
- package/dist/hooks/useUserStatus.d.ts +9 -0
- package/dist/hooks/useUserStatus.d.ts.map +1 -0
- package/dist/hooks/useUserStatus.js +57 -0
- package/dist/hooks/useUsers.d.ts +17 -0
- package/dist/hooks/useUsers.d.ts.map +1 -0
- package/dist/hooks/useUsers.js +120 -0
- package/dist/hooks/useWeb3.d.ts +69 -6
- package/dist/hooks/useWeb3.d.ts.map +1 -1
- package/dist/hooks/useWeb3.js +177 -17
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31540 -794
- package/dist/index.js.map +1 -1
- package/dist/polyfills/index.d.ts +1 -1
- package/dist/polyfills/index.d.ts.map +1 -1
- package/dist/polyfills/index.js +155 -9
- package/dist/polyfills/web3-polyfills.d.ts +7 -0
- package/dist/polyfills/web3-polyfills.d.ts.map +1 -0
- package/dist/polyfills/web3-polyfills.js +85 -0
- package/dist/providers/PersSDKProvider.d.ts +28 -18
- package/dist/providers/PersSDKProvider.d.ts.map +1 -1
- package/dist/providers/PersSDKProvider.js +63 -129
- package/dist/providers/react-native-auth-provider.d.ts +19 -64
- package/dist/providers/react-native-auth-provider.d.ts.map +1 -1
- package/dist/providers/react-native-auth-provider.js +57 -175
- package/package.json +177 -157
- package/src/hooks/index.ts +13 -1
- package/src/hooks/useAnalytics.ts +150 -0
- package/src/hooks/useAuth.ts +286 -27
- package/src/hooks/useBusiness.ts +193 -30
- package/src/hooks/useCampaigns.ts +121 -43
- package/src/hooks/useDonations.ts +68 -0
- package/src/hooks/useFiles.ts +160 -0
- package/src/hooks/usePurchases.ts +69 -0
- package/src/hooks/useRedemptions.ts +135 -68
- package/src/hooks/useTenants.ts +77 -0
- package/src/hooks/useTokens.ts +189 -21
- package/src/hooks/useTransactionSigner.ts +287 -0
- package/src/hooks/useTransactions.ts +163 -41
- package/src/hooks/useUserStatus.ts +65 -0
- package/src/hooks/useUsers.ts +133 -0
- package/src/hooks/useWeb3.ts +201 -21
- package/src/index.ts +22 -17
- package/src/polyfills/index.ts +163 -10
- package/src/polyfills/web3-polyfills.ts +98 -0
- package/src/providers/PersSDKProvider.tsx +114 -178
- package/src/providers/react-native-auth-provider.ts +65 -208
- package/dist/index.esm.js +0 -1057
- package/dist/index.esm.js.map +0 -1
- package/dist/polyfills/index.simple.d.ts +0 -2
- package/dist/polyfills/index.simple.d.ts.map +0 -1
- package/dist/polyfills/index.simple.js +0 -17
- package/src/polyfills/index.simple.ts +0 -22
- package/src/types/external-modules.d.ts +0 -13
package/dist/hooks/useTokens.js
CHANGED
|
@@ -1,48 +1,209 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
/**
|
|
4
|
+
* React hook for token operations in the PERS SDK
|
|
5
|
+
*
|
|
6
|
+
* Provides methods for fetching various types of tokens including credit, reward, and status tokens.
|
|
7
|
+
* Supports both general token queries and specific contract-based token retrieval.
|
|
8
|
+
*
|
|
9
|
+
* @returns Token hook with methods for token management
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* function TokensComponent() {
|
|
14
|
+
* const { getTokens, getActiveCreditToken, getRewardTokens } = useTokens();
|
|
15
|
+
*
|
|
16
|
+
* const loadTokens = async () => {
|
|
17
|
+
* try {
|
|
18
|
+
* const tokens = await getTokens();
|
|
19
|
+
* const creditToken = await getActiveCreditToken();
|
|
20
|
+
* console.log('Loaded tokens:', tokens);
|
|
21
|
+
* console.log('Active credit token:', creditToken);
|
|
22
|
+
* } catch (error) {
|
|
23
|
+
* console.error('Failed to load tokens:', error);
|
|
24
|
+
* }
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* return <button onClick={loadTokens}>Load Tokens</button>;
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
3
31
|
export const useTokens = () => {
|
|
4
|
-
const {
|
|
32
|
+
const { sdk, isInitialized, isAuthenticated } = usePersSDK();
|
|
5
33
|
if (!isAuthenticated && isInitialized) {
|
|
6
34
|
console.warn('SDK not authenticated. Some token operations may fail.');
|
|
7
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Retrieves all tokens available to the current user
|
|
38
|
+
*
|
|
39
|
+
* @returns Promise resolving to array of tokens
|
|
40
|
+
* @throws Error if SDK is not initialized
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const { getTokens } = useTokens();
|
|
45
|
+
* const tokens = await getTokens();
|
|
46
|
+
* console.log('Available tokens:', tokens);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
8
49
|
const getTokens = useCallback(async () => {
|
|
9
|
-
if (!isInitialized) {
|
|
50
|
+
if (!isInitialized || !sdk) {
|
|
10
51
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
11
52
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
53
|
+
try {
|
|
54
|
+
const result = await sdk.tokens.getTokens();
|
|
55
|
+
console.log('Tokens fetched successfully:', result);
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error('Failed to fetch tokens:', error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}, [sdk, isInitialized]);
|
|
63
|
+
/**
|
|
64
|
+
* Retrieves the currently active credit token
|
|
65
|
+
*
|
|
66
|
+
* @returns Promise resolving to active credit token
|
|
67
|
+
* @throws Error if SDK is not initialized
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const { getActiveCreditToken } = useTokens();
|
|
72
|
+
* const creditToken = await getActiveCreditToken();
|
|
73
|
+
* console.log('Active credit token:', creditToken.name);
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
const getActiveCreditToken = useCallback(async () => {
|
|
77
|
+
if (!isInitialized || !sdk) {
|
|
78
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const result = await sdk.tokens.getActiveCreditToken();
|
|
82
|
+
console.log('Active credit token fetched successfully:', result);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
console.error('Failed to fetch active credit token:', error);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}, [sdk, isInitialized]);
|
|
90
|
+
/**
|
|
91
|
+
* Retrieves all reward tokens available in the system
|
|
92
|
+
*
|
|
93
|
+
* @returns Promise resolving to array of reward tokens
|
|
94
|
+
* @throws Error if SDK is not initialized
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const { getRewardTokens } = useTokens();
|
|
99
|
+
* const rewardTokens = await getRewardTokens();
|
|
100
|
+
* console.log('Reward tokens:', rewardTokens);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
const getRewardTokens = useCallback(async () => {
|
|
104
|
+
if (!isInitialized || !sdk) {
|
|
105
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
15
106
|
}
|
|
16
107
|
try {
|
|
17
|
-
const result = await tokens.
|
|
18
|
-
console.log('
|
|
108
|
+
const result = await sdk.tokens.getRewardTokens();
|
|
109
|
+
console.log('Reward tokens fetched successfully:', result);
|
|
19
110
|
return result;
|
|
20
111
|
}
|
|
21
112
|
catch (error) {
|
|
22
|
-
console.error('
|
|
113
|
+
console.error('Failed to fetch reward tokens:', error);
|
|
23
114
|
throw error;
|
|
24
115
|
}
|
|
25
|
-
}, [
|
|
26
|
-
|
|
27
|
-
|
|
116
|
+
}, [sdk, isInitialized]);
|
|
117
|
+
/**
|
|
118
|
+
* Retrieves all available token types in the system
|
|
119
|
+
*
|
|
120
|
+
* @returns Promise resolving to array of token types
|
|
121
|
+
* @throws Error if SDK is not initialized
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const { getTokenTypes } = useTokens();
|
|
126
|
+
* const types = await getTokenTypes();
|
|
127
|
+
* console.log('Token types:', types);
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
const getTokenTypes = useCallback(async () => {
|
|
131
|
+
if (!isInitialized || !sdk) {
|
|
28
132
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
29
133
|
}
|
|
30
|
-
|
|
31
|
-
|
|
134
|
+
try {
|
|
135
|
+
const result = await sdk.tokens.getTokenTypes();
|
|
136
|
+
console.log('Token types fetched successfully:', result);
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
console.error('Failed to fetch token types:', error);
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}, [sdk, isInitialized]);
|
|
144
|
+
/**
|
|
145
|
+
* Retrieves all status tokens (tokens that represent user status/achievements)
|
|
146
|
+
*
|
|
147
|
+
* @returns Promise resolving to array of status tokens
|
|
148
|
+
* @throws Error if SDK is not initialized
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const { getStatusTokens } = useTokens();
|
|
153
|
+
* const statusTokens = await getStatusTokens();
|
|
154
|
+
* console.log('Status tokens:', statusTokens);
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
const getStatusTokens = useCallback(async () => {
|
|
158
|
+
if (!isInitialized || !sdk) {
|
|
159
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const result = await sdk.tokens.getStatusTokens();
|
|
163
|
+
console.log('Status tokens fetched successfully:', result);
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
console.error('Failed to fetch status tokens:', error);
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
}, [sdk, isInitialized]);
|
|
171
|
+
/**
|
|
172
|
+
* Retrieves a specific token by its contract address and optional token ID
|
|
173
|
+
*
|
|
174
|
+
* @param contractAddress - The contract address of the token
|
|
175
|
+
* @param contractTokenId - Optional specific token ID within the contract
|
|
176
|
+
* @returns Promise resolving to the token data
|
|
177
|
+
* @throws Error if SDK is not initialized
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const { getTokenByContract } = useTokens();
|
|
182
|
+
* const token = await getTokenByContract('0x123...', 'token-1');
|
|
183
|
+
* console.log('Token from contract:', token);
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
const getTokenByContract = useCallback(async (contractAddress, contractTokenId) => {
|
|
187
|
+
if (!isInitialized || !sdk) {
|
|
188
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
32
189
|
}
|
|
33
190
|
try {
|
|
34
|
-
const result = await tokens.
|
|
35
|
-
console.log('
|
|
191
|
+
const result = await sdk.tokens.getTokenByContract(contractAddress, contractTokenId);
|
|
192
|
+
console.log('Token by contract fetched successfully:', result);
|
|
36
193
|
return result;
|
|
37
194
|
}
|
|
38
195
|
catch (error) {
|
|
39
|
-
console.error('
|
|
196
|
+
console.error('Failed to fetch token by contract:', error);
|
|
40
197
|
throw error;
|
|
41
198
|
}
|
|
42
|
-
}, [
|
|
199
|
+
}, [sdk, isInitialized]);
|
|
43
200
|
return {
|
|
44
201
|
getTokens,
|
|
45
|
-
|
|
46
|
-
|
|
202
|
+
getActiveCreditToken,
|
|
203
|
+
getRewardTokens,
|
|
204
|
+
getTokenTypes,
|
|
205
|
+
getStatusTokens,
|
|
206
|
+
getTokenByContract,
|
|
207
|
+
isAvailable: isInitialized && !!sdk?.tokens,
|
|
47
208
|
};
|
|
48
209
|
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
interface TransactionSigningResult {
|
|
2
|
+
success: boolean;
|
|
3
|
+
transactionHash?: string;
|
|
4
|
+
signature?: string;
|
|
5
|
+
error?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* React Native hook for blockchain transaction signing using PERS Signer SDK
|
|
9
|
+
*
|
|
10
|
+
* Provides WebAuthn-based transaction signing capabilities integrated with PERS ecosystem.
|
|
11
|
+
* Automatically handles user authentication and transaction signing workflows.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* function TransactionComponent() {
|
|
16
|
+
* const { signTransaction, initializeSigner, isSignerAvailable } = useTransactionSigner();
|
|
17
|
+
*
|
|
18
|
+
* useEffect(() => {
|
|
19
|
+
* initializeSigner({
|
|
20
|
+
* tenantId: 'my-tenant',
|
|
21
|
+
* ethersProviderUrl: 'https://sepolia.infura.io/v3/...'
|
|
22
|
+
* });
|
|
23
|
+
* }, []);
|
|
24
|
+
*
|
|
25
|
+
* const handleSign = async () => {
|
|
26
|
+
* try {
|
|
27
|
+
* const result = await signTransaction('transaction-id-123');
|
|
28
|
+
* if (result.success) {
|
|
29
|
+
* console.log('Transaction signed:', result.transactionHash);
|
|
30
|
+
* }
|
|
31
|
+
* } catch (error) {
|
|
32
|
+
* console.error('Signing failed:', error);
|
|
33
|
+
* }
|
|
34
|
+
* };
|
|
35
|
+
*
|
|
36
|
+
* return (
|
|
37
|
+
* <button onClick={handleSign} disabled={!isSignerAvailable}>
|
|
38
|
+
* Sign Transaction
|
|
39
|
+
* </button>
|
|
40
|
+
* );
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare const useTransactionSigner: () => {
|
|
45
|
+
/**
|
|
46
|
+
* Sign a blockchain transaction with WebAuthn authentication
|
|
47
|
+
*/
|
|
48
|
+
signTransaction: (jwt: string) => Promise<TransactionSigningResult>;
|
|
49
|
+
/**
|
|
50
|
+
* Initialize the transaction signer with configuration
|
|
51
|
+
*/
|
|
52
|
+
initializeSigner: (config?: {
|
|
53
|
+
tenantId?: string;
|
|
54
|
+
ethersProviderUrl?: string;
|
|
55
|
+
}) => Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Whether the transaction signer has been initialized
|
|
58
|
+
*/
|
|
59
|
+
isSignerInitialized: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Whether transaction signing is available (all requirements met)
|
|
62
|
+
*/
|
|
63
|
+
isSignerAvailable: boolean;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Type definition for the transaction signer hook
|
|
67
|
+
*/
|
|
68
|
+
export type TransactionSignerHook = ReturnType<typeof useTransactionSigner>;
|
|
69
|
+
/**
|
|
70
|
+
* Export the transaction signing result type for external usage
|
|
71
|
+
*/
|
|
72
|
+
export type { TransactionSigningResult };
|
|
73
|
+
//# sourceMappingURL=useTransactionSigner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTransactionSigner.d.ts","sourceRoot":"","sources":["../../src/hooks/useTransactionSigner.ts"],"names":[],"mappings":"AAmEA,UAAU,wBAAwB;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAKD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,oBAAoB;IA8I7B;;OAEG;2BA/D2C,MAAM,KAAG,QAAQ,wBAAwB,CAAC;IAkExF;;OAEG;gCApHgD;QACnD,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,CAAC,EAAE,MAAM,CAAC;KAC5B;IAoHC;;OAEG;;IAGH;;OAEG;;CAGN,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAE5E;;GAEG;AACH,YAAY,EAAE,wBAAwB,EAAE,CAAC"}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { useCallback, useState, useRef, useEffect } from 'react';
|
|
2
|
+
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
// Dynamic import the signer SDK to avoid build issues with static dependencies
|
|
4
|
+
let createPersSignerSDK = null;
|
|
5
|
+
try {
|
|
6
|
+
const signerModule = require('@explorins/pers-signer/react-native');
|
|
7
|
+
createPersSignerSDK = signerModule.createPersSignerSDK;
|
|
8
|
+
console.log('[useTransactionSigner] PERS Signer SDK loaded successfully');
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
console.warn('[useTransactionSigner] PERS Signer SDK not available:', error.message);
|
|
12
|
+
console.warn('[useTransactionSigner] Real blockchain signing will not be available');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Utility to decode JWT and extract user information
|
|
16
|
+
* This will be used to extract user data for the signer
|
|
17
|
+
*/
|
|
18
|
+
const extractUserInfoFromJWT = async (jwt) => {
|
|
19
|
+
try {
|
|
20
|
+
// Dynamically import jwt-decode to avoid bundling issues
|
|
21
|
+
const { jwtDecode } = await import('jwt-decode');
|
|
22
|
+
const decoded = jwtDecode(jwt);
|
|
23
|
+
return {
|
|
24
|
+
userId: decoded.user_id || decoded.userId || decoded.uid || decoded.sub,
|
|
25
|
+
email: decoded.email,
|
|
26
|
+
sub: decoded.sub
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.warn('[useTransactionSigner] Failed to decode JWT:', error);
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
// Constants - TODO: Move to environment config later
|
|
35
|
+
const DEFAULT_ETHERS_PROVIDER = "https://sepolia.infura.io/v3/2781b4b5242343d5b0954c98f287b29e";
|
|
36
|
+
/**
|
|
37
|
+
* React Native hook for blockchain transaction signing using PERS Signer SDK
|
|
38
|
+
*
|
|
39
|
+
* Provides WebAuthn-based transaction signing capabilities integrated with PERS ecosystem.
|
|
40
|
+
* Automatically handles user authentication and transaction signing workflows.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* function TransactionComponent() {
|
|
45
|
+
* const { signTransaction, initializeSigner, isSignerAvailable } = useTransactionSigner();
|
|
46
|
+
*
|
|
47
|
+
* useEffect(() => {
|
|
48
|
+
* initializeSigner({
|
|
49
|
+
* tenantId: 'my-tenant',
|
|
50
|
+
* ethersProviderUrl: 'https://sepolia.infura.io/v3/...'
|
|
51
|
+
* });
|
|
52
|
+
* }, []);
|
|
53
|
+
*
|
|
54
|
+
* const handleSign = async () => {
|
|
55
|
+
* try {
|
|
56
|
+
* const result = await signTransaction('transaction-id-123');
|
|
57
|
+
* if (result.success) {
|
|
58
|
+
* console.log('Transaction signed:', result.transactionHash);
|
|
59
|
+
* }
|
|
60
|
+
* } catch (error) {
|
|
61
|
+
* console.error('Signing failed:', error);
|
|
62
|
+
* }
|
|
63
|
+
* };
|
|
64
|
+
*
|
|
65
|
+
* return (
|
|
66
|
+
* <button onClick={handleSign} disabled={!isSignerAvailable}>
|
|
67
|
+
* Sign Transaction
|
|
68
|
+
* </button>
|
|
69
|
+
* );
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export const useTransactionSigner = () => {
|
|
74
|
+
const { sdk, isInitialized, isAuthenticated, user } = usePersSDK();
|
|
75
|
+
const [isSignerInitialized, setIsSignerInitialized] = useState(false);
|
|
76
|
+
const signerSDKRef = useRef(null);
|
|
77
|
+
// Auto-initialize signer when user is authenticated and real SDK is available
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (isInitialized && isAuthenticated && user && !isSignerInitialized) {
|
|
80
|
+
console.log('[useTransactionSigner] Auto-initializing PERS transaction signer...');
|
|
81
|
+
initializeSigner({
|
|
82
|
+
tenantId: 'auto-tenant', // TODO: Get from SDK config or environment
|
|
83
|
+
ethersProviderUrl: DEFAULT_ETHERS_PROVIDER
|
|
84
|
+
}).catch((error) => {
|
|
85
|
+
console.error('[useTransactionSigner] Auto-initialization failed:', error);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}, [isInitialized, isAuthenticated, user, isSignerInitialized, createPersSignerSDK]);
|
|
89
|
+
/**
|
|
90
|
+
* Initialize the blockchain signer with configuration
|
|
91
|
+
*
|
|
92
|
+
* @param config - Signer configuration options
|
|
93
|
+
* @param config.tenantId - Multi-tenant identifier for the signer
|
|
94
|
+
* @param config.ethersProviderUrl - Custom blockchain provider URL
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* await initializeSigner({
|
|
99
|
+
* tenantId: 'my-tenant-id',
|
|
100
|
+
* ethersProviderUrl: 'https://sepolia.infura.io/v3/your-key'
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
const initializeSigner = useCallback(async (config) => {
|
|
105
|
+
if (!createPersSignerSDK) {
|
|
106
|
+
throw new Error('PERS Signer SDK not available. Please ensure dependencies are properly installed.');
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
console.log('[useTransactionSigner] Initializing PERS transaction signer...');
|
|
110
|
+
const signerSDK = await createPersSignerSDK({
|
|
111
|
+
tenantId: config?.tenantId,
|
|
112
|
+
ethersProviderUrl: config?.ethersProviderUrl || DEFAULT_ETHERS_PROVIDER
|
|
113
|
+
});
|
|
114
|
+
signerSDKRef.current = signerSDK;
|
|
115
|
+
setIsSignerInitialized(true);
|
|
116
|
+
console.log('[useTransactionSigner] PERS Signer SDK initialized successfully');
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
console.error('[useTransactionSigner] Failed to initialize transaction signer:', error);
|
|
120
|
+
throw new Error(`Signer initialization failed: ${error}`);
|
|
121
|
+
}
|
|
122
|
+
}, []);
|
|
123
|
+
/**
|
|
124
|
+
* Sign a blockchain transaction using WebAuthn authentication
|
|
125
|
+
*
|
|
126
|
+
* @param jwt - JWT token containing transaction information and user context
|
|
127
|
+
* @returns Promise resolving to signing result with transaction hash
|
|
128
|
+
*
|
|
129
|
+
* @throws {Error} When SDK not initialized, user not authenticated, or signer not initialized
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* try {
|
|
134
|
+
* const result = await signTransaction(jwtToken);
|
|
135
|
+
* if (result.success) {
|
|
136
|
+
* console.log('Signed transaction hash:', result.transactionHash);
|
|
137
|
+
* } else {
|
|
138
|
+
* console.error('Signing failed:', result.error);
|
|
139
|
+
* }
|
|
140
|
+
* } catch (error) {
|
|
141
|
+
* console.error('Signing error:', error);
|
|
142
|
+
* }
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
const signTransaction = useCallback(async (jwt) => {
|
|
146
|
+
if (!isInitialized || !sdk) {
|
|
147
|
+
throw new Error('SDK not initialized. Call initialize() first.');
|
|
148
|
+
}
|
|
149
|
+
if (!isAuthenticated || !user) {
|
|
150
|
+
throw new Error('User must be authenticated to sign transactions.');
|
|
151
|
+
}
|
|
152
|
+
if (!isSignerInitialized || !signerSDKRef.current) {
|
|
153
|
+
throw new Error('Transaction signer not initialized. Call initializeSigner() first.');
|
|
154
|
+
}
|
|
155
|
+
if (!createPersSignerSDK) {
|
|
156
|
+
throw new Error('PERS Signer SDK not available. Blockchain signing is not supported.');
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
console.log('[useTransactionSigner] Extracting user info from JWT for transaction signing...');
|
|
160
|
+
// Extract user information from JWT
|
|
161
|
+
const jwtUserInfo = await extractUserInfoFromJWT(jwt);
|
|
162
|
+
// Create user info for signer, prioritizing current user data
|
|
163
|
+
const currentUser = user;
|
|
164
|
+
const signerUserInfo = {
|
|
165
|
+
identifier: currentUser.email || currentUser.id || jwtUserInfo.userId || jwtUserInfo.sub || `user-${Date.now()}`,
|
|
166
|
+
email: currentUser.email || jwtUserInfo.email,
|
|
167
|
+
id: currentUser.id || jwtUserInfo.userId
|
|
168
|
+
};
|
|
169
|
+
// Authenticate user with blockchain signer
|
|
170
|
+
console.log('[useTransactionSigner] Authenticating user with signer:', signerUserInfo.identifier);
|
|
171
|
+
const signerUser = await signerSDKRef.current.authenticateUser(signerUserInfo);
|
|
172
|
+
// Sign the PERS transaction using JWT
|
|
173
|
+
console.log('[useTransactionSigner] Signing PERS transaction with JWT containing transaction data');
|
|
174
|
+
const result = await signerSDKRef.current.signPersTransaction(signerUser, jwt);
|
|
175
|
+
// Convert PERS SDK result to our format
|
|
176
|
+
const convertedResult = {
|
|
177
|
+
success: result.success || false,
|
|
178
|
+
transactionHash: result.transactionHash,
|
|
179
|
+
signature: result.signature,
|
|
180
|
+
error: result.error
|
|
181
|
+
};
|
|
182
|
+
if (convertedResult.success) {
|
|
183
|
+
console.log('[useTransactionSigner] Transaction signed successfully:', convertedResult.transactionHash);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
console.warn('[useTransactionSigner] Transaction signing completed with warnings:', convertedResult.error);
|
|
187
|
+
}
|
|
188
|
+
return convertedResult;
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
console.error('[useTransactionSigner] Failed to sign transaction:', error);
|
|
192
|
+
return {
|
|
193
|
+
success: false,
|
|
194
|
+
error: `Transaction signing failed: ${error}`
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}, [sdk, isInitialized, isAuthenticated, user, isSignerInitialized]);
|
|
198
|
+
return {
|
|
199
|
+
/**
|
|
200
|
+
* Sign a blockchain transaction with WebAuthn authentication
|
|
201
|
+
*/
|
|
202
|
+
signTransaction,
|
|
203
|
+
/**
|
|
204
|
+
* Initialize the transaction signer with configuration
|
|
205
|
+
*/
|
|
206
|
+
initializeSigner,
|
|
207
|
+
/**
|
|
208
|
+
* Whether the transaction signer has been initialized
|
|
209
|
+
*/
|
|
210
|
+
isSignerInitialized,
|
|
211
|
+
/**
|
|
212
|
+
* Whether transaction signing is available (all requirements met)
|
|
213
|
+
*/
|
|
214
|
+
isSignerAvailable: isInitialized && isAuthenticated && isSignerInitialized,
|
|
215
|
+
};
|
|
216
|
+
};
|
|
@@ -1,7 +1,49 @@
|
|
|
1
|
+
import type { TransactionRequestDTO, TransactionRequestResponseDTO, TransactionDTO } from '@explorins/pers-shared';
|
|
2
|
+
import type { TransactionPaginationParams } from '@explorins/pers-sdk/transaction';
|
|
3
|
+
/**
|
|
4
|
+
* React hook for transaction operations in the PERS SDK
|
|
5
|
+
*
|
|
6
|
+
* Provides comprehensive transaction management including creation, retrieval, history,
|
|
7
|
+
* and administrative operations. Supports pagination and CSV export functionality.
|
|
8
|
+
*
|
|
9
|
+
* @returns Transaction hook with methods for transaction management
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* function TransactionsComponent() {
|
|
14
|
+
* const {
|
|
15
|
+
* createTransaction,
|
|
16
|
+
* getUserTransactionHistory
|
|
17
|
+
* } = useTransactions();
|
|
18
|
+
*
|
|
19
|
+
* const handleCreateTransaction = async (request) => {
|
|
20
|
+
* try {
|
|
21
|
+
* const result = await createTransaction(request);
|
|
22
|
+
* console.log('Transaction created:', result);
|
|
23
|
+
* // Handle signature URL if returned
|
|
24
|
+
* } catch (error) {
|
|
25
|
+
* console.error('Transaction failed:', error);
|
|
26
|
+
* }
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* return (
|
|
30
|
+
* <div>
|
|
31
|
+
* <button onClick={() => handleCreateTransaction(transactionRequest)}>
|
|
32
|
+
* Create Transaction
|
|
33
|
+
* </button>
|
|
34
|
+
* </div>
|
|
35
|
+
* );
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
1
39
|
export declare const useTransactions: () => {
|
|
2
|
-
createTransaction: (request:
|
|
3
|
-
getTransactionById: (transactionId: string) => Promise<
|
|
4
|
-
|
|
40
|
+
createTransaction: (request: TransactionRequestDTO) => Promise<TransactionRequestResponseDTO>;
|
|
41
|
+
getTransactionById: (transactionId: string) => Promise<TransactionDTO | null>;
|
|
42
|
+
getUserTransactionHistory: (type?: string) => Promise<TransactionDTO[]>;
|
|
43
|
+
getTenantTransactions: () => Promise<TransactionDTO[]>;
|
|
44
|
+
getPaginatedTransactions: (params: TransactionPaginationParams) => Promise<any>;
|
|
45
|
+
exportTransactionsCSV: () => Promise<Blob>;
|
|
5
46
|
isAvailable: boolean;
|
|
6
47
|
};
|
|
48
|
+
export type TransactionHook = ReturnType<typeof useTransactions>;
|
|
7
49
|
//# sourceMappingURL=useTransactions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTransactions.d.ts","sourceRoot":"","sources":["../../src/hooks/useTransactions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useTransactions.d.ts","sourceRoot":"","sources":["../../src/hooks/useTransactions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,qBAAqB,EACrB,6BAA6B,EAC7B,cAAc,EACf,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AAEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,eAAe;iCA6B4B,qBAAqB,KAAG,QAAQ,6BAA6B,CAAC;wCAgCvD,MAAM,KAAG,QAAQ,cAAc,GAAG,IAAI,CAAC;uCA6BzC,MAAM,KAAW,QAAQ,cAAc,EAAE,CAAC;iCAejD,QAAQ,cAAc,EAAE,CAAC;uCAejB,2BAA2B,KAAG,QAAQ,GAAG,CAAC;iCAelD,QAAQ,IAAI,CAAC;;CAwBlE,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC"}
|