cardano-serialization-lib
cardano-serialization-lib copied to clipboard
.to_bytes() returns empty Uint8Array(0) []
I am currently facing this issue : were transaction.to_bytes() defined over here (blockfrost-js/index.ts at master · blockfrost/blockfrost-js · GitHub) return an empty array : Uint8Array(0)
It sometimes return correct Array when i run from one file, then if i call this from some other file (lets say i call it from a route inside a node server) it returns this empty Array.
The variable transaction is of class Transaction only (when i log it).
Could this be somehow related to some wasm memory buffer issue ?
I am open to any feedback or suggestion.
Thanks in advance
Could this be somehow related to some wasm memory buffer issue ?
It sounds like it, or at least something to do with wasm. It's definitely not the rust code because the rust code at bare minimum returns the CBOR array wrapper tags, even if you haven't set any fields on the TX.
Had the same problem in Electron main process, txBuilder.build().to_bytes(); returns an empty string.
class TClass {
static buildTransaction(param) {
const {
vins,
vouts,
ttl, // expire_block_height
minFeeA,
minFeeB,
minUtxoValue,
poolDeposit,
keyDeposit,
changeAddress,
} = param;
const minFeeABigNum = CardanoWasm.BigNum.from_str(minFeeA);
const minFeeBBigNum = CardanoWasm.BigNum.from_str(minFeeB);
const linearFee = CardanoWasm.LinearFee.new(minFeeABigNum, minFeeBBigNum);
const minUtxoValueBigNum = CardanoWasm.BigNum.from_str(minUtxoValue);
const poolDepositBigNum = CardanoWasm.BigNum.from_str(poolDeposit);
const keyDepositBigNum = CardanoWasm.BigNum.from_str(keyDeposit);
const txBuilder = CardanoWasm.TransactionBuilder.new(linearFee, minUtxoValueBigNum, poolDepositBigNum, keyDepositBigNum);
minFeeABigNum.free();
minFeeBBigNum.free();
linearFee.free();
minUtxoValueBigNum.free();
poolDepositBigNum.free();
keyDepositBigNum.free();
vins.forEach(({ hash, vout_index: voutIndex, address, amount, tokens }) => {
const transactionHash = CardanoWasm.TransactionHash.from_bytes(Buffer.from(hash, 'hex'));
const transactionInput = CardanoWasm.TransactionInput.new(transactionHash, voutIndex);
const amountBigNum = CardanoWasm.BigNum.from_str(amount);
const value = CardanoWasm.Value.new(amountBigNum);
if (tokens && tokens.length > 0) {
const multiAsset = CardanoWasm.MultiAsset.new();
for (const token of tokens) {
const name = CardanoWasm.AssetName.new(Buffer.from(token.name, 'hex'));
const asset = CardanoWasm.Assets.new();
asset.insert(name, CardanoWasm.BigNum.from_str(token.amount));
const sHash = CardanoWasm.ScriptHash.from_bytes(Buffer.from(token.ScriptHash, 'hex'));
multiAsset.insert(sHash, asset);
}
value.set_multiasset(multiAsset);
}
let addr;
if (CardanoWasm.ByronAddress.is_valid(address)) {
addr = CardanoWasm.ByronAddress.from_base58(address);
txBuilder.add_bootstrap_input(addr, transactionInput, value);
} else {
addr = CardanoWasm.Address.from_bech32(address);
txBuilder.add_input(addr, transactionInput, value);
}
addr.free();
transactionHash.free();
transactionInput.free();
amountBigNum.free();
value.free();
});
vouts.forEach(({ address, amount, tokens }) => {
let addr;
if (CardanoWasm.ByronAddress.is_valid(address)) {
addr = CardanoWasm.ByronAddress.from_base58(address).to_address();
} else {
addr = CardanoWasm.Address.from_bech32(address);
}
const amountBigNum = CardanoWasm.BigNum.from_str(amount);
const value = CardanoWasm.Value.new(amountBigNum);
if (tokens && tokens.length > 0) {
const multiAsset = CardanoWasm.MultiAsset.new();
for (const token of tokens) {
const name = CardanoWasm.AssetName.new(Buffer.from(token.name, 'hex'));
const asset = CardanoWasm.Assets.new();
asset.insert(name, CardanoWasm.BigNum.from_str(token.amount));
const sHash = CardanoWasm.ScriptHash.from_bytes(Buffer.from(token.ScriptHash, 'hex'));
multiAsset.insert(sHash, asset);
}
value.set_multiasset(multiAsset);
}
const transactionOutput = CardanoWasm.TransactionOutput.new(addr, value);
txBuilder.add_output(transactionOutput);
addr.free();
amountBigNum.free();
value.free();
transactionOutput.free();
});
txBuilder.set_ttl(ttl);
let address;
if (CardanoWasm.ByronAddress.is_valid(changeAddress)) {
address = CardanoWasm.ByronAddress.from_base58(changeAddress).to_address();
} else {
address = CardanoWasm.Address.from_bech32(changeAddress);
}
txBuilder.add_change_if_needed(address);
const txBytes = txBuilder.build().to_bytes();
address.free();
txBuilder.free();
return Buffer.from(txBytes).toString('hex');
}
}
// tx param
const param = {
vins: [
{
hash: '1c25479e035b1d4448c5ef7aca2c37cb7f19dbcad8ad9fe9738a06830349d93a',
vout_index: 0,
address: 'addr1q872d3z2745864x5gmtld63vk7q6sn5362euldvk4lp2z07l56n0l7lclvs78095g32467kdu2p0wva0v45k7nlhuskq542wne',
amount: '7400000',
},
],
vouts: [
{
address: 'DdzFFzCqrhstNyU7sYq9vBzgEonshkri3zLFA7EJFULz6kivSgS9yb3ahxnvpkFW5Wsj8ETVKkJFvU3n9Lh1fVkocWbnkXDHqtVgwSqv',
amount: '3200000',
},
{
address: 'Ae2tdPwUPEZFNp8XqKWsuaVdgEJLqcxiMJAkXxKQHo8rLmXbSBh9T7g1t87',
amount: '3000000',
},
{
address: 'addr1q872d3z2745864x5gmtld63vk7q6sn5362euldvk4lp2z07l56n0l7lclvs78095g32467kdu2p0wva0v45k7nlhuskq542wne',
amount: '1000000',
},
],
fee: '2000000',
ttl: 17582561,
minFeeA: '44',
minFeeB: '155381',
minUtxoValue: '1000000',
poolDeposit: '500000000',
keyDeposit: '2000000',
changeAddress: 'addr1q872d3z2745864x5gmtld63vk7q6sn5362euldvk4lp2z07l56n0l7lclvs78095g32467kdu2p0wva0v45k7nlhuskq542wne',
}
const unsignedTxHex = TClass.buildTransaction(param);
console.log(unsignedTxHex)
Running in a separate file is no problem.
Does this happen exclusively with the transaction-builder results? Can you try replicating a minimal possible example, something like
CardanoWasm.BigNum.from_str('1').to_bytes();
Will it also be an empty string?
Yes, this is also returning empty array. o/p: Uint8Array []
function simpleTest() {
const str = '1';
const strBn = CardanoWasm.BigNum.from_str(str);
const toBytesValue = strBn.to_bytes();
const toStrValue = strBn.to_str();
console.log(str);
console.log('toBytesValue: ', toBytesValue);
console.log('toStrValue: ', toStrValue);
}
simpleTest()
// Output:
// toBytesValue: Uint8Array []
// toStrValue: 1
@akshitv1
it is because Uint8Array.prototype.slice has been changed by some other package that I have installed
Hi @EvanXzj ! Could you tell me what package changes Uint8Array.prototype.slice ?
@lisicky https://github.com/QuantumMechanics/NEM-sdk/issues/77
@EvanXzj thanks !