Last active
July 30, 2019 23:29
-
-
Save dagurval/dea9947e6eaead98e7d09cf72fc177e5 to your computer and use it in GitHub Desktop.
Electrum server cashaccount lookup
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
const ElectrumCli = require('electrum-client') | |
const CashAccounts = require('cashaccounts') | |
const BITBOX = require("bitbox-sdk").BITBOX | |
const assert = require("assert") | |
const crypto = require('crypto') | |
const bitbox = new BITBOX() | |
// Electrum API implementation is blockchain agnostic, so remember to add | |
// the modifier to the request. | |
const BCH_BLOCK_MODIFIER = 563620 | |
const main = async () => { | |
// Connect to our electrum server. If you're running it locally, you can | |
// pass 50001, '127.0.0.1', 'tcp' and avoid dealing with SSL. | |
const ecl = new ElectrumCli(50002, 'bitcoincash.network', 'tls') // tcp or tls | |
await ecl.connect() | |
// Lookup dagur#216, returns all transactions that register this identifier. | |
// In this case, there is no conflict, so only 1 transaction is returned. | |
const transactions = await ecl.request("cashaccount.query.name", ["dagur", 216 + BCH_BLOCK_MODIFIER]); | |
console.log("Results:", transactions); | |
// Lets use the BITBOX SDK to decode the first (and only in this case) result. | |
const tx_hex = transactions.pop()["tx"] | |
const tx = await bitbox.RawTransactions.decodeRawTransaction(tx_hex) | |
// One of the outputs hash the OP_RETURN output containing the registration. | |
const opreturn = tx.vout.find(o => o.scriptPubKey.type === 'nulldata').scriptPubKey.asm | |
const cashaccounts = new CashAccounts() | |
const payload = await cashaccounts.parsePaymentInfo(opreturn) | |
console.log("Payload:", payload); | |
// This address contains one regular payment address | |
assert(payload[0].type === 'Key Hash') | |
assert(payload[0].address === 'bitcoincash:qzxqjtkze0vy96y5xtru2w65mvaftryr55yzmhy8sl'); | |
// We already have an electrum connection open, so lets see what else | |
// we can learn about this address. | |
// Electrum uses scriphash for lookups, so we need to convert. | |
const scriptHash = toScriptHash(payload[0].address) | |
console.log("Transaction history: ", | |
await ecl.request('blockchain.scripthash.get_history', [scriptHash])) | |
console.log("Address balance: ", | |
await ecl.request('blockchain.scripthash.get_balance', [scriptHash])) | |
// disconnect from electrum server | |
await ecl.close() | |
} | |
// Convert cashaddr to scripthash | |
// See https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes | |
function toScriptHash(cashaddr) { | |
const hash160 = bitbox.Address.cashToHash160(cashaddr); | |
let scriptHash = bitbox.Script.fromASM( | |
"OP_DUP OP_HASH160 " + hash160 + " OP_EQUALVERIFY OP_CHECKSIG") | |
scriptHash = crypto.createHash('sha256').update(scriptHash).digest() | |
scriptHash.reverse() | |
return scriptHash.toString('hex') | |
} | |
main().catch((e) => { console.log("Error:", e) }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment