<p>The fix was simple. I changed:</p>
<pre><code>tx.Outputs.Add(&quot;21&quot;, k.PubKey.WitHash);
tx.Sign(new[] { k }, CreateFakeCoins(tx.Inputs, scriptPubKey));
</code></pre>
<p>To</p>
<pre><code>tx.Outputs.Add(&quot;21&quot;, privkey.PubKey.WitHash);
tx.Sign(new[] { k }, CreateFakeCoins(tx.Inputs, scriptPubKey.ToWitScript()));
</code></pre>
<p>After this it worked. I was adding the 21 coins to the receiving private key, not the sending private key. Also I had to convert <code>scriptPubKey</code> which was a <code>Script</code> to a <code>WitScript</code>.</p>

CC BY-SA 4.0
2022-08-01T19:41:17.550Z
2022-08-01T19:41:17.550Z
ChristianOConnor
<p>I am writing tests for an app I built that uses NBitcoin to send bitcoin and make bitcoin transactions.</p>
<p>I have the following code to create fake bitcoins for an integration test:</p>
<pre><code>private ICoin[] CreateFakeCoins(TxInList inputs, Script scriptPubKey)
{
    var coins = inputs.Select(i =&gt; new Coin(i.PrevOut, inputs.Transaction.Outputs.CreateNewTxOut(Money.Coins(0.1m), scriptPubKey.WitHash.ScriptPubKey))).ToArray();
    for (int i = 0; i &lt; coins.Length; i++)
    {
        coins[i] = coins[i].ToScriptCoin(scriptPubKey);
    }
    return coins;
}
</code></pre>
<p>My integration test has this piece of code that's supposed to send the fake bitcoins to another wallet.</p>
<pre><code>var k = new BitcoinSecret(new Key(), Network.TestNet);
var moneyInWallet1 = BitcoinHelper.GetBitcoinAddress(walletTestNet?.Address, Network.TestNet);

Transaction tx = Network.TestNet.CreateTransaction();
var privkey = new BitcoinSecret(walletTestNet?.Address, Network.TestNet);
var scriptPubKey = PayToWitScriptHashTemplate.Instance.GenerateScriptPubKey(k.PubKey.ScriptPubKey.WitHash);
var scriptPubKeyRec = PayToWitScriptHashTemplate.Instance.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(&quot;21&quot;, k.PubKey.WitHash);
tx.Sign(new[] { k }, CreateFakeCoins(tx.Inputs, scriptPubKey));

Assert.Equal(k.GetAddress(ScriptPubKeyType.Segwit).ToString().Substring(0, 3), &quot;tb1&quot;);
Assert.Equal(tx.Outputs[0].Value.Satoshi / 100000000, 21);
Assert.Equal(moneyInWallet1.Address.ToString().Substring(0, 3), &quot;tb1&quot;);

var builder = Network.TestNet.CreateTransactionBuilder();
var (receiveAddress, receiveBtcSecret) = BitcoinHelper.GetBitcoinAddress(privKeyCurrent, Network.TestNet);
var (fromAddressProcessed, fromBtcSecret) = BitcoinHelper.GetBitcoinAddress(walletTestNet?.Address, Network.TestNet);
            
var amountSatoshiMult = Math.Ceiling(0.0006*100000000.00);
Money amountConvertedtoMoney = new Money((long)amountSatoshiMult);

var testibleOutputs = tx.Outputs.AsCoins().Cast&lt;ICoin&gt;().ToArray();
var txBuild = builder
    .AddCoins(testibleOutputs)
    .AddKeys(fromBtcSecret)
    .Send(receiveAddress, amountConvertedtoMoney)
    .SetChange(fromAddressProcessed, ChangeType.All)
    .SendFees(Money.Coins(new decimal(0.0001)));

NBitcoin.Policy.TransactionPolicyError[] errors = null;
Transaction nextTx = txBuild.BuildTransaction(true);
var testerForBuilder = builder.Verify(nextTx, out errors);
Console.WriteLine(errors);
Console.WriteLine(testerForBuilder);
Assert.Equal(true, testerForBuilder);
</code></pre>
<p>In this code, the <code>BitcoinHelper.GetBitcoinAddress</code> function simply receives an address string and returns a proper NBitcoin BitcoinAddress object.</p>
<p>This is a picture of the error I get:<br />
<a href="https://i.sstatic.net/Ny11v.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ny11v.jpg" alt="enter image description here" /></a></p>
<p>Notice the <code>WitnessProgramMissmatch</code> error.</p>
<p>What is the problem? Is <code>CreateFakeCoins</code> not correctly creating fake coins for testing? How can I make this work?</p>

CC BY-SA 4.0
2022-08-01T14:07:11.307Z
Does this answer your question? [Witness program hash mismatch while signing bech32 transaction](https://bitcoin.stackexchange.com/questions/85352/witness-program-hash-mismatch-while-signing-bech32-transaction)
RedGrittyBrick
<testnet><output><c#><nbitcoin><unit-test>
NBitcoin in C# MVC App Gives Me A WitnessProgramMissmatch Error When I Try to Build The Transaction
ChristianOConnor
# NBitcoin in C# MVC App Gives Me A WitnessProgramMissmatch Error When I Try to Build The Transaction

(Asked by: username_0 on 2022-07-30)

I am writing tests for an app I built that uses NBitcoin to send bitcoin and make bitcoin transactions.

I have the following code to create fake bitcoins for an integration test:

```
private ICoin[] CreateFakeCoins(TxInList inputs, Script scriptPubKey)
{
    var coins = inputs.Select(i => new Coin(i.PrevOut, inputs.Transaction.Outputs.CreateNewTxOut(Money.Coins(0.1m), scriptPubKey.WitHash.ScriptPubKey))).ToArray();
    for (int i = 0; i < coins.Length; i++)
    {
        coins[i] = coins[i].ToScriptCoin(scriptPubKey);
    }
    return coins;
}

```

My integration test has this piece of code that's supposed to send the fake bitcoins to another wallet.

```
var k = new BitcoinSecret(new Key(), Network.TestNet);
var moneyInWallet1 = BitcoinHelper.GetBitcoinAddress(walletTestNet?.Address, Network.TestNet);

Transaction tx = Network.TestNet.CreateTransaction();
var privkey = new BitcoinSecret(walletTestNet?.Address, Network.TestNet);
var scriptPubKey = PayToWitScriptHashTemplate.Instance.GenerateScriptPubKey(k.PubKey.ScriptPubKey.WitHash);
var scriptPubKeyRec = PayToWitScriptHashTemplate.Instance.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", k.PubKey.WitHash);
tx.Sign(new[] { k }, CreateFakeCoins(tx.Inputs, scriptPubKey));

Assert.Equal(k.GetAddress(ScriptPubKeyType.Segwit).ToString().Substring(0, 3), "tb1");
Assert.Equal(tx.Outputs[0].Value.Satoshi / 100000000, 21);
Assert.Equal(moneyInWallet1.Address.ToString().Substring(0, 3), "tb1");

var builder = Network.TestNet.CreateTransactionBuilder();
var (receiveAddress, receiveBtcSecret) = BitcoinHelper.GetBitcoinAddress(privKeyCurrent, Network.TestNet);
var (fromAddressProcessed, fromBtcSecret) = BitcoinHelper.GetBitcoinAddress(walletTestNet?.Address, Network.TestNet);
            
var amountSatoshiMult = Math.Ceiling(0.0006*100000000.00);
Money amountConvertedtoMoney = new Money((long)amountSatoshiMult);

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

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

```

In this code, the `BitcoinHelper.GetBitcoinAddress` function simply receives an address string and returns a proper NBitcoin BitcoinAddress object.

This is a picture of the error I get:  
[![enter image description here](https://i.sstatic.net/Ny11v.jpg)](https://i.sstatic.net/Ny11v.jpg)

Notice the `WitnessProgramMissmatch` error.

What is the problem? Is `CreateFakeCoins` not correctly creating fake coins for testing? How can I make this work?

**Tags:** testnet, output, c#, nbitcoin, unit-test

> **Comments:**
> **username_1**: Does this answer your question? [Witness program hash mismatch while signing bech32 transaction](https://bitcoin.stackexchange.com/questions/85352/witness-program-hash-mismatch-while-signing-bech32-transaction)  

---

## 1 answers

### Answer by username_0 on 2022-08-01. Score: 0 | Accepted answer

The fix was simple. I changed:

```
tx.Outputs.Add("21", k.PubKey.WitHash);
tx.Sign(new[] { k }, CreateFakeCoins(tx.Inputs, scriptPubKey));

```

To

```
tx.Outputs.Add("21", privkey.PubKey.WitHash);
tx.Sign(new[] { k }, CreateFakeCoins(tx.Inputs, scriptPubKey.ToWitScript()));

```

After this it worked. I was adding the 21 coins to the receiving private key, not the sending private key. Also I had to convert `scriptPubKey` which was a `Script` to a `WitScript`.


---

