stampery 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/example.rb +7 -5
- data/lib/stampery.rb +38 -10
- data/stampery.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa901dab9f6a1790702189407abce72bced44a75
|
4
|
+
data.tar.gz: 268831e361df1e0a93b71edcbc9393dd53d36b93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f2191a15250098f1576de274620ae45d7e5c2971a5a0af67f54b65c0fc82b3004fbe69f31f31a21e2d5fe4848e5225350a747975b4f72135568f664cdccf8d4
|
7
|
+
data.tar.gz: ff2fadedc2af8ca3adbf6db60d6385ca822c0b2d3bd93e2b5dedbdf7d368ce871c4b376272fb0c072aad6e32e360af32c5e661dd953993a10e5a87bda1389f85
|
data/README.md
CHANGED
@@ -22,13 +22,16 @@ Or install it yourself as:
|
|
22
22
|
require 'stampery'
|
23
23
|
|
24
24
|
# Sign up and get your secret token at https://api-dashboard.stampery.com
|
25
|
-
stampery = Client.new '
|
25
|
+
stampery = Client.new 'user_secret'
|
26
26
|
|
27
27
|
stampery.on :proof do |hash, proof|
|
28
|
-
puts
|
29
|
-
puts hash
|
28
|
+
puts "Received proof for \n#{hash}\n\n"
|
30
29
|
puts 'Proof'
|
31
|
-
puts proof
|
30
|
+
puts "Version: #{proof['version']}\nSiblings: #{proof['siblings']}\nRoot: #{proof['root']}"
|
31
|
+
puts "Anchor:\n Chain: #{proof['anchor']['chain']}\n Tx: #{proof['anchor']['tx']}\n"
|
32
|
+
# validate proof
|
33
|
+
valid = stampery.prove hash, proof
|
34
|
+
puts "Prove validity #{valid}\n\n"
|
32
35
|
end
|
33
36
|
|
34
37
|
stampery.on :error do |err|
|
@@ -36,7 +39,7 @@ stampery.on :error do |err|
|
|
36
39
|
end
|
37
40
|
|
38
41
|
stampery.on :ready do
|
39
|
-
digest = stampery.hash 'Hello, blockchain!'
|
42
|
+
digest = stampery.hash 'Hello, blockchain!' + Random.rand().to_s
|
40
43
|
stampery.stamp digest
|
41
44
|
end
|
42
45
|
|
data/example.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'stampery'
|
2
2
|
|
3
3
|
# Sign up and get your secret token at https://api-dashboard.stampery.com
|
4
|
-
stampery = Client.new '
|
4
|
+
stampery = Client.new 'user_secret'
|
5
5
|
|
6
6
|
stampery.on :proof do |hash, proof|
|
7
|
-
puts
|
8
|
-
puts hash
|
7
|
+
puts "Received proof for \n#{hash}\n\n"
|
9
8
|
puts 'Proof'
|
10
|
-
puts proof
|
9
|
+
puts "Version: #{proof['version']}\nSiblings: #{proof['siblings']}\nRoot: #{proof['root']}"
|
10
|
+
puts "Anchor:\n Chain: #{proof['anchor']['chain']}\n Tx: #{proof['anchor']['tx']}\n"
|
11
|
+
valid = stampery.prove hash, proof
|
12
|
+
puts "Prove validity #{valid}\n\n"
|
11
13
|
end
|
12
14
|
|
13
15
|
stampery.on :error do |err|
|
@@ -15,7 +17,7 @@ stampery.on :error do |err|
|
|
15
17
|
end
|
16
18
|
|
17
19
|
stampery.on :ready do
|
18
|
-
digest = stampery.hash 'Hello, blockchain!'
|
20
|
+
digest = stampery.hash 'Hello, blockchain!' + Random.rand().to_s
|
19
21
|
stampery.stamp digest
|
20
22
|
end
|
21
23
|
|
data/lib/stampery.rb
CHANGED
@@ -13,7 +13,7 @@ class Client
|
|
13
13
|
@@amqp_end_points = { 'prod' => ['young-squirrel.rmq.cloudamqp.com', 5672, 'consumer', '9FBln3UxOgwgLZtYvResNXE7', 'ukgmnhoi'],
|
14
14
|
'beta' => ['young-squirrel.rmq.cloudamqp.com', 5672, 'consumer', '9FBln3UxOgwgLZtYvResNXE7', 'beta'] }
|
15
15
|
|
16
|
-
def initialize
|
16
|
+
def initialize(secret, branch = 'prod')
|
17
17
|
@client_secret = secret
|
18
18
|
@client_id = Digest::MD5.hexdigest(secret)[0, 15]
|
19
19
|
@api_end_point = @@api_end_points[branch] || @@api_end_points['prod']
|
@@ -22,12 +22,10 @@ class Client
|
|
22
22
|
|
23
23
|
def start
|
24
24
|
api_login @api_end_point
|
25
|
-
if @auth
|
26
|
-
amqp_login @amqp_end_point
|
27
|
-
end
|
25
|
+
amqp_login @amqp_end_point if @auth
|
28
26
|
end
|
29
27
|
|
30
|
-
def stamp
|
28
|
+
def stamp(data)
|
31
29
|
puts "Stamping \n#{data}"
|
32
30
|
begin
|
33
31
|
@api_client.call 'stamp', data.upcase
|
@@ -36,15 +34,40 @@ class Client
|
|
36
34
|
end
|
37
35
|
end
|
38
36
|
|
39
|
-
def hash
|
37
|
+
def hash(data)
|
40
38
|
SHA3::Digest.hexdigest(:sha512, data).upcase
|
41
39
|
end
|
42
40
|
|
41
|
+
def prove(hash, proof)
|
42
|
+
validate hash, proof['siblings'], proof['root']
|
43
|
+
end
|
44
|
+
|
43
45
|
private
|
44
46
|
|
45
|
-
def
|
47
|
+
def validate(hash, siblings, root)
|
48
|
+
if siblings.size > 0
|
49
|
+
mixed = mix hash, siblings[0]
|
50
|
+
return validate(mixed, siblings[1..-1], root)
|
51
|
+
end
|
52
|
+
hash == root
|
53
|
+
end
|
54
|
+
|
55
|
+
def mix(a, b)
|
56
|
+
a = hex_to_bin a
|
57
|
+
b = hex_to_bin b
|
58
|
+
commuted = a > b ? a + b : b + a
|
59
|
+
hash commuted
|
60
|
+
end
|
61
|
+
|
62
|
+
# Take the hex digits two at a time (since each byte can range from 00 to FF),
|
63
|
+
# convert the digits to a character, and join them back together
|
64
|
+
def hex_to_bin(s)
|
65
|
+
s.scan(/../).map { |x| x.hex.chr }.join
|
66
|
+
end
|
67
|
+
|
68
|
+
def api_login(end_point)
|
46
69
|
@api_client = MessagePack::RPC::Client.new(end_point[0], end_point[1])
|
47
|
-
user_agent = "ruby-#{Gem::Specification
|
70
|
+
user_agent = "ruby-#{Gem::Specification.load('stampery.gemspec').version}"
|
48
71
|
req = @api_client.call_async('stampery.3.auth', @client_id, @client_secret, user_agent)
|
49
72
|
req.join
|
50
73
|
@auth = req.result
|
@@ -55,7 +78,7 @@ class Client
|
|
55
78
|
end
|
56
79
|
end
|
57
80
|
|
58
|
-
def amqp_login
|
81
|
+
def amqp_login(end_point)
|
59
82
|
amqp_conn = Bunny.new(automatically_recover: true, host: end_point[0],
|
60
83
|
port: end_point[1], user: end_point[2],
|
61
84
|
pass: end_point[3], vhost: end_point[4])
|
@@ -69,11 +92,16 @@ class Client
|
|
69
92
|
begin
|
70
93
|
queue.subscribe(block: true) do |delivery_info, _metadata, queueMsg|
|
71
94
|
hash = delivery_info.routing_key
|
72
|
-
proof = MessagePack.unpack queueMsg
|
95
|
+
proof = process_proof MessagePack.unpack queueMsg
|
73
96
|
emit :proof, hash, proof
|
74
97
|
end
|
75
98
|
rescue Exception => _
|
76
99
|
puts "\n\nClosing the client"
|
77
100
|
end
|
78
101
|
end
|
102
|
+
|
103
|
+
def process_proof(raw_proof)
|
104
|
+
Hash['version' => raw_proof[0], 'siblings' => raw_proof[1], 'root' => raw_proof[2],
|
105
|
+
'anchor' => Hash['chain' => raw_proof[3][0], 'tx' => raw_proof[3][1]]]
|
106
|
+
end
|
79
107
|
end
|
data/stampery.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stampery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johann Ortiz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sha3
|