avalanchejs icon indicating copy to clipboard operation
avalanchejs copied to clipboard

UnsignedTx fromBuffer function doesn't contract the unsignedTx correct

Open harryEth opened this issue 3 years ago • 1 comments

I need to transfer the unsignedTX to another place to sign, so using the fromBuffer funciton to contract the new unsignedTx

    let myNetworkID = 5; //default is 3, we want to override that for our local network
    let myBlockchainID = "X"; // The XChain blockchainID on this network
    let ava = new Avalanche(
        "api.avax-test.network",
        443,
        "https",
        myNetworkID,
        myBlockchainID
    );
    let xchain = ava.XChain();

    let assetId = "U8iRqJoiJm8xZHAacmvYyZVwqQx6uDNtQeP3CQ6fcgQk3JqnK";

    const myKeychain = xchain.keyChain();

    myKeychain.importKey('PrivateKey-xxxxxxxx');

    let myAddresses = xchain.keyChain().getAddresses(); 
    let addressStrings = xchain.keyChain().getAddressStrings();

    let utxos = (await xchain.getUTXOs(addressStrings)).utxos;

    let unsignedTx = await xchain.buildBaseTx(utxos, new BN(2), assetId, addressStrings, addressStrings, addressStrings);

    const api = avm;

    const buffer = unsignedTx.toBuffer();

    const Utx = new api.UnsignedTx();

   Utx.fromBuffer(buffer);

  const signedTx = Utx.sign(myKeychain); // this one throw error

   const signedTX1 = unsignedTx.sign(myKeychain); // this one works

the error is image

I dug into the code, source in sigidxs are all 0, that cause the keypair is undefined.

{
    key: "sign",
    value: function sign(msg, kc) {
      var sigs = [];

      for (var i = 0; i < this.ins.length; i++) {
        var cred = credentials_1.SelectCredentialClass(this.ins[i].getInput().getCredentialID());
        var sigidxs = this.ins[i].getInput().getSigIdxs();

        for (var j = 0; j < sigidxs.length; j++) {
          var keypair = kc.getKey(sigidxs[j].getSource()); // keypair is undefined 
          var signval = keypair.sign(msg);
          var sig = new credentials_2.Signature();
          sig.fromBuffer(signval);
          cred.addSignature(sig);
        }

        sigs.push(cred);
      }

      return sigs;
    }

image

anyone can help on this?

harryEth avatar Sep 07 '21 07:09 harryEth

Hi

Were you able to solve this issue? I think i may have a similar problem:

https://github.com/ava-labs/avalanchejs/issues/619

yashutanna avatar Jul 06 '22 10:07 yashutanna