bitcoinrb 0.3.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +37 -0
  3. data/.rspec_parallel +2 -0
  4. data/.ruby-version +1 -1
  5. data/README.md +17 -6
  6. data/bitcoinrb.gemspec +9 -8
  7. data/exe/bitcoinrbd +5 -0
  8. data/lib/bitcoin.rb +37 -19
  9. data/lib/bitcoin/bip85_entropy.rb +111 -0
  10. data/lib/bitcoin/block_filter.rb +14 -0
  11. data/lib/bitcoin/block_header.rb +2 -0
  12. data/lib/bitcoin/chain_params.rb +9 -8
  13. data/lib/bitcoin/chainparams/regtest.yml +1 -1
  14. data/lib/bitcoin/chainparams/signet.yml +39 -0
  15. data/lib/bitcoin/chainparams/testnet.yml +1 -1
  16. data/lib/bitcoin/constants.rb +44 -10
  17. data/lib/bitcoin/descriptor.rb +1 -1
  18. data/lib/bitcoin/errors.rb +19 -0
  19. data/lib/bitcoin/ext.rb +6 -0
  20. data/lib/bitcoin/ext/array_ext.rb +22 -0
  21. data/lib/bitcoin/ext/ecdsa.rb +36 -0
  22. data/lib/bitcoin/ext/json_parser.rb +46 -0
  23. data/lib/bitcoin/ext_key.rb +51 -20
  24. data/lib/bitcoin/key.rb +89 -30
  25. data/lib/bitcoin/key_path.rb +12 -5
  26. data/lib/bitcoin/message.rb +79 -0
  27. data/lib/bitcoin/message/addr_v2.rb +34 -0
  28. data/lib/bitcoin/message/base.rb +17 -0
  29. data/lib/bitcoin/message/cf_parser.rb +16 -0
  30. data/lib/bitcoin/message/cfcheckpt.rb +36 -0
  31. data/lib/bitcoin/message/cfheaders.rb +40 -0
  32. data/lib/bitcoin/message/cfilter.rb +35 -0
  33. data/lib/bitcoin/message/fee_filter.rb +1 -1
  34. data/lib/bitcoin/message/filter_load.rb +3 -3
  35. data/lib/bitcoin/message/get_cfcheckpt.rb +29 -0
  36. data/lib/bitcoin/message/get_cfheaders.rb +24 -0
  37. data/lib/bitcoin/message/get_cfilters.rb +25 -0
  38. data/lib/bitcoin/message/header_and_short_ids.rb +1 -1
  39. data/lib/bitcoin/message/inventory.rb +1 -1
  40. data/lib/bitcoin/message/merkle_block.rb +1 -1
  41. data/lib/bitcoin/message/network_addr.rb +141 -18
  42. data/lib/bitcoin/message/ping.rb +1 -1
  43. data/lib/bitcoin/message/pong.rb +1 -1
  44. data/lib/bitcoin/message/send_addr_v2.rb +13 -0
  45. data/lib/bitcoin/message/send_cmpct.rb +2 -2
  46. data/lib/bitcoin/message/tx.rb +1 -1
  47. data/lib/bitcoin/message/version.rb +7 -0
  48. data/lib/bitcoin/message_sign.rb +47 -0
  49. data/lib/bitcoin/mnemonic.rb +7 -7
  50. data/lib/bitcoin/network/peer.rb +9 -4
  51. data/lib/bitcoin/network/peer_discovery.rb +1 -1
  52. data/lib/bitcoin/node/cli.rb +14 -10
  53. data/lib/bitcoin/node/configuration.rb +3 -1
  54. data/lib/bitcoin/node/spv.rb +9 -1
  55. data/lib/bitcoin/opcodes.rb +14 -1
  56. data/lib/bitcoin/out_point.rb +2 -0
  57. data/lib/bitcoin/payment_code.rb +92 -0
  58. data/lib/bitcoin/payments/payment.pb.rb +1 -1
  59. data/lib/bitcoin/psbt/hd_key_path.rb +1 -1
  60. data/lib/bitcoin/psbt/input.rb +9 -18
  61. data/lib/bitcoin/psbt/output.rb +1 -1
  62. data/lib/bitcoin/psbt/tx.rb +12 -17
  63. data/lib/bitcoin/rpc/bitcoin_core_client.rb +22 -12
  64. data/lib/bitcoin/rpc/request_handler.rb +5 -5
  65. data/lib/bitcoin/script/script.rb +96 -39
  66. data/lib/bitcoin/script/script_error.rb +27 -1
  67. data/lib/bitcoin/script/script_interpreter.rb +166 -66
  68. data/lib/bitcoin/script/tx_checker.rb +62 -14
  69. data/lib/bitcoin/secp256k1.rb +1 -0
  70. data/lib/bitcoin/secp256k1/native.rb +184 -17
  71. data/lib/bitcoin/secp256k1/rfc6979.rb +43 -0
  72. data/lib/bitcoin/secp256k1/ruby.rb +112 -56
  73. data/lib/bitcoin/sighash_generator.rb +156 -0
  74. data/lib/bitcoin/store.rb +1 -0
  75. data/lib/bitcoin/store/chain_entry.rb +1 -0
  76. data/lib/bitcoin/store/utxo_db.rb +226 -0
  77. data/lib/bitcoin/taproot.rb +9 -0
  78. data/lib/bitcoin/taproot/leaf_node.rb +23 -0
  79. data/lib/bitcoin/taproot/simple_builder.rb +139 -0
  80. data/lib/bitcoin/tx.rb +34 -104
  81. data/lib/bitcoin/tx_in.rb +4 -5
  82. data/lib/bitcoin/tx_out.rb +2 -3
  83. data/lib/bitcoin/util.rb +22 -6
  84. data/lib/bitcoin/version.rb +1 -1
  85. data/lib/bitcoin/wallet.rb +1 -0
  86. data/lib/bitcoin/wallet/account.rb +2 -1
  87. data/lib/bitcoin/wallet/base.rb +2 -2
  88. data/lib/bitcoin/wallet/master_key.rb +1 -0
  89. data/lib/bitcoin/wallet/utxo.rb +37 -0
  90. metadata +86 -32
  91. data/.travis.yml +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df5d412165565c3dde3e626417f6a08e08d8698f66712c080b967f5d1e4e1688
4
- data.tar.gz: 305d2ffba9338edfb26364da1d76ed166c1d2bbb253a2f3337db346a565247ea
3
+ metadata.gz: af836e45167490d93d1d9b5d8f29f4e473a78dc314b98cd789bb47e347a42037
4
+ data.tar.gz: 020fe745057f3b987c8bd8828be9f7f657d3085a7d8ee37cf0218b0761202325
5
5
  SHA512:
6
- metadata.gz: 171bd45d707d1d49398d1f272ee7559bc88b5df66a8a4c15afbc107eb9c631eb243c3fad3ec3073d455c8e6c49048ddcd0bedbe96cb4c06b6af7daaad9d31c20
7
- data.tar.gz: ef995e0e5db4d6b7352de9f7efa7d97bd2a6c0de39c811e352595c89a6b79966ab50304b7e0059bc7c2afff1826a37c99748233f79956d1dca80a745d6aec888
6
+ metadata.gz: 0db2c744f372a2e71c337cb8970bb8cad81f37aa81824504d5fe6ee46819516ca6869c3cab0af1474aa0906905496af3e8f97eacc6cdaf9b7eced0a6cd247ff2
7
+ data.tar.gz: dab1256db8085c43ec5ab5d784d465cbdb3d74e663d4f052fd76161decacd9fb5fe22b3c2f99cc20381dda8ea203d63866db45e6e8c8ed6d8b4a2d4c27acfe89
@@ -0,0 +1,37 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Install leveldb
27
+ run: sudo apt-get install libleveldb-dev
28
+ - name: Set up Ruby
29
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
30
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
31
+ # uses: ruby/setup-ruby@v1
32
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
33
+ with:
34
+ ruby-version: ${{ matrix.ruby-version }}
35
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
36
+ - name: Run tests
37
+ run: bundle exec rake spec
data/.rspec_parallel ADDED
@@ -0,0 +1,2 @@
1
+ --format ParallelTests::RSpec::RuntimeLogger --out tmp/parallel_runtime_rspec.log
2
+ --format ParallelTests::RSpec::SummaryLogger --out tmp/spec_summary.log
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.6.3
1
+ ruby-3.0.0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Bitcoinrb [![Build Status](https://travis-ci.org/chaintope/bitcoinrb.svg?branch=master)](https://travis-ci.org/chaintope/bitcoinrb) [![Gem Version](https://badge.fury.io/rb/bitcoinrb.svg)](https://badge.fury.io/rb/bitcoinrb) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) <img src="http://segwit.co/static/public/images/logo.png" width="100">
1
+ # Bitcoinrb [![Build Status](https://github.com/chaintope/bitcoinrb/actions/workflows/ruby.yml/badge.svg?branch=master)](https://github.com/chaintope/bitcoinrb/actions/workflows/ruby.yml) [![Gem Version](https://badge.fury.io/rb/bitcoinrb.svg)](https://badge.fury.io/rb/bitcoinrb) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) <img src="http://segwit.co/static/public/images/logo.png" width="100">
2
2
 
3
3
 
4
4
  Bitcoinrb is a Ruby implementation of Bitcoin Protocol.
@@ -9,14 +9,17 @@ NOTE: Bitcoinrb work in progress, and there is a possibility of incompatible cha
9
9
 
10
10
  Bitcoinrb supports following feature:
11
11
 
12
- * Bitcoin script interpreter(including [BIP-65](https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki), [BIP-68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki), [BIP-112](https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki))
13
- * De/serialization of Bitcoin protocol network messages
14
- * De/serialization of blocks and transactions
12
+ * [Bitcoin script interpreter](https://github.com/chaintope/bitcoinrb/wiki/Script)(including [BIP-65](https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki), [BIP-68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki), [BIP-112](https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki))
13
+ * [De/serialization of Bitcoin protocol network messages](https://github.com/chaintope/bitcoinrb/wiki/P2P-Message)
14
+ * De/serialization of blocks and [transactions](https://github.com/chaintope/bitcoinrb/wiki/Transaction)
15
15
  * Key generation and verification for ECDSA, including [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) and [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) supports.
16
16
  * ECDSA signature(RFC6979 -Deterministic ECDSA, LOW-S, LOW-R support)
17
17
  * Segwit support (parsing segwit payload, Bech32 address, sign for segwit tx, [BIP-141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki), [BIP-143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki), [BIP-144](https://github.com/bitcoin/bips/blob/master/bip-0144.mediawiki))
18
18
  * [BIP-173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) Bech32 address support
19
19
  * [BIP-174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki) PSBT(Partially Signed Bitcoin Transaction) support
20
+ * [BIP-85](https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki) Deterministic Entropy From BIP32 Keychains support by `Bitcoin::BIP85Entropy` class.
21
+ * Schnorr signature([BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki))
22
+ * Taproot consensus([BIP-341](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki) and [BIP-342](https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki))
20
23
  * [WIP] SPV node
21
24
  * [WIP] 0ff-chain protocol
22
25
 
@@ -38,8 +41,8 @@ If you use node features, please install level DB as follows.
38
41
 
39
42
  and put `leveldb-native` in your Gemfile and run bundle install.
40
43
 
41
- ```
42
- gem leveldb-native
44
+ ```ruby
45
+ gem 'leveldb-native'
43
46
  ```
44
47
 
45
48
  ## Installation
@@ -92,6 +95,14 @@ Bitcoin.chain_params = :regtest
92
95
 
93
96
  This parameter is described in https://github.com/chaintope/bitcoinrb/blob/master/lib/bitcoin/chainparams/regtest.yml.
94
97
 
98
+ * default signet
99
+
100
+ ```ruby
101
+ Bitcoin.chain_params = :signet
102
+ ```
103
+
104
+ This parameter is described in https://github.com/chaintope/bitcoinrb/blob/master/lib/bitcoin/chainparams/signet.yml.
105
+
95
106
  ## Contributing
96
107
 
97
108
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bitcoinrb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/bitcoinrb.gemspec CHANGED
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ["azuchi"]
11
11
  spec.email = ["azuchi@chaintope.com"]
12
12
 
13
- spec.summary = %q{[WIP]The implementation of Bitcoin Protocol for Ruby.}
14
- spec.description = %q{[WIP]The implementation of Bitcoin Protocol for Ruby.}
13
+ spec.summary = %q{The implementation of Bitcoin Protocol for Ruby.}
14
+ spec.description = %q{The implementation of Bitcoin Protocol for Ruby.}
15
15
  spec.homepage = 'https://github.com/chaintope/bitcoinrb'
16
16
  spec.license = "MIT"
17
17
 
@@ -23,25 +23,26 @@ Gem::Specification.new do |spec|
23
23
  spec.add_runtime_dependency 'ecdsa'
24
24
  spec.add_runtime_dependency 'eventmachine'
25
25
  spec.add_runtime_dependency 'murmurhash3'
26
- spec.add_runtime_dependency 'bech32', '~> 1.0.3'
26
+ spec.add_runtime_dependency 'bech32', '~> 1.1.0'
27
27
  spec.add_runtime_dependency 'daemon-spawn'
28
28
  spec.add_runtime_dependency 'thor'
29
29
  spec.add_runtime_dependency 'ffi'
30
30
  spec.add_runtime_dependency 'leb128', '~> 1.0.0'
31
31
  spec.add_runtime_dependency 'eventmachine_httpserver'
32
- spec.add_runtime_dependency 'rest-client'
33
32
  spec.add_runtime_dependency 'iniparse'
34
33
  spec.add_runtime_dependency 'siphash'
35
34
  spec.add_runtime_dependency 'protobuf', '3.8.5'
36
- spec.add_runtime_dependency 'scrypt'
37
- spec.add_runtime_dependency 'activesupport', '~> 5.2.3'
35
+ spec.add_runtime_dependency 'json_pure', '>= 2.3.1'
36
+ spec.add_runtime_dependency 'bip-schnorr', '>= 0.4.0'
37
+ spec.add_runtime_dependency 'base32', '>= 0.3.4'
38
38
 
39
39
  # for options
40
40
  spec.add_development_dependency 'leveldb-native'
41
41
 
42
42
  spec.add_development_dependency 'bundler'
43
- spec.add_development_dependency 'rake', '~> 10.0'
43
+ spec.add_development_dependency 'rake', '>= 12.3.3'
44
44
  spec.add_development_dependency 'rspec', '~> 3.0'
45
45
  spec.add_development_dependency 'timecop'
46
-
46
+ spec.add_development_dependency 'webmock', '>= 3.11.1'
47
+ spec.add_development_dependency 'parallel', '>= 1.20.1'
47
48
  end
data/exe/bitcoinrbd CHANGED
@@ -12,6 +12,11 @@ class BitcoinDaemon < DaemonSpawn::Base
12
12
  node.run
13
13
  end
14
14
 
15
+ def stop
16
+ puts "Stopping Bitcoinrb daemon : #{Time.now}"
17
+ node.shutdown
18
+ end
19
+
15
20
  end
16
21
 
17
22
  class Bitcoinrbd < Thor
data/lib/bitcoin.rb CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  require 'bitcoin/version'
5
5
  require 'eventmachine'
6
- require 'ecdsa'
6
+ require 'schnorr'
7
7
  require 'securerandom'
8
8
  require 'json'
9
9
  require 'bech32'
@@ -14,6 +14,7 @@ require_relative 'openassets'
14
14
 
15
15
  module Bitcoin
16
16
 
17
+ autoload :Ext, 'bitcoin/ext'
17
18
  autoload :Util, 'bitcoin/util'
18
19
  autoload :ChainParams, 'bitcoin/chain_params'
19
20
  autoload :Message, 'bitcoin/message'
@@ -54,8 +55,16 @@ module Bitcoin
54
55
  autoload :KeyPath, 'bitcoin/key_path'
55
56
  autoload :Descriptor, 'bitcoin/descriptor'
56
57
  autoload :SLIP39, 'bitcoin/slip39'
58
+ autoload :Aezeed, 'bitcoin/aezeed'
59
+ autoload :PaymentCode, 'bitcoin/payment_code'
60
+ autoload :BIP85Entropy, 'bitcoin/bip85_entropy'
61
+ autoload :Errors, 'bitcoin/errors'
62
+ autoload :SigHashGenerator, 'bitcoin/sighash_generator'
63
+ autoload :MessageSign, 'bitcoin/message_sign'
64
+ autoload :Taproot, 'bitcoin/taproot'
57
65
 
58
66
  require_relative 'bitcoin/constants'
67
+ require_relative 'bitcoin/ext/ecdsa'
59
68
 
60
69
  extend Util
61
70
 
@@ -63,7 +72,7 @@ module Bitcoin
63
72
 
64
73
  # set bitcoin network chain params
65
74
  def self.chain_params=(name)
66
- raise "chain params for #{name} is not defined." unless %i(mainnet testnet regtest).include?(name.to_sym)
75
+ raise "chain params for #{name} is not defined." unless %i(mainnet testnet regtest signet).include?(name.to_sym)
67
76
  @current_chain = nil
68
77
  @chain_param = name.to_sym
69
78
  end
@@ -78,6 +87,8 @@ module Bitcoin
78
87
  @current_chain = Bitcoin::ChainParams.testnet
79
88
  when :regtest
80
89
  @current_chain = Bitcoin::ChainParams.regtest
90
+ when :signet
91
+ @current_chain = Bitcoin::ChainParams.signet
81
92
  end
82
93
  @current_chain
83
94
  end
@@ -104,7 +115,7 @@ module Bitcoin
104
115
  class ::String
105
116
  # binary convert to hex string
106
117
  def bth
107
- unpack('H*').first
118
+ unpack1('H*')
108
119
  end
109
120
 
110
121
  # hex string convert to binary
@@ -124,14 +135,7 @@ module Bitcoin
124
135
 
125
136
  # get opcode
126
137
  def opcode
127
- case encoding
128
- when Encoding::ASCII_8BIT
129
- each_byte.next
130
- when Encoding::US_ASCII
131
- ord
132
- else
133
- to_i
134
- end
138
+ force_encoding(Encoding::ASCII_8BIT).ord
135
139
  end
136
140
 
137
141
  def opcode?
@@ -161,6 +165,27 @@ module Bitcoin
161
165
  self[offset..-1]
162
166
  end
163
167
 
168
+ def valid_pushdata_length?
169
+ buf = StringIO.new(self)
170
+ opcode = buf.read(1).ord
171
+ offset = 1
172
+ return false if buf.eof?
173
+ len = case opcode
174
+ when Bitcoin::Opcodes::OP_PUSHDATA1
175
+ offset += 1
176
+ buf.read(1).unpack1('C')
177
+ when Bitcoin::Opcodes::OP_PUSHDATA2
178
+ offset += 2
179
+ buf.read(2).unpack1('v')
180
+ when Bitcoin::Opcodes::OP_PUSHDATA4
181
+ offset += 4
182
+ buf.read(4).unpack1('V')
183
+ else
184
+ opcode
185
+ end
186
+ self.bytesize == len + offset
187
+ end
188
+
164
189
  # whether value is hex or not hex
165
190
  # @return [Boolean] return true if data is hex
166
191
  def valid_hex?
@@ -188,7 +213,7 @@ module Bitcoin
188
213
  if value.is_a?(Array)
189
214
  result.update(key => value.map{|v|v.to_h})
190
215
  else
191
- result.update(key => value)
216
+ result.update(key => value.class.to_s.start_with?("Bitcoin::") ? value.to_h : value)
192
217
  end
193
218
  end
194
219
  end
@@ -215,11 +240,4 @@ module Bitcoin
215
240
  end
216
241
  end
217
242
 
218
- class ::ECDSA::Signature
219
- # convert signature to der string.
220
- def to_der
221
- ECDSA::Format::SignatureDerString.encode(self)
222
- end
223
- end
224
-
225
243
  end
@@ -0,0 +1,111 @@
1
+ module Bitcoin
2
+
3
+ # Deterministic Entropy From BIP32 Keychains
4
+ # https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki
5
+ class BIP85Entropy
6
+
7
+ BIP85_PATH = 83696968 + HARDENED_THRESHOLD
8
+
9
+ include Bitcoin::KeyPath
10
+
11
+ attr_reader :root_key #hex format
12
+
13
+ # Import root key.
14
+ # @param [String] base58 master bip32 root key.
15
+ # @return [Bitcoin::BIP85Entropy]
16
+ def self.from_base58(base58)
17
+ key = Bitcoin::ExtKey.from_base58(base58)
18
+ self.new(key)
19
+ end
20
+
21
+ # derive entropy
22
+ # @param [String] path derive path.
23
+ # @return [Tuple(String, Object)] a tuple of entropy with hex format and results depending the application.
24
+ def derive(path)
25
+ raise ArgumentError, "Invalid BIP85 path format." unless path.start_with?("m/83696968'")
26
+ derived_key = root_key
27
+ parse_key_path(path).each{|num| derived_key = derived_key.derive(num)}
28
+ derived_key = derived_key.priv
29
+ entropy = Bitcoin.hmac_sha512("bip-entropy-from-k", derived_key.htb).bth
30
+ app_no = path.split('/')[2]
31
+ case app_no
32
+ when "39'"
33
+ bip39_entropy(path, entropy)
34
+ when "2'"
35
+ hd_seed_entropy(entropy)
36
+ when "32'"
37
+ xprv_entropy(entropy)
38
+ else
39
+ [entropy, entropy]
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def initialize(root_key)
46
+ @root_key = root_key
47
+ end
48
+
49
+ # derive BIP39 entropy.
50
+ def bip39_entropy(path, entropy)
51
+ params = path.split('/')
52
+ word_len = params[4]
53
+ language = code_to_language(params[3])
54
+ entropy = case word_len
55
+ when "12'"
56
+ entropy[0...32]
57
+ when "18'"
58
+ entropy[0...48]
59
+ when "24'"
60
+ entropy[0...64]
61
+ else
62
+ raise ArgumentError, "Word length #{word_len} does not supported."
63
+ end
64
+ mnemonic = Bitcoin::Mnemonic.new(language)
65
+ [entropy, mnemonic.to_mnemonic(entropy)]
66
+ end
67
+
68
+ # derive HD-Seed WIF entropy.
69
+ def hd_seed_entropy(entropy)
70
+ result = entropy[0...64]
71
+ [result, Bitcoin::Key.new(priv_key: result).to_wif]
72
+ end
73
+
74
+ # derive xprv entropy
75
+ def xprv_entropy(entropy)
76
+ chaincode = entropy[0...64]
77
+ private_key = Bitcoin::Key.new(priv_key: entropy[64..-1])
78
+ ext_key = Bitcoin::ExtKey.new
79
+ ext_key.key = private_key
80
+ ext_key.chain_code = chaincode.htb
81
+ ext_key.depth = 0
82
+ ext_key.number = 0
83
+ ext_key.parent_fingerprint = Bitcoin::ExtKey::MASTER_FINGERPRINT
84
+ [entropy, ext_key.to_base58]
85
+ end
86
+
87
+ # convert language code to language string.
88
+ def code_to_language(code)
89
+ case code
90
+ when "0'"
91
+ "english"
92
+ when "1'"
93
+ "japanese"
94
+ when "3'"
95
+ "spanish"
96
+ when "4'"
97
+ "chinese_simplified"
98
+ when "5'"
99
+ "chinese_traditional"
100
+ when "6'"
101
+ "french"
102
+ when "7'"
103
+ "italian"
104
+ else
105
+ raise ArgumentError, "bitcoinrb does not support language: #{code}"
106
+ end
107
+ end
108
+
109
+ end
110
+
111
+ end
@@ -43,6 +43,20 @@ module Bitcoin
43
43
  BlockFilter.new(filter_type, filter, block.block_hash)
44
44
  end
45
45
 
46
+ # Decode Block Filter from encoded filter
47
+ # @param [Integer] filter_type filter type.
48
+ # @param [String] block_hash block hash with hex format. not little endian.
49
+ # @param [String] encoded encoded_filter with hex format.
50
+ # @return [Bitcoin::BlockFilter] block filter object.
51
+ def self.decode(filter_type, block_hash, encoded)
52
+ filter = case filter_type
53
+ when TYPE[:basic]
54
+ GCSFilter.new(block_hash.htb[0...16], BASIC_FILTER_P, BASIC_FILTER_M, encoded_filter: encoded)
55
+ else
56
+ raise "unknown filter type: #{filter_type}."
57
+ end
58
+ BlockFilter.new(filter_type, filter, block_hash)
59
+ end
46
60
 
47
61
  # calculate filter hash.
48
62
  # @return [String] this filter hash with hex format.
@@ -3,6 +3,8 @@ module Bitcoin
3
3
  # Block Header
4
4
  class BlockHeader
5
5
 
6
+ include Bitcoin::HexConverter
7
+
6
8
  attr_accessor :version
7
9
  attr_accessor :prev_hash
8
10
  attr_accessor :merkle_root
@@ -36,9 +36,6 @@ module Bitcoin
36
36
 
37
37
  attr_accessor :dust_relay_fee
38
38
 
39
- # fork coin id.
40
- attr_accessor :fork_id
41
-
42
39
  # mainnet genesis
43
40
  def self.mainnet
44
41
  init('mainnet')
@@ -54,6 +51,11 @@ module Bitcoin
54
51
  init('regtest')
55
52
  end
56
53
 
54
+ # signet genesis
55
+ def self.signet
56
+ init('signet')
57
+ end
58
+
57
59
  def mainnet?
58
60
  network == 'mainnet'
59
61
  end
@@ -66,6 +68,10 @@ module Bitcoin
66
68
  network == 'regtest'
67
69
  end
68
70
 
71
+ def signet?
72
+ network == 'signet'
73
+ end
74
+
69
75
  def genesis_block
70
76
  header = Bitcoin::BlockHeader.new(
71
77
  genesis['version'], genesis['prev_hash'].rhex, genesis['merkle_root'].rhex,
@@ -73,11 +79,6 @@ module Bitcoin
73
79
  Bitcoin::Block.new(header)
74
80
  end
75
81
 
76
- # whether fork coin.
77
- def fork_chain?
78
- !fork_id.nil?
79
- end
80
-
81
82
  def self.init(name)
82
83
  i = YAML.load(File.open("#{__dir__}/chainparams/#{name}.yml"))
83
84
  i.dust_relay_fee ||= Bitcoin::DUST_RELAY_TX_FEE