-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbcwallet.rb
More file actions
executable file
·1458 lines (1178 loc) · 34.8 KB
/
bcwallet.rb
File metadata and controls
executable file
·1458 lines (1178 loc) · 34.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
#
# bcwallet.rb: Educational Bitcoin Client
#
# This is a tiny Bitcoin client implementation which uses
# Simplified Payment Verification (SPV).
#
# WARNING: This client is for technical education,
# skips a lot of validations, and may have critical bugs.
#
# USE OF THE CLIENT IN MAIN NETWORK MAY CAUSE YOUR COINS LOST.
#
# DO NOT SET THIS VALUE "false".
#
IS_TESTNET = true
# Remote host to use: It is recommended to use this client with a local client.
# Install Bitcoin-Qt and then launch with -testnet option to connect Testnet.
HOST = 'localhost'
# This software is licensed under the MIT license.
#
# The MIT License (MIT)
#
# Copyright (c) 2014 peryaudo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
require 'openssl'
require 'socket'
#
# A class which manages both public key and private key in OpenSSL's ECDSA.
#
class Key
#
# Bitcoin mainly uses SHA-256(SHA-256(plain)) as a cryptographic hash function
# when a hash is needed.
#
def self.hash256(plain)
OpenSSL::Digest::SHA256.digest(OpenSSL::Digest::SHA256.digest(plain))
end
#
# RIPEMD-160(SHA-256(plain)) is used when a shorter hash is preferable.
#
def self.hash160(plain)
OpenSSL::Digest::RIPEMD160.digest(OpenSSL::Digest::SHA256.digest(plain))
end
BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
#
# Modified version of Base58 is used in Bitcoin to convert binaries into human-typable strings.
#
def self.encode_base58(plain)
# plain is big endian
num = plain.unpack("H*").first.hex
res = ''
while num > 0
res += BASE58[num % 58]
num /= 58
end
# restore leading zeroes
plain.each_byte do |c|
break if c != 0
res += BASE58[0]
end
res.reverse
end
def self.decode_base58(encoded)
num = 0
encoded.each_char do |c|
num *= 58
num += BASE58.index(c)
end
res = num.to_s(16)
if res.length % 2 == 1
res = '0' + res
end
# restore leading zeroes
encoded.each_char do |c|
break if c != BASE58[0]
res = '00' + res
end
[res].pack('H*')
end
#
# Base58 with the type of data and the checksum is called Base58Check in Bitcoin protocol.
# It is used as a Bitcoin address, human-readable private key, etc.
#
def self.encode_base58check(type, plain)
leading_bytes = {
main: { public_key: 0, private_key: 128 },
testnet: { public_key: 111, private_key: 239 }
}
leading_byte = [leading_bytes[IS_TESTNET ? :testnet : :main][type]].pack('C')
data = leading_byte + plain
checksum = Key.hash256(data)[0, 4]
Key.encode_base58(data + checksum)
end
def self.decode_base58check(encoded)
decoded = Key.decode_base58(encoded)
raise "invalid base58 checksum" if Key.hash256(decoded[0, decoded.length - 4])[0, 4] != decoded[-4, 4]
types = {
main: { 0 => :public_key, 128 => :private_key },
testnet: { 111 => :public_key, 239 => :private_key }
}
type = types[IS_TESTNET ? :testnet : :main][decoded[0].unpack('C').first]
{ type: type, data: decoded[1, decoded.length - 5] }
end
#
# Initialize with ASCII-encoded DER format string (nil to generate a new key)
#
def initialize(der = nil)
if der
@key = OpenSSL::PKey::EC.new([der].pack('H*'))
else
@key = OpenSSL::PKey::EC.new('secp256k1')
@key = @key.generate_key
end
@key.check_key
end
#
# Sign the data with the key.
#
def sign(data)
@key.dsa_sign_asn1(data)
end
#
# Convert public key to Bitcoin address.
#
def to_address_s
Key.encode_base58check(:public_key, Key.hash160(@key.public_key.to_bn.to_s(2)))
end
#
# Convert the private key to Bitcoin private key import format.
#
def to_private_key_s
Key.encode_base58check(:private_key, @key.private_key.to_s(2))
end
#
# Convert the key pair into ASCII-encoded DER format string.
#
def to_der_hex_s
@key.to_der.unpack('H*').first
end
def to_public_key
@key.public_key.to_bn.to_s(2)
end
def to_public_key_hash
Key.hash160(@key.public_key.to_bn.to_s(2))
end
end
#
# A class which generates Bloom filter.
# Bloom filter is a data structure used in Bitcoin to filter transactions for SPV clients.
# It enables you to quickly test whether an element is included in a set,
# but may have false positives (i.e. probabilistic data structure).
#
class BloomFilter
public
#
# len = length of bloom filter
# hash_funcs = number of hash functions to use
# tweak = a random number
#
def initialize(len, hash_funcs, tweak)
@filter = Array.new(len, 0)
@hash_funcs = hash_funcs
@tweak = tweak
end
#
# See an array as a huge little endian integer, and fill idx-th bit
#
def set_bit(idx)
@filter[idx >> 3] |= (1 << (7 & idx))
end
def rotate_left_32(x, r)
((x << r) | (x >> (32 - r))) & 0xffffffff
end
#
# The hash functions is called MurmurHash3 (32-bit).
# Reference implementation is somewhat tricky one,
# so I recommend you to read bitcoinj's one if you want to know the detail.
#
def hash(seed, data)
mask = 0xffffffff
h1 = seed & mask
c1 = 0xcc9e2d51
c2 = 0x1b873593
data.unpack('V*').each do |k1|
k1 = (k1 * c1) & mask
k1 = rotate_left_32(k1, 15)
k1 = (k1 * c2) & mask
h1 = h1 ^ k1
h1 = rotate_left_32(h1, 13)
h1 = (h1 * 5 + 0xe6546b64) & mask
end
padded_remaining_bytes = data[(data.length & (mask ^ 3))..-1] + "\0" * (4 - (data.length & 3))
k1 = padded_remaining_bytes.unpack('V').first
k1 = (k1 * c1) & mask
k1 = rotate_left_32(k1, 15)
k1 = (k1 * c2) & mask
h1 = h1 ^ k1
h1 = (h1 ^ data.length) & mask
h1 = h1 ^ (h1 >> 16)
h1 = (h1 * 0x85ebca6b) & mask
h1 = h1 ^ (h1 >> 13)
h1 = (h1 * 0xc2b2ae35) & mask
h1 = h1 ^ (h1 >> 16)
h1
end
#
# Insert the data into the Bloom filter
#
def insert(data)
@hash_funcs.times do |i|
set_bit(hash(i * 0xfba4c795 + @tweak, data) % (@filter.length * 8))
end
end
def to_s
res = ''
@filter.each do |byte|
res += [byte].pack('C')
end
res
end
end
#
# A class for message serializers and deserializers.
# It contains type definitions of structures.
#
class Message
#
# Constants used in inventory vector
#
MSG_TX = 1
MSG_BLOCK = 2
MSG_FILTERED_BLOCK = 3
def initialize
#
# Message definitions.
#
@message_definitions = {
version: [
[:version, :uint32],
[:services, :uint64],
[:timestamp, :uint64],
[:your_addr, :net_addr],
[:my_addr, :net_addr],
[:nonce, :uint64],
[:agent, :string],
[:height, :uint32],
[:relay, :relay_flag]
],
ping: [[:nonce, :uint64]],
pong: [[:nonce, :uint64]],
alert: [],
verack: [],
mempool: [],
addr: [[:addr, array_for(:net_addr)]],
inv: [[:inventory, array_for(:inv_vect)]],
merkleblock: [
[:hash, :block_hash],
[:version, :uint32],
[:prev_block, :hash256],
[:merkle_root, :hash256],
[:timestamp, :uint32],
[:bits, :uint32],
[:nonce, :uint32],
[:total_txs, :uint32],
[:hashes, array_for(:hash256)],
[:flags, :string]
],
tx: [
[:hash, :tx_hash],
[:version, :uint32],
[:tx_in, array_for(:tx_in)],
[:tx_out, array_for(:tx_out)],
[:lock_time, :uint32]
],
filterload: [
[:filter, :string],
[:hash_funcs, :uint32],
[:tweak, :uint32],
[:flag, :uint8]
],
getblocks: [
[:version, :uint32],
[:block_locator, array_for(:hash256)],
[:hash_stop, :hash256]
],
getdata: [[:inventory, array_for(:inv_vect)]],
inv_vect: [
[:type, :uint32],
[:hash, :hash256]],
outpoint: [
[:hash, :hash256],
[:index, :uint32]],
tx_in: [
[:previous_output, :outpoint],
[:signature_script, :string],
[:sequence, :uint32]],
tx_out: [
[:value, :uint64],
[:pk_script, :string]]
}
end
#
# Serialize a message using message definitions.
#
def serialize(message)
@payload = ''
serialize_struct(message[:command], message)
@payload
end
#
# Deserialize a message using message definitions.
#
def deserialize(command, payload)
raise unless @message_definitions.has_key?(command)
@payload = payload
res = deserialize_struct(command)
res[:command] = command
res
end
#
# Read a message and parse it using message definitions.
#
def read(socket)
packet = read_packet(socket)
expected_magic = [IS_TESTNET ? '0b110907' : 'f9beb4d9'].pack('H*')
expected_checksum = Key.hash256(packet[:payload])[0, 4]
if packet[:magic] != expected_magic
raise 'invalid magic received'
end
if packet[:checksum] != expected_checksum
raise 'incorrect checksum'
end
unless @message_definitions.has_key?(packet[:command])
raise 'invalid message type'
end
deserialize(packet[:command], packet[:payload])
end
#
# Actually send a message to the remote host.
#
def write(socket, message)
# Create payload
payload = serialize(message)
# 4bytes: magic
raw_message = [IS_TESTNET ? '0b110907' : 'f9beb4d9'].pack('H*')
# 12bytes: command (padded with zeroes)
raw_message += [message[:command].to_s].pack('a12')
# 4bytes: length of payload
raw_message += [payload.length].pack('V')
# 4bytes: checksum
raw_message += Key.hash256(payload)[0, 4]
# payload
raw_message += payload
socket.write raw_message
socket.flush
end
private
def read_packet(socket)
magic = socket.read(4)
command = socket.read(12).unpack('A12').first.to_sym
length = socket.read(4).unpack('V').first
checksum = socket.read(4)
payload = socket.read(length)
{ magic: magic, command: command, checksum: checksum, payload: payload }
end
def serialize_struct(type, struct)
if type.kind_of?(Proc)
type.call(:write, struct)
return
end
if @message_definitions.has_key?(type)
@message_definitions[type].each do |definition|
next if struct.has_key?(:command) && definition.first == :hash
serialize_struct(definition.last, struct[definition.first])
end
else
method(type).call(:write, struct)
end
end
def deserialize_struct(type)
if type.kind_of?(Proc)
return type.call(:read)
end
if @message_definitions.has_key?(type)
res = {}
@message_definitions[type].each do |definition|
res[definition.first] = deserialize_struct(definition.last)
end
res
else
method(type).call(:read)
end
end
#
# Higher order function to generate array serializer / deserializer
#
def array_for(elm)
lambda do |rw, val = nil|
case rw
when :read
count = integer(:read)
res = []
count.times do
res.push deserialize_struct(elm)
end
res
when :write
integer(:write, val.length)
val.each do |v|
serialize_struct(elm, v)
end
val
end
end
end
#
# Serializer & deserializer methods
#
def read_bytes(len)
res = @payload[0, len]
@payload = @payload[len..-1]
res
end
def write_bytes(val)
@payload += val
end
def fixed_integer(templ, len, rw, val = nil)
case rw
when :read
read_bytes(len).unpack(templ).first
when :write
write_bytes([val].pack(templ))
end
end
def uint8(rw, val = nil)
fixed_integer('C', 1, rw, val)
end
def uint16(rw, val = nil)
fixed_integer('v', 2, rw, val)
end
def uint32(rw, val = nil)
fixed_integer('V', 4, rw, val)
end
def uint64(rw, val = nil)
fixed_integer('Q', 8, rw, val)
end
def read_integer
top = uint8(:read)
case top
when 0xfd then uint16(:read)
when 0xfe then uint32(:read)
when 0xff then uint64(:read)
else top
end
end
def write_integer(val)
if val < 0xfd
uint8(:write, val)
elsif val <= 0xffff
uint8(:write, 0xfd)
uint16(:write, val)
elsif val <= 0xffffffff
uint8(:write, 0xfe)
uint32(:write, val)
else
uint8(:write, 0xff)
uint64(:write, val)
end
end
def integer(rw, val = nil)
case rw
when :read
read_integer
when :write
write_integer(val)
end
end
def string(rw, val = nil)
case rw
when :read
len = integer(:read)
read_bytes(len)
when :write
integer(:write, val.length)
write_bytes(val)
val
end
end
def net_addr(rw, val = nil)
# accurate serializing is not necessary
case rw
when :read
read_bytes(26)
nil
when :write
write_bytes([0, '00000000000000000000FFFF', '00000000', 8333].pack('QH*H*n'))
val
end
end
def relay_flag(rw, val = nil)
case rw
when :read
if @payload.length > 0
uint8(:read)
else
true
end
when :write
unless val
uint8(:write, 0)
end
val
end
end
def hash256(rw, val = nil)
case rw
when :read
read_bytes(32)
when :write
write_bytes(val)
val
end
end
def block_hash(rw, val = nil)
case rw
when :read
Key.hash256(@payload[0, 80])
end
end
def tx_hash(rw, val = nil)
case rw
when :read
Key.hash256(@payload)
end
end
end
#
# The blockchain class. It manages and stores Bitcoin blockchain data.
#
class Blockchain
def initialize(keys, data_file_name)
@data_file_name = data_file_name
keys_hash = Key.hash256(keys.collect { |key, _| key }.sort.join)
init_data(keys_hash)
load_data
# new keys are added since the last synchronization
init_data(keys_hash) if @data[:keys_hash] != keys_hash
end
def init_data(keys_hash)
@data = { blocks: {}, txs: {}, last_height: 0, keys_hash: keys_hash }
end
def load_data
return unless File.exists?(@data_file_name)
open(@data_file_name, 'rb') do |file|
@data = Marshal.restore(file)
end
end
def save_data
open(@data_file_name, 'wb') do |file|
Marshal.dump @data, file
end
end
def calc_last_hash
# These hashes are genesis blocks' ones.
last_hash = { timestamp: 0,
hash: [IS_TESTNET ?
'000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943' :
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'].pack('H*').reverse }
@data[:blocks].each do |hash, block|
if block[:timestamp] > last_hash[:timestamp]
last_hash = { timestamp: block[:timestamp], hash: hash }
end
end
last_hash
end
def blocks
@data[:blocks]
end
def txs
@data[:txs]
end
def last_height
@data[:last_height]
end
def last_height=(v)
@data[:last_height] = v
end
#
# Get balance for the keys
#
def get_balance(keys)
balance = {}
keys.each do |addr, _|
balance[addr] = 0
end
set_spent_for_tx_outs!
@data[:txs].each do |tx_hash, tx|
keys.each do |addr, key|
public_key_hash = key.to_public_key_hash
tx[:tx_out].each do |tx_out|
# The tx_out was already spent
next if tx_out[:spent]
if extract_public_key_hash_from_script(tx_out[:pk_script]) == public_key_hash
balance[addr] += tx_out[:value]
end
end
end
end
balance
end
#
# This is a heuristic function to find out whether the block is an independent young block.
# An independent block here means a block which have not received one of its ancestors yet.
# We may receive this kind of blocks regardless of getblocks -> inv -> getdata iteration.
#
# To implement it more robustly, you have to construct graph from received blocks,
# do a lot of validations, and actually take the longest block chain.
#
# The reason why the client took this way is simplicity and performance.
# Doing them in Ruby is painful, and also it's not ciritical to explain how Bitcoin client works.
#
def is_young_block(hash)
(@data[:blocks][hash][:timestamp] - Time.now.to_i).abs <= 60 * 60 && !is_too_high(hash)
end
def accumulate_txs(from_key, amount)
public_key_hash = from_key.to_public_key_hash
# Refresh spent flags of tx_outs
set_spent_for_tx_outs!
# In a real SPV client, we should walk along merkle trees to validate the transaction.
# It will be implemented to this client soon.
total_satoshis = 0
tx_in = []
@data[:txs].each do |tx_hash, tx|
break if total_satoshis >= amount
matched = nil
pk_script = nil
tx[:tx_out].each_with_index do |tx_out, index|
next if tx_out[:spent]
if extract_public_key_hash_from_script(tx_out[:pk_script]) == public_key_hash
total_satoshis += tx_out[:value]
matched = index
pk_script = tx_out[:pk_script]
break
end
end
if matched
tx_in.push({ previous_output: { :hash => tx[:hash], :index => matched },
signature_script: '',
sequence: ((1 << 32) - 1),
# not included in serialized data, but used to make signature
pk_script: pk_script })
end
end
{ total_satoshis: total_satoshis, tx_in: tx_in }
end
#
# Set spent flags for all tx_outs.
# If the tx_out is already spent on another transaction's tx_in, it will be set.
#
def set_spent_for_tx_outs!
@data[:txs].each do |tx_hash, tx|
tx[:tx_in].each do |tx_in|
hash = tx_in[:previous_output][:hash]
index = tx_in[:previous_output][:index]
if @data[:txs].has_key?(hash)
@data[:txs][hash][:tx_out][index][:spent] = true
end
end
end
end
private
#
# This checks whether the block has previous 5 (= threshold) blocks
# in the received data.
#
def is_too_high(hash)
threshold = 5
cur = 0
while @data[:blocks].has_key?(hash) && cur < threshold
hash = @data[:blocks][hash][:prev_block]
cur += 1
end
cur == threshold
end
#
# Bitcoin has complex scripting system for its payment,
# but we will only support very basic one.
#
def extract_public_key_hash_from_script(script)
# OP_DUP OP_HASH160 (public key hash) OP_EQUALVERIFY OP_CHECKSIG
unless script[0, 3] == ['76a914'].pack('H*') &&
script[23, 2] == ['88ac'].pack('H*') &&
script.length == 25
raise 'unsupported script format'
end
script[3, 20]
end
end
#
# The network class. It may be split into two or three classes
# to manage multiple connections and features in production.
#
class Network
attr_reader :status, :data
#
# keys = { name => ECDSA key objects }
#
def initialize(keys, data_file_name)
@message = Message.new
@keys = keys
@blockchain = Blockchain.new(@keys, data_file_name)
@last_hash = @blockchain.calc_last_hash
@is_sync_finished = true
@requested_data = 0
@received_data = 0
end
#
# Synchronize the block chain.
# It creates a new thread and returns immediately.
# To know whether the thread was finished, use Network#sync_finished?
#
def sync
Thread.abort_on_exception = true
@is_sync_finished = false
t = Thread.new do
unless @socket
@status = 'connection establishing ... '
@socket = TCPSocket.open(HOST, IS_TESTNET ? 18333 : 8333)
send_version
end
if @created_transaction
@status = 'announcing transaction ... '
send_transaction_inv
end
loop do
break if dispatch_message
end
@is_sync_finished = true
end
t.run
end
def sync_finished?
@is_sync_finished
end
#
# Send coins to the address.
# from_key = Key object which the client sends coins from
# to_addr = Receiving address (string)
# transaction_fee = Transaction fee which miners receive
#
def send(from_key, to_addr, amount, transaction_fee = 0)
# The process of announcing a created transaction is as follows:
# Generate tx message and get its hash, and send inv message with the hash to the remote host.
# Then the remote host will send getdata, so you can now actually send tx message.
to_addr_decoded = Key.decode_base58check(to_addr)
if to_addr_decoded[:type] != :public_key
raise 'invalid address'
end
accumulated = @blockchain.accumulate_txs(from_key, amount)
payback = accumulated[:total_satoshis] - amount - transaction_fee
unless payback >= 0
raise "you don't have enough balance to pay"
end
@created_transaction = sign_transaction(from_key, {
command: :tx,
version: 1,
tx_in: accumulated[:tx_in],
tx_out: [{ value: amount, pk_script: generate_pk_script(to_addr_decoded[:data]) },
{ value: payback, pk_script: generate_pk_script(from_key.to_public_key_hash) }],
lock_time: 0
})
@status = ''
end
#
# Get balance for the keys
#
def get_balance
@blockchain.get_balance(@keys)
end
def block(hash)
@blockchain.blocks[hash]
end
private
PROTOCOL_VERSION = 70002
#
# Send version message to the remote host.
#
def send_version
@message.write(@socket, {
command: :version,
version: PROTOCOL_VERSION,
# This client should not be asked for full blocks.
services: 0,
timestamp: Time.now.to_i,
your_addr: nil, # I found that at least Satoshi client doesn't check it,
my_addr: nil, # so it will be enough for this client.
nonce: (rand(1 << 64) - 1), # A random number.
agent: '/bcwallet.rb:1.00/',
height: (@blockchain.blocks.length - 1), # Height of possessed blocks
# It forces the remote host not to send any 'inv' messages till it receive 'filterload' message.
relay: false
})
end
#
# Send filterload message.
#
def send_filterload
hash_funcs = 10
tweak = rand(1 << 32) - 1
bf = BloomFilter.new(512, hash_funcs, tweak)