1
1
import { PayjoinURL } from 'payjoin-react-native' ;
2
2
import { UTXO } from 'silent-payments' ;
3
3
4
- export const PayJoin = ( url : string ) => {
5
- // Create a PayjoinURL instance
4
+ // Interface definitions for better type safety
5
+ interface PSBTResponse extends Response {
6
+ json ( ) : Promise < any > ;
7
+ }
8
+
9
+ interface SendPSBTConfig {
10
+ endpoint ?: string ;
11
+ timeout ?: number ;
12
+ }
13
+
14
+ // Payjoin URL creation function with input validation
15
+ export const createPayjoin = ( url : string ) : PayjoinURL => {
16
+ if ( ! url || typeof url !== 'string' ) {
17
+ throw new Error ( 'Invalid URL provided' ) ;
18
+ }
6
19
const payjoinURL = new PayjoinURL ( url ) ;
7
20
return payjoinURL ;
8
21
} ;
9
22
10
- // Function to create a PSBT instance
11
- export const createPSBT = ( url : string ) => {
12
- const payjoinURL = new PayjoinURL ( url ) ;
13
- return payjoinURL . psbt ; // Assuming `psbt` is a property of PayjoinURL
23
+ // Function to create a PSBT instance with input validation
24
+ export const createPSBT = ( url : string ) : string => {
25
+ if ( ! url || typeof url !== 'string' ) {
26
+ throw new Error ( 'Invalid URL provided' ) ;
27
+ }
28
+ // Access the static member PayjoinURL.psbt
29
+ return PayjoinURL . psbt ;
14
30
} ;
15
31
16
- // Send the PSBT to the payjoin server
17
- export const sendPSBT = async ( psbt : string ) : Promise < Response > => {
32
+ // Function to send PSBT to the payjoin server with config options
33
+ export const sendPSBT = async (
34
+ psbt : string ,
35
+ config : SendPSBTConfig = { }
36
+ ) : Promise < PSBTResponse > => {
37
+ const {
38
+ endpoint = 'https://payjoin.example.com/payjoin' ,
39
+ timeout = 30000
40
+ } = config ;
41
+
42
+ if ( ! psbt || typeof psbt !== 'string' ) {
43
+ throw new Error ( 'Invalid PSBT provided' ) ;
44
+ }
45
+
18
46
try {
19
- const response = await fetch ( 'https://payjoin.example.com/payjoin' , {
47
+ const controller = new AbortController ( ) ;
48
+ const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeout ) ;
49
+
50
+ const response = await fetch ( endpoint , {
20
51
method : 'POST' ,
21
52
headers : {
22
53
'Content-Type' : 'application/json' ,
54
+ 'Accept' : 'application/json' ,
23
55
} ,
24
56
body : JSON . stringify ( { psbt } ) ,
57
+ signal : controller . signal ,
25
58
} ) ;
26
59
60
+ clearTimeout ( timeoutId ) ;
61
+
27
62
if ( ! response . ok ) {
28
- throw new Error ( `Error: ${ response . status } - ${ response . statusText } ` ) ;
63
+ const errorText = await response . text ( ) ;
64
+ throw new Error ( `HTTP Error ${ response . status } : ${ errorText } ` ) ;
29
65
}
30
66
31
- return response ;
67
+ return response as PSBTResponse ;
32
68
} catch ( error ) {
33
69
console . error ( 'Failed to send PSBT:' , error ) ;
34
- throw error ;
70
+ throw error instanceof Error ? error : new Error ( 'Unknown error occurred' ) ;
35
71
}
36
72
} ;
37
73
38
- // Example usage of UTXO
39
- const utxo : UTXO = {
74
+ // UTXO constant with proper typing
75
+ export const demoUTXO : UTXO = {
40
76
txid : 'txid' ,
41
77
vout : 0 ,
42
- scriptPubKey : 'scriptPubKey' ,
43
- amount : 1000000
78
+ amount : 1000000 ,
44
79
} ;
45
- // Log the UTXO for demonstration purposes
46
- console . log ( utxo ) ;
80
+ // Utility function to log UTXO
81
+ export const logUTXO = ( utxo : UTXO ) : void => {
82
+ if ( ! utxo || typeof utxo !== 'object' ) {
83
+ throw new Error ( 'Invalid UTXO provided' ) ;
84
+ }
85
+ console . log ( 'UTXO Details:' , utxo ) ;
86
+ } ;
87
+
88
+ // Example usage with error handling
89
+ try {
90
+ logUTXO ( demoUTXO ) ;
91
+ } catch ( error ) {
92
+ console . error ( 'Error logging UTXO:' , error ) ;
93
+ }
47
94
48
- // Export the PayjoinURL for external use
49
- export { PayjoinURL } ;
95
+ // Export PayjoinURL class
96
+ export { PayjoinURL } ;
0 commit comments