RecordsKeeperRubyLib 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,213 +1,178 @@
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
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 Block class resources.
19
+ # Import values from config file.
20
+ cfg = YAML::load(File.open('config.yaml','r'))
21
+ @network = cfg['network']
22
+ @url = cfg['network']['url']
23
+ @user = cfg['network']['rkuser']
24
+ @password = cfg['network']['passwd']
25
+ @chain = cfg['network']['chain']
26
+
27
+ def self.variable
28
+ net = @network
29
+ return net
30
+ end
31
+
32
+ # Function to send transaction on RecordsKeeper Blockchain
33
+ def self.sendTransaction sender_address, receiveraddress, data, amount
34
+ data_hex = data.to_hex_string
35
+ datahex = data_hex.delete(' ')
36
+ auth = {:username => @user, :password => @password}
37
+ options = {
38
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
39
+ :basic_auth => auth,
40
+ :body => [ {"method":"createrawsendfrom","params":[sender_address, { receiveraddress => amount }, [datahex], "send"],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
41
+ }
42
+ response = HTTParty.get(@url, options)
43
+ out = response.parsed_response
44
+ txid = out[0]['result']
45
+ return txid;
46
+ end
47
+
48
+ # Function to create transaction hex on RecordsKeeper Blockchain
49
+ def self.createRawTransaction sender_address, receiveraddress, data, amount
50
+ data_hex = data.to_hex_string
51
+ datahex = data_hex.delete(' ')
52
+ auth = {:username => @user, :password => @password}
53
+ options = {
54
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
55
+ :basic_auth => auth,
56
+ :body => [ {"method":"createrawsendfrom","params":[sender_address, {receiveraddress => amount}, [datahex], ''],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
57
+ }
58
+ response = HTTParty.get(@url, options)
59
+ out = response.parsed_response
60
+ txhex = out[0]['result']
61
+ return txhex;
62
+ end
63
+
64
+ # Function to sign transaction on RecordsKeeper Blockchain
65
+ def self.signRawTransaction txHex, private_key
66
+ priv_key = []
67
+ priv_key.push(private_key)
68
+ auth = {:username => @user, :password => @password}
69
+ options = {
70
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
71
+ :basic_auth => auth,
72
+ :body => [ {"method":"signrawtransaction","params":[txHex, [], priv_key],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
73
+ }
74
+ response = HTTParty.get(@url, options)
75
+ out = response.parsed_response
76
+ signedHex = out[0]['result']['hex']
77
+ return signedHex;
78
+ end
79
+
80
+ # Function to send raw transaction on RecordsKeeper Blockchain
81
+ def self.sendRawTransaction signed_txHex
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":"sendrawtransaction","params":[signed_txHex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
87
+ }
88
+ response = HTTParty.get(@url, options)
89
+ out = response.parsed_response
90
+ txn = out[0]['result']
91
+ if txn.nil?
92
+ txid = out[0]['error']['message']
93
+ else
94
+ txid = txn
95
+ end
96
+ return txid;
97
+ end
98
+
99
+ # Function to send signed transaction on RecordsKeeper Blockchain
100
+ def self.sendSignedTransaction sender_address, receiveraddress, data, amount, private_key
101
+ data_hex = data.to_hex_string
102
+ datahex = data_hex.delete(' ')
103
+ def self.createRawTransaction sender_address, receiveraddress, amount, datahex
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":"createrawsendfrom","params":[sender_address, {receiveraddress => amount}, [datahex], ""],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
109
+ }
110
+ response = HTTParty.get(@url, options)
111
+ out = response.parsed_response
112
+ return out[0]['result']
113
+ end
114
+ txHex = createRawTransaction sender_address, receiveraddress, Float(amount), datahex
115
+ def self.signRawTransaction txHex, private_key
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":"signrawtransaction","params":[txHex, [], [private_key]],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
121
+ }
122
+ response = HTTParty.get(@url, options)
123
+ out = response.parsed_response
124
+ return out[0]['result']['hex']
125
+ end
126
+ signed_tx_hex = signRawTransaction txHex, private_key
127
+ def self.sendRawTransaction signed_tx_hex
128
+ auth = {:username => @user, :password => @password}
129
+ options = {
130
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
131
+ :basic_auth => auth,
132
+ :body => [ {"method":"sendrawtransaction","params":[signed_tx_hex],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
133
+ }
134
+ response = HTTParty.get(@url, options)
135
+ out = response.parsed_response
136
+ return out[0]['result']
137
+ end
138
+ tx_id = sendRawTransaction signed_tx_hex
139
+ return tx_id;
140
+ end
141
+
142
+ # Function to retrieve transaction on RecordsKeeper Blockchain
143
+ def self.retrieveTransaction tx_id
144
+ auth = {:username => @user, :password => @password}
145
+ options = {
146
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
147
+ :basic_auth => auth,
148
+ :body => [ {"method":"getrawtransaction","params":[tx_id, 1],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
149
+ }
150
+ response = HTTParty.get(@url, options)
151
+ out = response.parsed_response
152
+ sent_hex_data = out[0]['result']['data'][0]
153
+ sent_data = sent_hex_data.to_byte_string
154
+ sent_amount = out[0]['result']['vout'][0]['value']
155
+ retrieve = {:sent_data => sent_data,:sent_amount => sent_amount }
156
+ retrievedinfo = JSON.generate retrieve
157
+ return retrievedinfo
158
+ end
159
+
160
+ # Function to calculate transaction's fee on RecordsKeeper Blockchain
161
+ def self.getFee address, tx_id
162
+ auth = {:username => @user, :password => @password}
163
+ options = {
164
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
165
+ :basic_auth => auth,
166
+ :body => [ {"method":"getaddresstransaction","params":[address, tx_id, true],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
167
+ }
168
+ response = HTTParty.get(@url, options)
169
+ out = response.parsed_response
170
+ sent_amount = out[0]['result']['vout'][0]['amount']
171
+ balance_amount = out[0]['result']['balance']['amount']
172
+ fees = balance_amount.abs - sent_amount
173
+ return fees; #returns fees
174
+ end
175
+
176
+ end
177
+
178
+ end
@@ -1,3 +1,3 @@
1
1
  module RecordsKeeperRubyLib
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,263 +1,247 @@
1
- # Library to work with RecordsKeeper wallet.
2
-
3
- # You can create wallet, create multisignature wallet, retrieve wallet's information, retrieve private key of a particular
4
- # wallet address, sign message verify message, dump wallet file, backup wallet file, import wallet file, encrypt wallet by
5
- # using wallet class. 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
- require 'hex_string'
13
-
14
- module RecordsKeeperRubyLib
15
- class Wallet
16
-
17
- # # Entry point for accessing Wallet 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 create wallet on RecordsKeeper Blockchain
37
- def self.createWallet
38
- auth = {:username => @user, :password => @password}
39
- options = {
40
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
41
- :basic_auth => auth,
42
- :body => [ {"method":"createkeypairs","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
43
- }
44
- response = HTTParty.get(@url, options)
45
- out = response.parsed_response
46
- public_address = out[0]['result'][0]['address'] # returns public address of the wallet
47
- private_key = out[0]['result'][0]['privkey'] # returns private key of the wallet
48
- public_key = out[0]['result'][0]['pubkey'] # returns public key of the wallet
49
-
50
- def self.importAddress public_address
51
- auth = {:username => @user, :password => @password}
52
- options = {
53
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
54
- :basic_auth => auth,
55
- :body => [ {"method":"importaddress","params":[public_address, " ", false],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
56
- }
57
- response = HTTParty.get(@url, options)
58
- out = response.parsed_response
59
- result = out[0]['result']
60
- return result;
61
- end
62
- import_address = importAddress public_address
63
- retrieve = {:public_address => public_address,:private_key => private_key,:public_key => public_key}
64
- retrievedinfo = JSON.generate retrieve
65
- return retrievedinfo
66
- end
67
-
68
- # Function to retrieve private key of a wallet address on RecordsKeeper Blockchain
69
- def self.getPrivateKey public_address
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":"dumpprivkey","params":[public_address],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
75
- }
76
- response = HTTParty.get(@url, options)
77
- out = response.parsed_response
78
- result = out[0]['result']
79
- if result.nil?
80
- private_key = out[0]['error']['message']
81
- else
82
- private_key = out[0]['result']
83
- end
84
- return private_key; #returns private key
85
- end
86
-
87
- # Function to retrieve wallet's information on RecordsKeeper Blockchain
88
- def self.retrieveWalletinfo
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":"getwalletinfo","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
94
- }
95
- response = HTTParty.get(@url, options)
96
- out = response.parsed_response
97
- balance = out[0]['result']['balance']
98
- tx_count = out[0]['result']['txcount']
99
- unspent_tx = out[0]['result']['utxocount']
100
- retrieve = {:balance => balance,:tx_count => tx_count,:unspent_tx => unspent_tx}
101
- retrievedinfo = JSON.generate retrieve
102
- return retrievedinfo
103
- end
104
-
105
- # Function to create wallet's backup on RecordsKeeper Blockchain
106
- def self.backupWallet filename
107
- auth = {:username => @user, :password => @password}
108
- options = {
109
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
110
- :basic_auth => auth,
111
- :body => [ {"method":"backupwallet","params":[filename],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
112
- }
113
- response = HTTParty.get(@url, options)
114
- out = response.parsed_response
115
- result = out[0]['result']
116
- if result.nil?
117
- res = "Backup successful!"
118
- else
119
- res = out[0]['error']['message']
120
- end
121
- return res; #returns result
122
- end
123
-
124
- # Function to import wallet's backup on RecordsKeeper Blockchain
125
- def self.importWallet filename
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":"importwallet","params":[filename],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
131
- }
132
- response = HTTParty.get(@url, options)
133
- out = response.parsed_response
134
- result = out[0]['result']
135
- if result.nil?
136
- res = "Wallet is successfully imported"
137
- else
138
- res = out[0]['error']['message']
139
- end
140
- return res; #returns result
141
- end
142
-
143
- # Function to dump wallet on RecordsKeeper Blockchain
144
- def self.dumpWallet filename
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":"dumpwallet","params":[filename],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
150
- }
151
- response = HTTParty.get(@url, options)
152
- out = response.parsed_response
153
- result = out[0]['result']
154
- if result.nil?
155
- res = "Wallet is successfully dumped"
156
- else
157
- res = out[0]['error']['message']
158
- end
159
- return res; #returns result
160
- end
161
-
162
- # Function to lock wallet on RecordsKeeper Blockchain
163
- def self.lockWallet password
164
- auth = {:username => @user, :password => @password}
165
- options = {
166
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
167
- :basic_auth => auth,
168
- :body => [ {"method":"encryptwallet","params":[password],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
169
- }
170
- response = HTTParty.get(@url, options)
171
- out = response.parsed_response
172
- result = out[0]['result']
173
- if result.nil?
174
- res = "Wallet is successfully encrypted."
175
- else
176
- res = out[0]['error']['message']
177
- end
178
- return res; #returns result
179
- end
180
-
181
- # Function to unlock wallet on RecordsKeeper Blockchain
182
- def self.unlockWallet password, unlocktime
183
- auth = {:username => @user, :password => @password}
184
- options = {
185
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
186
- :basic_auth => auth,
187
- :body => [ {"method":"walletpassphrase","params":[password, unlocktime],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
188
- }
189
- response = HTTParty.get(@url, options)
190
- out = response.parsed_response
191
- result = out[0]['result']
192
- if result.nil?
193
- res = "Wallet is successfully unlocked."
194
- else
195
- res = out[0]['error']['message']
196
- end
197
- return res; #returns result
198
- end
199
-
200
- # Function to change password for wallet on RecordsKeeper Blockchain
201
- def self.changeWalletPassword old_password, new_password
202
- auth = {:username => @user, :password => @password}
203
- options = {
204
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
205
- :basic_auth => auth,
206
- :body => [ {"method":"walletpassphrasechange","params":[old_password, new_password],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
207
- }
208
- response = HTTParty.get(@url, options)
209
- out = response.parsed_response
210
- result = out[0]['result']
211
- if result.nil?
212
- res = "Password successfully changed!"
213
- else
214
- res = out[0]['error']['message']
215
- end
216
- return res; #returns result
217
- end
218
-
219
- # Function to sign message on RecordsKeeper Blockchain
220
- def self.signMessage private_key, message
221
- auth = {:username => @user, :password => @password}
222
- options = {
223
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
224
- :basic_auth => auth,
225
- :body => [ {"method":"signmessage","params":[private_key, message],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
226
- }
227
- response = HTTParty.get(@url, options)
228
- out = response.parsed_response
229
- check = out[0]['result']
230
-
231
- if check.nil?
232
- signedMessage = out[0]['error']['message']
233
- else
234
- signedMessage = out[0]['result']
235
- end
236
- return signedMessage; #returns private key
237
- end
238
-
239
- # Function to verify message on RecordsKeeper Blockchain
240
- def self.verifyMessage address, signedMessage, message
241
- auth = {:username => @user, :password => @password}
242
- options = {
243
- :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
244
- :basic_auth => auth,
245
- :body => [ {"method":"verifymessage","params":[address, signedMessage, message],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
246
- }
247
- response = HTTParty.get(@url, options)
248
- out = response.parsed_response
249
- verifiedMessage = out[0]['result']
250
- error = out[0]['error']
251
- if verifiedMessage
252
- validity = "Yes, message is verified"
253
- elsif error.nil?
254
- validity = "No, signedMessage is not correct"
255
- else
256
- validity = error['message']
257
- end
258
- return validity; #returns validity
259
- end
260
- end
261
-
262
- end
263
-
1
+ # Library to work with RecordsKeeper wallet.
2
+
3
+ # You can create wallet, create multisignature wallet, retrieve wallet's information, retrieve private key of a particular
4
+ # wallet address, sign message verify message, dump wallet file, backup wallet file, import wallet file, encrypt wallet by
5
+ # using wallet class. 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
+ require 'hex_string'
13
+
14
+ module RecordsKeeperRubyLib
15
+ class Wallet
16
+ # Entry point for accessing Block class resources.
17
+ # Import values from config file.
18
+ cfg = YAML::load(File.open('config.yaml','r'))
19
+ @network = cfg['network']
20
+ @url = cfg['network']['url']
21
+ @user = cfg['network']['rkuser']
22
+ @password = cfg['network']['passwd']
23
+ @chain = cfg['network']['chain']
24
+
25
+ def self.variable
26
+ net = @network
27
+ return net
28
+ end
29
+
30
+ # Function to create wallet on RecordsKeeper Blockchain
31
+ def self.createWallet
32
+ auth = {:username => @user, :password => @password}
33
+ options = {
34
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
35
+ :basic_auth => auth,
36
+ :body => [ {"method":"createkeypairs","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
37
+ }
38
+ response = HTTParty.get(@url, options)
39
+ out = response.parsed_response
40
+ public_address = out[0]['result'][0]['address'] # returns public address of the wallet
41
+ private_key = out[0]['result'][0]['privkey'] # returns private key of the wallet
42
+ public_key = out[0]['result'][0]['pubkey'] # returns public key of the wallet
43
+
44
+ def self.importAddress public_address
45
+ auth = {:username => @user, :password => @password}
46
+ options = {
47
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
48
+ :basic_auth => auth,
49
+ :body => [ {"method":"importaddress","params":[public_address, " ", false],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
50
+ }
51
+ response = HTTParty.get(@url, options)
52
+ out = response.parsed_response
53
+ result = out[0]['result']
54
+ return result;
55
+ end
56
+ import_address = importAddress public_address
57
+ retrieve = {:public_address => public_address,:private_key => private_key,:public_key => public_key}
58
+ retrievedinfo = JSON.generate retrieve
59
+ return retrievedinfo
60
+ end
61
+
62
+ # Function to retrieve private key of a wallet on RecordsKeeper Blockchain
63
+ def self.getPrivateKey public_address
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":"dumpprivkey","params":[public_address],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
69
+ }
70
+ response = HTTParty.get(@url, options)
71
+ out = response.parsed_response
72
+ result = out[0]['result']
73
+ if result.nil?
74
+ private_key = out[0]['error']['message']
75
+ else
76
+ private_key = out[0]['result']
77
+ end
78
+ return private_key; #returns private key
79
+ end
80
+
81
+ # Function to retrieve wallet's information on RecordsKeeper Blockchain
82
+ def self.retrieveWalletinfo
83
+ auth = {:username => @user, :password => @password}
84
+ options = {
85
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
86
+ :basic_auth => auth,
87
+ :body => [ {"method":"getwalletinfo","params":[],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
88
+ }
89
+ response = HTTParty.get(@url, options)
90
+ out = response.parsed_response
91
+ balance = out[0]['result']['balance']
92
+ tx_count = out[0]['result']['txcount']
93
+ unspent_tx = out[0]['result']['utxocount']
94
+ retrieve = {:balance => balance,:tx_count => tx_count,:unspent_tx => unspent_tx}
95
+ retrievedinfo = JSON.generate retrieve
96
+ return retrievedinfo
97
+ end
98
+
99
+ # Function to create wallet's backup on RecordsKeeper Blockchain
100
+ def self.backupWallet filename
101
+ auth = {:username => @user, :password => @password}
102
+ options = {
103
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
104
+ :basic_auth => auth,
105
+ :body => [ {"method":"backupwallet","params":[filename],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
106
+ }
107
+ response = HTTParty.get(@url, options)
108
+ out = response.parsed_response
109
+ result = out[0]['result']
110
+ if result.nil?
111
+ res = "Backup successful!"
112
+ else
113
+ res = out[0]['error']['message']
114
+ end
115
+ return res; #returns result
116
+ end
117
+
118
+ # Function to import wallet's backup on RecordsKeeper Blockchain
119
+ def self.importWallet filename
120
+ auth = {:username => @user, :password => @password}
121
+ options = {
122
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
123
+ :basic_auth => auth,
124
+ :body => [ {"method":"importwallet","params":[filename],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
125
+ }
126
+ response = HTTParty.get(@url, options)
127
+ out = response.parsed_response
128
+ result = out[0]['result']
129
+ if result.nil?
130
+ res = "Wallet is successfully imported"
131
+ else
132
+ res = out[0]['error']['message']
133
+ end
134
+ return res; #returns result
135
+ end
136
+
137
+ # Function to dump wallet on RecordsKeeper Blockchain
138
+ def self.dumpWallet filename
139
+ auth = {:username => @user, :password => @password}
140
+ options = {
141
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
142
+ :basic_auth => auth,
143
+ :body => [ {"method":"dumpwallet","params":[filename],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
144
+ }
145
+ response = HTTParty.get(@url, options)
146
+ out = response.parsed_response
147
+ result = out[0]['result']
148
+ if result.nil?
149
+ res = "Wallet is successfully dumped"
150
+ else
151
+ res = out[0]['error']['message']
152
+ end
153
+ return res; #returns result
154
+ end
155
+
156
+ # Function to lock wallet on RecordsKeeper Blockchain
157
+ def self.lockWallet password
158
+ auth = {:username => @user, :password => @password}
159
+ options = {
160
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
161
+ :basic_auth => auth,
162
+ :body => [ {"method":"encryptwallet","params":[password],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
163
+ }
164
+ response = HTTParty.get(@url, options)
165
+ out = response.parsed_response
166
+ result = out[0]['result']
167
+ if result.nil?
168
+ res = "Wallet is successfully encrypted."
169
+ else
170
+ res = out[0]['error']['message']
171
+ end
172
+ return res; #returns result
173
+ end
174
+
175
+ # Function to unlock wallet on RecordsKeeper Blockchain
176
+ def self.unlockWallet password, unlocktime
177
+ auth = {:username => @user, :password => @password}
178
+ options = {
179
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
180
+ :basic_auth => auth,
181
+ :body => [ {"method":"walletpassphrase","params":[password, unlocktime],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
182
+ }
183
+ response = HTTParty.get(@url, options)
184
+ out = response.parsed_response
185
+ result = out[0]['result']
186
+ if result.nil?
187
+ res = "Wallet is successfully unlocked."
188
+ else
189
+ res = out[0]['error']['message']
190
+ end
191
+ return res; #returns result
192
+ end
193
+
194
+ # Function to change password for wallet on RecordsKeeper Blockchain
195
+ def self.changeWalletPassword old_password, new_password
196
+ auth = {:username => @user, :password => @password}
197
+ options = {
198
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
199
+ :basic_auth => auth,
200
+ :body => [ {"method":"walletpassphrasechange","params":[old_password, new_password],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
201
+ }
202
+ response = HTTParty.get(@url, options)
203
+ out = response.parsed_response
204
+ result = out[0]['result']
205
+ if result.nil?
206
+ res = "Password successfully changed!"
207
+ else
208
+ res = out[0]['error']['message']
209
+ end
210
+ return res; #returns result
211
+ end
212
+
213
+ # Function to sign message on RecordsKeeper Blockchain
214
+ def self.signMessage private_key, message
215
+ auth = {:username => @user, :password => @password}
216
+ options = {
217
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
218
+ :basic_auth => auth,
219
+ :body => [ {"method":"signmessage","params":[private_key, message],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
220
+ }
221
+ response = HTTParty.get(@url, options)
222
+ out = response.parsed_response
223
+ signedMessage = out[0]['result']
224
+ return signedMessage; #returns private key
225
+ end
226
+
227
+ # Function to verify message on RecordsKeeper Blockchain
228
+ def self.verifyMessage address, signedMessage, message
229
+ auth = {:username => @user, :password => @password}
230
+ options = {
231
+ :headers => headers= {"Content-Type"=> "application/json","Cache-Control" => "no-cache"},
232
+ :basic_auth => auth,
233
+ :body => [ {"method":"verifymessage","params":[address, signedMessage, message],"jsonrpc":2.0,"id":"curltext","chain_name":@chain}].to_json
234
+ }
235
+ response = HTTParty.get(@url, options)
236
+ out = response.parsed_response
237
+ verifiedMessage = out[0]['result']
238
+ if verifiedMessage
239
+ validity = "Yes, message is verified"
240
+ else
241
+ validity = "No, signedMessage is not correct"
242
+ end
243
+ return validity; #returns validity
244
+ end
245
+ end
246
+
247
+ end