1
1
import React , { useState } from 'react' ;
2
- import { View , Text , Button } from 'react-native' ;
2
+ import { View , Text , Button , StyleSheet } from 'react-native' ;
3
3
import * as bitcoin from 'bitcoinjs-lib' ;
4
4
import QRCode from 'react-native-qrcode-svg' ;
5
- import * as bolt11 from 'light-bolt11-decoder' ;
5
+ import { encode } from 'light-bolt11-decoder' ; // Changed import to get encode directly
6
6
7
7
const CreateWallet = ( ) => {
8
8
const [ segwitAddress , setSegwitAddress ] = useState < string | null > ( null ) ;
@@ -11,53 +11,103 @@ const CreateWallet = () => {
11
11
12
12
// Function to generate SegWit and Taproot addresses
13
13
const generateAddresses = ( ) => {
14
- const keyPair = bitcoin . ECPair . makeRandom ( ) ;
15
- const { address : segwitAddr } = bitcoin . payments . p2wpkh ( {
16
- pubkey : keyPair . publicKey ,
17
- } ) ;
18
- const { address : taprootAddr } = bitcoin . payments . p2tr ( {
19
- pubkey : keyPair . publicKey ,
20
- } ) ;
21
-
22
- setSegwitAddress ( segwitAddr || null ) ; // Ensure it's null if undefined
23
- setTaprootAddress ( taprootAddr || null ) ; // Ensure it's null if undefined
14
+ try {
15
+ const keyPair = bitcoin . ECPair . makeRandom ( { network : bitcoin . networks . bitcoin } ) ;
16
+ const { address : segwitAddr } = bitcoin . payments . p2wpkh ( {
17
+ pubkey : keyPair . publicKey ,
18
+ network : bitcoin . networks . bitcoin ,
19
+ } ) ;
20
+ const { address : taprootAddr } = bitcoin . payments . p2tr ( {
21
+ pubkey : keyPair . publicKey ,
22
+ network : bitcoin . networks . bitcoin ,
23
+ } ) ;
24
+
25
+ setSegwitAddress ( segwitAddr || null ) ;
26
+ setTaprootAddress ( taprootAddr || null ) ;
27
+ } catch ( error ) {
28
+ console . error ( 'Error generating addresses:' , error ) ;
29
+ }
24
30
} ;
25
31
26
32
// Function to create a Lightning invoice (BOLT11)
27
33
const generateBolt11Invoice = ( ) => {
28
- const amountSats = 10000 ; // Example amount in satoshis
29
- const invoice = bolt11 . encode ( {
30
- millisatoshis : amountSats * 1000 ,
31
- tags : [ { tagName : 'purpose' , data : 'Payment for goods' } ] ,
32
- } ) ;
33
- setInvoice ( invoice || null ) ; // Ensure it's null if undefined
34
+ try {
35
+ const amountSats = 10000 ; // Example amount in satoshis
36
+ const paymentRequest = encode ( {
37
+ paymentHash : Buffer . from ( '00' . repeat ( 32 ) , 'hex' ) , // Dummy payment hash
38
+ amount : amountSats . toString ( ) , // Amount in satoshis as string
39
+ timestamp : Math . floor ( Date . now ( ) / 1000 ) ,
40
+ tags : [
41
+ {
42
+ tagName : 'payment_hash' ,
43
+ data : '00' . repeat ( 32 ) , // 32 bytes of zeros
44
+ } ,
45
+ {
46
+ tagName : 'description' ,
47
+ data : 'Payment for goods' ,
48
+ } ,
49
+ ] ,
50
+ } ) ;
51
+ setInvoice ( paymentRequest || null ) ;
52
+ } catch ( error ) {
53
+ console . error ( 'Error generating invoice:' , error ) ;
54
+ }
34
55
} ;
35
56
36
57
return (
37
- < View >
58
+ < View style = { styles . container } >
38
59
< Button title = "Generate Wallet Addresses" onPress = { generateAddresses } />
60
+
39
61
{ segwitAddress && (
40
- < View >
41
- < Text > SegWit Address: { segwitAddress } </ Text >
62
+ < View style = { styles . addressContainer } >
63
+ < Text style = { styles . label } > SegWit Address:</ Text >
64
+ < Text style = { styles . address } > { segwitAddress } </ Text >
42
65
< QRCode value = { segwitAddress } size = { 150 } />
43
66
</ View >
44
67
) }
68
+
45
69
{ taprootAddress && (
46
- < View >
47
- < Text > Taproot Address: { taprootAddress } </ Text >
70
+ < View style = { styles . addressContainer } >
71
+ < Text style = { styles . label } > Taproot Address:</ Text >
72
+ < Text style = { styles . address } > { taprootAddress } </ Text >
48
73
< QRCode value = { taprootAddress } size = { 150 } />
49
74
</ View >
50
75
) }
76
+
51
77
< Button title = "Generate Lightning Invoice" onPress = { generateBolt11Invoice } />
78
+
52
79
{ invoice && (
53
- < View >
54
- < Text > Lightning Invoice: { invoice } </ Text >
80
+ < View style = { styles . addressContainer } >
81
+ < Text style = { styles . label } > Lightning Invoice:</ Text >
82
+ < Text style = { styles . address } > { invoice } </ Text >
55
83
< QRCode value = { invoice } size = { 150 } />
56
84
</ View >
57
85
) }
58
86
</ View >
59
87
) ;
60
88
} ;
61
89
90
+ const styles = StyleSheet . create ( {
91
+ container : {
92
+ flex : 1 ,
93
+ padding : 16 ,
94
+ alignItems : 'center' ,
95
+ } ,
96
+ addressContainer : {
97
+ marginVertical : 16 ,
98
+ alignItems : 'center' ,
99
+ } ,
100
+ label : {
101
+ fontSize : 16 ,
102
+ fontWeight : 'bold' ,
103
+ marginBottom : 8 ,
104
+ } ,
105
+ address : {
106
+ fontSize : 14 ,
107
+ marginBottom : 8 ,
108
+ textAlign : 'center' ,
109
+ } ,
110
+ } ) ;
111
+
62
112
export default CreateWallet ;
63
113
0 commit comments