forked from apradillap/simple-blockchain-in-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.rb
122 lines (109 loc) · 2.64 KB
/
blockchain.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
require 'digest'
require 'pp'
require_relative 'block'
require_relative 'transaction'
LEDGER = []
USERS = {
"FanXu" => Digest::SHA256.hexdigest("Fan123"),
"Raquel" => Digest::SHA256.hexdigest("Raquel123"),
"Celia" => Digest::SHA256.hexdigest("Celia123")
}
TRANSACTIONS_FILE = "transactions.csv"
def authenticate_user
puts "Enter username: "
username = gets.chomp
puts "Enter password"
password = gets.chomp
if USERS[username] == Digest::SHA256.hexdigest(password)
puts "Authentication successful"
return true
else
puts "Authentication error: incorrect username or password"
return false
end
end
def create_transactions_file
CSV.open(TRANSACTIONS_FILE, "w") {}
end
def create_first_block
i = 0
instance_variable_set(
"@b#{i}",
Block.first(
{ from: "Dutchgrown", to: "Vincent", what: "Tulip Bloemendaal Sunset", qty: 10 },
{ from: "Keukenhof", to: "Anne", what: "Tulip Semper Augustus", qty: 7 }
)
)
LEDGER << instance_variable_get("@b#{i}")
pp instance_variable_get("@b#{i}")
p "============================"
add_block
end
def add_block
i = 1
loop do
if authenticate_user
instance_variable_set(
"@b#{i}",
Block.next(instance_variable_get("@b#{i - 1}"), get_transactions_data)
)
LEDGER << instance_variable_get("@b#{i}")
p "============================"
pp instance_variable_get("@b#{i}")
p "============================"
i += 1
end
break unless continue_operation?
end
end
def write_transactions_to_csv(transactions)
CSV.open(TRANSACTIONS_FILE, "a") do |csv|
transactions.each do |transaction|
csv << [transaction[:from], transaction[:to], transaction[:what], transaction[:qty]]
end
end
end
def continue_operation?
puts ""
puts "Do you want to continue with another user? (Y/N)"
choice = gets.chomp.downcase
choice == 'y'
end
def launcher
puts "==========================="
puts ""
puts "Welcome to Simple Blockchain In Ruby !"
puts ""
sleep 1.5
puts "This program was created by Anthony Amar for educational purposes"
puts ""
sleep 1.5
puts "Wait for the genesis (the first block of the blockchain)"
puts ""
10.times do
print "."
sleep 0.5
end
puts ""
puts ""
puts "==========================="
while true
puts ""
puts "Choose an option: "
puts "1. Enter the blockchain with a username and password"
puts "2. Exit the program"
opcion = gets.chomp.to_i
case opcion
when 1
create_transactions_file
create_first_block
break
when 2
puts "Bye!"
break
else
puts "Invalid option"
end
end
end
launcher