Skip to content

Commit e83b36d

Browse files
committed
Final Payjoin version
1 parent c7b4064 commit e83b36d

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

src/app/payjoin/index.ts

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,96 @@
11
import { PayjoinURL } from 'payjoin-react-native';
22
import { UTXO } from 'silent-payments';
33

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+
}
619
const payjoinURL = new PayjoinURL(url);
720
return payjoinURL;
821
};
922

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;
1430
};
1531

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+
1846
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, {
2051
method: 'POST',
2152
headers: {
2253
'Content-Type': 'application/json',
54+
'Accept': 'application/json',
2355
},
2456
body: JSON.stringify({ psbt }),
57+
signal: controller.signal,
2558
});
2659

60+
clearTimeout(timeoutId);
61+
2762
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}`);
2965
}
3066

31-
return response;
67+
return response as PSBTResponse;
3268
} catch (error) {
3369
console.error('Failed to send PSBT:', error);
34-
throw error;
70+
throw error instanceof Error ? error : new Error('Unknown error occurred');
3571
}
3672
};
3773

38-
// Example usage of UTXO
39-
const utxo: UTXO = {
74+
// UTXO constant with proper typing
75+
export const demoUTXO: UTXO = {
4076
txid: 'txid',
4177
vout: 0,
42-
scriptPubKey: 'scriptPubKey',
43-
amount: 1000000
78+
amount: 1000000,
4479
};
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+
}
4794

48-
// Export the PayjoinURL for external use
49-
export { PayjoinURL };
95+
// Export PayjoinURL class
96+
export { PayjoinURL };

0 commit comments

Comments
 (0)