@@ -8,7 +8,10 @@ import {
88 CoinConstructor ,
99 SignTransactionOptions as BaseSignTransactionOptions ,
1010 SignedTransaction ,
11+ ITransactionRecipient ,
1112} from '../' ;
13+ import { isBolt11Invoice , LIGHTNING_INVOICE } from '../lightning' ;
14+
1215import { Ofc } from './ofc' ;
1316
1417export interface SignTransactionOptions extends BaseSignTransactionOptions {
@@ -21,6 +24,7 @@ export interface SignTransactionOptions extends BaseSignTransactionOptions {
2124export { OfcTokenConfig } ;
2225
2326const publicIdRegex = / ^ [ a - f \d ] { 32 } $ / i;
27+
2428export class OfcToken extends Ofc {
2529 public readonly tokenConfig : OfcTokenConfig ;
2630
@@ -65,6 +69,37 @@ export class OfcToken extends Ofc {
6569 return this . tokenConfig . type ;
6670 }
6771
72+ checkRecipient ( recipient : ITransactionRecipient ) : void {
73+ if ( isBolt11Invoice ( recipient . address ) ) {
74+ // should throw error if this isnt bitcoin (mainnet or testnet)
75+ if ( this . backingCoin !== 'btc' && this . backingCoin !== 'tbtc' ) {
76+ throw new Error ( `invalid argument - lightning invoice is only supported for bitcoin` ) ;
77+ }
78+
79+ // amount for bolt11 invoices is either 'invoice' or a non-zero bigint
80+ if ( recipient . amount === LIGHTNING_INVOICE ) {
81+ return ;
82+ }
83+ // try to parse the amount as a bigint
84+ let amount : bigint ;
85+ try {
86+ amount = BigInt ( recipient . amount ) ;
87+ } catch ( e ) {
88+ throw new Error (
89+ `invalid argument ${ recipient . amount } for amount - lightning invoice amount must be >= 0 or ${ LIGHTNING_INVOICE } `
90+ ) ;
91+ }
92+ if ( amount > 0n ) {
93+ return ;
94+ }
95+ throw new Error (
96+ `invalid argument for amount - lightning invoice amount must be a non-zero bigint or ${ LIGHTNING_INVOICE } `
97+ ) ;
98+ }
99+
100+ super . checkRecipient ( recipient ) ;
101+ }
102+
68103 /**
69104 * Flag for sending value of 0
70105 * @returns {boolean } True if okay to send 0 value, false otherwise
0 commit comments