stampery 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1534e27adafd15f44bbb8906d184a4b46bf7c6e1
4
- data.tar.gz: 5575bcb669a5366ef27d3e68c149086a256119f5
3
+ metadata.gz: fa901dab9f6a1790702189407abce72bced44a75
4
+ data.tar.gz: 268831e361df1e0a93b71edcbc9393dd53d36b93
5
5
  SHA512:
6
- metadata.gz: ecb7ab3c49a2e65a54ac84215182d610f28912265f36102e274d0ba41bbeb0b789bd9d4adefe9799a621bc1c2fde94207d06b6424ec024f6114cf86fd56df352
7
- data.tar.gz: 9c15da2e5a4ecc0072e6694f8ecf98a4a559222c3789e2be3847a3dc34211a0f58d52a026dfe102ac26abcd419b436bc68444c72e3fea47b3542da2aa2730ea7
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 'user-secret'
25
+ stampery = Client.new 'user_secret'
26
26
 
27
27
  stampery.on :proof do |hash, proof|
28
- puts 'Received proof for'
29
- puts hash
28
+ puts "Received proof for \n#{hash}\n\n"
30
29
  puts 'Proof'
31
- puts proof.to_s
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 'user-secret'
4
+ stampery = Client.new 'user_secret'
5
5
 
6
6
  stampery.on :proof do |hash, proof|
7
- puts 'Received proof for'
8
- puts hash
7
+ puts "Received proof for \n#{hash}\n\n"
9
8
  puts 'Proof'
10
- puts proof.to_s
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
 
@@ -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 secret, branch = 'prod'
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 data
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 data
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 api_login end_point
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::load("stampery.gemspec").version}"
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 end_point
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
@@ -5,7 +5,7 @@ require 'stampery/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "stampery"
8
- spec.version = "0.1.2"
8
+ spec.version = "0.1.3"
9
9
  spec.authors = ["Johann Ortiz"]
10
10
  spec.email = ["johann@stampery.com"]
11
11
 
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.2
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-26 00:00:00.000000000 Z
11
+ date: 2016-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sha3