Saturday, April 1, 2023
HomeBitcointestnet - NBitcoin in C# MVC App Provides Me A WitnessProgramMissmatch Error...

testnet – NBitcoin in C# MVC App Provides Me A WitnessProgramMissmatch Error Once I Attempt to Construct The Transaction


I’m writing exams for an app I constructed that makes use of NBitcoin to ship bitcoin and make bitcoin transactions.

I’ve the next code to create faux bitcoins for an integration check:

non-public ICoin[] CreateFakeCoins(TxInList inputs, Script scriptPubKey)
{
    var cash = inputs.Choose(i => new Coin(i.PrevOut, inputs.Transaction.Outputs.CreateNewTxOut(Cash.Cash(0.1m), scriptPubKey.WitHash.ScriptPubKey))).ToArray();
    for (int i = 0; i < cash.Size; i++)
    {
        cash[i] = cash[i].ToScriptCoin(scriptPubKey);
    }
    return cash;
}

My integration check has this piece of code that is imagined to ship the faux bitcoins to a different pockets.

var ok = new BitcoinSecret(new Key(), Community.TestNet);
var moneyInWallet1 = BitcoinHelper.GetBitcoinAddress(walletTestNet?.Deal with, Community.TestNet);

Transaction tx = Community.TestNet.CreateTransaction();
var privkey = new BitcoinSecret(walletTestNet?.Deal with, Community.TestNet);
var scriptPubKey = PayToWitScriptHashTemplate.Occasion.GenerateScriptPubKey(ok.PubKey.ScriptPubKey.WitHash);
var scriptPubKeyRec = PayToWitScriptHashTemplate.Occasion.GenerateScriptPubKey(privkey.PubKey.ScriptPubKey.WitHash);

tx.Inputs.Add(new OutPoint(tx.GetHash(), 0), scriptPubKey.ToWitScript());
tx.Inputs.Add(new OutPoint(tx.GetHash(), 1), scriptPubKey.ToWitScript());
tx.Outputs.Add("21", ok.PubKey.WitHash);
tx.Signal(new[] { ok }, CreateFakeCoins(tx.Inputs, scriptPubKey));

Assert.Equal(ok.GetAddress(ScriptPubKeyType.Segwit).ToString().Substring(0, 3), "tb1");
Assert.Equal(tx.Outputs[0].Worth.Satoshi / 100000000, 21);
Assert.Equal(moneyInWallet1.Deal with.ToString().Substring(0, 3), "tb1");

var builder = Community.TestNet.CreateTransactionBuilder();
var (receiveAddress, receiveBtcSecret) = BitcoinHelper.GetBitcoinAddress(privKeyCurrent, Community.TestNet);
var (fromAddressProcessed, fromBtcSecret) = BitcoinHelper.GetBitcoinAddress(walletTestNet?.Deal with, Community.TestNet);
            
var amountSatoshiMult = Math.Ceiling(0.0006*100000000.00);
Cash amountConvertedtoMoney = new Cash((lengthy)amountSatoshiMult);

var testibleOutputs = tx.Outputs.AsCoins().Solid<ICoin>().ToArray();
var txBuild = builder
    .AddCoins(testibleOutputs)
    .AddKeys(fromBtcSecret)
    .Ship(receiveAddress, amountConvertedtoMoney)
    .SetChange(fromAddressProcessed, ChangeType.All)
    .SendFees(Cash.Cash(new decimal(0.0001)));

NBitcoin.Coverage.TransactionPolicyError[] errors = null;
Transaction nextTx = txBuild.BuildTransaction(true);
var testerForBuilder = builder.Confirm(nextTx, out errors);
Console.WriteLine(errors);
Console.WriteLine(testerForBuilder);
Assert.Equal(true, testerForBuilder);

On this code, the BitcoinHelper.GetBitcoinAddress operate merely receives an handle string and returns a correct NBitcoin BitcoinAddress object.

It is a image of the error I get:
enter image description here

Discover the WitnessProgramMissmatch error.

What’s the drawback? Is CreateFakeCoins not appropriately creating faux cash for testing? How can I make this work?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments