@explorins/pers-sdk-react-native 1.5.1 → 1.5.3
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/index.d.ts +2 -0
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/useRedemptions.d.ts.map +1 -1
- package/dist/hooks/useRedemptions.js +33 -9
- 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.map +1 -1
- package/dist/hooks/useTransactions.js +1 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19390 -47
- package/dist/index.js.map +1 -1
- package/package.json +8 -2
- package/src/hooks/index.ts +3 -1
- package/src/hooks/useRedemptions.ts +35 -9
- package/src/hooks/useTransactionSigner.ts +287 -0
- package/src/hooks/useTransactions.ts +1 -15
- package/src/index.ts +1 -0
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { useAuth } from './useAuth';
|
|
2
2
|
export { useTokens } from './useTokens';
|
|
3
3
|
export { useTransactions } from './useTransactions';
|
|
4
|
+
export { useTransactionSigner } from './useTransactionSigner';
|
|
4
5
|
export { useBusiness } from './useBusiness';
|
|
5
6
|
export { useCampaigns } from './useCampaigns';
|
|
6
7
|
export { useRedemptions } from './useRedemptions';
|
|
@@ -13,4 +14,5 @@ export { useFiles } from './useFiles';
|
|
|
13
14
|
export { useAnalytics } from './useAnalytics';
|
|
14
15
|
export { useDonations } from './useDonations';
|
|
15
16
|
export type { RawUserData } from './useAuth';
|
|
17
|
+
export type { TransactionSignerHook, TransactionSigningResult } from './useTransactionSigner';
|
|
16
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,YAAY,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,YAAY,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/hooks/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
export { useAuth } from './useAuth';
|
|
3
3
|
export { useTokens } from './useTokens';
|
|
4
4
|
export { useTransactions } from './useTransactions';
|
|
5
|
+
export { useTransactionSigner } from './useTransactionSigner';
|
|
5
6
|
export { useBusiness } from './useBusiness';
|
|
6
7
|
export { useCampaigns } from './useCampaigns';
|
|
7
8
|
export { useRedemptions } from './useRedemptions';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useRedemptions.d.ts","sourceRoot":"","sources":["../../src/hooks/useRedemptions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useRedemptions.d.ts","sourceRoot":"","sources":["../../src/hooks/useRedemptions.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACb,mBAAmB,EACnB,kCAAkC,EAClC,iBAAiB,EAElB,MAAM,wBAAwB,CAAC;AAEhC,eAAO,MAAM,cAAc;gCAI0B,QAAQ,aAAa,EAAE,CAAC;8BAe1B,QAAQ,mBAAmB,EAAE,CAAC;gCAmB1B,MAAM,KAAG,QAAQ,kCAAkC,CAAC;8BAsFxD,QAAQ,iBAAiB,EAAE,CAAC;uCA/BjB,0BAA0B,KAAG,QAAQ,aAAa,CAAC;iCAgBzD,OAAO,KAAG,QAAQ,aAAa,EAAE,CAAC;qCA8B9B,MAAM,kBAAkB,0BAA0B,KAAG,QAAQ,aAAa,CAAC;2CAerE,MAAM,KAAG,QAAQ,aAAa,CAAC;;CA0BhG,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { usePersSDK } from '../providers/PersSDKProvider';
|
|
3
|
+
import { useTransactionSigner } from './useTransactionSigner';
|
|
3
4
|
export const useRedemptions = () => {
|
|
4
5
|
const { sdk, isInitialized, isAuthenticated } = usePersSDK();
|
|
6
|
+
const { signTransaction, isSignerAvailable } = useTransactionSigner();
|
|
5
7
|
const getActiveRedemptions = useCallback(async () => {
|
|
6
8
|
if (!isInitialized || !sdk) {
|
|
7
9
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
@@ -44,25 +46,47 @@ export const useRedemptions = () => {
|
|
|
44
46
|
try {
|
|
45
47
|
console.log('Redeeming redemption:', redemptionId);
|
|
46
48
|
const result = await sdk.redemptions.redeemOffer(redemptionId);
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
+
// Check if result has signing fields and sign transaction if required and signer is available
|
|
50
|
+
console.log('Redemption processed successfully:', result);
|
|
51
|
+
const txToken = result.senderTransaction?.actionable?.authToken;
|
|
52
|
+
if (txToken && isSignerAvailable) {
|
|
53
|
+
console.log('Transaction requires blockchain signing, processing with WebAuthn signer...');
|
|
49
54
|
try {
|
|
50
|
-
const
|
|
51
|
-
console.log('
|
|
52
|
-
|
|
55
|
+
const signingResult = await signTransaction(txToken);
|
|
56
|
+
console.log('Blockchain signing result:', signingResult);
|
|
57
|
+
if (signingResult.success) {
|
|
58
|
+
console.log('Transaction signed successfully:', signingResult.transactionHash);
|
|
59
|
+
// Return enhanced result with signing information
|
|
60
|
+
return {
|
|
61
|
+
...result,
|
|
62
|
+
transactionHash: signingResult.transactionHash,
|
|
63
|
+
signature: signingResult.signature,
|
|
64
|
+
isSigned: true,
|
|
65
|
+
signedAt: new Date().toISOString()
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
console.error('Transaction signing failed:', signingResult.error);
|
|
70
|
+
throw new Error(signingResult.error || 'Transaction signing failed');
|
|
71
|
+
}
|
|
53
72
|
}
|
|
54
|
-
catch (
|
|
55
|
-
console.error('
|
|
73
|
+
catch (signingError) {
|
|
74
|
+
console.error('Blockchain signing error:', signingError);
|
|
75
|
+
throw new Error(`Transaction signing failed: ${signingError}`);
|
|
56
76
|
}
|
|
57
77
|
}
|
|
58
|
-
|
|
78
|
+
else if (txToken && !isSignerAvailable) {
|
|
79
|
+
console.warn('Transaction requires signature but signer is not available');
|
|
80
|
+
throw new Error('Transaction requires signature but blockchain signer is not initialized');
|
|
81
|
+
}
|
|
82
|
+
// Return original result if no signing was required
|
|
59
83
|
return result;
|
|
60
84
|
}
|
|
61
85
|
catch (error) {
|
|
62
86
|
console.error('Failed to redeem redemption:', error);
|
|
63
87
|
throw error;
|
|
64
88
|
}
|
|
65
|
-
}, [sdk, isInitialized, isAuthenticated]);
|
|
89
|
+
}, [sdk, isInitialized, isAuthenticated, signTransaction, isSignerAvailable]);
|
|
66
90
|
// Admin methods
|
|
67
91
|
const createRedemption = useCallback(async (redemptionData) => {
|
|
68
92
|
if (!isInitialized || !sdk) {
|
|
@@ -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 +1 @@
|
|
|
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;
|
|
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"}
|
|
@@ -68,19 +68,8 @@ export const useTransactions = () => {
|
|
|
68
68
|
throw new Error('SDK not initialized. Call initialize() first.');
|
|
69
69
|
}
|
|
70
70
|
try {
|
|
71
|
-
console.log('Creating transaction with request:', request);
|
|
72
71
|
const result = await sdk.transactions.createTransaction(request);
|
|
73
|
-
// Cross-platform:
|
|
74
|
-
if (result?.actionable?.actionUrl) {
|
|
75
|
-
try {
|
|
76
|
-
const { Linking } = require('react-native');
|
|
77
|
-
console.log('Opening signature URL:', result.actionable.actionUrl);
|
|
78
|
-
await Linking.openURL(result.actionable.actionUrl);
|
|
79
|
-
}
|
|
80
|
-
catch (linkingError) {
|
|
81
|
-
console.error('Failed to open signature URL:', linkingError);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
72
|
+
// Cross-platform: Dont handle signature URLs here, leave to caller
|
|
84
73
|
console.log('Transaction created successfully:', result);
|
|
85
74
|
return result;
|
|
86
75
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import './polyfills';
|
|
2
2
|
export { ReactNativeAuthProvider, type ReactNativeAuthConfig } from './providers/react-native-auth-provider';
|
|
3
3
|
export { PersSDKProvider, usePersSDK, type PersConfig, type PersSDKContext } from './providers/PersSDKProvider';
|
|
4
|
-
export { useAuth, useTokens, useTransactions, useBusiness, useCampaigns, useRedemptions, useWeb3, usePurchases, useTenants, useUsers, useUserStatus, useFiles, useAnalytics, useDonations } from './hooks';
|
|
4
|
+
export { useAuth, useTokens, useTransactions, useTransactionSigner, useBusiness, useCampaigns, useRedemptions, useWeb3, usePurchases, useTenants, useUsers, useUserStatus, useFiles, useAnalytics, useDonations } from './hooks';
|
|
5
5
|
export { ReactNativeHttpClient } from './providers/react-native-http-client';
|
|
6
6
|
export { initializeReactNativePolyfills } from './polyfills';
|
|
7
7
|
export * from '@explorins/pers-shared';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,CAAC;AAGrB,OAAO,EACL,uBAAuB,EACvB,KAAK,qBAAqB,EAC3B,MAAM,wCAAwC,CAAC;AAGhD,OAAO,EACL,eAAe,EACf,UAAU,EACV,KAAK,UAAU,EACf,KAAK,cAAc,EACpB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,EACL,OAAO,EACP,SAAS,EACT,eAAe,EACf,WAAW,EACX,YAAY,EACZ,cAAc,EACd,OAAO,EACP,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,YAAY,EACZ,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,qBAAqB,EACtB,MAAM,sCAAsC,CAAC;AAG9C,OAAO,EACL,8BAA8B,EAC/B,MAAM,aAAa,CAAC;AAGrB,cAAc,wBAAwB,CAAC;AAGvC,YAAY,EACV,mBAAmB,EACnB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,sBAAsB,EACvB,MAAM,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,aAAa,CAAC;AAGrB,OAAO,EACL,uBAAuB,EACvB,KAAK,qBAAqB,EAC3B,MAAM,wCAAwC,CAAC;AAGhD,OAAO,EACL,eAAe,EACf,UAAU,EACV,KAAK,UAAU,EACf,KAAK,cAAc,EACpB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,EACL,OAAO,EACP,SAAS,EACT,eAAe,EACf,oBAAoB,EACpB,WAAW,EACX,YAAY,EACZ,cAAc,EACd,OAAO,EACP,YAAY,EACZ,UAAU,EACV,QAAQ,EACR,aAAa,EACb,QAAQ,EACR,YAAY,EACZ,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,qBAAqB,EACtB,MAAM,sCAAsC,CAAC;AAG9C,OAAO,EACL,8BAA8B,EAC/B,MAAM,aAAa,CAAC;AAGrB,cAAc,wBAAwB,CAAC;AAGvC,YAAY,EACV,mBAAmB,EACnB,YAAY,EACZ,aAAa,EACb,eAAe,EACf,sBAAsB,EACvB,MAAM,0BAA0B,CAAC"}
|