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

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

获取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);
    });  
}

发送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); 
    }
}

此方法出错。这种方法有什么问题?

尝试使用 bitcore-insight 使 getUtxos 工作。

最好的方法是 return getUtxos() 函数中的承诺,然后您可以使用它,最好在 sendingBtc() 函数中使用 async-await。

这里有一段代码可以帮助您解决问题。

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)
    })

希望这段代码能帮到你,记住你还需要派生你的私钥来签署交易并设置找零地址(可选但推荐)!