gpt4 book ai didi

android - 为什么会出现这个错误?类型错误 : Object is not a valid argument for 'instanceof' (evaluating 'from instanceof Address - REACT-NATIVE

转载 作者:行者123 更新时间:2023-11-29 18:41:03 25 4
gpt4 key购买 nike

获取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 工作。

最好的方法是在 getUtxos() 函数中返回一个 promise,然后您可以使用它,最好在 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)
})

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

关于android - 为什么会出现这个错误?类型错误 : Object is not a valid argument for 'instanceof' (evaluating 'from instanceof Address - REACT-NATIVE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com