I am trying to create a transaction using the Cardano Serialization Library but I cannot figure out what is going wrong. The code is
const BlockFrost = require('@blockfrost/blockfrost-js');
const {
PrivateKey, Vkey, Ed25519Signature, PublicKey, Vkeywitnesses , TransactionHash
TransactionBuilderConfigBuilder,
LinearFee,
BigNum,
TransactionBuilder,
Transaction,
TransactionWitnessSet,
hash_transaction,
make_vkey_witness,
Address,
Value,
TransactionOutput,
TransactionInput,
} = require('@emurgo/cardano-serialization-lib-nodejs');
const BLOCKFROST_API_KEY = 'xxxxx';
const PRIVATE_KEY =
PrivateKey.from_bech32('ed25519e_sk14p2a82zpk7y27pme7sy8yxaw0ad93j47k7dvgnc55e50lfm66a88rmz0zh0ezfqd7x30mclsjaqtkzmazy2l6crfxxd0j9ynmzxn6ac6vryrz');
const RECEIVER_ADDRESS = Address.from_bech32(
'addr_test1qpzqmyejxwvwal9vx6w0wgezvpmjlp3cqwfg2vkjt0y9fns0d5cyvvu88y78wtfl8xyey3gr3ygpxpphevvmz7ulaf2qrhxz34',
);
const SEND_AMOUNT = '5000000';
const API = new BlockFrost.BlockFrostAPI({
projectId: BLOCKFROST_API_KEY,
isTestnet: true,
});
const _transactionConfig = async () => {
const epoch = (await API.epochsLatest()).epoch;
return await API.epochsParameters(epoch);
};
const _configureTransactionBuilder = async () => {
// const protocolParams = await _transactionConfig();
try {
return (
TransactionBuilderConfigBuilder.new()
.fee_algo(
LinearFee.new(BigNum.from_str('44'), BigNum.from_str('155381')),
)
.pool_deposit(BigNum.from_str('500000000'))
.key_deposit(BigNum.from_str('2000000'))
.max_tx_size(16384)
.max_value_size(5000)
.coins_per_utxo_word(BigNum.from_str('34482'))
.prefer_pure_change(true)
.build()
);
} catch (error) {
console.log(`Error in tx config: ${error}`);
}
};
const buildTransaction = async () => {
try {
const txBuilderConfiguration = await _configureTransactionBuilder();
const txBuilder = TransactionBuilder.new(txBuilderConfiguration);
//add key hash input
txBuilder.add_key_input(
PRIVATE_KEY.to_public().hash(),
TransactionInput.new(
TransactionHash.from_bytes(
Buffer.from('d56839c08b51e159a3931b36c65b1305aa82e8625a0b2d6969f200b3ddf806cf', 'hex'), //tx hash on ada received from faucet
0, //index
),
),
Value.new(BigNum.from_str(SEND_AMOUNT))
)
//tx outputs
const txOutputs = TransactionOutput.new(
RECEIVER_ADDRESS,
Value.new(BigNum.from_str(SEND_AMOUNT)),
);
txBuilder.add_output(txOutputs);
//latest block
const latestBlock = await API.blocksLatest();
const currentSlot = latestBlock.slot;
txBuilder.set_ttl(currentSlot + 7200); //+2h from current slot
//metadata
// txBuilder.add_json_metadatum(BigNum.from_str('0'), 'today is a great day');
txBuilder.set_fee(BigNum.from_str("190000"));
// txBuilder.add_change_if_needed(SENDER_ADDRESS);
//build tx
const txBody = txBuilder.build();
//make witness
const txHash = hash_transaction(txBody);
const witnesses= TransactionWitnessSet.new();
const vKeyWitness = Vkeywitnesses.new();
vKeyWitness.add(make_vkey_witness(txHash, PRIVATE_KEY));
witnesses.set_vkeys(vKeyWitness);
const signedTx = Transaction.new(txBody, witnesses);
const response = API.txSubmit(signedTx.to_bytes());
return response;
} catch (error) {
console.log(`Tx compose error: ${error}\n`);
console.error(error.stack);
}
};
module.exports = {
buildTransaction,
};
I am getting the error below. From my research it apparently means I am not using the correct keys for the witness. But I can't find a way to fix it. Any ideas on what could be wrong?
BlockfrostServerError: "transaction submit error ShelleyTxValidationError ShelleyBasedEraAlonzo (ApplyTxError [UtxowFailure (WrappedShelleyEraFailure (MissingVKeyWitnessesUTXOW (WitHashes (fromList [KeyHash \"0c7a0e8dc785e893f8fca246646ba10df9d5bdd5ebed92e72b163e75\"])))),UtxowFailure (WrappedShelleyEraFailure (UtxoFailure (ValueNotConservedUTxO (Value 1000000000 (fromList [])) (Value 5190000 (fromList [])))))])"