class Client
Public Class Methods
new(secret, branch = 'prod')
click to toggle source
# File lib/stampery.rb, line 16 def initialize(secret, branch = 'prod') @client_secret = secret @client_id = Digest::MD5.hexdigest(secret)[0, 15] @api_end_point = @@api_end_points[branch] || @@api_end_points['prod'] @amqp_end_point = @@amqp_end_points[branch] || @@amqp_end_points['prod'] end
Public Instance Methods
hash(data)
click to toggle source
# File lib/stampery.rb, line 37 def hash(data) SHA3::Digest.hexdigest(:sha512, data).upcase end
prove(hash, proof)
click to toggle source
# File lib/stampery.rb, line 41 def prove(hash, proof) validate hash, proof['siblings'], proof['root'] end
stamp(data)
click to toggle source
# File lib/stampery.rb, line 28 def stamp(data) puts "Stamping \n#{data}" begin @api_client.call 'stamp', data.upcase rescue Exception => e emit :error, e end end
start()
click to toggle source
# File lib/stampery.rb, line 23 def start api_login @api_end_point amqp_login @amqp_end_point if @auth end
Private Instance Methods
amqp_login(end_point)
click to toggle source
# File lib/stampery.rb, line 81 def amqp_login(end_point) amqp_conn = Bunny.new(automatically_recover: true, host: end_point[0], port: end_point[1], user: end_point[2], pass: end_point[3], vhost: end_point[4]) amqp_conn.start puts '[QUEUE] Connected to Rabbit!' emit :ready amqp_channel = amqp_conn.create_channel queue = amqp_channel.queue(@client_id + '-clnt', no_declare: true) begin queue.subscribe(block: true) do |delivery_info, _metadata, queueMsg| hash = delivery_info.routing_key proof = process_proof MessagePack.unpack queueMsg emit :proof, hash, proof end rescue Exception => _ puts "\n\nClosing the client" end end
api_login(end_point)
click to toggle source
# File lib/stampery.rb, line 68 def api_login(end_point) @api_client = MessagePack::RPC::Client.new(end_point[0], end_point[1]) user_agent = "ruby-#{Gem::Specification.load('stampery.gemspec').version}" req = @api_client.call_async('stampery.3.auth', @client_id, @client_secret, user_agent) req.join @auth = req.result if @auth puts "logged #{@client_id}" else emit :error, "Couldn't log in" end end
hex_to_bin(s)
click to toggle source
Take the hex digits two at a time (since each byte can range from 00 to FF), convert the digits to a character, and join them back together
# File lib/stampery.rb, line 64 def hex_to_bin(s) s.scan(/../).map { |x| x.hex.chr }.join end
mix(a, b)
click to toggle source
# File lib/stampery.rb, line 55 def mix(a, b) a = hex_to_bin a b = hex_to_bin b commuted = a > b ? a + b : b + a hash commuted end
process_proof(raw_proof)
click to toggle source
# File lib/stampery.rb, line 103 def process_proof(raw_proof) Hash['version' => raw_proof[0], 'siblings' => raw_proof[1], 'root' => raw_proof[2], 'anchor' => Hash['chain' => raw_proof[3][0], 'tx' => raw_proof[3][1]]] end
validate(hash, siblings, root)
click to toggle source
# File lib/stampery.rb, line 47 def validate(hash, siblings, root) if siblings.size > 0 mixed = mix hash, siblings[0] return validate(mixed, siblings[1..-1], root) end hash == root end