mrtnetwork / bitcoin_base

A versatile library for Bitcoin, Dogecoin, Litecoin, Dash, BSV and Bitcoin Cash. Supports P2PK, P2PKH, P2SH, P2WPKH, P2WSH, Taproot, with advanced creation, signing, and spending capabilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

i got non-mandatory-script-verify-flag (Invalid Schnorr signature)

weixuefeng opened this issue · comments

i use this code, i receive address for regtest, and change blockchain_utils for support regtest.

Future<void> spendingP2TR(ECPrivate sWallet, ECPrivate rWallet) async {
  // All the steps are the same as in the first tutorial;
  // the only difference is the transaction input type,
  // and we use method `buildP2trTransaction` to create the transaction.
  // we use `signTapRoot` of ECPrivate for signing taproot transaction
  final addr = sWallet.getPublic();
  // P2TR address
  final sender = addr.toTaprootAddress();
  var txHash =
      "d3ff326b6c592916d6fff13aa3a7a3e3b762a80d8bd99b80b9a8c7e9c904ea05";
  var value = BigInt.from(99999780);
  var vout = 1;
  var scriptType = BitcoinAddressType.p2tr;
  var blockHeight = 441;
  final utxo = [
    UtxoWithAddress(
        utxo: BitcoinUtxo(
            txHash: txHash,
            value: value,
            vout: vout,
            scriptType: scriptType,
            blockHeight: blockHeight),
        ownerDetails:
            UtxoAddressDetails(address: sender, publicKey: addr.toHex()))
  ];
  final sumOfUtxo = utxo.sumOfUtxosValue();
  if (sumOfUtxo == BigInt.zero) {
    throw Exception(
        "account does not have any unspent transaction or mybe no confirmed");
  }

  final feeRate = BitcoinFeeRate.fromMempool({
    "fastestFee": 1,
    "halfHourFee": 1,
    "minimumFee": 1,
  });
  final prive = sWallet;

  final recPub = rWallet.getPublic();
  final receiver = recPub.toSegwitAddress();
  final changeAddress = recPub.toSegwitAddress();
  final List<BitcoinAddress> outputsAdress = [receiver, changeAddress];
  final transactionSize = BitcoinTransactionBuilder.estimateTransactionSize(
      utxos: utxo, outputs: outputsAdress, network: network);
  final estimateFee = BigInt.from(transactionSize * 2);
  final canSpend = sumOfUtxo - estimateFee;
  final outPutWithValue = outputsAdress
      .map((e) => BitcoinOutput(
          address: e, value: canSpend ~/ BigInt.from(outputsAdress.length)))
      .toList();

  final transaction = buildP2trTransaction(
    receiver: outPutWithValue,
    sign: (p0, publicKey, sigHash) {
      // Use signTapRoot instead of signInput for the taproot transaction input.
      return prive.signTapRoot(p0, sighash: sigHash, tweak: true);
    },
    utxo: utxo,
  );
  final ser = transaction.serialize();
  final id = transaction.txId();
  print(ser);
}

got error:

non-mandatory-script-verify-flag (Invalid Schnorr signature)