Search code examples
androidreact-nativebitcore

Why does getting this error? TypeError: Object is not a valid argument for 'instanceof' (evaluating 'from instanceof Address - REACT-NATIVE


Method of get UTXO

getUtoxs(address){    var options;
 if(Global.btc.network === "testnet"){
     options = {
         url: testnet.apiBaseUrl + "/api/addr/" + address + "/utxo"
 };   }else{
    options = {
        url: livenet.apiBaseUrl + "/addr/" + address + "/utxo"
  };
} 
   return  fetch(options.url)
    .then((res) => res.json())
    .then((data) =>data)
    .catch((error) =>{
        console.error(error);
    });  
}

Method of send btc

sendingBTC(utxos, tx) {
    try {
        var options;         

        var transaction = new bitcore.Transaction()
            .from(utxos)  //this line gets error
            .to(tx.to,tx.value*100000000)
            .change(tx.from)
            .sign(tx.password)
            .serialize();   
       /*.......................*/
    } catch (e) {
        console.error(e); 
    }
}

this methods are getting an error. What is the wrong in this methods ?


Solution

  • Try using bitcore-insight to make the getUtxos work.

    The preferable way to do this would be to return a promise in the getUtxos() function which you can then consume, preferably using async-await in the sendingBtc() function.

    Here's an excerpt of code to help you out.

    var bitcore = require('node_modules/bitcore-explorers/node_modules/bitcore-lib');
    var Insight = require("node_modules/bitcore-explorers").Insight;
    var insight = new Insight("testnet");
    
    function getUtxos(address){
        return new Promise((resolve, reject)=>{
            insight.getUnspentUtxos(address, (err, utxos)=>{
                if(err) reject(err)
                else{
                    resolve(utxos);
                }
            })
        })
    }
    
    async function sendingBtc() {
        console.log(req.body)
        let utxos = await getUtxos(address);
        // Another function to derive your private key 
        let privateKey = await generatePrivKey 
        bitcore.Transaction()
            .from(utxos)
            .to(req.body.txSendAddress,amount*100000000 - 3000)
            .change(changeAddress)
            .sign(privateKey);
        insight.broadcast(tx, (err, returnedTxId)=>{
            if(!err) console.log(returnedTxId)
        })

    Hope this piece of code helps you out, remember you also need to derive your private key to sign the transaction and set up a change address (optional but recommended)!