tapyrus 0.2.3 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +37 -0
  3. data/.prettierignore +3 -0
  4. data/.prettierrc.yaml +3 -0
  5. data/CODE_OF_CONDUCT.md +7 -7
  6. data/README.md +14 -17
  7. data/Rakefile +3 -3
  8. data/lib/openassets.rb +0 -2
  9. data/lib/openassets/marker_output.rb +0 -4
  10. data/lib/openassets/payload.rb +4 -10
  11. data/lib/schnorr.rb +2 -3
  12. data/lib/schnorr/signature.rb +3 -6
  13. data/lib/tapyrus.rb +7 -30
  14. data/lib/tapyrus/base58.rb +7 -6
  15. data/lib/tapyrus/block.rb +1 -2
  16. data/lib/tapyrus/block_header.rb +15 -9
  17. data/lib/tapyrus/bloom_filter.rb +5 -3
  18. data/lib/tapyrus/chain_params.rb +1 -4
  19. data/lib/tapyrus/chainparams/dev.yml +3 -2
  20. data/lib/tapyrus/chainparams/prod.yml +3 -2
  21. data/lib/tapyrus/constants.rb +29 -23
  22. data/lib/tapyrus/errors.rb +1 -3
  23. data/lib/tapyrus/ext.rb +1 -1
  24. data/lib/tapyrus/ext/ecdsa.rb +4 -4
  25. data/lib/tapyrus/ext/json_parser.rb +1 -4
  26. data/lib/tapyrus/ext_key.rb +38 -34
  27. data/lib/tapyrus/key.rb +31 -35
  28. data/lib/tapyrus/key_path.rb +15 -12
  29. data/lib/tapyrus/logger.rb +20 -16
  30. data/lib/tapyrus/merkle_tree.rb +19 -20
  31. data/lib/tapyrus/message.rb +14 -16
  32. data/lib/tapyrus/message/addr.rb +1 -7
  33. data/lib/tapyrus/message/base.rb +0 -3
  34. data/lib/tapyrus/message/block.rb +2 -9
  35. data/lib/tapyrus/message/block_transaction_request.rb +3 -6
  36. data/lib/tapyrus/message/block_transactions.rb +2 -6
  37. data/lib/tapyrus/message/block_txn.rb +0 -4
  38. data/lib/tapyrus/message/cmpct_block.rb +1 -7
  39. data/lib/tapyrus/message/error.rb +1 -4
  40. data/lib/tapyrus/message/fee_filter.rb +1 -4
  41. data/lib/tapyrus/message/filter_add.rb +0 -4
  42. data/lib/tapyrus/message/filter_clear.rb +0 -4
  43. data/lib/tapyrus/message/filter_load.rb +2 -5
  44. data/lib/tapyrus/message/get_addr.rb +0 -4
  45. data/lib/tapyrus/message/get_block_txn.rb +0 -4
  46. data/lib/tapyrus/message/get_blocks.rb +0 -3
  47. data/lib/tapyrus/message/get_data.rb +1 -4
  48. data/lib/tapyrus/message/get_headers.rb +1 -3
  49. data/lib/tapyrus/message/header_and_short_ids.rb +3 -9
  50. data/lib/tapyrus/message/headers.rb +0 -4
  51. data/lib/tapyrus/message/headers_parser.rb +3 -8
  52. data/lib/tapyrus/message/inv.rb +1 -4
  53. data/lib/tapyrus/message/inventories_parser.rb +2 -7
  54. data/lib/tapyrus/message/inventory.rb +12 -5
  55. data/lib/tapyrus/message/mem_pool.rb +0 -4
  56. data/lib/tapyrus/message/merkle_block.rb +4 -9
  57. data/lib/tapyrus/message/network_addr.rb +7 -6
  58. data/lib/tapyrus/message/not_found.rb +0 -3
  59. data/lib/tapyrus/message/ping.rb +0 -3
  60. data/lib/tapyrus/message/pong.rb +0 -3
  61. data/lib/tapyrus/message/prefilled_tx.rb +0 -4
  62. data/lib/tapyrus/message/reject.rb +0 -3
  63. data/lib/tapyrus/message/send_cmpct.rb +1 -3
  64. data/lib/tapyrus/message/send_headers.rb +0 -3
  65. data/lib/tapyrus/message/tx.rb +0 -4
  66. data/lib/tapyrus/message/ver_ack.rb +1 -5
  67. data/lib/tapyrus/message/version.rb +2 -5
  68. data/lib/tapyrus/mnemonic.rb +17 -15
  69. data/lib/tapyrus/network.rb +0 -2
  70. data/lib/tapyrus/network/connection.rb +0 -3
  71. data/lib/tapyrus/network/message_handler.rb +61 -60
  72. data/lib/tapyrus/network/peer.rb +13 -12
  73. data/lib/tapyrus/network/peer_discovery.rb +3 -5
  74. data/lib/tapyrus/network/pool.rb +12 -12
  75. data/lib/tapyrus/node.rb +1 -1
  76. data/lib/tapyrus/node/cli.rb +12 -14
  77. data/lib/tapyrus/node/configuration.rb +1 -3
  78. data/lib/tapyrus/node/spv.rb +2 -3
  79. data/lib/tapyrus/opcodes.rb +9 -7
  80. data/lib/tapyrus/out_point.rb +5 -5
  81. data/lib/tapyrus/rpc/http_server.rb +21 -22
  82. data/lib/tapyrus/rpc/request_handler.rb +42 -44
  83. data/lib/tapyrus/rpc/tapyrus_core_client.rb +53 -25
  84. data/lib/tapyrus/script/color.rb +20 -2
  85. data/lib/tapyrus/script/multisig.rb +13 -12
  86. data/lib/tapyrus/script/script.rb +104 -67
  87. data/lib/tapyrus/script/script_error.rb +1 -4
  88. data/lib/tapyrus/script/script_interpreter.rb +439 -399
  89. data/lib/tapyrus/script/tx_checker.rb +20 -10
  90. data/lib/tapyrus/secp256k1.rb +0 -4
  91. data/lib/tapyrus/secp256k1/native.rb +17 -18
  92. data/lib/tapyrus/secp256k1/rfc6979.rb +7 -4
  93. data/lib/tapyrus/secp256k1/ruby.rb +10 -12
  94. data/lib/tapyrus/slip39.rb +20 -5
  95. data/lib/tapyrus/slip39/share.rb +41 -29
  96. data/lib/tapyrus/slip39/sss.rb +101 -57
  97. data/lib/tapyrus/store.rb +1 -3
  98. data/lib/tapyrus/store/chain_entry.rb +0 -4
  99. data/lib/tapyrus/store/db.rb +0 -2
  100. data/lib/tapyrus/store/db/level_db.rb +5 -9
  101. data/lib/tapyrus/store/spv_chain.rb +11 -17
  102. data/lib/tapyrus/tx.rb +45 -37
  103. data/lib/tapyrus/tx_builder.rb +158 -0
  104. data/lib/tapyrus/tx_in.rb +1 -6
  105. data/lib/tapyrus/tx_out.rb +2 -7
  106. data/lib/tapyrus/util.rb +20 -7
  107. data/lib/tapyrus/validation.rb +12 -11
  108. data/lib/tapyrus/version.rb +1 -1
  109. data/lib/tapyrus/wallet/account.rb +22 -18
  110. data/lib/tapyrus/wallet/base.rb +12 -9
  111. data/lib/tapyrus/wallet/db.rb +6 -9
  112. data/lib/tapyrus/wallet/master_key.rb +2 -4
  113. data/tapyrusrb.gemspec +13 -16
  114. metadata +20 -31
  115. data/.travis.yml +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3838e6da04049e2e82adfa960eb79f538ac4c6778fb3010b9a1423a7c8f2977d
4
- data.tar.gz: 7c14929d2b2d7dcc388107e2ebbfdbd3891ecfc76a79a12282c518ee5cd160f0
3
+ metadata.gz: 0ad31bf4aa539aa394eeb5002fe11fc9b8c6a43aab73c9e36a63220159c2fb30
4
+ data.tar.gz: 8b7aaacfd238de278b334b7b26249045facf14843b8fb32985045c0a52b9cdc9
5
5
  SHA512:
6
- metadata.gz: 828f2fb6bf0a3bf120dab92afb04e4e7e8fce663108328ef6a6be122ff002a197807e0135310b225b65c493492afd0ccf7316b1e499eaa1ac92c0a38e9771199
7
- data.tar.gz: 2ddd51984e99b5e8d1742d2d2c21b48ab50fd3efbcb64e08eb7908bfa678f788b6caff53029005af5a2ccbfc423d8e4aeab38607654ec73a1c9d199e6a446e84
6
+ metadata.gz: ad3bc4d2fbaaf0e228cd7c07ee78e878f6b913e971645b8a6e188df70c9e363fbc210456833436f1e7ea780881bd10accdd7a069365e26fc09195744fe606766
7
+ data.tar.gz: b267bb2ff699df8822feffc75573db5e506dc5490f5f79146cfaf16d20abfd14660ddbfa4f2f01401d54b5bb9ecdb646613de8c7a28b01d48260aebb862834d3
@@ -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
+ runs-on: ubuntu-latest
19
+ strategy:
20
+ matrix:
21
+ ruby-version: ["2.5", "2.6", "2.7", "3.0"]
22
+
23
+ steps:
24
+ - run: sudo apt install libleveldb-dev
25
+ - uses: actions/checkout@v2
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ # uses: ruby/setup-ruby@v1
30
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Run prettier
35
+ run: bundle exec rbprettier --check .
36
+ - name: Run tests
37
+ run: bundle exec rake
data/.prettierignore ADDED
@@ -0,0 +1,3 @@
1
+ /vendor/
2
+ /tmp/
3
+ /spec/fixtures/**/*.json
data/.prettierrc.yaml ADDED
@@ -0,0 +1,3 @@
1
+ printWidth: 120
2
+ trailingComma: "none"
3
+ rubyArrayLiteral: false
data/CODE_OF_CONDUCT.md CHANGED
@@ -12,13 +12,13 @@ body size, race, ethnicity, age, religion, or nationality.
12
12
 
13
13
  Examples of unacceptable behavior by participants include:
14
14
 
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
15
+ - The use of sexualized language or imagery
16
+ - Personal attacks
17
+ - Trolling or insulting/derogatory comments
18
+ - Public or private harassment
19
+ - Publishing other's private information, such as physical or electronic
20
20
  addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
21
+ - Other unethical or unprofessional conduct
22
22
 
23
23
  Project maintainers have the right and responsibility to remove, edit, or
24
24
  reject comments, commits, code, wiki edits, issues, and other contributions
@@ -46,4 +46,4 @@ version 1.3.0, available at
46
46
  [http://contributor-covenant.org/version/1/3/0/][version]
47
47
 
48
48
  [homepage]: http://contributor-covenant.org
49
- [version]: http://contributor-covenant.org/version/1/3/0/
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/README.md CHANGED
@@ -1,21 +1,20 @@
1
1
  # Tapyrusrb [![Build Status](https://travis-ci.org/chaintope/tapyrusrb.svg?branch=master)](https://travis-ci.org/chaintope/tapyrusrb) [![Gem Version](https://badge.fury.io/rb/tapyrus.svg)](https://badge.fury.io/rb/tapyrus) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
2
2
 
3
-
4
3
  Tapyrusrb is a Ruby implementation of [Tapyrus](https://github.com/chaintope/tapyrus-core) Protocol.
5
4
 
6
- NOTE: Tapyrusrb work in progress, and there is a possibility of incompatible change.
5
+ NOTE: Tapyrusrb work in progress, and there is a possibility of incompatible change.
7
6
 
8
7
  ## Features
9
8
 
10
9
  Tapyrusrb supports following feature:
11
10
 
12
- * Tapyrus script interpreter
13
- * De/serialization of Tapyrus protocol network messages
14
- * De/serialization of blocks and transactions
15
- * Key generation and verification for Schnorr and 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
- * ECDSA signature(RFC6979 -Deterministic ECDSA, LOW-S, LOW-R support)
17
- * [WIP] SPV node
18
- * [WIP] 0ff-chain protocol
11
+ - Tapyrus script interpreter
12
+ - De/serialization of Tapyrus protocol network messages
13
+ - De/serialization of blocks and transactions
14
+ - Key generation and verification for Schnorr and 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).
15
+ - ECDSA signature(RFC6979 -Deterministic ECDSA, LOW-S, LOW-R support)
16
+ - [WIP] SPV node
17
+ - [WIP] 0ff-chain protocol
19
18
 
20
19
  ## Requirements
21
20
 
@@ -25,13 +24,13 @@ If you use node features, please install level DB as follows.
25
24
 
26
25
  #### install LevelDB
27
26
 
28
- * for Ubuntu
27
+ - for Ubuntu
29
28
 
30
- $ sudo apt-get install libleveldb-dev
29
+ $ sudo apt-get install libleveldb-dev
31
30
 
32
- + for Mac
31
+ * for Mac
33
32
 
34
- $ brew install leveldb
33
+ $ brew install leveldb
35
34
 
36
35
  and put `leveldb-native` in your Gemfile and run bundle install.
37
36
 
@@ -65,7 +64,7 @@ And then add to your .rb file:
65
64
 
66
65
  The parameters of the blockchain are managed by `Tapyrus::ChainParams`. Switch chain parameters as follows:
67
66
 
68
- * prod
67
+ - prod
69
68
 
70
69
  ```ruby
71
70
  Tapyrus.chain_params = :prod
@@ -73,7 +72,7 @@ Tapyrus.chain_params = :prod
73
72
 
74
73
  This parameter is described in https://github.com/chaintope/tapyrusrb/blob/master/lib/tapyrus/chainparams/prod.yml.
75
74
 
76
- * dev
75
+ - dev
77
76
 
78
77
  ```ruby
79
78
  Tapyrus.chain_params = :dev
@@ -85,8 +84,6 @@ This parameter is described in https://github.com/chaintope/tapyrusrb/blob/maste
85
84
 
86
85
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tapyrusrb. 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.
87
86
 
88
-
89
87
  ## License
90
88
 
91
89
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
92
-
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/lib/openassets.rb CHANGED
@@ -1,9 +1,7 @@
1
1
  require 'leb128'
2
2
 
3
3
  module OpenAssets
4
-
5
4
  autoload :MarkerOutput, 'openassets/marker_output'
6
5
  autoload :Payload, 'openassets/payload'
7
6
  autoload :Util, 'openassets/util'
8
-
9
7
  end
@@ -1,7 +1,5 @@
1
1
  module OpenAssets
2
-
3
2
  module MarkerOutput
4
-
5
3
  # whether this output is marker output for open assets.
6
4
  def open_assets_marker?
7
5
  return false unless script_pubkey.op_return?
@@ -14,7 +12,5 @@ module OpenAssets
14
12
  return nil unless script_pubkey.op_return?
15
13
  Payload.parse_from_payload(script_pubkey.op_return_data)
16
14
  end
17
-
18
15
  end
19
-
20
16
  end
@@ -1,13 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OpenAssets
4
-
5
4
  MARKER = "\x4f\x41"
6
5
  VERSION = "\x01\x00"
7
6
 
8
7
  # the open assets payload
9
8
  class Payload
10
-
11
9
  attr_accessor :quantities
12
10
  attr_accessor :metadata
13
11
 
@@ -26,14 +24,12 @@ module OpenAssets
26
24
  count = Tapyrus.unpack_var_int_from_io(buf)
27
25
  return nil unless count
28
26
  quantities = []
29
- count.times do
30
- quantities << LEB128.decode_unsigned(buf, buf.pos)
31
- end
27
+ count.times { quantities << LEB128.decode_unsigned(buf, buf.pos) }
32
28
  metadata_length = Tapyrus.unpack_var_int_from_io(buf)
33
29
  return nil if metadata_length.nil? || buf.length < metadata_length + buf.pos
34
30
  metadata = buf.read(metadata_length).each_byte.map(&:chr).join
35
31
  new(quantities, metadata)
36
- rescue
32
+ rescue StandardError
37
33
  # LEB128#decode_unsigned raise 'undefined method `unpack' for nil:NilClass'
38
34
  # for invalid format such as "018f8f" (the most significant bit of the last byte should be 0)
39
35
  nil
@@ -44,11 +40,9 @@ module OpenAssets
44
40
  payload = String.new
45
41
  payload << MARKER
46
42
  payload << VERSION
47
- payload << Tapyrus.pack_var_int(quantities.size) << quantities.map{|q| LEB128.encode_unsigned(q).read }.join
48
- payload << Tapyrus.pack_var_int(metadata.length) << metadata.bytes.map{|b| sprintf("%02x", b)}.join.htb
43
+ payload << Tapyrus.pack_var_int(quantities.size) << quantities.map { |q| LEB128.encode_unsigned(q).read }.join
44
+ payload << Tapyrus.pack_var_int(metadata.length) << metadata.bytes.map { |b| sprintf('%02x', b) }.join.htb
49
45
  payload
50
46
  end
51
-
52
47
  end
53
-
54
48
  end
data/lib/schnorr.rb CHANGED
@@ -76,8 +76,7 @@ module Schnorr
76
76
  # @return (Integer) digest e.
77
77
  def create_challenge(x, p, message)
78
78
  r_x = ECDSA::Format::IntegerOctetString.encode(x, GROUP.byte_length)
79
- p_str= p.to_hex.htb
79
+ p_str = p.to_hex.htb
80
80
  (ECDSA.normalize_digest(Digest::SHA256.digest(r_x + p_str + message), GROUP.bit_length)) % GROUP.order
81
81
  end
82
-
83
- end
82
+ end
@@ -1,11 +1,10 @@
1
1
  module Schnorr
2
-
3
- class InvalidSignatureError < StandardError; end
2
+ class InvalidSignatureError < StandardError
3
+ end
4
4
 
5
5
  # Instances of this class represents Schnorr signatures,
6
6
  # which are simply a pair of integers named `r` and `s`.
7
7
  class Signature
8
-
9
8
  attr_reader :r
10
9
  attr_reader :s
11
10
 
@@ -32,7 +31,5 @@ module Schnorr
32
31
  def encode
33
32
  ECDSA::Format::IntegerOctetString.encode(r, 32) + ECDSA::Format::IntegerOctetString.encode(s, 32)
34
33
  end
35
-
36
34
  end
37
-
38
- end
35
+ end
data/lib/tapyrus.rb CHANGED
@@ -13,7 +13,6 @@ require_relative 'openassets'
13
13
  require_relative 'schnorr'
14
14
 
15
15
  module Tapyrus
16
-
17
16
  autoload :Ext, 'tapyrus/ext'
18
17
  autoload :Util, 'tapyrus/util'
19
18
  autoload :ChainParams, 'tapyrus/chain_params'
@@ -49,6 +48,7 @@ module Tapyrus
49
48
  autoload :SLIP39, 'tapyrus/slip39'
50
49
  autoload :Color, 'tapyrus/script/color'
51
50
  autoload :Errors, 'tapyrus/errors'
51
+ autoload :TxBuilder, 'tapyrus/tx_builder'
52
52
 
53
53
  require_relative 'tapyrus/constants'
54
54
  require_relative 'tapyrus/ext/ecdsa'
@@ -59,7 +59,7 @@ module Tapyrus
59
59
 
60
60
  # set tapyrus network chain params
61
61
  def self.chain_params=(name)
62
- raise "chain params for #{name} is not defined." unless %i(prod dev).include?(name.to_sym)
62
+ raise "chain params for #{name} is not defined." unless %i[prod dev].include?(name.to_sym)
63
63
  @current_chain = nil
64
64
  @chain_param = name.to_sym
65
65
  end
@@ -118,14 +118,7 @@ module Tapyrus
118
118
 
119
119
  # get opcode
120
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
121
+ force_encoding(Encoding::ASCII_8BIT).ord
129
122
  end
130
123
 
131
124
  def opcode?
@@ -160,17 +153,11 @@ module Tapyrus
160
153
  def valid_hex?
161
154
  !self[/\H/]
162
155
  end
163
-
164
156
  end
165
157
 
166
158
  class ::Object
167
-
168
159
  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
160
+ self.is_a?(Array) ? "[#{self.map { |o| o.to_h.to_json }.join(',')}]" : to_h.to_json
174
161
  end
175
162
 
176
163
  def to_h
@@ -179,14 +166,9 @@ module Tapyrus
179
166
  key = var.to_s
180
167
  key.slice!(0) if key.start_with?('@')
181
168
  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
169
+ value.is_a?(Array) ? result.update(key => value.map { |v| v.to_h }) : result.update(key => value)
187
170
  end
188
171
  end
189
-
190
172
  end
191
173
 
192
174
  class ::Integer
@@ -200,13 +182,8 @@ module Tapyrus
200
182
  end
201
183
 
202
184
  # 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
185
+ def to_bits(length = nil)
186
+ length ? to_s(2).rjust(length, '0') : to_s(2)
209
187
  end
210
188
  end
211
-
212
189
  end
@@ -1,5 +1,4 @@
1
1
  module Tapyrus
2
-
3
2
  # Base58Check encoding
4
3
  # https://en.bitcoin.it/wiki/Base58Check_encoding
5
4
  module Base58
@@ -23,16 +22,18 @@ module Tapyrus
23
22
  # decode base58 string to hex value.
24
23
  def decode(base58_val)
25
24
  int_val = 0
26
- base58_val.reverse.split(//).each_with_index do |char,index|
27
- raise ArgumentError, 'Value passed not a valid Base58 String.' if (char_index = ALPHABET.index(char)).nil?
28
- int_val += char_index * (SIZE ** index)
29
- end
25
+ base58_val
26
+ .reverse
27
+ .split(//)
28
+ .each_with_index do |char, index|
29
+ raise ArgumentError, 'Value passed not a valid Base58 String.' if (char_index = ALPHABET.index(char)).nil?
30
+ int_val += char_index * (SIZE**index)
31
+ end
30
32
  s = int_val.to_even_length_hex
31
33
  s = '' if s == '00'
32
34
  leading_zero_bytes = (base58_val.match(/^([1]+)/) ? $1 : '').size
33
35
  s = ('00' * leading_zero_bytes) + s if leading_zero_bytes > 0
34
36
  s
35
37
  end
36
-
37
38
  end
38
39
  end
data/lib/tapyrus/block.rb CHANGED
@@ -37,6 +37,5 @@ module Tapyrus
37
37
  def height
38
38
  transactions.first.in.first.out_point.index
39
39
  end
40
-
41
40
  end
42
- end
41
+ end
@@ -1,18 +1,17 @@
1
1
  module Tapyrus
2
-
3
2
  # Block Header
4
3
  class BlockHeader
5
4
  include Tapyrus::HexConverter
6
5
  extend Tapyrus::Util
7
6
  include Tapyrus::Util
8
7
 
9
- X_FILED_TYPES = {none: 0, aggregate_pubkey: 1}
8
+ X_FILED_TYPES = { none: 0, aggregate_pubkey: 1 }
10
9
 
11
10
  attr_accessor :features
12
11
  attr_accessor :prev_hash
13
12
  attr_accessor :merkle_root
14
13
  attr_accessor :im_merkle_root # merkel root of immulable merkle tree which consist of immutable txid.
15
- attr_accessor :time # unix timestamp
14
+ attr_accessor :time # unix timestamp
16
15
  attr_accessor :x_field_type
17
16
  attr_accessor :x_field
18
17
  attr_accessor :proof
@@ -33,7 +32,16 @@ module Tapyrus
33
32
  features, prev_hash, merkle_root, im_merkle_root, time, x_filed_type = buf.read(105).unpack('Va32a32a32Vc')
34
33
  x_field = buf.read(unpack_var_int_from_io(buf)) unless x_filed_type == X_FILED_TYPES[:none]
35
34
  proof = buf.read(unpack_var_int_from_io(buf))
36
- new(features, prev_hash.bth, merkle_root.bth, im_merkle_root.bth, time, x_filed_type, x_field ? x_field.bth : x_field, proof.bth)
35
+ new(
36
+ features,
37
+ prev_hash.bth,
38
+ merkle_root.bth,
39
+ im_merkle_root.bth,
40
+ time,
41
+ x_filed_type,
42
+ x_field ? x_field.bth : x_field,
43
+ proof.bth
44
+ )
37
45
  end
38
46
 
39
47
  def to_payload(skip_proof = false)
@@ -87,9 +95,9 @@ module Tapyrus
87
95
  # @return [Boolean] if valid return true, otherwise false
88
96
  def valid_x_field?
89
97
  case x_field_type
90
- when X_FILED_TYPES[:none] then
98
+ when X_FILED_TYPES[:none]
91
99
  x_field.nil?
92
- when X_FILED_TYPES[:aggregate_pubkey] then
100
+ when X_FILED_TYPES[:aggregate_pubkey]
93
101
  Tapyrus::Key.new(pubkey: x_field).fully_valid_pubkey?
94
102
  else
95
103
  false
@@ -111,7 +119,5 @@ module Tapyrus
111
119
  def size
112
120
  to_payload.bytesize
113
121
  end
114
-
115
122
  end
116
-
117
- end
123
+ end