Last active
September 21, 2018 13:24
-
-
Save makevoid/1eb139e935309117913c5d8490c4da33 to your computer and use it in GitHub Desktop.
BTC OP_RETURN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// // package.json | |
// { | |
// "dependencies": { | |
// "bitcoincashjs": "^0.1.10", | |
// "bitcore-explorers": "^1.0.1", | |
// } | |
// } | |
const fs = require('fs') | |
const bitcoin = require('bitcoincashjs') // npm i bitcoincashjs --save | |
// ( see https://bitcore.io/api/lib for docs ) | |
const Transaction = bitcoin.Transaction | |
// Private key part | |
// --- | |
// // generate and save private key | |
// const pvt = new bitcoin.PrivateKey() | |
// console.log(pvt.toString('hex')) | |
// load private key from a file | |
const privateKeyString = fs.readFileSync('./private-key.txt').toString().trim() | |
// load private key from local string | |
// const privateKeyString = "your-private-key-as-string" | |
const privateKey = new bitcoin.PrivateKey(privateKeyString) | |
const address = privateKey.toAddress().toString() | |
console.log("loaded private key") | |
console.log("address:", address) | |
// UTXOs part | |
// --- | |
var Insight = require('bitcore-explorers').Insight; | |
var insight = new Insight("https://blockdozer.com"); | |
const getUTXOs = (address) => { | |
return new Promise((resolve, reject) => { | |
insight.getUnspentUtxos(address, (err, data) => { | |
if (err) { reject(err); return } | |
resolve(data) | |
}) | |
}) | |
} | |
const pushTx = (rawTx) => { | |
return new Promise((resolve, reject) => { | |
insight.broadcast(rawTx, (err, txId) => { | |
if (err) { reject(err); return } | |
resolve(txId) | |
}) | |
}) | |
} | |
;(async () => { | |
// get utxos | |
let utxos = await getUTXOs(address) | |
console.log("UTXOs selection:") | |
console.log(utxos) | |
console.log("------------") | |
utxos = [utxos[0]] | |
console.log(utxos) | |
// // create transaction | |
let tx = new Transaction() | |
.from(utxos) | |
.to(address, 10000) // minimum amount | |
.change(address) // send and refund on the same address | |
.fee(10000) | |
// add OP_RETURN script | |
tx = tx.addData(' { "appliedblockchain": "rulez", "lorem": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut...." }') | |
// signing with local private key | |
tx = tx.sign(privateKey) | |
const rawTx = tx.serialize() | |
const txId = await pushTx(rawTx) | |
console.log("TX:", txId) | |
console.log(`https://blockchair.com/bitcoin-cash/transaction/${txId}`) | |
})() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment