tapyrus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +12 -0
  7. data/CODE_OF_CONDUCT.md +49 -0
  8. data/Gemfile +6 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +100 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/exe/tapyrusrb-cli +5 -0
  15. data/exe/tapyrusrbd +41 -0
  16. data/lib/openassets/marker_output.rb +20 -0
  17. data/lib/openassets/payload.rb +54 -0
  18. data/lib/openassets/util.rb +28 -0
  19. data/lib/openassets.rb +9 -0
  20. data/lib/tapyrus/base58.rb +38 -0
  21. data/lib/tapyrus/block.rb +77 -0
  22. data/lib/tapyrus/block_header.rb +88 -0
  23. data/lib/tapyrus/bloom_filter.rb +78 -0
  24. data/lib/tapyrus/chain_params.rb +90 -0
  25. data/lib/tapyrus/chainparams/mainnet.yml +41 -0
  26. data/lib/tapyrus/chainparams/regtest.yml +38 -0
  27. data/lib/tapyrus/chainparams/testnet.yml +41 -0
  28. data/lib/tapyrus/constants.rb +195 -0
  29. data/lib/tapyrus/descriptor.rb +147 -0
  30. data/lib/tapyrus/ext_key.rb +337 -0
  31. data/lib/tapyrus/key.rb +296 -0
  32. data/lib/tapyrus/key_path.rb +26 -0
  33. data/lib/tapyrus/logger.rb +42 -0
  34. data/lib/tapyrus/merkle_tree.rb +149 -0
  35. data/lib/tapyrus/message/addr.rb +35 -0
  36. data/lib/tapyrus/message/base.rb +28 -0
  37. data/lib/tapyrus/message/block.rb +46 -0
  38. data/lib/tapyrus/message/block_transaction_request.rb +45 -0
  39. data/lib/tapyrus/message/block_transactions.rb +31 -0
  40. data/lib/tapyrus/message/block_txn.rb +27 -0
  41. data/lib/tapyrus/message/cmpct_block.rb +42 -0
  42. data/lib/tapyrus/message/error.rb +10 -0
  43. data/lib/tapyrus/message/fee_filter.rb +27 -0
  44. data/lib/tapyrus/message/filter_add.rb +28 -0
  45. data/lib/tapyrus/message/filter_clear.rb +17 -0
  46. data/lib/tapyrus/message/filter_load.rb +39 -0
  47. data/lib/tapyrus/message/get_addr.rb +17 -0
  48. data/lib/tapyrus/message/get_block_txn.rb +27 -0
  49. data/lib/tapyrus/message/get_blocks.rb +29 -0
  50. data/lib/tapyrus/message/get_data.rb +21 -0
  51. data/lib/tapyrus/message/get_headers.rb +28 -0
  52. data/lib/tapyrus/message/header_and_short_ids.rb +57 -0
  53. data/lib/tapyrus/message/headers.rb +35 -0
  54. data/lib/tapyrus/message/headers_parser.rb +24 -0
  55. data/lib/tapyrus/message/inv.rb +21 -0
  56. data/lib/tapyrus/message/inventories_parser.rb +23 -0
  57. data/lib/tapyrus/message/inventory.rb +51 -0
  58. data/lib/tapyrus/message/mem_pool.rb +17 -0
  59. data/lib/tapyrus/message/merkle_block.rb +42 -0
  60. data/lib/tapyrus/message/network_addr.rb +63 -0
  61. data/lib/tapyrus/message/not_found.rb +21 -0
  62. data/lib/tapyrus/message/ping.rb +30 -0
  63. data/lib/tapyrus/message/pong.rb +26 -0
  64. data/lib/tapyrus/message/prefilled_tx.rb +29 -0
  65. data/lib/tapyrus/message/reject.rb +46 -0
  66. data/lib/tapyrus/message/send_cmpct.rb +43 -0
  67. data/lib/tapyrus/message/send_headers.rb +16 -0
  68. data/lib/tapyrus/message/tx.rb +30 -0
  69. data/lib/tapyrus/message/ver_ack.rb +17 -0
  70. data/lib/tapyrus/message/version.rb +69 -0
  71. data/lib/tapyrus/message.rb +70 -0
  72. data/lib/tapyrus/mnemonic/wordlist/chinese_simplified.txt +2048 -0
  73. data/lib/tapyrus/mnemonic/wordlist/chinese_traditional.txt +2048 -0
  74. data/lib/tapyrus/mnemonic/wordlist/english.txt +2048 -0
  75. data/lib/tapyrus/mnemonic/wordlist/french.txt +2048 -0
  76. data/lib/tapyrus/mnemonic/wordlist/italian.txt +2048 -0
  77. data/lib/tapyrus/mnemonic/wordlist/japanese.txt +2048 -0
  78. data/lib/tapyrus/mnemonic/wordlist/spanish.txt +2048 -0
  79. data/lib/tapyrus/mnemonic.rb +77 -0
  80. data/lib/tapyrus/network/connection.rb +73 -0
  81. data/lib/tapyrus/network/message_handler.rb +241 -0
  82. data/lib/tapyrus/network/peer.rb +223 -0
  83. data/lib/tapyrus/network/peer_discovery.rb +42 -0
  84. data/lib/tapyrus/network/pool.rb +135 -0
  85. data/lib/tapyrus/network.rb +13 -0
  86. data/lib/tapyrus/node/cli.rb +112 -0
  87. data/lib/tapyrus/node/configuration.rb +38 -0
  88. data/lib/tapyrus/node/spv.rb +79 -0
  89. data/lib/tapyrus/node.rb +7 -0
  90. data/lib/tapyrus/opcodes.rb +178 -0
  91. data/lib/tapyrus/out_point.rb +44 -0
  92. data/lib/tapyrus/rpc/http_server.rb +65 -0
  93. data/lib/tapyrus/rpc/request_handler.rb +150 -0
  94. data/lib/tapyrus/rpc/tapyrus_core_client.rb +72 -0
  95. data/lib/tapyrus/rpc.rb +7 -0
  96. data/lib/tapyrus/script/multisig.rb +92 -0
  97. data/lib/tapyrus/script/script.rb +551 -0
  98. data/lib/tapyrus/script/script_error.rb +111 -0
  99. data/lib/tapyrus/script/script_interpreter.rb +668 -0
  100. data/lib/tapyrus/script/tx_checker.rb +81 -0
  101. data/lib/tapyrus/script_witness.rb +38 -0
  102. data/lib/tapyrus/secp256k1/native.rb +174 -0
  103. data/lib/tapyrus/secp256k1/ruby.rb +123 -0
  104. data/lib/tapyrus/secp256k1.rb +12 -0
  105. data/lib/tapyrus/slip39/share.rb +122 -0
  106. data/lib/tapyrus/slip39/sss.rb +245 -0
  107. data/lib/tapyrus/slip39/wordlist/english.txt +1024 -0
  108. data/lib/tapyrus/slip39.rb +93 -0
  109. data/lib/tapyrus/store/chain_entry.rb +67 -0
  110. data/lib/tapyrus/store/db/level_db.rb +98 -0
  111. data/lib/tapyrus/store/db.rb +9 -0
  112. data/lib/tapyrus/store/spv_chain.rb +101 -0
  113. data/lib/tapyrus/store.rb +9 -0
  114. data/lib/tapyrus/tx.rb +347 -0
  115. data/lib/tapyrus/tx_in.rb +89 -0
  116. data/lib/tapyrus/tx_out.rb +74 -0
  117. data/lib/tapyrus/util.rb +133 -0
  118. data/lib/tapyrus/validation.rb +115 -0
  119. data/lib/tapyrus/version.rb +3 -0
  120. data/lib/tapyrus/wallet/account.rb +151 -0
  121. data/lib/tapyrus/wallet/base.rb +162 -0
  122. data/lib/tapyrus/wallet/db.rb +81 -0
  123. data/lib/tapyrus/wallet/master_key.rb +110 -0
  124. data/lib/tapyrus/wallet.rb +8 -0
  125. data/lib/tapyrus.rb +219 -0
  126. data/tapyrusrb.conf.sample +0 -0
  127. data/tapyrusrb.gemspec +47 -0
  128. metadata +451 -0
data/lib/tapyrus.rb ADDED
@@ -0,0 +1,219 @@
1
+ # Porting part of the code from bitcoin-ruby. see the license.
2
+ # https://github.com/lian/bitcoin-ruby/blob/master/COPYING
3
+
4
+ require 'tapyrus/version'
5
+ require 'eventmachine'
6
+ require 'ecdsa'
7
+ require 'securerandom'
8
+ require 'json'
9
+ require 'bech32'
10
+ require 'ffi'
11
+ require 'observer'
12
+ require 'tmpdir'
13
+ require_relative 'openassets'
14
+
15
+ module Tapyrus
16
+
17
+ autoload :Util, 'tapyrus/util'
18
+ autoload :ChainParams, 'tapyrus/chain_params'
19
+ autoload :Message, 'tapyrus/message'
20
+ autoload :Logger, 'tapyrus/logger'
21
+ autoload :Block, 'tapyrus/block'
22
+ autoload :BlockHeader, 'tapyrus/block_header'
23
+ autoload :Tx, 'tapyrus/tx'
24
+ autoload :Script, 'tapyrus/script/script'
25
+ autoload :Multisig, 'tapyrus/script/multisig'
26
+ autoload :ScriptInterpreter, 'tapyrus/script/script_interpreter'
27
+ autoload :ScriptError, 'tapyrus/script/script_error'
28
+ autoload :TxChecker, 'tapyrus/script/tx_checker'
29
+ autoload :TxIn, 'tapyrus/tx_in'
30
+ autoload :TxOut, 'tapyrus/tx_out'
31
+ autoload :OutPoint, 'tapyrus/out_point'
32
+ autoload :ScriptWitness, 'tapyrus/script_witness'
33
+ autoload :MerkleTree, 'tapyrus/merkle_tree'
34
+ autoload :Key, 'tapyrus/key'
35
+ autoload :ExtKey, 'tapyrus/ext_key'
36
+ autoload :ExtPubkey, 'tapyrus/ext_key'
37
+ autoload :Opcodes, 'tapyrus/opcodes'
38
+ autoload :Node, 'tapyrus/node'
39
+ autoload :Base58, 'tapyrus/base58'
40
+ autoload :Secp256k1, 'tapyrus/secp256k1'
41
+ autoload :Mnemonic, 'tapyrus/mnemonic'
42
+ autoload :ValidationState, 'tapyrus/validation'
43
+ autoload :Network, 'tapyrus/network'
44
+ autoload :Store, 'tapyrus/store'
45
+ autoload :RPC, 'tapyrus/rpc'
46
+ autoload :Wallet, 'tapyrus/wallet'
47
+ autoload :BloomFilter, 'tapyrus/bloom_filter'
48
+ autoload :KeyPath, 'tapyrus/key_path'
49
+ autoload :Descriptor, 'tapyrus/descriptor'
50
+ autoload :SLIP39, 'tapyrus/slip39'
51
+
52
+ require_relative 'tapyrus/constants'
53
+
54
+ extend Util
55
+
56
+ @chain_param = :mainnet
57
+
58
+ # set tapyrus network chain params
59
+ def self.chain_params=(name)
60
+ raise "chain params for #{name} is not defined." unless %i(mainnet testnet regtest).include?(name.to_sym)
61
+ @current_chain = nil
62
+ @chain_param = name.to_sym
63
+ end
64
+
65
+ # current tapyrus network chain params.
66
+ def self.chain_params
67
+ return @current_chain if @current_chain
68
+ case @chain_param
69
+ when :mainnet
70
+ @current_chain = Tapyrus::ChainParams.mainnet
71
+ when :testnet
72
+ @current_chain = Tapyrus::ChainParams.testnet
73
+ when :regtest
74
+ @current_chain = Tapyrus::ChainParams.regtest
75
+ end
76
+ @current_chain
77
+ end
78
+
79
+ # base dir path that store blockchain data and wallet data
80
+ def self.base_dir
81
+ "#{Dir.home}/.tapyrusrb/#{@chain_param}"
82
+ end
83
+
84
+ # get secp implementation module
85
+ def self.secp_impl
86
+ path = ENV['SECP256K1_LIB_PATH']
87
+ (path && File.exist?(path)) ? Tapyrus::Secp256k1::Native : Tapyrus::Secp256k1::Ruby
88
+ end
89
+
90
+ def self.hmac_sha512(key, data)
91
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('SHA512'), key, data)
92
+ end
93
+
94
+ def self.hmac_sha256(key, data)
95
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('SHA256'), key, data)
96
+ end
97
+
98
+ class ::String
99
+ # binary convert to hex string
100
+ def bth
101
+ unpack('H*').first
102
+ end
103
+
104
+ # hex string convert to binary
105
+ def htb
106
+ [self].pack('H*')
107
+ end
108
+
109
+ # binary convert to integer
110
+ def bti
111
+ bth.to_i(16)
112
+ end
113
+
114
+ # reverse hex string endian
115
+ def rhex
116
+ htb.reverse.bth
117
+ end
118
+
119
+ # get opcode
120
+ def opcode
121
+ case encoding
122
+ when Encoding::ASCII_8BIT
123
+ each_byte.next
124
+ when Encoding::US_ASCII
125
+ ord
126
+ else
127
+ to_i
128
+ end
129
+ end
130
+
131
+ def opcode?
132
+ !pushdata?
133
+ end
134
+
135
+ def push_opcode?
136
+ [Tapyrus::Opcodes::OP_PUSHDATA1, Tapyrus::Opcodes::OP_PUSHDATA2, Tapyrus::Opcodes::OP_PUSHDATA4].include?(opcode)
137
+ end
138
+
139
+ # whether data push only?
140
+ def pushdata?
141
+ opcode <= Tapyrus::Opcodes::OP_PUSHDATA4 && opcode > Tapyrus::Opcodes::OP_0
142
+ end
143
+
144
+ def pushed_data
145
+ return nil unless pushdata?
146
+ offset = 1
147
+ case opcode
148
+ when Tapyrus::Opcodes::OP_PUSHDATA1
149
+ offset += 1
150
+ when Tapyrus::Opcodes::OP_PUSHDATA2
151
+ offset += 2
152
+ when Tapyrus::Opcodes::OP_PUSHDATA4
153
+ offset += 4
154
+ end
155
+ self[offset..-1]
156
+ end
157
+
158
+ # whether value is hex or not hex
159
+ # @return [Boolean] return true if data is hex
160
+ def valid_hex?
161
+ !self[/\H/]
162
+ end
163
+
164
+ end
165
+
166
+ class ::Object
167
+
168
+ def build_json
169
+ if self.is_a?(Array)
170
+ "[#{self.map{|o|o.to_h.to_json}.join(',')}]"
171
+ else
172
+ to_h.to_json
173
+ end
174
+ end
175
+
176
+ def to_h
177
+ return self if self.is_a?(String)
178
+ instance_variables.inject({}) do |result, var|
179
+ key = var.to_s
180
+ key.slice!(0) if key.start_with?('@')
181
+ value = instance_variable_get(var)
182
+ if value.is_a?(Array)
183
+ result.update(key => value.map{|v|v.to_h})
184
+ else
185
+ result.update(key => value)
186
+ end
187
+ end
188
+ end
189
+
190
+ end
191
+
192
+ class ::Integer
193
+ def to_even_length_hex
194
+ hex = to_s(16)
195
+ hex.rjust((hex.length / 2.0).ceil * 2, '0')
196
+ end
197
+
198
+ def itb
199
+ to_even_length_hex.htb
200
+ end
201
+
202
+ # convert bit string
203
+ def to_bits(length = nil )
204
+ if length
205
+ to_s(2).rjust(length, '0')
206
+ else
207
+ to_s(2)
208
+ end
209
+ end
210
+ end
211
+
212
+ class ::ECDSA::Signature
213
+ # convert signature to der string.
214
+ def to_der
215
+ ECDSA::Format::SignatureDerString.encode(self)
216
+ end
217
+ end
218
+
219
+ end
File without changes
data/tapyrusrb.gemspec ADDED
@@ -0,0 +1,47 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tapyrus/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = "tapyrus"
9
+ spec.version = Tapyrus::VERSION
10
+ spec.authors = ["azuchi"]
11
+ spec.email = ["azuchi@chaintope.com"]
12
+
13
+ spec.summary = %q{[WIP]The implementation of Tapyrus Protocol for Ruby.}
14
+ spec.description = %q{[WIP]The implementation of Tapyrus Protocol for Ruby.}
15
+ spec.homepage = 'https://github.com/chaintope/tapyrusrb'
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency 'ecdsa'
24
+ spec.add_runtime_dependency 'eventmachine'
25
+ spec.add_runtime_dependency 'murmurhash3'
26
+ spec.add_runtime_dependency 'bech32', '~> 1.0.3'
27
+ spec.add_runtime_dependency 'daemon-spawn'
28
+ spec.add_runtime_dependency 'thor'
29
+ spec.add_runtime_dependency 'ffi'
30
+ spec.add_runtime_dependency 'leb128', '~> 1.0.0'
31
+ spec.add_runtime_dependency 'eventmachine_httpserver'
32
+ spec.add_runtime_dependency 'rest-client'
33
+ spec.add_runtime_dependency 'iniparse'
34
+ spec.add_runtime_dependency 'siphash'
35
+ spec.add_runtime_dependency 'protobuf', '3.8.5'
36
+ spec.add_runtime_dependency 'scrypt'
37
+ spec.add_runtime_dependency 'activesupport', '>= 5.2.3'
38
+
39
+ # for options
40
+ spec.add_development_dependency 'leveldb-native'
41
+
42
+ spec.add_development_dependency 'bundler'
43
+ spec.add_development_dependency 'rake', '~> 10.0'
44
+ spec.add_development_dependency 'rspec', '~> 3.0'
45
+ spec.add_development_dependency 'timecop'
46
+
47
+ end