user2068: I am building a daap and i want the user to be able to mint their app-tokens but the policy of the token must include the signature of the app wallet as well, for this I did the policy with the following code: where appKeyHash and clientKeyHash are the respective hashes for client and app addresses
const nativeScripts = Loader.Cardano.NativeScripts.new();
  const script = Loader.Cardano.ScriptPubkey.new(clientKeyHash);
  const nativeScript = Loader.Cardano.NativeScript.new_script_pubkey(script);
  const appScript = Loader.Cardano.ScriptPubkey.new(appKeyHash);
  const appNativeScript =
    Loader.Cardano.NativeScript.new_script_pubkey(appScript);

  const lockScript = Loader.Cardano.NativeScript.new_timelock_expiry(
    Loader.Cardano.TimelockExpiry.new(ttl)
  );
  nativeScripts.add(nativeScript);
  nativeScripts.add(appNativeScript);
  nativeScripts.add(lockScript);

  const finalScript = Loader.Cardano.NativeScript.new_script_all(
    Loader.Cardano.ScriptAll.new(nativeScripts)
  );
  const policyId = Buffer.from(
    Loader.Cardano.ScriptHash.from_bytes(
      finalScript.hash().to_bytes()
    ).to_bytes(),
    "hex"
  ).toString("hex");

Then I do all the transaction fee calculation and use Nami wallet to partial-sign the Tx (in the client side). Then I send to my backend the partial-signed transaction, and I add one vkeywitness using the private key of the app and Cardano-Serialization-Lib with the following code:
 const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txString, "hex"));
const body = tx.body();
const txHash = CardanoWasm.hash_transaction(body);
const txWitnesses = tx.witness_set();
const txVkeys = txWitnesses.vkeys();
const txScripts = txWitnesses.native_scripts();

const addWitnesses = CardanoWasm.TransactionWitnessSet.new();
const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();

const vkeyWitness = CardanoWasm.make_vkey_witness(txHash, prvKey);
vkeyWitnesses.add(vkeyWitness);
addWitnesses.set_vkeys(vkeyWitnesses);
const addVkeys = addWitnesses.vkeys();
const addScripts = addWitnesses.native_scripts();

const totalVkeys = CardanoWasm.Vkeywitnesses.new();
const totalScripts = CardanoWasm.NativeScripts.new();
if (txVkeys) {
  for (let i = 0; i < txVkeys.len(); i++) {
    totalVkeys.add(txVkeys.get(i));
  }
}
if (txScripts) {
  for (let i = 0; i < txScripts.len(); i++) {
    totalScripts.add(txScripts.get(i));
  }
}
if (addVkeys) {
  for (let i = 0; i < addVkeys.len(); i++) {
    totalVkeys.add(addVkeys.get(i));
  }
}
if (addScripts) {
  for (let i = 0; i < addScripts.len(); i++) {
    totalScripts.add(addScripts.get(i));
  }
}
const totalWitnesses = CardanoWasm.TransactionWitnessSet.new();
totalWitnesses.set_vkeys(totalVkeys);
totalWitnesses.set_native_scripts(totalScripts);
const signedTx = CardanoWasm.Transaction.new(
  tx.body(),
  totalWitnesses,
  tx.auxiliary_data()
);

Where tx is the hex-CBOR transaction i get from my front end (with the front-end witnessvkey already attached)
But when i try to send it i get the following error :
"transaction submit error ShelleyTxValidationError ShelleyBasedEraAlonzo (ApplyTxError [UtxowFailure (WrappedShelleyEraFailure (ScriptWitnessNotValidatingUTXOW (fromList [ScriptHash \"afae51e0836484a4196a3ef11018ab78990008df6459e70f7db6cfb4\"]))),UtxowFailure (WrappedShelleyEraFailure (UtxoFailure (FeeTooSmallUTxO (Coin 187325) (Coin 187193))))])"

The first error its because i am not correctly validating the transaction, the seccond is because the fees go up a little because I added one extra signature. I am not worried about the seccond error, what i do not underestand is
why is happening the first error:
WrappedShelleyEraFailure (ScriptWitnessNotValidatingUTXOW (fromList [ScriptHash \"afae51e0836484a4196a3ef11018ab78990008df6459e70f7db6cfb4\"]))) 

Even if i signed the Tx with the correponding privakekeys ?

user2629: There seems to be a bug on @emurgo/cardano-serialization-lib end related to the issue that you are facing, submitted here

user4043: This is not a particularly trivial task, as we faced it today! I can't tell exactly what is off about your code, but it's possibly something very minor. I've noticed that the JS -> WASM handover can cause some weird type confusion for example if you pass a TimelockExpiry object into txBuilder.set_ttl it will actually set it to 0. It's very easy to make mistakes!
I can however offer an example that we tested and is working, based off some existing code we had as well as andycandy's example on the Github Issue. You can find the Gist here (hopefully it will help you figure out what is wrong with your code!):
https://gist.github.com/lovalabs/83d08796ab9ff65cbf188fbca8d03343
Note that we're in the process of releasing a minting SDK for cardano-serialization-lib that will be wrap around these low-level details and make minting assets much easier. Since the version of csl this is based off is still in beta, we're waiting for it to be stable before releasing.
EDIT:
After some further testing I've discovered that there may be a bug in the TransactionBuilder implementation of build() vs build_tx() which could be the reason for your error. When I do this:
const tx = txBuilder.build_tx()

I don't get a ScriptWitnessNotValidatingUTXOW error. But if I do this:
const txBody = txBuilder.build()
const tx = S.Transaction.new(
        txBody,
        txWitnessSet,
        auxiliaryData
)

Then I do get a ScriptWitnessNotValidatingUTXOW error. Will investigate further before raising an issue on Github though perhaps you could confirm this.

user4627: There might be an issue with the policy id generated in your first code block.
The hash function on NativeScript should take a namespace parameter (see here https://github.com/Emurgo/cardano-serialization-lib/blob/3e5dc377829d1ba46348713196b35476d8604bd9/rust/pkg/cardano_serialization_lib.js.flow#L2699),
that is prepended when calculating the hash (see here https://github.com/Emurgo/cardano-serialization-lib/blob/da6eba20a5ffab2eeae1b1823c26ee67b1426f42/rust/src/lib.rs#L1629)
Try adding the zero like this:
const nativeScript = CardanoWasm.NativeScript.new_script_all(CardanoWasm.ScriptAll.new(nativeScripts))

const policyId = Buffer.from(nativeScript.hash(0).to_bytes()).toString('hex');

instead of this part:
const finalScript = Loader.Cardano.NativeScript.new_script_all(
  Loader.Cardano.ScriptAll.new(nativeScripts)
);
const policyId = Buffer.from(
  Loader.Cardano.ScriptHash.from_bytes(
    finalScript.hash().to_bytes()
  ).to_bytes(),
  "hex"
).toString("hex");


user2068: After days of searching I finally found my mistake: I was tryng to do the signing from backend, so I needed to generate the prvKey for this I followed the instructions provided in Cardano Serialization Lib, my code was
const Bip32PrivateKey =
  "xprv1wrgj3q60pxt5uq094h6f9nj00jx4hrwddgqrnv632a86llc94egpkn77l9mg9ewham96309yj6yv4zdsdr05flv6xsntjgcgj3pfp59ef589a87gen99t4ru5jhnfm4ar0tvlry4guz6l95tnjcjtnf4jv3rf52k";
const prvKeyBip32 = CardanoWasm.Bip32PrivateKey.from_bech32(Bip32PrivateKey);


function harden(num) {
  return 0x80000000 + num;
}

const rootKey = CardanoWasm.Bip32PrivateKey.from_bech32(Bip32PrivateKey);
const accountKey = rootKey
  .derive(harden(1852)) // purpose
  .derive(harden(1815)) // coin type
  .derive(harden(0)); // account #0

const utxoPubKey = accountKey
  .derive(0) // external
  .derive(0)
  .to_public();

const stakeKey = accountKey
  .derive(2) // chimeric
  .derive(0)
  .to_public();

const baseAddr = CardanoWasm.BaseAddress.new(
  CardanoWasm.NetworkInfo.testnet().network_id(),
  CardanoWasm.StakeCredential.from_keyhash(utxoPubKey.to_raw_key().hash()),
  CardanoWasm.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash())
);


const prvKey = rootKey.to_raw_key();

And I was trying to sign with prvKey, for a UTXO in baseAddr, that did not work, but if O do
const prvKey = accountKey
  .derive(0) 
  .derive(0)
  .to_raw_key();

Then I can sign and submit the tx for UTXOs in the baseAddr.

2068: <p>I am building a daap and i want the user to be able to mint their app-tokens but the policy of the token must include the signature of the app wallet as well, for this I did the policy with the following code: where appKeyHash and clientKeyHash are the respective hashes for client and app addresses</p>
<pre><code>const nativeScripts = Loader.Cardano.NativeScripts.new();
  const script = Loader.Cardano.ScriptPubkey.new(clientKeyHash);
  const nativeScript = Loader.Cardano.NativeScript.new_script_pubkey(script);
  const appScript = Loader.Cardano.ScriptPubkey.new(appKeyHash);
  const appNativeScript =
    Loader.Cardano.NativeScript.new_script_pubkey(appScript);

  const lockScript = Loader.Cardano.NativeScript.new_timelock_expiry(
    Loader.Cardano.TimelockExpiry.new(ttl)
  );
  nativeScripts.add(nativeScript);
  nativeScripts.add(appNativeScript);
  nativeScripts.add(lockScript);

  const finalScript = Loader.Cardano.NativeScript.new_script_all(
    Loader.Cardano.ScriptAll.new(nativeScripts)
  );
  const policyId = Buffer.from(
    Loader.Cardano.ScriptHash.from_bytes(
      finalScript.hash().to_bytes()
    ).to_bytes(),
    &quot;hex&quot;
  ).toString(&quot;hex&quot;);
</code></pre>
<p>Then I do all the transaction fee calculation and use Nami wallet to partial-sign the Tx (in the client side). Then I send to my backend the partial-signed transaction, and I add one vkeywitness using the private key of the app and Cardano-Serialization-Lib with the following code:</p>
<pre><code> const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txString, &quot;hex&quot;));
const body = tx.body();
const txHash = CardanoWasm.hash_transaction(body);
const txWitnesses = tx.witness_set();
const txVkeys = txWitnesses.vkeys();
const txScripts = txWitnesses.native_scripts();

const addWitnesses = CardanoWasm.TransactionWitnessSet.new();
const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();

const vkeyWitness = CardanoWasm.make_vkey_witness(txHash, prvKey);
vkeyWitnesses.add(vkeyWitness);
addWitnesses.set_vkeys(vkeyWitnesses);
const addVkeys = addWitnesses.vkeys();
const addScripts = addWitnesses.native_scripts();

const totalVkeys = CardanoWasm.Vkeywitnesses.new();
const totalScripts = CardanoWasm.NativeScripts.new();
if (txVkeys) {
  for (let i = 0; i &lt; txVkeys.len(); i++) {
    totalVkeys.add(txVkeys.get(i));
  }
}
if (txScripts) {
  for (let i = 0; i &lt; txScripts.len(); i++) {
    totalScripts.add(txScripts.get(i));
  }
}
if (addVkeys) {
  for (let i = 0; i &lt; addVkeys.len(); i++) {
    totalVkeys.add(addVkeys.get(i));
  }
}
if (addScripts) {
  for (let i = 0; i &lt; addScripts.len(); i++) {
    totalScripts.add(addScripts.get(i));
  }
}
const totalWitnesses = CardanoWasm.TransactionWitnessSet.new();
totalWitnesses.set_vkeys(totalVkeys);
totalWitnesses.set_native_scripts(totalScripts);
const signedTx = CardanoWasm.Transaction.new(
  tx.body(),
  totalWitnesses,
  tx.auxiliary_data()
);
</code></pre>
<p>Where tx is the hex-CBOR transaction i get from my front end (with the front-end witnessvkey already attached)</p>
<p>But when i try to send it i get the following error :</p>
<pre><code>&quot;transaction submit error ShelleyTxValidationError ShelleyBasedEraAlonzo (ApplyTxError [UtxowFailure (WrappedShelleyEraFailure (ScriptWitnessNotValidatingUTXOW (fromList [ScriptHash \&quot;afae51e0836484a4196a3ef11018ab78990008df6459e70f7db6cfb4\&quot;]))),UtxowFailure (WrappedShelleyEraFailure (UtxoFailure (FeeTooSmallUTxO (Coin 187325) (Coin 187193))))])&quot;
</code></pre>
<p>The first error its because i am not correctly validating the transaction, the seccond is because the fees go up a little because I added one extra signature. I am not worried about the seccond error, what i do not underestand is</p>
<p>why is happening the first error:</p>
<pre><code>WrappedShelleyEraFailure (ScriptWitnessNotValidatingUTXOW (fromList [ScriptHash \&quot;afae51e0836484a4196a3ef11018ab78990008df6459e70f7db6cfb4\&quot;]))) 
</code></pre>
<p>Even if i signed the Tx with the correponding privakekeys ?</p>
 2629: <p>There seems to be a bug on <code>@emurgo/cardano-serialization-lib</code> end related to the issue that you are facing, submitted <a href="https://github.com/Emurgo/cardano-serialization-lib/issues/272" rel="nofollow noreferrer">here</a></p>
 4043: <p>This is not a particularly trivial task, as we faced it today! I can't tell exactly what is off about your code, but it's possibly something very minor. I've noticed that the JS -&gt; WASM handover can cause some weird type confusion for example if you pass a <code>TimelockExpiry</code> object into <code>txBuilder.set_ttl</code> it will actually set it to 0. It's very easy to make mistakes!</p>
<p>I can however offer an example that we tested and is working, based off some existing code we had as well as andycandy's example on the Github Issue. You can find the Gist here (hopefully it will help you figure out what is wrong with your code!):</p>
<p><a href="https://gist.github.com/lovalabs/83d08796ab9ff65cbf188fbca8d03343" rel="nofollow noreferrer">https://gist.github.com/lovalabs/83d08796ab9ff65cbf188fbca8d03343</a></p>
<p>Note that we're in the process of releasing a minting SDK for <code>cardano-serialization-lib</code> that will be wrap around these low-level details and make minting assets much easier. Since the version of csl this is based off is still in beta, we're waiting for it to be stable before releasing.</p>
<p>EDIT:
After some further testing I've discovered that there may be a bug in the <code>TransactionBuilder</code> implementation of <code>build()</code> vs <code>build_tx()</code> which could be the reason for your error. When I do this:</p>
<pre><code>const tx = txBuilder.build_tx()
</code></pre>
<p>I don't get a <code>ScriptWitnessNotValidatingUTXOW</code> error. But if I do this:</p>
<pre><code>const txBody = txBuilder.build()
const tx = S.Transaction.new(
        txBody,
        txWitnessSet,
        auxiliaryData
)
</code></pre>
<p>Then I do get a <code>ScriptWitnessNotValidatingUTXOW</code> error. Will investigate further before raising an issue on Github though perhaps you could confirm this.</p>
 4627: <p>There might be an issue with the policy id generated in your first code block.</p>
<p>The hash function on NativeScript should take a namespace parameter <em>(see here <a href="https://github.com/Emurgo/cardano-serialization-lib/blob/3e5dc377829d1ba46348713196b35476d8604bd9/rust/pkg/cardano_serialization_lib.js.flow#L2699" rel="nofollow noreferrer">https://github.com/Emurgo/cardano-serialization-lib/blob/3e5dc377829d1ba46348713196b35476d8604bd9/rust/pkg/cardano_serialization_lib.js.flow#L2699</a>)</em>,</p>
<p>that is prepended when calculating the hash <em>(see here <a href="https://github.com/Emurgo/cardano-serialization-lib/blob/da6eba20a5ffab2eeae1b1823c26ee67b1426f42/rust/src/lib.rs#L1629" rel="nofollow noreferrer">https://github.com/Emurgo/cardano-serialization-lib/blob/da6eba20a5ffab2eeae1b1823c26ee67b1426f42/rust/src/lib.rs#L1629</a>)</em></p>
<p>Try adding the zero like this:</p>
<pre><code>const nativeScript = CardanoWasm.NativeScript.new_script_all(CardanoWasm.ScriptAll.new(nativeScripts))

const policyId = Buffer.from(nativeScript.hash(0).to_bytes()).toString('hex');
</code></pre>
<p>instead of this part:</p>
<pre><code>const finalScript = Loader.Cardano.NativeScript.new_script_all(
  Loader.Cardano.ScriptAll.new(nativeScripts)
);
const policyId = Buffer.from(
  Loader.Cardano.ScriptHash.from_bytes(
    finalScript.hash().to_bytes()
  ).to_bytes(),
  &quot;hex&quot;
).toString(&quot;hex&quot;);
</code></pre>
 2068: <p>After days of searching I finally found my mistake: I was tryng to do the signing from backend, so I needed to generate the prvKey for this I followed the instructions provided in Cardano Serialization Lib, my code was</p>
<pre><code>const Bip32PrivateKey =
  &quot;xprv1wrgj3q60pxt5uq094h6f9nj00jx4hrwddgqrnv632a86llc94egpkn77l9mg9ewham96309yj6yv4zdsdr05flv6xsntjgcgj3pfp59ef589a87gen99t4ru5jhnfm4ar0tvlry4guz6l95tnjcjtnf4jv3rf52k&quot;;
const prvKeyBip32 = CardanoWasm.Bip32PrivateKey.from_bech32(Bip32PrivateKey);


function harden(num) {
  return 0x80000000 + num;
}

const rootKey = CardanoWasm.Bip32PrivateKey.from_bech32(Bip32PrivateKey);
const accountKey = rootKey
  .derive(harden(1852)) // purpose
  .derive(harden(1815)) // coin type
  .derive(harden(0)); // account #0

const utxoPubKey = accountKey
  .derive(0) // external
  .derive(0)
  .to_public();

const stakeKey = accountKey
  .derive(2) // chimeric
  .derive(0)
  .to_public();

const baseAddr = CardanoWasm.BaseAddress.new(
  CardanoWasm.NetworkInfo.testnet().network_id(),
  CardanoWasm.StakeCredential.from_keyhash(utxoPubKey.to_raw_key().hash()),
  CardanoWasm.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash())
);


const prvKey = rootKey.to_raw_key();
</code></pre>
<p>And I was trying to sign with <code>prvKey</code>, for a UTXO in <code>baseAddr</code>, that did not work, but if O do</p>
<pre><code>const prvKey = accountKey
  .derive(0) 
  .derive(0)
  .to_raw_key();
</code></pre>
<p>Then I can sign and submit the tx for UTXOs in the <code>baseAddr</code>.</p>
