static BigInt _changeValue(BigInt sum, List<BigInt> all) {
final sumAll = all.fold<BigInt>(
BigInt.zero, (previousValue, element) => previousValue + element);
final remind = sum - sumAll;
if (remind < BigInt.zero) {
throw ArgumentError("invalid values");
}
return remind;
}
static Future<void> sendDoge(String recipientAddress, double amount) async {
/// Define the network
const network = DogecoinNetwork.mainnet;
String mne = await BaseWalletManger.decoAESString(
BaseWalletNoti.baseWalletNotifier.value!.mne, '123');
final seed = bip39.mnemonicToSeed(mne);
final bip32Node = Bip32Slip10Secp256k1.fromSeed(seed);
final childNode = bip32Node.childKey(Bip32KeyIndex(3));
//发送方的公钥和私钥
final senderPrivateKey = ECPrivate.fromBytes(childNode.privateKey.raw);
final senderPublicKey = senderPrivateKey.getPublic();
//:获取未花费的交易输出
List<UtxoWithAddress> unspent = [];
BigInt unspentValue = BigInt.zero;
BaseWalletNoti.selectUtxos.asMap().forEach((index, element) {
unspentValue += BigInt.parse(element.value);
unspent.add(UtxoWithAddress(
utxo: BitcoinUtxo(
txHash: element.txid,
value: BigInt.parse(element.value),
vout: 1, //index, // 使用当前索引作为 vout
scriptType: senderPublicKey.toP2pkInP2sh().type,
),
ownerDetails: UtxoAddressDetails(
publicKey: senderPublicKey.toHex(),
address: senderPublicKey.toP2pkInP2sh(),
),
));
});
// double result = int.parse(BaseWalletNoti.txidsNotifier.value!.first.value) / 100000000;
final fee = BtcUtils.toSatoshi(BaseWalletNoti.feeNotifier.value!);
final change = _changeValue(
unspentValue,
[
BtcUtils.toSatoshi("1"),
fee,
]);
//:输出地址
final out1 =
P2pkhAddress.fromAddress(address: recipientAddress, network: network);
//:找零地址
final out2 = P2pkhAddress.fromAddress(
address: BaseWalletNoti.baseWalletNotifier.value!.address,
network: network);
;
final b = BitcoinTransactionBuilder(
outPuts: [
//:输出
BitcoinOutput(address: out1, value: BtcUtils.toSatoshi("1")),
//:找零
BitcoinOutput(address: out2, value: change),
],
fee: fee,
network: network,
// memo: "MRTNETWORK.com",
utxos: unspent,
);
final tr = b.buildTransaction((trDigest, utxo, publicKey, sighash) {
if (publicKey == senderPublicKey.toHex()) {
return senderPrivateKey.signInput(trDigest, sigHash: sighash);
}
throw UnimplementedError();
});
final txId = tr.txId();
final size = tr.getSize();
BaseWalletManger.managerSendTransaction(tr.serialize());
}
According to {dog_example.dart} to sendrawTransaction ,Always get this mistake.