Skip to content

Instantly share code, notes, and snippets.

@ottosch
Last active November 15, 2021 03:13
Show Gist options
  • Save ottosch/c64af1e6a94e16c76e059a18d7210b03 to your computer and use it in GitHub Desktop.
Save ottosch/c64af1e6a94e16c76e059a18d7210b03 to your computer and use it in GitHub Desktop.
Prints all taproot transactions and their messages at block 709635
#! /usr/bin/env node
// Create a directory with the script inside, then run npm init && npm i node-bitcoin-rpc
// Run it with ./first-taproot-txs.js or node first-taproot-txs.js
// Also change the username and password below
const rpc = require("node-bitcoin-rpc")
rpc.init("127.0.0.1", 8332, "rpcuser", "rpcpass");
rpc.setTimeout(Number(process.env.TIMEOUT) || 20000);
async function getTxHashes() {
return new Promise(resolve => {
rpc.call("getblock", ["00000000000000000001f9ee4f69cbc75ce61db5178175c2ad021fe1df5bad8f"], (e, d) => {
if (d && d.result) {
resolve(d.result["tx"]);
} else {
console.error(e);
resolve([]);
}
});
});
}
async function getTransaction(txHash) {
return new Promise(resolve => {
rpc.call("getrawtransaction", [txHash, 1], (e, d) => {
if (d && d.result) {
resolve(d.result);
} else {
console.error(e);
resolve({});
}
});
});
}
async function findTaprootTransactions() {
var txIds = await getTxHashes();
console.log(`Checking ${txIds.length} transactions...\n`);
var taprootTxs = [];
for (id of txIds) {
var tx = await getTransaction(id);
for (var input of tx.vin) {
if (input.coinbase) {
continue;
}
var prevTx = await getTransaction(input.txid);
var spentOutput = prevTx.vout[input.vout];
if (spentOutput.scriptPubKey.type === "witness_v1_taproot") {
var message = "";
for (var output of tx.vout) {
var script = output.scriptPubKey;
if (script.type === "nulldata") {
message = script.hex.substring(4); // removes op_return and push
break;
}
}
taprootTxs.push({
txid: id,
message: message ? Buffer.from(message, "hex").toString() : "no message"
});
break;
}
}
}
console.log(`Total taproot: ${taprootTxs.length}`);
for (var obj of taprootTxs) {
console.log(obj.txid);
console.log(obj.message);
console.log();
}
}
findTaprootTransactions();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment