-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathop_return_tx.js
35 lines (32 loc) · 1.38 KB
/
op_return_tx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var bitcore = require('bitcore-lib');
var Insight = require("bitcore-explorers").Insight;
var insight = new Insight("testnet");
var utxoSet;
var HDKey = require('./models/HDKey');
function sendTx(data, index) {
return new Promise((resolve, reject) => {
HDKey.find({}, (err, key) => {
var hdprivateKey = new bitcore.HDPrivateKey(key[0].hdprivate);
var signing_key = hdprivateKey.derive(parseInt(index)).privateKey;
var derived_address = signing_key.toAddress(bitcore.Networks.testnet);
console.log("Derived address = " + derived_address);
//TODO: Assert addresses here
insight.getUnspentUtxos(derived_address, function (error, utxos) {
if (error) {
console.log(error);
} else if(utxos.length) {
console.log("Got utxos ", utxos);
utxoSet = utxos;
tx = new bitcore.Transaction().from(utxoSet[0]).addData(data).sign(signing_key);
console.log(tx.toObject());
insight.broadcast(tx, function (err, txId) {
console.log(err);
console.log("Broadcasted timestamping data! ", txId);
resolve(txId);
});
}
});
});
});
}
module.exports.sendTx = sendTx;