cardano-serialization-lib icon indicating copy to clipboard operation
cardano-serialization-lib copied to clipboard

.to_bytes() returns empty Uint8Array(0) []

Open akshitv1 opened this issue 3 years ago • 9 comments

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

akshitv1 avatar Feb 16 '22 08:02 akshitv1

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.

rooooooooob avatar Feb 17 '22 17:02 rooooooooob

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.

EvanXzj avatar Feb 22 '22 01:02 EvanXzj

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?

vsubhuman avatar Feb 22 '22 08:02 vsubhuman

Yes, this is also returning empty array. o/p: Uint8Array []

akshitv1 avatar Feb 22 '22 10:02 akshitv1

  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

EvanXzj avatar Feb 22 '22 13:02 EvanXzj

it is because Uint8Array.prototype.slice has been changed by some other package that I have installed

EvanXzj avatar Apr 24 '22 02:04 EvanXzj

Hi @EvanXzj ! Could you tell me what package changes Uint8Array.prototype.slice ?

lisicky avatar Jan 05 '23 12:01 lisicky

@lisicky https://github.com/QuantumMechanics/NEM-sdk/issues/77

EvanXzj avatar Jan 05 '23 12:01 EvanXzj

@EvanXzj thanks !

lisicky avatar Jan 05 '23 13:01 lisicky