blockchain 1.0.2 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,298 +1,298 @@
1
- require 'json'
2
- require_relative 'util'
3
-
4
- module Blockchain
5
-
6
- def self.get_block(block_id, api_code = nil)
7
- params = api_code.nil? ? { } : { 'api_code' => api_code }
8
- resource = 'rawblock/' + block_id
9
- response = Blockchain::call_api(resource, method: 'get', data: params)
10
- return Block.new(JSON.parse(response))
11
- end
12
-
13
- def self.get_tx(tx_id, api_code = nil)
14
- params = api_code.nil? ? { } : { 'api_code' => api_code }
15
- resource = 'rawtx/' + tx_id
16
- response = Blockchain::call_api(resource, method: 'get', data: params)
17
- return Transaction.new(JSON.parse(response))
18
- end
19
-
20
- def self.get_block_height(height, api_code = nil)
21
- params = { 'format' => 'json' }
22
- if !api_code.nil? then params['api_code'] = api_code end
23
- resource = "block-height/#{height}"
24
- response = Blockchain::call_api(resource, method: 'get', data: params)
25
- return JSON.parse(response)['blocks'].map{ |b| Block.new(b) }
26
- end
27
-
28
- def self.get_address(address, api_code = nil)
29
- params = api_code.nil? ? { } : { 'api_code' => api_code }
30
- resource = 'rawaddr/' + address
31
- response = Blockchain::call_api(resource, method: 'get', data: params)
32
- return Address.new(JSON.parse(response))
33
- end
34
-
35
- def self.get_unspent_outputs(address, api_code = nil)
36
- params = { 'active' => address }
37
- if !api_code.nil? then params['api_code'] = api_code end
38
- resource = 'unspent'
39
- response = Blockchain::call_api(resource, method: 'get', data: params)
40
- return JSON.parse(response)['unspent_outputs'].map{ |o| UnspentOutput.new(o) }
41
- end
42
-
43
- def self.get_unconfirmed_tx(api_code = nil)
44
- params = { 'format' => 'json' }
45
- if !api_code.nil? then params['api_code'] = api_code end
46
- resource = 'unconfirmed-transactions'
47
- response = Blockchain::call_api(resource, method: 'get', data: params)
48
- return JSON.parse(response)['txs'].map{ |t| Transaction.new(t) }
49
- end
50
-
51
- def self.get_blocks(api_code = nil, time: nil, pool_name: nil)
52
- params = { 'format' => 'json' }
53
- if !api_code.nil? then params['api_code'] = api_code end
54
- resource = "blocks/"
55
- if !time.nil?
56
- resource += time.to_s
57
- elsif !pool_name.nil?
58
- resource += pool_name
59
- end
60
- response = Blockchain::call_api(resource, method: 'get', data: params)
61
- return JSON.parse(response)['blocks'].map{ |b| SimpleBlock.new(b) }
62
- end
63
-
64
- def self.get_latest_block(api_code = nil)
65
- params = {}
66
- if !api_code.nil? then params['api_code'] = api_code end
67
- resource = 'latestblock'
68
- response = Blockchain::call_api(resource, method: 'get', data: params)
69
- return LatestBlock.new(JSON.parse(response))
70
- end
71
-
72
- def self.get_inventory_data(hash, api_code = nil)
73
- params = { 'format' => 'json' }
74
- if !api_code.nil? then params['api_code'] = api_code end
75
- resource = "inv/#{hash}"
76
- response = Blockchain::call_api(resource, method: 'get', data: params)
77
- return InventoryData.new(JSON.parse(response))
78
- end
79
-
80
- class SimpleBlock
81
- attr_reader :height
82
- attr_reader :hash
83
- attr_reader :time
84
- attr_reader :main_chain
85
-
86
- def initialize(b)
87
- @height = b['height']
88
- @hash = b['hash']
89
- @time = b['time']
90
- @main_chain = b['main_chain']
91
- end
92
- end
93
-
94
- class LatestBlock
95
- attr_reader :hash
96
- attr_reader :time
97
- attr_reader :block_index
98
- attr_reader :height
99
- attr_reader :tx_indexes
100
-
101
- def initialize(b)
102
- @hash = b['hash']
103
- @time = b['time']
104
- @block_index = b['block_index']
105
- @height = b['height']
106
- @tx_indexes = b['txIndexes']
107
- end
108
- end
109
-
110
- class UnspentOutput
111
- attr_reader :tx_hash
112
- attr_reader :tx_index
113
- attr_reader :tx_output_n
114
- attr_reader :script
115
- attr_reader :value
116
- attr_reader :value_hex
117
- attr_reader :confirmations
118
-
119
- def initialize(o)
120
- @tx_hash = o['tx_hash']
121
- @tx_index = o['tx_index']
122
- @tx_output_n = o['tx_output_n']
123
- @script = o['script']
124
- @value = o['value']
125
- @value_hex = o['value_hex']
126
- @confirmations = o['confirmations']
127
- end
128
- end
129
-
130
- class Address
131
- attr_reader :hash160
132
- attr_reader :address
133
- attr_reader :n_tx
134
- attr_reader :total_received
135
- attr_reader :total_sent
136
- attr_reader :final_balance
137
- attr_reader :transactions
138
-
139
- def initialize(a)
140
- @hash160 = a['hash160']
141
- @address = a['address']
142
- @n_tx = a['n_tx']
143
- @total_received = a['total_received']
144
- @total_sent = a['total_sent']
145
- @final_balance = a['final_balance']
146
- @transactions = a['txs'].map{ |tx| Transaction.new(tx) }
147
- end
148
- end
149
-
150
- class Input
151
- attr_reader :n
152
- attr_reader :value
153
- attr_reader :address
154
- attr_reader :tx_index
155
- attr_reader :type
156
- attr_reader :script
157
- attr_reader :script_sig
158
- attr_reader :sequence
159
-
160
- def initialize(i)
161
- obj = i['prev_out']
162
- if !obj.nil?
163
- # regular TX
164
- @n = obj['n']
165
- @value = obj['value']
166
- @address = obj['addr']
167
- @tx_index = obj['tx_index']
168
- @type = obj['type']
169
- @script = obj['script']
170
- @script_sig = i['script']
171
- @sequence = i['sequence']
172
- else
173
- # coinbase TX
174
- @script_sig = i['script']
175
- @sequence = i['sequence']
176
- end
177
- end
178
- end
179
-
180
- class Output
181
- attr_reader :n
182
- attr_reader :value
183
- attr_reader :address
184
- attr_reader :tx_index
185
- attr_reader :script
186
- attr_reader :spent
187
-
188
- def initialize(o)
189
- @n = o['n']
190
- @value = o['value']
191
- @address = o['addr']
192
- @tx_index = o['tx_index']
193
- @script = o['script']
194
- @spent = o['spent']
195
- end
196
- end
197
-
198
- class Transaction
199
- attr_reader :double_spend
200
- attr_reader :block_height
201
- attr_reader :time
202
- attr_reader :relayed_by
203
- attr_reader :hash
204
- attr_reader :tx_index
205
- attr_reader :version
206
- attr_reader :size
207
- attr_reader :inputs
208
- attr_reader :outputs
209
-
210
- def initialize(t)
211
- @double_spend = t.fetch('double_spend', false)
212
- @block_height = t.fetch('block_height', false)
213
- @time = t['time']
214
- @relayed_by = t['relayed_by']
215
- @hash = t['hash']
216
- @tx_index = t['tx_index']
217
- @version = t['ver']
218
- @size = t['size']
219
- @inputs = t['inputs'].map{ |i| Input.new(i) }
220
- @outputs = t['out'].map{ |o| Output.new(o) }
221
-
222
- if @block_height.nil?
223
- @block_height = -1
224
- end
225
- end
226
-
227
- def adjust_block_height(h)
228
- @block_height = h
229
- end
230
- end
231
-
232
-
233
- class Block
234
- attr_reader :hash
235
- attr_reader :version
236
- attr_reader :previous_block
237
- attr_reader :merkle_root
238
- attr_reader :time
239
- attr_reader :bits
240
- attr_reader :fee
241
- attr_reader :nonce
242
- attr_reader :n_tx
243
- attr_reader :size
244
- attr_reader :block_index
245
- attr_reader :main_chain
246
- attr_reader :height
247
- attr_reader :received_time
248
- attr_reader :relayed_by
249
- attr_reader :transactions
250
-
251
- def initialize(b)
252
- @hash = b['hash']
253
- @version = b['ver']
254
- @previous_block = b['prev_block']
255
- @merkle_root = b['mrkl_root']
256
- @time = b['time']
257
- @bits = b['bits']
258
- @fee = b['fee']
259
- @nonce = b['nonce']
260
- @n_tx = b['n_tx']
261
- @size = b['size']
262
- @block_index = b['block_index']
263
- @main_chain = b['main_chain']
264
- @height = b['height']
265
- @received_time = b['received_time']
266
- @relayed_by = b['relayed_by']
267
- @transactions = b['tx'].map{ |tx| Transaction.new(tx) }
268
- @transactions.each do |tx|
269
- tx.adjust_block_height(@height)
270
- end
271
-
272
- if @received_time.nil?
273
- @received_time = @time
274
- end
275
- end
276
- end
277
-
278
- class InventoryData
279
- attr_reader :hash
280
- attr_reader :type
281
- attr_reader :initial_time
282
- attr_reader :initial_ip
283
- attr_reader :nconnected
284
- attr_reader :relayed_count
285
- attr_reader :relayed_percent
286
-
287
- def initialize(i)
288
- @hash = i['hash']
289
- @type = i['type']
290
- @initial_time = i['initial_time'].to_i
291
- @initial_ip = i['initial_ip']
292
- @nconnected = i['nconnected'].to_i
293
- @relayed_count = i['relayed_count'].to_i
294
- @relayed_percent = i['relayed_percent'].to_i
295
- end
296
- end
297
-
1
+ require 'json'
2
+ require_relative 'util'
3
+
4
+ module Blockchain
5
+
6
+ def self.get_block(block_id, api_code = nil)
7
+ params = api_code.nil? ? { } : { 'api_code' => api_code }
8
+ resource = 'rawblock/' + block_id
9
+ response = Blockchain::call_api(resource, method: 'get', data: params)
10
+ return Block.new(JSON.parse(response))
11
+ end
12
+
13
+ def self.get_tx(tx_id, api_code = nil)
14
+ params = api_code.nil? ? { } : { 'api_code' => api_code }
15
+ resource = 'rawtx/' + tx_id
16
+ response = Blockchain::call_api(resource, method: 'get', data: params)
17
+ return Transaction.new(JSON.parse(response))
18
+ end
19
+
20
+ def self.get_block_height(height, api_code = nil)
21
+ params = { 'format' => 'json' }
22
+ if !api_code.nil? then params['api_code'] = api_code end
23
+ resource = "block-height/#{height}"
24
+ response = Blockchain::call_api(resource, method: 'get', data: params)
25
+ return JSON.parse(response)['blocks'].map{ |b| Block.new(b) }
26
+ end
27
+
28
+ def self.get_address(address, api_code = nil)
29
+ params = api_code.nil? ? { } : { 'api_code' => api_code }
30
+ resource = 'rawaddr/' + address
31
+ response = Blockchain::call_api(resource, method: 'get', data: params)
32
+ return Address.new(JSON.parse(response))
33
+ end
34
+
35
+ def self.get_unspent_outputs(address, api_code = nil)
36
+ params = { 'active' => address }
37
+ if !api_code.nil? then params['api_code'] = api_code end
38
+ resource = 'unspent'
39
+ response = Blockchain::call_api(resource, method: 'get', data: params)
40
+ return JSON.parse(response)['unspent_outputs'].map{ |o| UnspentOutput.new(o) }
41
+ end
42
+
43
+ def self.get_unconfirmed_tx(api_code = nil)
44
+ params = { 'format' => 'json' }
45
+ if !api_code.nil? then params['api_code'] = api_code end
46
+ resource = 'unconfirmed-transactions'
47
+ response = Blockchain::call_api(resource, method: 'get', data: params)
48
+ return JSON.parse(response)['txs'].map{ |t| Transaction.new(t) }
49
+ end
50
+
51
+ def self.get_blocks(api_code = nil, time: nil, pool_name: nil)
52
+ params = { 'format' => 'json' }
53
+ if !api_code.nil? then params['api_code'] = api_code end
54
+ resource = "blocks/"
55
+ if !time.nil?
56
+ resource += time.to_s
57
+ elsif !pool_name.nil?
58
+ resource += pool_name
59
+ end
60
+ response = Blockchain::call_api(resource, method: 'get', data: params)
61
+ return JSON.parse(response)['blocks'].map{ |b| SimpleBlock.new(b) }
62
+ end
63
+
64
+ def self.get_latest_block(api_code = nil)
65
+ params = {}
66
+ if !api_code.nil? then params['api_code'] = api_code end
67
+ resource = 'latestblock'
68
+ response = Blockchain::call_api(resource, method: 'get', data: params)
69
+ return LatestBlock.new(JSON.parse(response))
70
+ end
71
+
72
+ def self.get_inventory_data(hash, api_code = nil)
73
+ params = { 'format' => 'json' }
74
+ if !api_code.nil? then params['api_code'] = api_code end
75
+ resource = "inv/#{hash}"
76
+ response = Blockchain::call_api(resource, method: 'get', data: params)
77
+ return InventoryData.new(JSON.parse(response))
78
+ end
79
+
80
+ class SimpleBlock
81
+ attr_reader :height
82
+ attr_reader :hash
83
+ attr_reader :time
84
+ attr_reader :main_chain
85
+
86
+ def initialize(b)
87
+ @height = b['height']
88
+ @hash = b['hash']
89
+ @time = b['time']
90
+ @main_chain = b['main_chain']
91
+ end
92
+ end
93
+
94
+ class LatestBlock
95
+ attr_reader :hash
96
+ attr_reader :time
97
+ attr_reader :block_index
98
+ attr_reader :height
99
+ attr_reader :tx_indexes
100
+
101
+ def initialize(b)
102
+ @hash = b['hash']
103
+ @time = b['time']
104
+ @block_index = b['block_index']
105
+ @height = b['height']
106
+ @tx_indexes = b['txIndexes']
107
+ end
108
+ end
109
+
110
+ class UnspentOutput
111
+ attr_reader :tx_hash
112
+ attr_reader :tx_index
113
+ attr_reader :tx_output_n
114
+ attr_reader :script
115
+ attr_reader :value
116
+ attr_reader :value_hex
117
+ attr_reader :confirmations
118
+
119
+ def initialize(o)
120
+ @tx_hash = o['tx_hash']
121
+ @tx_index = o['tx_index']
122
+ @tx_output_n = o['tx_output_n']
123
+ @script = o['script']
124
+ @value = o['value']
125
+ @value_hex = o['value_hex']
126
+ @confirmations = o['confirmations']
127
+ end
128
+ end
129
+
130
+ class Address
131
+ attr_reader :hash160
132
+ attr_reader :address
133
+ attr_reader :n_tx
134
+ attr_reader :total_received
135
+ attr_reader :total_sent
136
+ attr_reader :final_balance
137
+ attr_reader :transactions
138
+
139
+ def initialize(a)
140
+ @hash160 = a['hash160']
141
+ @address = a['address']
142
+ @n_tx = a['n_tx']
143
+ @total_received = a['total_received']
144
+ @total_sent = a['total_sent']
145
+ @final_balance = a['final_balance']
146
+ @transactions = a['txs'].map{ |tx| Transaction.new(tx) }
147
+ end
148
+ end
149
+
150
+ class Input
151
+ attr_reader :n
152
+ attr_reader :value
153
+ attr_reader :address
154
+ attr_reader :tx_index
155
+ attr_reader :type
156
+ attr_reader :script
157
+ attr_reader :script_sig
158
+ attr_reader :sequence
159
+
160
+ def initialize(i)
161
+ obj = i['prev_out']
162
+ if !obj.nil?
163
+ # regular TX
164
+ @n = obj['n']
165
+ @value = obj['value']
166
+ @address = obj['addr']
167
+ @tx_index = obj['tx_index']
168
+ @type = obj['type']
169
+ @script = obj['script']
170
+ @script_sig = i['script']
171
+ @sequence = i['sequence']
172
+ else
173
+ # coinbase TX
174
+ @script_sig = i['script']
175
+ @sequence = i['sequence']
176
+ end
177
+ end
178
+ end
179
+
180
+ class Output
181
+ attr_reader :n
182
+ attr_reader :value
183
+ attr_reader :address
184
+ attr_reader :tx_index
185
+ attr_reader :script
186
+ attr_reader :spent
187
+
188
+ def initialize(o)
189
+ @n = o['n']
190
+ @value = o['value']
191
+ @address = o['addr']
192
+ @tx_index = o['tx_index']
193
+ @script = o['script']
194
+ @spent = o['spent']
195
+ end
196
+ end
197
+
198
+ class Transaction
199
+ attr_reader :double_spend
200
+ attr_reader :block_height
201
+ attr_reader :time
202
+ attr_reader :relayed_by
203
+ attr_reader :hash
204
+ attr_reader :tx_index
205
+ attr_reader :version
206
+ attr_reader :size
207
+ attr_reader :inputs
208
+ attr_reader :outputs
209
+
210
+ def initialize(t)
211
+ @double_spend = t.fetch('double_spend', false)
212
+ @block_height = t.fetch('block_height', false)
213
+ @time = t['time']
214
+ @relayed_by = t['relayed_by']
215
+ @hash = t['hash']
216
+ @tx_index = t['tx_index']
217
+ @version = t['ver']
218
+ @size = t['size']
219
+ @inputs = t['inputs'].map{ |i| Input.new(i) }
220
+ @outputs = t['out'].map{ |o| Output.new(o) }
221
+
222
+ if @block_height.nil?
223
+ @block_height = -1
224
+ end
225
+ end
226
+
227
+ def adjust_block_height(h)
228
+ @block_height = h
229
+ end
230
+ end
231
+
232
+
233
+ class Block
234
+ attr_reader :hash
235
+ attr_reader :version
236
+ attr_reader :previous_block
237
+ attr_reader :merkle_root
238
+ attr_reader :time
239
+ attr_reader :bits
240
+ attr_reader :fee
241
+ attr_reader :nonce
242
+ attr_reader :n_tx
243
+ attr_reader :size
244
+ attr_reader :block_index
245
+ attr_reader :main_chain
246
+ attr_reader :height
247
+ attr_reader :received_time
248
+ attr_reader :relayed_by
249
+ attr_reader :transactions
250
+
251
+ def initialize(b)
252
+ @hash = b['hash']
253
+ @version = b['ver']
254
+ @previous_block = b['prev_block']
255
+ @merkle_root = b['mrkl_root']
256
+ @time = b['time']
257
+ @bits = b['bits']
258
+ @fee = b['fee']
259
+ @nonce = b['nonce']
260
+ @n_tx = b['n_tx']
261
+ @size = b['size']
262
+ @block_index = b['block_index']
263
+ @main_chain = b['main_chain']
264
+ @height = b['height']
265
+ @received_time = b['received_time']
266
+ @relayed_by = b['relayed_by']
267
+ @transactions = b['tx'].map{ |tx| Transaction.new(tx) }
268
+ @transactions.each do |tx|
269
+ tx.adjust_block_height(@height)
270
+ end
271
+
272
+ if @received_time.nil?
273
+ @received_time = @time
274
+ end
275
+ end
276
+ end
277
+
278
+ class InventoryData
279
+ attr_reader :hash
280
+ attr_reader :type
281
+ attr_reader :initial_time
282
+ attr_reader :initial_ip
283
+ attr_reader :nconnected
284
+ attr_reader :relayed_count
285
+ attr_reader :relayed_percent
286
+
287
+ def initialize(i)
288
+ @hash = i['hash']
289
+ @type = i['type']
290
+ @initial_time = i['initial_time'].to_i
291
+ @initial_ip = i['initial_ip']
292
+ @nconnected = i['nconnected'].to_i
293
+ @relayed_count = i['relayed_count'].to_i
294
+ @relayed_percent = i['relayed_percent'].to_i
295
+ end
296
+ end
297
+
298
298
  end