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

Commit fa2f412

Browse files
committed
opal stub ruby code almost finished, fun but probably a pure ruby one based on bitcoin-ruby would be more realistic
1 parent 36f724b commit fa2f412

File tree

5 files changed

+151
-122
lines changed

5 files changed

+151
-122
lines changed

tmp/notes.rb

Lines changed: 0 additions & 122 deletions
This file was deleted.

tmp/ruby-opal/index.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require_relative 'lib/monkeypatches'
2+
path = EXPORTS.f(:path)
3+
fileRead = EXPORTS.f(:fileRead)
4+
bitcore = EXPORTS.f(:bitcore)
5+
PrivateKey = EXPORTS.f(:PrivateKey)
6+
Transaction = EXPORTS.f(:Transaction)
7+
getUTXOs = EXPORTS.f(:getUTXOs)
8+
pushTx = EXPORTS.f(:pushTx)
9+
10+
# # alternative syntax without explicit import:
11+
# bitcore = require('bitcore-lib')
12+
# PrivateKey = bitcore.private_key.()
13+
# Transaction = bitcore.transaction.()
14+
15+
pvt_key_string = fileRead.("#{path}/../../private-key.txt").strip()
16+
pvt_key = PrivateKey.new pvt_key_string
17+
18+
address = pvt_key.to_address()
19+
20+
puts "private key: #{pvt_key}"
21+
puts "address: #{address}"
22+
23+
utxos = getUTXOs.(address)
24+
25+
utxos = [ utxos[0] ]
26+
27+
puts "utxos: #{utxos}"
28+
29+
amount = 10_000 # sats (0.1 mbtc)
30+
31+
tx = Transaction.new
32+
tx.from(utxos)
33+
.to(address, amount)
34+
.change(address)
35+
.fee(1000)
36+
.sign(pvt_key)
37+
38+
tx_hex = tx.serialize()
39+
puts "tx: #{tx_hex}"
40+
41+
tx_status = pushTx.(tx_hex)
42+
puts "tx_status: #{tx_status.inspect}"

tmp/ruby-opal/lib/monkeypatches.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# TODO: implement DSL, run via opal
2+
3+
# current stubs (WIP):
4+
5+
require_relative 'private_key'
6+
require_relative 'transaction'
7+
8+
fileRead = -> (file) {
9+
File.read file
10+
}
11+
12+
def bitcore
13+
obj = {}
14+
obj[:private_key] = -> {
15+
"K..."
16+
}
17+
obj[:transaction] = -> {
18+
"x..."
19+
}
20+
end
21+
22+
getUTXOs = -> (address) {
23+
utxos = []
24+
# get_utxos ...
25+
utxos
26+
}
27+
28+
pushTx = -> (tx_hash) {
29+
# tx_hash ...
30+
{
31+
success: true,
32+
tx_hash: "abcd1234",
33+
}
34+
}
35+
36+
class Hash
37+
alias :f :fetch
38+
end
39+
40+
EXPORTS = {
41+
path: File.expand_path("../../", __FILE__),
42+
getUTXOs: getUTXOs,
43+
pushTx: pushTx,
44+
fileRead: fileRead,
45+
bitcore: bitcore,
46+
PrivateKey: PrivateKey,
47+
Transaction: Transaction,
48+
}

tmp/ruby-opal/lib/private_key.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class PrivateKey
2+
def initialize(private_key)
3+
@private_key_string = private_key
4+
end
5+
6+
def to_address
7+
# @private_key_string use bitcore via opal to convert to address
8+
"1...."
9+
end
10+
end

tmp/ruby-opal/lib/transaction.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
class NullAddress; end
3+
class NullAmount; end
4+
5+
class Transaction
6+
7+
def initialize
8+
@utxos = []
9+
@address = NullAddress.new
10+
@amount = NullAmount.new
11+
@change = NullAmount.new
12+
@fee = NullAmount.new
13+
end
14+
15+
def serialize
16+
# ...
17+
end
18+
19+
def from(utxos)
20+
@utxos = utxos
21+
self
22+
end
23+
24+
def to(address, amount)
25+
@address = address
26+
@amount = amount
27+
self
28+
end
29+
30+
def change(amount)
31+
@change = amount
32+
self
33+
end
34+
35+
def fee(amount)
36+
@fee = amount
37+
self
38+
end
39+
40+
def sign(private_key)
41+
perform_signature(private_key: private_key)
42+
self
43+
end
44+
45+
private
46+
47+
def perform_signature(private_key:)
48+
# ...
49+
end
50+
51+
end

0 commit comments

Comments
 (0)