Tengo un NodeJS
script codificado bitcoin
para enviar todo el saldo por dirección a otra. Ahora quiero hacer esto para el litecoin
. Porque bitcoin
he usado bitcore-lib y bitcore-explorers libs para hacer eso.
Porque litecoin
encontré bifurcación y adopté lib litecore-lib , pero no puedo encontrar una solución para recibir UTXO
datos para alguna dirección LTC como lo hago en el caso de bitcoin
:
var Insight = require("bitcore-explorers").Insight;
var insight = new Insight(network);
insight.getUnspentUtxos(sourceAddress, function (error, utxos) {
// determine balance, fee and sign transaction
}
¿Cómo puedo hacer esto para litecoin
? ¡Gracias!
Usé la request
biblioteca para recuperar manualmente la información de la API de información. También usé la litecore-lib
biblioteca para crear la transacción.
var Litecoin = require("litecore-lib");
var request = require("request");
//manually hit an insight api to retrieve utxos of address
function getUTXOs(address) {
return new Promise((resolve, reject) => {
request({
uri: 'https://insight.litecore.io/api/addr/' + address + '/utxo',
json: true
},
(error, response, body) => {
if(error) reject(error);
resolve(body)
}
)
})
}
//manually hit an insight api to broadcast your tx
function broadcastTX(rawtx) {
return new Promise((resolve, reject) => {
request({
uri: 'https://insight.litecore.io/tx/send',
method: 'POST',
json: {
rawtx
}
},
(error, response, body) => {
if(error) reject(error);
resolve(body.txid)
}
)
})
}
//your private key and address here
var privateKey = PrivateKey.fromWIF('YOUR_PRIVATE_KEY_HERE');
var address = privateKey.toPublicKey().toAddress();
getUTXOs(address)
.then((utxos) => {
let balance = 0;
for (var i = 0; i < utxos.length; i++) {
balance +=utxos[i]['satoshis'];
} //add up the balance in satoshi format from all utxos
var fee = 1500; //fee for the tx
var tx = new Litecoin.Transaction() //use litecore-lib to create a transaction
.from(utxos)
.to('TO_ADDRESS', balance - fee) //note: you are sending all your balance AKA sweeping
.fee(fee)
.sign(privateKey)
.serialize();
return broadcastTX(tx) //broadcast the serialized tx
})
.then((result) => {
console.log(result) // txid
})
.catch((error) => {
throw error;
})
¡Salud!
Puede usar https://github.com/litecoin-project/litecore-explorers así:
var explorers = require('bitcore-explorers');
var insight = new explorers.Insight(https://insight.litecore.io', 'mainnet');
insight.getUtxos('mgBmKKZEkK...', function(err, utxos) {
if (err) {
// Handle errors...
} else {
// Do something with utxos
}
});
Dmitro Zarezenko
bitcore-explorers
un paquete para litecoin, ¡así que no funciona! ¡No intentes usarlo!