22
33# TODO: implement DSL, run via opal
44
5+ # current stubs (WIP):
6+
57def require ( module )
68 # call node's require
79end
@@ -12,74 +14,104 @@ def File.read
1214
1315def 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
2133end
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 / / 10 k sats ( 0.1 mbtc )
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