|
| 1 | +# rewrite using a dsl dialect |
| 2 | + |
| 3 | +# TODO: implement DSL, run via opal |
| 4 | + |
| 5 | +def require(module) |
| 6 | + # call node's require |
| 7 | +end |
| 8 | + |
| 9 | +def File.read |
| 10 | + "...." |
| 11 | +end |
| 12 | + |
| 13 | +def bitcore |
| 14 | + obj = Object.new |
| 15 | + def obj.private_key |
| 16 | + "K..." |
| 17 | + end |
| 18 | + def obj.transaction |
| 19 | + "x...." |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +def bitcore.privatekey |
| 24 | + |
| 25 | +end |
| 26 | + |
| 27 | +# ---- |
| 28 | + |
| 29 | +# final code: |
| 30 | + |
| 31 | +bitcore = require('bitcore-lib') |
| 32 | +PrivateKey = bitcore.private_key |
| 33 | +Transaction = bitcore.transaction |
| 34 | + |
| 35 | +pvt_key_string = File.read.strip() |
| 36 | +pvt_key = PrivateKey.new pvt_key_string |
| 37 | + |
| 38 | +address = pvt_key.toAddress() |
| 39 | + |
| 40 | +puts "private key: ${pvt_key}" |
| 41 | + |
| 42 | + |
| 43 | +# ---- |
| 44 | + |
| 45 | +const { readFileSync } = require('fs') |
| 46 | +const bitcoin = require('bitcore-lib') |
| 47 | +const { PrivateKey, Transaction } = bitcoin |
| 48 | + |
| 49 | +const pvtKeyString = readFileSync('./private-key.txt').toString().trim() |
| 50 | +const pvtKey = new PrivateKey(pvtKeyString) |
| 51 | + |
| 52 | +const address = pvtKey.toAddress().toString() |
| 53 | + |
| 54 | +console.log("private key:", pvtKey.toString()) |
| 55 | +console.log("address:", address) |
| 56 | + |
| 57 | +// --- 6 lines |
| 58 | + |
| 59 | +const getUTXOs = require('./blockchain-api/get-utxos') |
| 60 | +const pushTx = require('./blockchain-api/push-tx') |
| 61 | +// const { pushTx, getUTXOs } = require('./blockchain-api') // TODO: NPM MODULE |
| 62 | +
|
| 63 | +;(async () => { |
| 64 | + try { |
| 65 | + let utxos = await getUTXOs({ address }) |
| 66 | +
|
| 67 | + utxos = [ utxos[0] ] |
| 68 | +
|
| 69 | + console.log("utxos:", utxos) |
| 70 | +
|
| 71 | + const amount = 10000 // 10k sats (0.1mbtc) |
| 72 | + |
| 73 | + const tx = new Transaction() |
| 74 | + .from(utxos) |
| 75 | + .to(address, amount) |
| 76 | + .change(address) |
| 77 | + .fee(1000) |
| 78 | + .sign(pvtKey) |
| 79 | + |
| 80 | + const txHex = tx.serialize() |
| 81 | + |
| 82 | + console.log("tx:", txHex) |
| 83 | + |
| 84 | + const { success, txHash } = pushTx(txHex) |
| 85 | + console.log("success:", success, "txHash:", txHash) |
| 86 | + |
| 87 | + } catch (err) { |
| 88 | + console.error(err) |
| 89 | + } |
| 90 | +})() |
0 commit comments