RecordsKeeperRubyLib 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -1
  3. data/CODE_OF_CONDUCT.md +0 -8
  4. data/Gemfile +0 -1
  5. data/Gemfile.lock +4 -62
  6. data/LICENSE.txt +2 -2
  7. data/README.md +92 -1
  8. data/{RecordsKeeperRuby.gemspec → RecordsKeeperRubyLib.gemspec} +4 -5
  9. data/bin/console +1 -1
  10. data/config.yaml +8 -0
  11. data/docs/{addressdoc.rb → address_doc.rb} +105 -116
  12. data/docs/{assetsdoc.rb → asset_doc.rb} +54 -65
  13. data/docs/block_doc.rb +63 -0
  14. data/docs/blockchain_doc.rb +77 -0
  15. data/docs/{permissionsdoc.rb → permissions_doc.rb} +54 -65
  16. data/docs/{streamdoc.rb → stream_doc.rb} +79 -84
  17. data/docs/{transactiondoc.rb → transaction_doc.rb} +110 -121
  18. data/docs/{walletdoc.rb → wallet_doc.rb} +122 -133
  19. data/lib/RecordsKeeperRubyLib.rb +9 -0
  20. data/lib/{RecordsKeeperRuby → RecordsKeeperRubyLib}/address.rb +198 -186
  21. data/lib/{RecordsKeeperRuby → RecordsKeeperRubyLib}/assets.rb +99 -87
  22. data/lib/{RecordsKeeperRuby → RecordsKeeperRubyLib}/block.rb +94 -89
  23. data/lib/{RecordsKeeperRuby → RecordsKeeperRubyLib}/blockchain.rb +148 -135
  24. data/lib/RecordsKeeperRubyLib/config.yaml +8 -0
  25. data/lib/{RecordsKeeperRuby → RecordsKeeperRubyLib}/permissions.rb +77 -77
  26. data/lib/RecordsKeeperRubyLib/stream.rb +220 -0
  27. data/lib/{RecordsKeeperRuby → RecordsKeeperRubyLib}/transaction.rb +213 -182
  28. data/lib/RecordsKeeperRubyLib/version.rb +3 -0
  29. data/lib/{RecordsKeeperRuby → RecordsKeeperRubyLib}/wallet.rb +263 -252
  30. data/lib/config.yaml +8 -0
  31. metadata +27 -41
  32. data/docs/blockchaindoc.rb +0 -77
  33. data/docs/blockdoc.rb +0 -76
  34. data/lib/RecordsKeeperRuby.rb +0 -9
  35. data/lib/RecordsKeeperRuby/sample_config.yaml +0 -60
  36. data/lib/RecordsKeeperRuby/stream.rb +0 -169
  37. data/lib/RecordsKeeperRuby/version.rb +0 -3
  38. data/lib/sample_config.yaml +0 -60
  39. data/sample_config.yaml +0 -60
@@ -0,0 +1,8 @@
1
+ # config.yaml
2
+
3
+ url: testurl
4
+ rkuser: rkuser
5
+ passwd: rkpwd
6
+ chain: test-chain
7
+ port: testport
8
+
@@ -1,77 +1,77 @@
1
- # Library to work with RecordsKeeper Blockchain permissions.
2
-
3
- # You can grant and revoke permissions to any node on Recordskeeper Blockchain by using permissions class.
4
- # You just have to pass parameters to invoke the pre-defined functions.
5
-
6
- require 'rubygems'
7
- require 'httparty'
8
- require 'json'
9
- require 'binary_parser'
10
- require 'yaml'
11
- require 'hex_string'
12
-
13
- module RecordsKeeperRuby
14
-
15
- class Permissions
16
-
17
- # Entry point for accessing Block class resources.
18
- # Import values from config file.
19
- cfg = YAML::load(File.open('config.yaml','r'))
20
- @network = cfg['testnet'] # Network variable to store the networrk that you want to access
21
- if @network==cfg['testnet']
22
- @url = cfg['testnet']['url']
23
- @user = cfg['testnet']['rkuser']
24
- @password = cfg['testnet']['passwd']
25
- @chain = cfg['testnet']['chain']
26
- else
27
- @url = cfg['mainnet']['url']
28
- @user = cfg['mainnet']['rkuser']
29
- @password = cfg['mainnet']['passwd']
30
- @chain = cfg['mainnet']['chain']
31
- end
32
-
33
- def self.variable
34
- net = @network
35
- return net
36
- end
37
-
38
- # Function to grant permissions on RecordsKeeper Blockchain
39
- def self.grantPermission address, permissions
40
- auth = {:username => @user, :password => @password}
41
- options = {
42
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
43
- :basic_auth => auth,
44
- :body => [ {"method":"grant","params":[address, permissions],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
45
- }
46
- response = HTTParty.get(@url, options)
47
- out = response.parsed_response
48
- result = out[0]['result']
49
- if result.nil?
50
- res = out[0]['error']['message']
51
- else
52
- res = out[0]['result']
53
- end
54
- return res; #returns permissions tx id
55
- end
56
-
57
- # Function to revoke permissions on RecordsKeeper Blockchain
58
- def self.revokePermission address, permissions
59
- auth = {:username => @user, :password => @password}
60
- options = {
61
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
62
- :basic_auth => auth,
63
- :body => [ {"method":"revoke","params":[address, permissions],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
64
- }
65
- response = HTTParty.get(@url, options)
66
- out = response.parsed_response
67
- result = out[0]['result']
68
- if result.nil?
69
- res = out[0]['error']['message']
70
- else
71
- res = out[0]['result']
72
- end
73
- return res; #returns revoke permissions tx id
74
- end
75
- end
76
-
77
- end
1
+ # Library to work with RecordsKeeper Blockchain permissions.
2
+
3
+ # You can grant and revoke permissions to any node on Recordskeeper Blockchain by using permissions class.
4
+ # You just have to pass parameters to invoke the pre-defined functions.
5
+
6
+ require 'rubygems'
7
+ require 'httparty'
8
+ require 'json'
9
+ require 'binary_parser'
10
+ require 'yaml'
11
+ require 'hex_string'
12
+
13
+ module RecordsKeeperRubyLib
14
+
15
+ class Permissions
16
+
17
+ # # Entry point for accessing Permissions class functions
18
+ if File.exist?('config.yaml')
19
+ # Import values from configuration file.
20
+ cfg = YAML::load(File.open('config.yaml','r'))
21
+
22
+ @url = cfg['url']
23
+ @user = cfg['rkuser']
24
+ @password = cfg['passwd']
25
+ @chain = cfg['chain']
26
+
27
+ else
28
+ #Import using ENV variables
29
+
30
+ @url = ENV['url']
31
+ @user = ENV['rkuser']
32
+ @password = ENV['passwd']
33
+ @chain = ENV['chain']
34
+ end
35
+
36
+
37
+ # Function to grant permissions on RecordsKeeper Blockchain
38
+ def self.grantPermission address, permissions
39
+ auth = {:username => @user, :password => @password}
40
+ options = {
41
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
42
+ :basic_auth => auth,
43
+ :body => [ {"method":"grant","params":[address, permissions],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
44
+ }
45
+ response = HTTParty.get(@url, options)
46
+ out = response.parsed_response
47
+ result = out[0]['result']
48
+ if result.nil?
49
+ res = out[0]['error']['message']
50
+ else
51
+ res = out[0]['result']
52
+ end
53
+ return res; #returns permissions tx id
54
+ end
55
+
56
+ # Function to revoke permissions on RecordsKeeper Blockchain
57
+ def self.revokePermission address, permissions
58
+ auth = {:username => @user, :password => @password}
59
+ options = {
60
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
61
+ :basic_auth => auth,
62
+ :body => [ {"method":"revoke","params":[address, permissions],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
63
+ }
64
+ response = HTTParty.get(@url, options)
65
+ out = response.parsed_response
66
+ result = out[0]['result']
67
+ if result.nil?
68
+ res = out[0]['error']['message']
69
+ else
70
+ res = out[0]['result']
71
+ end
72
+ return res; #returns revoke permissions tx id
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,220 @@
1
+ # Library to work with RecordsKeeper streams.
2
+
3
+ # You can publish, retrieve and verify stream data by using stream class.
4
+ # You just have to pass parameters to invoke the pre-defined functions.
5
+
6
+ require 'rubygems'
7
+ require 'httparty'
8
+ require 'json'
9
+ require 'binary_parser'
10
+ require 'yaml'
11
+ require 'hex_string'
12
+
13
+ module RecordsKeeperRubyLib
14
+
15
+ class Stream
16
+
17
+ # # Entry point for accessing Stream class functions
18
+ if File.exist?('config.yaml')
19
+ # Import values from configuration file.
20
+ cfg = YAML::load(File.open('config.yaml','r'))
21
+
22
+ @url = cfg['url']
23
+ @user = cfg['rkuser']
24
+ @password = cfg['passwd']
25
+ @chain = cfg['chain']
26
+
27
+ else
28
+ #Import using ENV variables
29
+
30
+ @url = ENV['url']
31
+ @user = ENV['rkuser']
32
+ @password = ENV['passwd']
33
+ @chain = ENV['chain']
34
+ end
35
+
36
+ # Function to publish data into the stream
37
+ def self.publish address, stream, key, data
38
+ datahex1 = data.to_hex_string
39
+ datahex = datahex1.delete(' ')
40
+ auth = {:username => @user, :password => @password}
41
+ options = {
42
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
43
+ :basic_auth => auth,
44
+ :body => [ {"method":"publishfrom","params":[address, stream, key, datahex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
45
+ }
46
+ response = HTTParty.get(@url, options)
47
+ out = response.parsed_response
48
+
49
+ txid = out[0]['result']
50
+
51
+ if txid.nil?
52
+ txid = out[0]['error']['message']
53
+ end
54
+
55
+ return txid;
56
+ end
57
+
58
+ # Function to retrieve data against transaction id from the stream
59
+ def self.retrieve stream, txid
60
+ auth = {:username => @user, :password => @password}
61
+ options = {
62
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
63
+ :basic_auth => auth,
64
+ :body => [ {"method":"getstreamitem","params":[stream, txid],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
65
+ }
66
+ response = HTTParty.get(@url, options)
67
+ out = response.parsed_response
68
+ check = out[0]['result']
69
+
70
+ if check.nil?
71
+ raw_data = out[0]['error']['message']
72
+ else
73
+ data = out[0]['result']['data']
74
+ raw_data = data.to_byte_string
75
+ end
76
+ return raw_data;
77
+ end
78
+
79
+ # Function to retrieve data against a particular publisher address
80
+ def self.retrieveWithAddress stream, address, count
81
+ check = []
82
+ auth = {:username => @user, :password => @password}
83
+ options = {
84
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
85
+ :basic_auth => auth,
86
+ :body => [ {"method":"liststreampublisheritems","params":[stream, address, false, count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
87
+ }
88
+ response = HTTParty.get(@url, options)
89
+ out = response.parsed_response
90
+
91
+
92
+ check = out[0]['result']
93
+
94
+
95
+ if check.empty?
96
+ retrievedinfo = "No published data found for this address"
97
+
98
+ else
99
+ key = []
100
+ raw_data = []
101
+ txid = []
102
+ for i in 0...check.length
103
+ key.push(out[0]['result'][i]['key']) #returns key value of the published data
104
+ data = out[0]['result'][i]['data'] #returns hex data
105
+ raw_data.push(data.to_byte_string) #returns raw data
106
+ txid.push(out[0]['result'][i]['txid']) #returns tx id
107
+ end
108
+ retrieve = {:key => key,:raw_data => raw_data,:txid => txid}
109
+ retrievedinfo = JSON.generate retrieve
110
+ end
111
+ return retrievedinfo
112
+ end
113
+
114
+ # Function to retrieve data against a particular key value
115
+ def self.retrieveWithKey stream, key, count
116
+ auth = {:username => @user, :password => @password}
117
+ options = {
118
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
119
+ :basic_auth => auth,
120
+ :body => [ {"method":"liststreamkeyitems","params":[stream, key, false, count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
121
+ }
122
+ response = HTTParty.get(@url, options)
123
+ out = response.parsed_response
124
+
125
+ check = out[0]['result']
126
+
127
+ if check.nil?
128
+ retrievedinfo = out[0]['error']['message']
129
+
130
+ else
131
+
132
+ publisher = []
133
+ raw_data = []
134
+ txid = []
135
+
136
+ for i in 0...check.length
137
+ publisher.push(out[0]['result'][i]['publishers'][0]) #returns publisher's address of published data
138
+ data = out[0]['result'][i]['data'] #returns published hex data
139
+ raw_data.push(data.to_byte_string) #returns data published
140
+ txid.push(out[0]['result'][i]['txid']) #returns transaction id of published data
141
+ end
142
+ retrieve = {:publisher => publisher,:raw_data => raw_data,:txid => txid}
143
+ retrievedinfo = JSON.generate retrieve
144
+ return retrievedinfo
145
+ end
146
+ end
147
+
148
+ # Function to verify data on RecordsKeeper Blockchain
149
+ def self.verifyData stream, data, count
150
+ auth = {:username => @user, :password => @password}
151
+ options = {
152
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
153
+ :basic_auth => auth,
154
+ :body => [ {"method":"liststreamitems","params":[stream,false ,count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
155
+ }
156
+ response = HTTParty.get(@url, options)
157
+ out = response.parsed_response
158
+
159
+ check = out[0]['result']
160
+
161
+ if check.nil?
162
+ result = out[0]['error']['message']
163
+ else
164
+ raw_data = []
165
+ for i in 0...check.length
166
+ result_data = out[0]['result'][i]['data'] # returns hex data
167
+
168
+ if result_data.unicode_normalized?
169
+ raw_data.push(result_data.to_byte_string) # returns raw data
170
+
171
+ else
172
+ raw_data.push("No data found")
173
+ end
174
+ end
175
+
176
+ if raw_data.include?data
177
+ result = "Data is successfully verified."
178
+ else
179
+ result = "Data not found."
180
+ end
181
+ end
182
+ return result;
183
+ end
184
+
185
+ # Function to list stream items on RecordsKeeper Blockchain
186
+ def self.retrieveItems stream, count
187
+ auth = {:username => @user, :password => @password}
188
+ options = {
189
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
190
+ :basic_auth => auth,
191
+ :body => [ {"method":"liststreamitems","params":[stream, false ,count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
192
+ }
193
+ response = HTTParty.get(@url, options)
194
+ out = response.parsed_response
195
+
196
+ check = out[0]['result']
197
+
198
+ if check.nil?
199
+ retrieveditems = out[0]['error']['message']
200
+ else
201
+ address =[]
202
+ key_value = []
203
+ raw_data = []
204
+ txid = []
205
+
206
+ for i in 0...check.length
207
+ address.push(out[0]['result'][i]['publishers']) # returns publisher address
208
+ key_value.push(out[0]['result'][i]['key']) # returns key value of data
209
+ data = out[0]['result'][i]['data'] # returns hex data
210
+ raw_data.push(data.to_byte_string) # returns raw data
211
+ txid.push(out[0]['result'][i]['txid']) # returns tx id
212
+ end
213
+ retrieve = {:address => address,:key_value => key_value,:raw_data => raw_data,:txid => txid}
214
+ retrieveditems = JSON.generate retrieve
215
+ end
216
+ return retrieveditems
217
+ end
218
+ end
219
+
220
+ end
@@ -1,182 +1,213 @@
1
- # Library to work with RecordsKeeper transactions.
2
-
3
- # You can send transaction, create raw transaction, sign raw transaction, send raw transaction, send signed transaction,
4
- # retrieve transaction information and calculate transaction's fees by using transaction class. You just have to pass
5
- # parameters to invoke the pre-defined functions.
6
-
7
- require 'rubygems'
8
- require 'httparty'
9
- require 'json'
10
- require 'binary_parser'
11
- require 'yaml'
12
- require 'hex_string'
13
-
14
- module RecordsKeeperRuby
15
-
16
- class Transaction
17
-
18
- # Entry point for accessing Block class resources.
19
- # Import values from config file.
20
- cfg = YAML::load(File.open('config.yaml','r'))
21
- @network = cfg['testnet'] # Network variable to store the networrk that you want to access
22
- if @network==cfg['testnet']
23
- @url = cfg['testnet']['url']
24
- @user = cfg['testnet']['rkuser']
25
- @password = cfg['testnet']['passwd']
26
- @chain = cfg['testnet']['chain']
27
- else
28
- @url = cfg['mainnet']['url']
29
- @user = cfg['mainnet']['rkuser']
30
- @password = cfg['mainnet']['passwd']
31
- @chain = cfg['mainnet']['chain']
32
- end
33
-
34
- def self.variable
35
- net = @network
36
- return net
37
- end
38
-
39
- # Function to send transaction on RecordsKeeper Blockchain
40
- def self.sendTransaction sender_address, receiveraddress, data, amount
41
- data_hex = data.to_hex_string
42
- datahex = data_hex.delete(' ')
43
- auth = {:username => @user, :password => @password}
44
- options = {
45
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
46
- :basic_auth => auth,
47
- :body => [ {"method":"createrawsendfrom","params":[sender_address, { receiveraddress => amount }, [datahex], "send"],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
48
- }
49
- response = HTTParty.get(@url, options)
50
- out = response.parsed_response
51
- txid = out[0]['result']
52
- return txid;
53
- end
54
-
55
- # Function to create transaction hex on RecordsKeeper Blockchain
56
- def self.createRawTransaction sender_address, receiveraddress, amount, data
57
- data_hex = data.to_hex_string
58
- datahex = data_hex.delete(' ')
59
- auth = {:username => @user, :password => @password}
60
- options = {
61
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
62
- :basic_auth => auth,
63
- :body => [ {"method":"createrawsendfrom","params":[sender_address, {receiveraddress => amount}, [datahex], ''],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
64
- }
65
- response = HTTParty.get(@url, options)
66
- out = response.parsed_response
67
- txhex = out[0]['result']
68
- return txhex;
69
- end
70
-
71
- # Function to sign transaction on RecordsKeeper Blockchain
72
- def self.signRawTransaction txHex, private_key
73
- priv_key = []
74
- priv_key.push(private_key)
75
- auth = {:username => @user, :password => @password}
76
- options = {
77
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
78
- :basic_auth => auth,
79
- :body => [ {"method":"signrawtransaction","params":[txHex, [], priv_key],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
80
- }
81
- response = HTTParty.get(@url, options)
82
- out = response.parsed_response
83
- signedHex = out[0]['result']['hex']
84
- return signedHex;
85
- end
86
-
87
- # Function to send raw transaction on RecordsKeeper Blockchain
88
- def self.sendRawTransaction signed_txHex
89
- auth = {:username => @user, :password => @password}
90
- options = {
91
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
92
- :basic_auth => auth,
93
- :body => [ {"method":"sendrawtransaction","params":[signed_txHex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
94
- }
95
- response = HTTParty.get(@url, options)
96
- out = response.parsed_response
97
- txn = out[0]['result']
98
- if txn.nil?
99
- txid = out[0]['error']['message']
100
- else
101
- txid = txn
102
- end
103
- return txid;
104
- end
105
-
106
- # Function to send signed transaction on RecordsKeeper Blockchain
107
- def self.sendSignedTransaction sender_address, receiveraddress, amount, private_key, data
108
- data_hex = data.to_hex_string
109
- datahex = data_hex.delete(' ')
110
- def self.createRawTransaction sender_address, receiveraddress, amount, datahex
111
- auth = {:username => @user, :password => @password}
112
- options = {
113
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
114
- :basic_auth => auth,
115
- :body => [ {"method":"createrawsendfrom","params":[sender_address, {receiveraddress => amount}, [datahex], ""],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
116
- }
117
- response = HTTParty.get(@url, options)
118
- out = response.parsed_response
119
- return out[0]['result']
120
- end
121
- txHex = createRawTransaction sender_address, receiveraddress, Float(amount), datahex
122
- def self.signRawTransaction txHex, private_key
123
- auth = {:username => @user, :password => @password}
124
- options = {
125
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
126
- :basic_auth => auth,
127
- :body => [ {"method":"signrawtransaction","params":[txHex, [], [private_key]],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
128
- }
129
- response = HTTParty.get(@url, options)
130
- out = response.parsed_response
131
- return out[0]['result']['hex']
132
- end
133
- signed_tx_hex = signRawTransaction txHex, private_key
134
- def self.sendRawTransaction signed_tx_hex
135
- auth = {:username => @user, :password => @password}
136
- options = {
137
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
138
- :basic_auth => auth,
139
- :body => [ {"method":"sendrawtransaction","params":[signed_tx_hex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
140
- }
141
- response = HTTParty.get(@url, options)
142
- out = response.parsed_response
143
- return out[0]['result']
144
- end
145
- tx_id = sendRawTransaction signed_tx_hex
146
- return tx_id;
147
- end
148
-
149
- # Function to retrieve transaction on RecordsKeeper Blockchain
150
- def self.retrieveTransaction tx_id
151
- auth = {:username => @user, :password => @password}
152
- options = {
153
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
154
- :basic_auth => auth,
155
- :body => [ {"method":"getrawtransaction","params":[tx_id, 1],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
156
- }
157
- response = HTTParty.get(@url, options)
158
- out = response.parsed_response
159
- sent_hex_data = out[0]['result']['data'][0]
160
- sent_data = sent_hex_data.to_byte_string
161
- sent_amount = out[0]['result']['vout'][0]['value']
162
- return sent_data, sent_amount; #returns data from retrieved transaction
163
- end
164
-
165
- # Function to calculate transaction's fee on RecordsKeeper Blockchain
166
- def self.getFee address, tx_id
167
- auth = {:username => @user, :password => @password}
168
- options = {
169
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
170
- :basic_auth => auth,
171
- :body => [ {"method":"getaddresstransaction","params":[address, tx_id, true],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
172
- }
173
- response = HTTParty.get(@url, options)
174
- out = response.parsed_response
175
- sent_amount = out[0]['result']['vout'][0]['amount']
176
- balance_amount = out[0]['result']['balance']['amount']
177
- fees = balance_amount.abs - sent_amount
178
- return fees; #returns fees
179
- end
180
- end
181
-
182
- end
1
+ # Library to work with RecordsKeeper transactions.
2
+
3
+ # You can send transaction, create raw transaction, sign raw transaction, send raw transaction, send signed transaction,
4
+ # retrieve transaction information and calculate transaction's fees by using transaction class. You just have to pass
5
+ # parameters to invoke the pre-defined functions.
6
+
7
+ require 'rubygems'
8
+ require 'httparty'
9
+ require 'json'
10
+ require 'binary_parser'
11
+ require 'yaml'
12
+ require 'hex_string'
13
+
14
+ module RecordsKeeperRubyLib
15
+
16
+ class Transaction
17
+
18
+ # # Entry point for accessing Transaction class functions
19
+ if File.exist?('config.yaml')
20
+ # Import values from configuration file.
21
+ cfg = YAML::load(File.open('config.yaml','r'))
22
+
23
+ @url = cfg['url']
24
+ @user = cfg['rkuser']
25
+ @password = cfg['passwd']
26
+ @chain = cfg['chain']
27
+
28
+ else
29
+ #Import using ENV variables
30
+
31
+ @url = ENV['url']
32
+ @user = ENV['rkuser']
33
+ @password = ENV['passwd']
34
+ @chain = ENV['chain']
35
+ end
36
+
37
+
38
+ # Function to send transaction on RecordsKeeper Blockchain
39
+ def self.sendTransaction sender_address, receiveraddress, data, amount
40
+ data_hex = data.to_hex_string
41
+ datahex = data_hex.delete(' ')
42
+ auth = {:username => @user, :password => @password}
43
+ options = {
44
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
45
+ :basic_auth => auth,
46
+ :body => [ {"method":"createrawsendfrom","params":[sender_address, { receiveraddress => amount }, [datahex], "send"],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
47
+ }
48
+ response = HTTParty.get(@url, options)
49
+ out = response.parsed_response
50
+ check = out[0]['result']
51
+
52
+ if check.nil?
53
+ txid = out[0]['error']['message']
54
+ else
55
+ txid = out[0]['result']
56
+ end
57
+ return txid;
58
+ end
59
+
60
+ # Function to create transaction hex on RecordsKeeper Blockchain
61
+ def self.createRawTransaction sender_address, receiveraddress, data, amount
62
+ data_hex = data.to_hex_string
63
+ datahex = data_hex.delete(' ')
64
+ auth = {:username => @user, :password => @password}
65
+ options = {
66
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
67
+ :basic_auth => auth,
68
+ :body => [ {"method":"createrawsendfrom","params":[sender_address, {receiveraddress => amount}, [datahex], ''],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
69
+ }
70
+ response = HTTParty.get(@url, options)
71
+ out = response.parsed_response
72
+ check = out[0]['result']
73
+
74
+ if check.nil?
75
+ txhex = out[0]['error']['message']
76
+ else
77
+ txhex = out[0]['result']
78
+ end
79
+ return txhex;
80
+ end
81
+
82
+ # Function to sign transaction on RecordsKeeper Blockchain
83
+ def self.signRawTransaction txHex, private_key
84
+ priv_key = []
85
+ priv_key.push(private_key)
86
+ auth = {:username => @user, :password => @password}
87
+ options = {
88
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
89
+ :basic_auth => auth,
90
+ :body => [ {"method":"signrawtransaction","params":[txHex, [], priv_key],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
91
+ }
92
+ response = HTTParty.get(@url, options)
93
+ out = response.parsed_response
94
+ if out[0]['result']['complete']
95
+ signedHex = out[0]['result']['hex']
96
+ else
97
+ signedHex = "Transaction has not been signed properly."
98
+ end
99
+ return signedHex;
100
+ end
101
+
102
+ # Function to send raw transaction on RecordsKeeper Blockchain
103
+ def self.sendRawTransaction signed_txHex
104
+ auth = {:username => @user, :password => @password}
105
+ options = {
106
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
107
+ :basic_auth => auth,
108
+ :body => [ {"method":"sendrawtransaction","params":[signed_txHex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
109
+ }
110
+ response = HTTParty.get(@url, options)
111
+ out = response.parsed_response
112
+ txn = out[0]['result']
113
+ if txn.nil?
114
+ txid = out[0]['error']['message']
115
+ else
116
+ txid = txn
117
+ end
118
+ return txid;
119
+ end
120
+
121
+ # Function to send signed transaction on RecordsKeeper Blockchain
122
+ def self.sendSignedTransaction sender_address, receiveraddress, data, amount, private_key
123
+ data_hex = data.to_hex_string
124
+ datahex = data_hex.delete(' ')
125
+ def self.createRawTransaction sender_address, receiveraddress, amount, datahex
126
+ auth = {:username => @user, :password => @password}
127
+ options = {
128
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
129
+ :basic_auth => auth,
130
+ :body => [ {"method":"createrawsendfrom","params":[sender_address, {receiveraddress => amount}, [datahex], ""],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
131
+ }
132
+ response = HTTParty.get(@url, options)
133
+ out = response.parsed_response
134
+ return out[0]['result']
135
+ end
136
+ txHex = createRawTransaction sender_address, receiveraddress, Float(amount), datahex
137
+ def self.signRawTransaction txHex, private_key
138
+ auth = {:username => @user, :password => @password}
139
+ options = {
140
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
141
+ :basic_auth => auth,
142
+ :body => [ {"method":"signrawtransaction","params":[txHex, [], [private_key]],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
143
+ }
144
+ response = HTTParty.get(@url, options)
145
+ out = response.parsed_response
146
+ return out[0]['result']['hex']
147
+ end
148
+ signed_tx_hex = signRawTransaction txHex, private_key
149
+ def self.sendRawTransaction signed_tx_hex
150
+ auth = {:username => @user, :password => @password}
151
+ options = {
152
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
153
+ :basic_auth => auth,
154
+ :body => [ {"method":"sendrawtransaction","params":[signed_tx_hex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
155
+ }
156
+ response = HTTParty.get(@url, options)
157
+ out = response.parsed_response
158
+ return out[0]['result']
159
+ end
160
+ tx_id = sendRawTransaction signed_tx_hex
161
+ return tx_id;
162
+ end
163
+
164
+ # Function to retrieve transaction on RecordsKeeper Blockchain
165
+ def self.retrieveTransaction tx_id
166
+ auth = {:username => @user, :password => @password}
167
+ options = {
168
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
169
+ :basic_auth => auth,
170
+ :body => [ {"method":"getrawtransaction","params":[tx_id, 1],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
171
+ }
172
+ response = HTTParty.get(@url, options)
173
+ out = response.parsed_response
174
+ check = out[0]['result']
175
+
176
+ if check.nil?
177
+ retrievedinfo = out[0]['error']['message']
178
+ else
179
+ sent_hex_data = out[0]['result']['data'][0]
180
+ sent_data = sent_hex_data.to_byte_string
181
+ sent_amount = out[0]['result']['vout'][0]['value']
182
+ retrieve = {:sent_data => sent_data,:sent_amount => sent_amount }
183
+ retrievedinfo = JSON.generate retrieve
184
+ end
185
+ return retrievedinfo
186
+ end
187
+
188
+ # Function to calculate transaction's fee on RecordsKeeper Blockchain
189
+ def self.getFee address, tx_id
190
+ auth = {:username => @user, :password => @password}
191
+ options = {
192
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
193
+ :basic_auth => auth,
194
+ :body => [ {"method":"getaddresstransaction","params":[address, tx_id, true],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
195
+ }
196
+ response = HTTParty.get(@url, options)
197
+ out = response.parsed_response
198
+ check = out[0]['result']
199
+
200
+ if check.nil?
201
+ fees = out[0]['error']['message']
202
+ else
203
+ sent_amount = out[0]['result']['vout'][0]['amount']
204
+ balance_amount = out[0]['result']['balance']['amount']
205
+ fees = balance_amount.abs - sent_amount
206
+ end
207
+ return fees; #returns fees
208
+ end
209
+
210
+
211
+ end
212
+
213
+ end