Skip to content

Commit

Permalink
Merge pull request #74 from LtbLightning/bugfix-drainTo
Browse files Browse the repository at this point in the history
Bugfix for drainTo
  • Loading branch information
BitcoinZavior authored Apr 28, 2023
2 parents c8a37b2 + 4854b45 commit b4a2c87
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To use the `bdk_flutter` package in your project, add it as a dependency in your

```dart
dependencies:
bdk_flutter: ^0.27.1
bdk_flutter: ^0.27.3
```

### Examples
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ class _MyAppState extends State<MyApp> {
final script = await address.scriptPubKey();
final feeRate = await estimateFeeRate(25);
final txBuilderResult = await txBuilder
.addRecipient(script, 700)
.feeRate(feeRate.asSatPerVb())
.addRecipient(script, 1200)
.finish(bdkWallet);
getTransactionDetails(txBuilderResult);
final sbt = await bdkWallet.sign(txBuilderResult.psbt);
Expand Down
2 changes: 1 addition & 1 deletion ios/bdk_flutter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
Pod::Spec.new do |s|
s.name = 'bdk_flutter'
s.version = '0.27.1'
s.version = '0.27.3'
s.summary = 'A Flutter library for the Bitcoin Development Kit (https://bitcoindevkit.org/)'
s.description = <<-DESC
A new Flutter project.
Expand Down
3 changes: 2 additions & 1 deletion lib/src/root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ class TxBuilder {
/// Returns a [TxBuilderResult].
Future<TxBuilderResult> finish(Wallet wallet) async {
if (_recipients.isEmpty) {
if (_recipients.isEmpty && _drainTo == null) {
throw const BdkException.unExpected("No Recipients Added");
}
try {
Expand Down Expand Up @@ -914,6 +914,7 @@ class TxBuilderResult {
final PartiallySignedTransaction psbt;

///The transaction details.
///
final TransactionDetails txDetails;

TxBuilderResult({required this.psbt, required this.txDetails});
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bdk_flutter
description: A Flutter library for the Bitcoin Development Kit(bdk) (https://bitcoindevkit.org/)
version: 0.27.2
version: 0.27.3
homepage: https://github.com/LtbLightning/bdk-flutter

environment:
Expand Down
49 changes: 39 additions & 10 deletions test/bdk_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'bdk_flutter_test.mocks.dart';
@GenerateNiceMocks([MockSpec<Address>()])
@GenerateNiceMocks([MockSpec<DerivationPath>()])
@GenerateNiceMocks([MockSpec<FeeRate>()])
@GenerateNiceMocks([MockSpec<TxBuilderResult>()])
void main() {
final mockWallet = MockWallet();
final mockSDescriptorSecret = MockDescriptorSecretKey();
Expand All @@ -27,6 +28,7 @@ void main() {
final mockAddress = MockAddress();
final mockBumpFeeTxBuilder = MockBumpFeeTxBuilder();
final mockScript = MockScript();
final mockTxBuilderResult = MockTxBuilderResult();

group('Blockchain', () {
test('verify getHeight', () async {
Expand Down Expand Up @@ -157,7 +159,7 @@ void main() {
expect(error, isA<BdkException>());
}
});
test('Should return aTxBuilderException when no recipients are added',
test('Should return a TxBuilderException when no recipients are added',
() async {
try {
when(mockTxBuilder.finish(mockWallet))
Expand All @@ -168,6 +170,7 @@ void main() {
expect(error, isA<BdkException>());
}
});

test('Verify addData() Exception', () async {
try {
when(mockTxBuilder.addData(data: List.empty()))
Expand All @@ -185,22 +188,48 @@ void main() {
vout: 1));
expect(res, isNot(mockTxBuilder));
});
test('Create a proper psbt transaction ', () async {
const psbt = "cHNidP8BAHEBAAAAAfU6uDG8hNUox2Qw1nodiir"
"QhnLkDCYpTYfnY4+lUgjFAAAAAAD+////Ag5EAAAAAAAAFgAUxYD3fd+pId3hWxeuvuWmiUlS+1PoAwAAAAAAABYAFP+dpWfmLzDqhlT6HV+9R774474TxqQkAAABAN4"
"BAAAAAAEBViD1JkR+REQpHyOkKYkuVcOIiPzB0wUr8hFmrebQxe8AAAAAAP7///8ClEgAAAAAAAAWABTwV07KrKa1zWpwKzW+ve93pbQ4R+gDAAAAAAAAFgAU/52lZ+YvMOqGVPodX71Hv"
"vjjvhMCRzBEAiAa6a72jEfDuiyaNtlBYAxsc2oSruDWF2vuNQ3rJSshggIgLtJ/YuB8FmhjrPvTC9r2w9gpdfUNLuxw/C7oqo95cEIBIQM9XzutA2SgZFHjPDAATuWwHg19TTkb/NKZD/"
"hfN7fWP8akJAABAR+USAAAAAAAABYAFPBXTsqsprXNanArNb6973eltDhHIgYCHrxaLpnD4ed01bFHcixnAicv15oKiiVHrcVmxUWBW54Y2R5q3VQAAIABAACAAAAAgAEAAABbAAAAACICAqS"
"F0mhBBlgMe9OyICKlkhGHZfPjA0Q03I559ccj9x6oGNkeat1UAACAAQAAgAAAAIABAAAAXAAAAAAA";

test(
'Should not return a TxBuilderException when, a drainTo script address is added (with feeRate)',
() async {
when(mockTxBuilder.drainTo("mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB"))
.thenReturn(mockTxBuilder);
when(mockTxBuilder.drainTo("mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB"))
.thenReturn(mockTxBuilder);
when(mockTxBuilder.feeRate(any)).thenReturn(mockTxBuilder);
when(mockTxBuilder.finish(mockWallet))
.thenAnswer((_) async => mockTxBuilderResult);
final txBuilder = mockTxBuilder
.drainTo("mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB")
.feeRate(25);
final res = await txBuilder.finish(mockWallet);
expect(res.psbt, isA<PartiallySignedTransaction>());
expect(res.txDetails, isA<TransactionDetails>());
});
test(
'Should not return a TxBuilderException when, a drainTo script address is added, instead of addRecipients',
() async {
when(mockTxBuilder.drainTo("mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB"))
.thenReturn(mockTxBuilder);
when(mockTxBuilder.finish(mockWallet))
.thenAnswer((_) async => mockTxBuilderResult);
final txBuilder =
mockTxBuilder.drainTo("mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB");
final res = await txBuilder.finish(mockWallet);
expect(res.psbt, isA<PartiallySignedTransaction>());
expect(res.txDetails, isA<TransactionDetails>());
});

test('Create a proper psbt transaction ', () async {
when(mockAddress.scriptPubKey()).thenAnswer((_) async => mockScript);
when(mockTxBuilder.addRecipient(mockScript, any))
.thenReturn(mockTxBuilder);

when(mockTxBuilder.finish(mockWallet))
.thenAnswer((_) async => mockTxBuilderResult);
final script = await mockAddress.scriptPubKey();
final txBuilder = mockTxBuilder.addRecipient(script, 1200);
final res = await txBuilder.finish(mockWallet);
expect(res.psbt, psbt);
expect(res, mockTxBuilderResult);
});
});
group('Bump Fee Tx Builder', () {
Expand Down
45 changes: 43 additions & 2 deletions test/bdk_flutter_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Mocks generated by Mockito 5.3.2 from annotations
// in bdk_flutter/example/ios/.symlinks/plugins/bdk_flutter/test/bdk_flutter_test.dart.
// Mocks generated by Mockito 5.4.0 from annotations
// in bdk_flutter/test/bdk_flutter_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
Expand Down Expand Up @@ -135,6 +135,17 @@ class _FakeScript_10 extends _i1.SmartFake implements _i3.Script {
);
}

class _FakeTransactionDetails_11 extends _i1.SmartFake
implements _i2.TransactionDetails {
_FakeTransactionDetails_11(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

/// A class which mocks [Wallet].
///
/// See the documentation for Mockito's code generation for more information.
Expand Down Expand Up @@ -1114,3 +1125,33 @@ class MockFeeRate extends _i1.Mock implements _i3.FeeRate {
returnValueForMissingStub: 0.0,
) as double);
}

/// A class which mocks [TxBuilderResult].
///
/// See the documentation for Mockito's code generation for more information.
class MockTxBuilderResult extends _i1.Mock implements _i3.TxBuilderResult {
@override
_i3.PartiallySignedTransaction get psbt => (super.noSuchMethod(
Invocation.getter(#psbt),
returnValue: _FakePartiallySignedTransaction_2(
this,
Invocation.getter(#psbt),
),
returnValueForMissingStub: _FakePartiallySignedTransaction_2(
this,
Invocation.getter(#psbt),
),
) as _i3.PartiallySignedTransaction);
@override
_i2.TransactionDetails get txDetails => (super.noSuchMethod(
Invocation.getter(#txDetails),
returnValue: _FakeTransactionDetails_11(
this,
Invocation.getter(#txDetails),
),
returnValueForMissingStub: _FakeTransactionDetails_11(
this,
Invocation.getter(#txDetails),
),
) as _i2.TransactionDetails);
}

0 comments on commit b4a2c87

Please sign in to comment.