RecordsKeeperRubyLib 0.1.0

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.
@@ -0,0 +1,89 @@
1
+ # Library to work with RecordsKeeper blocks.
2
+
3
+ # You can retrieve complete block information by using block class.
4
+ # You just have to pass parameters to invoke the pre-defined functions.
5
+
6
+ # Import the following libraries
7
+
8
+ require 'rubygems'
9
+ require 'httparty'
10
+ require 'json'
11
+ require 'binary_parser'
12
+ require 'yaml'
13
+
14
+ module RecordsKeeperRuby
15
+ class Block
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
+ def self.blockinfo block_height
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":"getblock","params":[block_height],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
44
+ }
45
+ response = HTTParty.get(@url, options)
46
+ out = response.parsed_response
47
+ tx_count_number = out[0]['result']['tx']
48
+ tx_count = tx_count_number.length # variable returns block's transaction count
49
+ miner = out[0]['result']['miner'] # variable returns block's miner
50
+ size = out[0]['result']['size'] # variable returns block's size
51
+ nonce = out[0]['result']['nonce'] # variable returns block's nonce
52
+ blockHash = out[0]['result']['hash'] # variable returns blockhash
53
+ prevblock = out[0]['result']['previousblockhash'] # variable returns prevblockhash
54
+ nextblock = out[0]['result']['nextblockhash'] # variable returns nextblockhash
55
+ merkleroot = out[0]['result']['merkleroot'] # variable returns merkleroot
56
+ blocktime = out[0]['result']['time'] # variable returns blocktime
57
+ difficulty = out[0]['result']['difficulty'] # variable returns difficulty
58
+ tx = [] # list to store transaction ids
59
+ for i in 0...tx_count
60
+ tx.push(out[0]['result']['tx'][i]) # pushes transaction ids onto tx list
61
+ end
62
+ return tx_count, tx, miner, size, nonce, blockHash, prevblock, nextblock, merkleroot, blocktime, difficulty;
63
+ end
64
+
65
+ def self.retrieveBlocks block_range
66
+ blockhash = []
67
+ miner = []
68
+ blocktime = []
69
+ tx_count = []
70
+ auth = {:username => @user, :password => @password}
71
+ options = {
72
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
73
+ :basic_auth => auth,
74
+ :body => [ {"method":"listblocks","params":[block_range],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
75
+ }
76
+ response = HTTParty.get(@url, options)
77
+ out = response.parsed_response
78
+ block_count_len = out[0]['result']
79
+ block_count = block_count_len.length
80
+ for i in 0...block_count
81
+ blockhash.push(out[0]['result'][i]['hash'])
82
+ miner.push(out[0]['result'][i]['miner'])
83
+ blocktime.push(out[0]['result'][i]['time'])
84
+ tx_count.push(out[0]['result'][i]['txcount'])
85
+ end
86
+ return blockhash, miner, blocktime, tx_count;
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,135 @@
1
+ # Library to work with RecordsKeeper Blockchain.
2
+
3
+ # You can retrieve blockchain information, node's information, node's balance, node's permissions, pending transaction details
4
+ # by using Blockchain class.
5
+ # You just have to pass 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
+
13
+ module RecordsKeeperRuby
14
+ class Blockchain
15
+
16
+ cfg = YAML::load(File.open('config.yaml','r'))
17
+ @network = cfg['testnet']
18
+ if @network==cfg['testnet']
19
+ @url = cfg['testnet']['url']
20
+ @user = cfg['testnet']['rkuser']
21
+ @password = cfg['testnet']['passwd']
22
+ @chain = cfg['testnet']['chain']
23
+ else
24
+ @url = cfg['mainnet']['url']
25
+ @user = cfg['mainnet']['rkuser']
26
+ @password = cfg['mainnet']['passwd']
27
+ @chain = cfg['mainnet']['chain']
28
+ end
29
+
30
+ def self.variable
31
+ net = @network
32
+ return net
33
+ end
34
+
35
+ # Function to retrieve RecordsKeeper Blockchain parameters
36
+ def self.getChainInfo
37
+ auth = {:username => @user, :password => @password}
38
+ options = {
39
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
40
+ :basic_auth => auth,
41
+ :body => [ {"method":"getblockchainparams","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
42
+ }
43
+ response = HTTParty.get(@url, options)
44
+ out = response.parsed_response
45
+ result = out[0]['result']
46
+ chain_protocol = result['chain-protocol']
47
+ chain_description = result['chain-description']
48
+ root_stream = result['root-stream-name']
49
+ max_blocksize = result['maximum-block-size']
50
+ default_networkport = result['default-network-port']
51
+ default_rpcport = result['default-rpc-port']
52
+ mining_diversity = result['mining-diversity']
53
+ chain_name = result['chain-name']
54
+ return chain_protocol, chain_description, root_stream, max_blocksize, default_networkport, default_rpcport, mining_diversity, chain_name; #returns chain parameters
55
+ end
56
+
57
+ # Function to retrieve node's information on RecordsKeeper Blockchain
58
+ def self.getNodeInfo
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":"getinfo","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
64
+ }
65
+ response = HTTParty.get(@url, options)
66
+ out = response.parsed_response
67
+ node_balance = out[0]['result']['balance']
68
+ synced_blocks = out[0]['result']['blocks']
69
+ node_address = out[0]['result']['nodeaddress']
70
+ difficulty = out[0]['result']['difficulty']
71
+ return node_balance, synced_blocks, node_address, difficulty; # Returns node's details
72
+ end
73
+
74
+ # Function to retrieve node's permissions on RecordsKeeper Blockchain
75
+ def self.permissions
76
+ auth = {:username => @user, :password => @password}
77
+ options = {
78
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
79
+ :basic_auth => auth,
80
+ :body => [ {"method":"listpermissions","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
81
+ }
82
+ response = HTTParty.get(@url, options)
83
+ out = response.parsed_response
84
+ pms_count_len = out[0]['result']
85
+ pms_count = pms_count_len.length
86
+ permissions = []
87
+ for i in 0...pms_count
88
+ permissions.push(out[0]['result'][i]['type'])
89
+ end
90
+ return permissions; # Returns list of permissions
91
+ end
92
+
93
+ # Function to retrieve pending transactions information on RecordsKeeper Blockchain
94
+ def self.getpendingTransactions
95
+ auth = {:username => @user, :password => @password}
96
+ options = {
97
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
98
+ :basic_auth => auth,
99
+ :body => [ {"method":"getmempoolinfo","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
100
+ }
101
+ response = HTTParty.get(@url, options)
102
+ out = response.parsed_response
103
+ tx_count = out[0]['result']['size'] # Stores pending tx count
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":"getrawmempool","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
109
+ }
110
+ response2 = HTTParty.get(@url, options)
111
+ out2 = response2.parsed_response
112
+ tx = []
113
+ x = 0
114
+ begin
115
+ tx.push(out2[0]['result'])
116
+ end until x <tx_count
117
+ return tx_count, tx; # Returns pending tx and tx count
118
+ end
119
+
120
+ # Function to check node's total balance
121
+ def self.checkNodeBalance
122
+ auth = {:username => @user, :password => @password}
123
+ options = {
124
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
125
+ :basic_auth => auth,
126
+ :body => [ {"method":"getmultibalances","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
127
+ }
128
+ response = HTTParty.get(@url, options)
129
+ out = response.parsed_response
130
+ balance = out[0]['result']['total'][0]['qty']
131
+ return balance; # Returns balance of node
132
+ end
133
+ end
134
+
135
+ end
@@ -0,0 +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
@@ -0,0 +1,60 @@
1
+ # config.yaml
2
+ testnet:
3
+ url: testurl
4
+ rkuser: rkuser
5
+ passwd: rkpwd
6
+ chain: test-chain
7
+ port: testport
8
+ stream: streamname
9
+ testdata: test_data
10
+ amount: test_amount
11
+ dumptxid: test_txid
12
+ privatekey: test_privkey
13
+ validaddress: valid_address
14
+ invalidaddress: invalid_address
15
+ miningaddress: mining_address
16
+ nonminingaddress: nonmining_address
17
+ multisigaddress: multisig_address
18
+ wrongimportaddress: wrongimport_address
19
+ mainaddress: main_address
20
+ permissionaddress: permission_address
21
+ dumptxhex: dump_txhex
22
+ dumpsignedtxhex: dump_signedtxhex
23
+ samplegetmultisigaddress: sample_multisigaddress
24
+ samplemerkleroot: sample_merkleroot
25
+ sampleblockhash: sample_blockhash
26
+ sampledata: sample_data
27
+ sampletransac: sample_transaction_id
28
+ transactionid: transaction_id
29
+ sampletxid: sample_txid
30
+
31
+
32
+
33
+ mainnet:
34
+ url: mainurl
35
+ rkuser: rkuser
36
+ passwd: rkpwd
37
+ chain: main-chain
38
+ port: mainport
39
+ stream: streamname
40
+ testdata: main_data
41
+ amount: main_amount
42
+ dumptxid: main_txid
43
+ privatekey: main_privkey
44
+ validaddress: valid_address
45
+ invalidaddress: invalid_address
46
+ miningaddress: mining_address
47
+ nonminingaddress: nonmining_address
48
+ multisigaddress: multisig_address
49
+ wrongimportaddress: wrongimport_address
50
+ mainaddress: main_address
51
+ permissionaddress: permission_address
52
+ dumptxhex: dump_txhex
53
+ dumpsignedtxhex: dump_signedtxhex
54
+ samplegetmultisigaddress: sample_multisigaddress
55
+ samplemerkleroot: sample_merkleroot
56
+ sampleblockhash: sample_blockhash
57
+ sampledata: sample_data
58
+ sampletransac: sample_transaction_id
59
+ transactionid: transaction_id
60
+ sampletxid: sample_txid
@@ -0,0 +1,169 @@
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 RecordsKeeperRuby
14
+
15
+ class Stream
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 publish data into the stream
39
+ def self.publish address, stream, key, data
40
+ datahex1 = data.to_hex_string
41
+ datahex = datahex1.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":"publishfrom","params":[address, stream, key, datahex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
47
+ }
48
+ response = HTTParty.get(@url, options)
49
+ out = response.parsed_response
50
+ txid = out[0]['result']
51
+ return txid;
52
+ end
53
+
54
+ # Function to retrieve data against transaction id from the stream
55
+ def self.retrieve stream, txid
56
+ auth = {:username => @user, :password => @password}
57
+ options = {
58
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
59
+ :basic_auth => auth,
60
+ :body => [ {"method":"getstreamitem","params":[stream, txid],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
61
+ }
62
+ response = HTTParty.get(@url, options)
63
+ out = response.parsed_response
64
+ data = out[0]['result']['data']
65
+ raw_data = data.to_byte_string
66
+ return raw_data;
67
+ end
68
+
69
+ # Function to retrieve data against a particular publisher address
70
+ def self.retrieveWithAddress stream, address, count
71
+ auth = {:username => @user, :password => @password}
72
+ options = {
73
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
74
+ :basic_auth => auth,
75
+ :body => [ {"method":"liststreampublisheritems","params":[stream, address, false, count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
76
+ }
77
+ response = HTTParty.get(@url, options)
78
+ out = response.parsed_response
79
+ key = []
80
+ raw_data = []
81
+ txid = []
82
+ i = 0
83
+ begin
84
+ key.push(out[0]['result'][i]['key']) #returns key value of the published data
85
+ data = out[0]['result'][i]['data'] #returns hex data
86
+ raw_data.push(data.to_byte_string) #returns raw data
87
+ txid.push(out[0]['result'][i]['txid']) #returns tx id
88
+ end until i <count
89
+ return key, raw_data, txid;
90
+ end
91
+
92
+ # Function to retrieve data against a particular key value
93
+ def self.retrieveWithKey stream, key, count
94
+ auth = {:username => @user, :password => @password}
95
+ options = {
96
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
97
+ :basic_auth => auth,
98
+ :body => [ {"method":"liststreamkeyitems","params":[stream, key, false, count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
99
+ }
100
+ response = HTTParty.get(@url, options)
101
+ out = response.parsed_response
102
+ publisher = []
103
+ raw_data = []
104
+ txid = []
105
+ i = 0
106
+ begin
107
+ publisher.push(out[0]['result'][i]['publishers'][0]) #returns publisher's address of published data
108
+ data = out[0]['result'][i]['data'] #returns published hex data
109
+ raw_data.push(data.to_byte_string) #returns data published
110
+ txid.push(out[0]['result'][i]['txid']) #returns transaction id of published data
111
+ end until i <count
112
+ return publisher, raw_data, txid;
113
+ end
114
+
115
+ # Function to verify data on RecordsKeeper Blockchain
116
+ def self.verifyData stream, data, count
117
+ auth = {:username => @user, :password => @password}
118
+ options = {
119
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
120
+ :basic_auth => auth,
121
+ :body => [ {"method":"liststreamitems","params":[stream,false ,count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
122
+ }
123
+ response = HTTParty.get(@url, options)
124
+ out = response.parsed_response
125
+ raw_data = []
126
+ i = 0
127
+ begin
128
+ result_data = out[0]['result'][i]['data'] # returns hex data
129
+ end until i <count
130
+ if result_data.unicode_normalized?
131
+ raw_data.push(result_data.to_byte_string) # returns raw data
132
+ else
133
+ raw_data.push("No data found")
134
+ end
135
+ if raw_data.include?data
136
+ result = "Data is successfully verified."
137
+ else
138
+ result = "Data not found."
139
+ end
140
+ return result;
141
+ end
142
+
143
+ # Function to list stream items on RecordsKeeper Blockchain
144
+ def self.retrieveItems stream, count
145
+ auth = {:username => @user, :password => @password}
146
+ options = {
147
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
148
+ :basic_auth => auth,
149
+ :body => [ {"method":"liststreamitems","params":[stream, false ,count],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
150
+ }
151
+ response = HTTParty.get(@url, options)
152
+ out = response.parsed_response
153
+ address =[]
154
+ key_value = []
155
+ raw_data = []
156
+ txid = []
157
+ i = 0
158
+ begin
159
+ address.push(out[0]['result'][i]['publishers']) # returns publisher address
160
+ key_value.push(out[0]['result'][i]['key']) # returns key value of data
161
+ data = out[0]['result'][i]['data'] # returns hex data
162
+ raw_data.push(data.to_byte_string) # returns raw data
163
+ txid.push(out[0]['result'][i]['txid']) # returns tx id
164
+ end until i <count
165
+ return address, key_value, raw_data, txid;
166
+ end
167
+ end
168
+
169
+ end