@@ -999,7 +999,9 @@ impl OfferContents {
999
999
let ( currency, amount) = match & self . amount {
1000
1000
None => ( None , None ) ,
1001
1001
Some ( Amount :: Bitcoin { amount_msats } ) => ( None , Some ( * amount_msats) ) ,
1002
- Some ( Amount :: Currency { iso4217_code, amount } ) => ( Some ( iso4217_code) , Some ( * amount) ) ,
1002
+ Some ( Amount :: Currency { iso4217_code, amount } ) => {
1003
+ ( Some ( iso4217_code. as_bytes ( ) ) , Some ( * amount) )
1004
+ } ,
1003
1005
} ;
1004
1006
1005
1007
let features = {
@@ -1076,7 +1078,59 @@ pub enum Amount {
1076
1078
}
1077
1079
1078
1080
/// An ISO 4217 three-letter currency code (e.g., USD).
1079
- pub type CurrencyCode = [ u8 ; 3 ] ;
1081
+ ///
1082
+ /// Currency codes must be exactly 3 ASCII uppercase letters.
1083
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
1084
+ pub struct CurrencyCode ( [ u8 ; 3 ] ) ;
1085
+
1086
+ impl CurrencyCode {
1087
+ /// Creates a new `CurrencyCode` from a 3-byte array.
1088
+ ///
1089
+ /// Returns an error if the bytes are not valid UTF-8 or not all ASCII uppercase.
1090
+ pub fn new ( code : [ u8 ; 3 ] ) -> Result < Self , CurrencyCodeError > {
1091
+ if !code. iter ( ) . all ( |c| c. is_ascii_uppercase ( ) ) {
1092
+ return Err ( CurrencyCodeError ) ;
1093
+ }
1094
+
1095
+ Ok ( Self ( code) )
1096
+ }
1097
+
1098
+ /// Returns the currency code as a byte array.
1099
+ pub fn as_bytes ( & self ) -> & [ u8 ; 3 ] {
1100
+ & self . 0
1101
+ }
1102
+
1103
+ /// Returns the currency code as a string slice.
1104
+ pub fn as_str ( & self ) -> & str {
1105
+ core:: str:: from_utf8 ( & self . 0 ) . expect ( "currency code is always valid UTF-8" )
1106
+ }
1107
+ }
1108
+
1109
+ impl FromStr for CurrencyCode {
1110
+ type Err = CurrencyCodeError ;
1111
+
1112
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
1113
+ if s. len ( ) != 3 {
1114
+ return Err ( CurrencyCodeError ) ;
1115
+ }
1116
+
1117
+ let mut code = [ 0u8 ; 3 ] ;
1118
+ code. copy_from_slice ( s. as_bytes ( ) ) ;
1119
+ Self :: new ( code)
1120
+ }
1121
+ }
1122
+
1123
+ impl AsRef < [ u8 ] > for CurrencyCode {
1124
+ fn as_ref ( & self ) -> & [ u8 ] {
1125
+ & self . 0
1126
+ }
1127
+ }
1128
+
1129
+ impl core:: fmt:: Display for CurrencyCode {
1130
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
1131
+ f. write_str ( self . as_str ( ) )
1132
+ }
1133
+ }
1080
1134
1081
1135
/// Quantity of items supported by an [`Offer`].
1082
1136
#[ derive( Clone , Copy , Debug , PartialEq ) ]
@@ -1115,7 +1169,7 @@ const OFFER_ISSUER_ID_TYPE: u64 = 22;
1115
1169
tlv_stream ! ( OfferTlvStream , OfferTlvStreamRef <' a>, OFFER_TYPES , {
1116
1170
( 2 , chains: ( Vec <ChainHash >, WithoutLength ) ) ,
1117
1171
( OFFER_METADATA_TYPE , metadata: ( Vec <u8 >, WithoutLength ) ) ,
1118
- ( 6 , currency: CurrencyCode ) ,
1172
+ ( 6 , currency: [ u8 ; 3 ] ) ,
1119
1173
( 8 , amount: ( u64 , HighZeroBytesDroppedBigSize ) ) ,
1120
1174
( 10 , description: ( String , WithoutLength ) ) ,
1121
1175
( 12 , features: ( OfferFeatures , WithoutLength ) ) ,
@@ -1209,7 +1263,11 @@ impl TryFrom<FullOfferTlvStream> for OfferContents {
1209
1263
} ,
1210
1264
( None , Some ( amount_msats) ) => Some ( Amount :: Bitcoin { amount_msats } ) ,
1211
1265
( Some ( _) , None ) => return Err ( Bolt12SemanticError :: MissingAmount ) ,
1212
- ( Some ( iso4217_code) , Some ( amount) ) => Some ( Amount :: Currency { iso4217_code, amount } ) ,
1266
+ ( Some ( currency_bytes) , Some ( amount) ) => {
1267
+ let iso4217_code = CurrencyCode :: new ( currency_bytes)
1268
+ . map_err ( |_| Bolt12SemanticError :: InvalidCurrencyCode ) ?;
1269
+ Some ( Amount :: Currency { iso4217_code, amount } )
1270
+ } ,
1213
1271
} ;
1214
1272
1215
1273
if amount. is_some ( ) && description. is_none ( ) {
@@ -1256,6 +1314,22 @@ impl core::fmt::Display for Offer {
1256
1314
}
1257
1315
}
1258
1316
1317
+ /// An error indicating that a currency code is invalid.
1318
+ ///
1319
+ /// A valid currency code must follow the ISO 4217 standard:
1320
+ /// - Exactly 3 characters in length.
1321
+ /// - Consist only of uppercase ASCII letters (A–Z).
1322
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
1323
+ pub struct CurrencyCodeError ;
1324
+
1325
+ impl core:: fmt:: Display for CurrencyCodeError {
1326
+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
1327
+ write ! ( f, "invalid currency code: must be 3 uppercase ASCII letters (ISO 4217)" )
1328
+ }
1329
+ }
1330
+
1331
+ impl core:: error:: Error for CurrencyCodeError { }
1332
+
1259
1333
#[ cfg( test) ]
1260
1334
mod tests {
1261
1335
#[ cfg( not( c_bindings) ) ]
@@ -1273,6 +1347,7 @@ mod tests {
1273
1347
use crate :: ln:: inbound_payment:: ExpandedKey ;
1274
1348
use crate :: ln:: msgs:: { DecodeError , MAX_VALUE_MSAT } ;
1275
1349
use crate :: offers:: nonce:: Nonce ;
1350
+ use crate :: offers:: offer:: CurrencyCode ;
1276
1351
use crate :: offers:: parse:: { Bolt12ParseError , Bolt12SemanticError } ;
1277
1352
use crate :: offers:: test_utils:: * ;
1278
1353
use crate :: types:: features:: OfferFeatures ;
@@ -1541,7 +1616,8 @@ mod tests {
1541
1616
#[ test]
1542
1617
fn builds_offer_with_amount ( ) {
1543
1618
let bitcoin_amount = Amount :: Bitcoin { amount_msats : 1000 } ;
1544
- let currency_amount = Amount :: Currency { iso4217_code : * b"USD" , amount : 10 } ;
1619
+ let currency_amount =
1620
+ Amount :: Currency { iso4217_code : CurrencyCode :: new ( * b"USD" ) . unwrap ( ) , amount : 10 } ;
1545
1621
1546
1622
let offer = OfferBuilder :: new ( pubkey ( 42 ) ) . amount_msats ( 1000 ) . build ( ) . unwrap ( ) ;
1547
1623
let tlv_stream = offer. as_tlv_stream ( ) ;
@@ -1820,6 +1896,36 @@ mod tests {
1820
1896
Bolt12ParseError :: InvalidSemantics ( Bolt12SemanticError :: InvalidAmount )
1821
1897
) ,
1822
1898
}
1899
+
1900
+ let mut tlv_stream = offer. as_tlv_stream ( ) ;
1901
+ tlv_stream. 0 . amount = Some ( 1000 ) ;
1902
+ tlv_stream. 0 . currency = Some ( b"\xFF \xFE \xFD " ) ; // invalid UTF-8 bytes
1903
+
1904
+ let mut encoded_offer = Vec :: new ( ) ;
1905
+ tlv_stream. write ( & mut encoded_offer) . unwrap ( ) ;
1906
+
1907
+ match Offer :: try_from ( encoded_offer) {
1908
+ Ok ( _) => panic ! ( "expected error" ) ,
1909
+ Err ( e) => assert_eq ! (
1910
+ e,
1911
+ Bolt12ParseError :: InvalidSemantics ( Bolt12SemanticError :: InvalidCurrencyCode )
1912
+ ) ,
1913
+ }
1914
+
1915
+ let mut tlv_stream = offer. as_tlv_stream ( ) ;
1916
+ tlv_stream. 0 . amount = Some ( 1000 ) ;
1917
+ tlv_stream. 0 . currency = Some ( b"usd" ) ; // invalid ISO 4217 code
1918
+
1919
+ let mut encoded_offer = Vec :: new ( ) ;
1920
+ tlv_stream. write ( & mut encoded_offer) . unwrap ( ) ;
1921
+
1922
+ match Offer :: try_from ( encoded_offer) {
1923
+ Ok ( _) => panic ! ( "expected error" ) ,
1924
+ Err ( e) => assert_eq ! (
1925
+ e,
1926
+ Bolt12ParseError :: InvalidSemantics ( Bolt12SemanticError :: InvalidCurrencyCode )
1927
+ ) ,
1928
+ }
1823
1929
}
1824
1930
1825
1931
#[ test]
0 commit comments