Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 74d65e2

Browse files
committed
rewriting in opal - notes
1 parent ee6541b commit 74d65e2

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const pushTx = require('./blockchain-api/push-tx')
6666

6767
```
6868

69-
Creating a private key and sending a bitcoin transaction in 30 lines of code - youtube video explainer #coding #bitcoin #tx #utxo #bitcoin-tx #bitcoin-utxo
69+
Creating a private key and sending a bitcoin transaction in 30 lines of code - youtube video explainer (WIP) #coding #bitcoin #tx #utxo #bitcoin-tx #bitcoin-utxo
7070

7171

7272
---

tmp/media/screenshot.png

1.01 MB
Loading

tmp/notes.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)