Skip to content

Commit c5377a1

Browse files
committed
Add PaymentPreimage convenience extension
1 parent bae6495 commit c5377a1

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

lib/src/utils/extensions.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import 'dart:typed_data';
2+
13
import '../generated/api/bolt11.dart' as bolt11;
24
import '../generated/api/bolt12.dart' as bolt12;
35
import '../generated/api/graph.dart' as graph;
46
import '../generated/api/spontaneous.dart' as spontaneous;
57
import '../generated/api/types.dart' as types;
68
import '../generated/api/unified_qr.dart' as unified_qr;
9+
import '../generated/lib.dart';
710
import '../generated/utils/error.dart' as error;
811
import '../utils/frb.dart';
912
import '../utils/exceptions.dart';
@@ -437,4 +440,22 @@ extension Bolt12PaymentExtensions on bolt12.FfiBolt12Payment {
437440
throw mapFfiNodeError(e);
438441
}
439442
}
443+
}
444+
445+
/// Extension methods for PaymentPreimage to provide convenience constructors
446+
extension PaymentPreimageExtensions on types.PaymentPreimage {
447+
/// Creates a PaymentPreimage from a List<int> of 32 bytes.
448+
///
449+
/// This is a convenience method that handles the U8Array32 conversion internally.
450+
///
451+
/// Example:
452+
/// ```dart
453+
/// final preimage = PaymentPreimageExtensions.fromBytes([1, 2, 3, ..., 32]);
454+
/// ```
455+
static types.PaymentPreimage fromBytes(List<int> bytes) {
456+
if (bytes.length != 32) {
457+
throw ArgumentError('PaymentPreimage must be exactly 32 bytes, got ${bytes.length}');
458+
}
459+
return types.PaymentPreimage(data: U8Array32(Uint8List.fromList(bytes)));
460+
}
440461
}

0 commit comments

Comments
 (0)