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

Commit 36f724b

Browse files
committed
add more notes for the js to ruby/opal conversion
1 parent 74d65e2 commit 36f724b

File tree

3 files changed

+70
-40
lines changed

3 files changed

+70
-40
lines changed

Readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ const address = pvtKey.toAddress().toString()
2929
console.log("private key:", pvtKey.toString())
3030
console.log("address:", address)
3131

32-
// --- 6 lines
33-
3432
const getUTXOs = require('./blockchain-api/get-utxos')
3533
const pushTx = require('./blockchain-api/push-tx')
3634
// const { pushTx, getUTXOs } = require('./blockchain-api') // TODO: NPM MODULE

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ const address = pvtKey.toAddress().toString()
1010
console.log("private key:", pvtKey.toString())
1111
console.log("address:", address)
1212

13-
// --- 6 lines
14-
1513
// const getUTXOs = require('blockchain-api/get-utxos')
1614
const getUTXOs = require('./blockchain-api/get-utxos')
1715

1816
;(async () => {
1917
try {
2018
let utxos = await getUTXOs({ address })
2119

20+
// TODO: replace with actual input selection or disable (uncomment)
21+
// NOTE: the current code selects always the first spendable input (usually the latest transaction, as it's using the blockchain.info `/unspent` endpoint)
2222
utxos = [ utxos[0] ]
2323

2424
console.log("utxos:", utxos)

tmp/notes.rb

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# TODO: implement DSL, run via opal
44

5+
# current stubs (WIP):
6+
57
def require(module)
68
# call node's require
79
end
@@ -12,74 +14,104 @@ def File.read
1214

1315
def bitcore
1416
obj = Object.new
15-
def obj.private_key
17+
obj.private_key = -> {
1618
"K..."
19+
}
20+
obj.transaction = -> {
21+
"x..."
22+
}
23+
end
24+
25+
class PrivateKey
26+
def initialize
27+
1728
end
18-
def obj.transaction
19-
"x...."
29+
30+
def to_address
31+
"1...."
2032
end
2133
end
2234

23-
def bitcore.privatekey
2435

25-
end
36+
class Transaction
2637

27-
# ----
38+
def initialize
2839

29-
# final code:
40+
end
3041

31-
bitcore = require('bitcore-lib')
32-
PrivateKey = bitcore.private_key
33-
Transaction = bitcore.transaction
42+
def from(utxos)
43+
self
44+
end
3445

35-
pvt_key_string = File.read.strip()
36-
pvt_key = PrivateKey.new pvt_key_string
46+
def to(address, amount)
47+
self
48+
end
49+
50+
def change(amount)
51+
self
52+
end
53+
54+
def fee(amount)
55+
self
56+
end
57+
58+
def sign(private_key)
59+
self
60+
end
3761

38-
address = pvt_key.toAddress()
62+
end
3963

40-
puts "private key: ${pvt_key}"
64+
getUTXOs = -> () {
65+
Object.new
66+
}
4167

68+
pushTX = -> () {
69+
{
70+
success: true,
71+
tx_hash: "abcd1234",
72+
}
73+
}
4274

4375
# ----
4476

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)
77+
# final DSL:
5178

52-
const address = pvtKey.toAddress().toString()
79+
bitcore = require('bitcore-lib')
80+
PrivateKey = bitcore.private_key.()
81+
Transaction = bitcore.transaction.()
5382

54-
console.log("private key:", pvtKey.toString())
55-
console.log("address:", address)
83+
pvt_key_string = File.read.strip()
84+
pvt_key = PrivateKey.new pvt_key_string
5685

57-
// --- 6 lines
86+
address = pvt_key.to_address.()
5887

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
88+
puts "private key: #{pvt_key}"
89+
puts "address: #{address}"
6290

63-
;(async () => {
64-
try {
65-
let utxos = await getUTXOs({ address })
91+
utxos = getUTXOs.(address)
6692

67-
utxos = [ utxos[0] ]
93+
utxos = [ utxos[0] ]
6894

69-
console.log("utxos:", utxos)
95+
puts "utxos: #{utxos}"
7096

71-
const amount = 10000 // 10k sats (0.1mbtc)
97+
amount = 10_000 // sats (0.1 mbtc)
7298

73-
const tx = new Transaction()
99+
tx = Transacttion.new
74100
.from(utxos)
75101
.to(address, amount)
76102
.change(address)
77103
.fee(1000)
78104
.sign(pvtKey)
79105

80-
const txHex = tx.serialize()
106+
tx_hex = tx.serialize()
107+
108+
puts "tx: #{tx_hex}"
109+
110+
tx_status = pushTx.(txHex)
111+
112+
113+
81114
82-
console.log("tx:", txHex)
83115
84116
const { success, txHash } = pushTx(txHex)
85117
console.log("success:", success, "txHash:", txHash)

0 commit comments

Comments
 (0)