@@ -7,7 +7,7 @@ import { Text } from "../components/ui/text";
77import { Input } from "../components/ui/input" ;
88import { Button } from "../components/ui/button" ;
99import { COLORS } from "../lib/constants" ;
10- import { decodeBolt11 , isArkPublicKey , isValidBitcoinAddress , isValidBolt11 } from "../constants " ;
10+ import { parseDestination , isValidDestination , type DestinationTypes } from "../lib/sendUtils " ;
1111import { useSend } from "../hooks/usePayments" ;
1212import SuccessAnimation from "../components/SuccessAnimation" ;
1313import { Card , CardContent , CardHeader , CardTitle } from "../components/ui/card" ;
@@ -32,43 +32,26 @@ const SendScreen = () => {
3232 const [ comment , setComment ] = useState ( "" ) ;
3333 const [ showCamera , setShowCamera ] = useState ( false ) ;
3434 const [ parsedResult , setParsedResult ] = useState < SendResult | null > ( null ) ;
35+ const [ destinationType , setDestinationType ] = useState < DestinationTypes | null > ( null ) ;
3536
3637 useEffect ( ( ) => {
37- if ( isValidBolt11 ( destination ) ) {
38- try {
39- const decoded = decodeBolt11 ( destination ) ;
40- if ( decoded === null ) {
41- throw new Error ( "Invalid invoice" ) ;
42- }
43-
44- const msats = decoded . sections . find ( ( n ) => n . name === "amount" ) ?. value ;
45-
46- if ( msats === undefined ) {
47- throw new Error ( "Missing amount" ) ;
48- }
38+ if ( destination ) {
39+ const {
40+ destinationType : newDestinationType ,
41+ amount : newAmount ,
42+ isAmountEditable : newIsAmountEditable ,
43+ error : parseError ,
44+ } = parseDestination ( destination ) ;
4945
50- if ( Number ( msats ) > 0 && Number ( msats ) < 1000 ) {
51- Alert . alert ( "Invalid Amount" , "Invoice amount is less than 1 satoshi." ) ;
52- setAmount ( "" ) ;
53- setIsAmountEditable ( true ) ;
54- return ;
55- }
56-
57- const sats = Number ( msats ) / 1000 ;
58-
59- if ( sats >= 1 ) {
60- setAmount ( sats . toString ( ) ) ;
61- setIsAmountEditable ( false ) ;
62- } else {
63- setAmount ( "" ) ;
64- setIsAmountEditable ( true ) ;
65- }
66- } catch ( e ) {
67- console . error ( "Failed to decode bolt11 invoice" , e ) ;
68- setAmount ( "" ) ;
69- setIsAmountEditable ( true ) ;
46+ if ( parseError ) {
47+ Alert . alert ( "Invalid Destination" , parseError ) ;
7048 }
49+
50+ setDestinationType ( newDestinationType ) ;
51+ setAmount ( newAmount ?. toString ( ) ?? "" ) ;
52+ setIsAmountEditable ( newIsAmountEditable ) ;
7153 } else {
54+ setDestinationType ( null ) ;
7255 setAmount ( "" ) ;
7356 setIsAmountEditable ( true ) ;
7457 }
@@ -87,17 +70,8 @@ const SendScreen = () => {
8770 }
8871 } , [ result ] ) ;
8972
90- const isValidDestination = ( dest : string ) => {
91- const cleanedDest = dest . replace ( / ^ ( b i t c o i n : | l i g h t n i n g : ) / i, "" ) ;
92- return (
93- isArkPublicKey ( cleanedDest ) ||
94- isValidBitcoinAddress ( cleanedDest ) ||
95- isValidBolt11 ( cleanedDest )
96- ) ;
97- } ;
98-
9973 const handleSend = ( ) => {
100- const amountSat = parseInt ( amount , 10 ) ;
74+ let amountSat : number | null = parseInt ( amount , 10 ) ;
10175 if ( ! isValidDestination ( destination ) ) {
10276 Alert . alert (
10377 "Invalid Destination" ,
@@ -109,7 +83,14 @@ const SendScreen = () => {
10983 Alert . alert ( "Invalid Amount" , "Please enter a valid amount." ) ;
11084 return ;
11185 }
86+
87+ if ( destinationType === "lightning" && amountSat !== 0 ) {
88+ amountSat = null ;
89+ }
90+
11291 const cleanedDestination = destination . replace ( / ^ ( b i t c o i n : | l i g h t n i n g : ) / i, "" ) ;
92+
93+ console . log ( "send details" , cleanedDestination , amountSat , comment ) ;
11394 send ( { destination : cleanedDestination , amountSat, comment : comment || null } ) ;
11495 } ;
11596
0 commit comments