Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got [object Object] error in react native when sending BCH #184

Open
cshung1994 opened this issue Jun 2, 2020 · 0 comments
Open

Got [object Object] error in react native when sending BCH #184

cshung1994 opened this issue Jun 2, 2020 · 0 comments

Comments

@cshung1994
Copy link

cshung1994 commented Jun 2, 2020

Here is how I sign and brocast bch tx:

// receiver, sender: cash type address
// mnemonic: 12 words mnemonic code
// amount: (unit BCH)
const transferBch = async (receiver, amount, sender, mnemonic) => {
  const transactionBuilder = new bitbox.TransactionBuilder('mainnet');
  try {
    // Utxo
    const utxo = await bitbox.Address.utxo(sender);
    let balance = 0;
    utxo.utxos.every((item, index) => {
      balance += item.satoshis;
      transactionBuilder.addInput(item.txid, item.vout);
    });
    const amountInSatoshi = bitbox.BitcoinCash.toSatoshi(amount);
    const byteCount = bitbox.BitcoinCash.getByteCount({P2PKH: 1}, {P2PKH: 2});
    const sendAmount = amountInSatoshi - byteCount;
    transactionBuilder.addOutput(receiver, sendAmount);
    // Change
    transactionBuilder.addOutput(sender, balance - sendAmount - byteCount);

    transactionBuilder.setLockTime(50000);
    // Sign
    const seedBuffer = bitbox.Mnemonic.toSeed(mnemonic);
    // create HDNode from seed buffer
    const hdNode = bitbox.HDNode.fromSeed(seedBuffer);
    // keypair
    let keyPair = bitbox.HDNode.toKeyPair(hdNode);
    // empty redeemScript variable
    let redeemScript;
    // sign w/ keyPair
    transactionBuilder.sign(
      0,
      keyPair,
      redeemScript,
      transactionBuilder.hashTypes.SIGHASH_ALL,
      amountInSatoshi,
      // transactionBuilder.signatureAlgorithms.SCHNORR,
    );
    const tx = transactionBuilder.build();
    const hex = tx.toHex();
    // sendRawTransaction to running BCH node
    try {
      const sendRawTransaction = await bitbox.RawTransactions.sendRawTransaction(
        hex,
      );
      console.log(rawTransaction);
    } catch (error) {
      throw new Error(error);
    }
  } catch (error) {
    console.log(error, 'errr');
    throw new Error(error);
  }
};

But I still can't send my bch by following the developer guide and doc

@cshung1994 cshung1994 changed the title Got [object Object] in my react native project Got [object Object] in react native when sending BCH Jun 2, 2020
@cshung1994 cshung1994 changed the title Got [object Object] in react native when sending BCH Got [object Object] error in react native when sending BCH Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant