|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:bdk_flutter/bdk_flutter.dart'; |
| 4 | +import 'package:flutter/foundation.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:integration_test/integration_test.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 10 | + group('Descriptor & Wallet', () { |
| 11 | + setUp(() async {}); |
| 12 | + testWidgets('generating psbt using a muti-sig wallet', (_) async { |
| 13 | + final externalDescriptor = await Descriptor.create( |
| 14 | + descriptor: |
| 15 | + "wsh(thresh(2,pk(tpubD6NzVbkrYhZ4XJBfEJ6gt9DiVdfWJijsQTCE3jtXByW3Tk6AVGQ3vL1NNxg3SjB7QkJAuutACCQjrXD8zdZSM1ZmBENszCqy49ECEHmD6rf/0/*),sj:and_v(v:pk(tpubD6NzVbkrYhZ4YfAr3jCBRk4SpqB9L1Hh442y83njwfMaker7EqZd7fHMqyTWrfRYJ1e5t2ue6BYjW5i5yQnmwqbzY1a3kfqNxog1AFcD1aE/0/*),n:older(6)),snj:and_v(v:pk(tprv8ZgxMBicQKsPeitVUz3s6cfyCECovNP7t82FaKPa4UKqV1kssWcXgLkMDjzDbgG9GWoza4pL7z727QitfzkiwX99E1Has3T3a1MKHvYWmQZ/0/*),after(630000))))", |
| 16 | + network: Network.signet); |
| 17 | + final internalDescriptor = await Descriptor.create( |
| 18 | + descriptor: |
| 19 | + "wsh(thresh(2,pk(tpubD6NzVbkrYhZ4XJBfEJ6gt9DiVdfWJijsQTCE3jtXByW3Tk6AVGQ3vL1NNxg3SjB7QkJAuutACCQjrXD8zdZSM1ZmBENszCqy49ECEHmD6rf/1/*),sj:and_v(v:pk(tpubD6NzVbkrYhZ4YfAr3jCBRk4SpqB9L1Hh442y83njwfMaker7EqZd7fHMqyTWrfRYJ1e5t2ue6BYjW5i5yQnmwqbzY1a3kfqNxog1AFcD1aE/1/*),n:older(6)),snj:and_v(v:pk(tprv8ZgxMBicQKsPeitVUz3s6cfyCECovNP7t82FaKPa4UKqV1kssWcXgLkMDjzDbgG9GWoza4pL7z727QitfzkiwX99E1Has3T3a1MKHvYWmQZ/1/*),after(630000))))", |
| 20 | + network: Network.signet); |
| 21 | + |
| 22 | + final wallet = await Wallet.create( |
| 23 | + descriptor: externalDescriptor, |
| 24 | + changeDescriptor: internalDescriptor, |
| 25 | + network: Network.signet, |
| 26 | + databaseConfig: const DatabaseConfig.memory()); |
| 27 | + final blockchain = await Blockchain.createMutinynet(); |
| 28 | + wallet.sync(blockchain: blockchain); |
| 29 | + debugPrint("Wallet balance: ${wallet.getBalance().total}"); |
| 30 | + final toAddress = wallet |
| 31 | + .getAddress(addressIndex: const AddressIndex.increase()) |
| 32 | + .address; |
| 33 | + debugPrint("Wallet address: ${toAddress.toString()}"); |
| 34 | + final externalWalletPolicy = wallet.policies(KeychainKind.externalChain); |
| 35 | + final ineternalWalletPolicy = wallet.policies(KeychainKind.internalChain); |
| 36 | + if (externalWalletPolicy != null && ineternalWalletPolicy != null) { |
| 37 | + // Construct external and internal policy paths |
| 38 | + final extPath = { |
| 39 | + ineternalWalletPolicy.id(): Uint32List.fromList([0, 1]) |
| 40 | + }; |
| 41 | + debugPrint("External Policy path: $extPath\n"); |
| 42 | + |
| 43 | + final intPath = { |
| 44 | + ineternalWalletPolicy.id(): Uint32List.fromList([0, 1]) |
| 45 | + }; |
| 46 | + debugPrint("Internal Policy Path: $intPath\n"); |
| 47 | + |
| 48 | + // Build the transaction |
| 49 | + final txBuilder = TxBuilder() |
| 50 | + .addRecipient( |
| 51 | + toAddress.scriptPubkey(), |
| 52 | + BigInt.from(1000), |
| 53 | + ) |
| 54 | + .doNotSpendChange() |
| 55 | + .policyPath(KeychainKind.internalChain, intPath) |
| 56 | + .policyPath(KeychainKind.externalChain, extPath); |
| 57 | + |
| 58 | + final (psbt, _) = await txBuilder.finish(wallet); |
| 59 | + debugPrint("Transaction serialized: ${psbt.toString()}\n"); |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | +} |
0 commit comments