tapyrus 0.2.13 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82146cdd150374b76b593273e44c31f4f578d38a1b7dcabea0b0d1fd9acbbb5f
4
- data.tar.gz: 8895de33dd2b36412f509c4e35465d513d3d6c98a0bcfc0e7db241a77acd8b3b
3
+ metadata.gz: 21c6e4a293a4eeb2945e7129cfa401927282e944913b992e837c92393a69f1bf
4
+ data.tar.gz: b03bb102f8856dce5246ba2a89ad6f648f7cf9df92c66616387bb9f1cc15810a
5
5
  SHA512:
6
- metadata.gz: e386ac9d8578846437902e78d79b3d4419c85df861c831c92641da94d2d46a37c4793efaafa8e28a1da4466e5347fbe79f66beb0d925fe2bfc83cca1d1e90c99
7
- data.tar.gz: a34fa0709093aafb118356b7736b409c733e1185a22538f7e2ed4392dac0ac661a652a31853f652d988860ee66c8b57124815b0fc28dd795b7bea97ad44182c5
6
+ metadata.gz: 4b15f3bc76c02d258b4f05def9f255a765e4b9f369d9d0ef5737b8a788ee7c352b605e2a2680da9e146c50567e06aa351316d3f7cbf6fe0830f39710c01616fa
7
+ data.tar.gz: 815a380de5e8c790beaf9d5508e5899d16e5ef58894de01ab9d64e011263b5d435b5c550c70f058ae640154944d99324967a9d1f74646107e08dc214317e6d8c
data/lib/tapyrus/ext.rb CHANGED
@@ -1,5 +1,21 @@
1
1
  module Tapyrus
2
2
  module Ext
3
3
  autoload :JsonParser, 'tapyrus/ext/json_parser'
4
+
5
+ refine Object do
6
+ def build_json
7
+ self.is_a?(Array) ? "[#{self.map { |o| o.to_h.to_json }.join(',')}]" : to_h.to_json
8
+ end
9
+
10
+ def to_h
11
+ return self if self.is_a?(String)
12
+ instance_variables.inject({}) do |result, var|
13
+ key = var.to_s
14
+ key.slice!(0) if key.start_with?('@')
15
+ value = instance_variable_get(var)
16
+ value.is_a?(Array) ? result.update(key => value.map { |v| v.to_h }) : result.update(key => value)
17
+ end
18
+ end
19
+ end
4
20
  end
5
21
  end
@@ -2,6 +2,8 @@ module Tapyrus
2
2
  module Network
3
3
  # P2P message handler used by peer connection class.
4
4
  module MessageHandler
5
+ using Tapyrus::Ext
6
+
5
7
  # handle p2p message.
6
8
  def handle(message)
7
9
  peer.last_recv = Time.now.to_i
@@ -96,7 +96,7 @@ module Tapyrus
96
96
  script = Tapyrus::Script.parse_from_payload(hex_script.htb)
97
97
  h = script.to_h
98
98
  h.delete(:hex)
99
- h[:p2sh] = script.to_p2sh.addresses.first unless script.p2sh?
99
+ h[:p2sh] = script.to_p2sh.to_addr unless script.p2sh?
100
100
  h
101
101
  rescue Exception
102
102
  raise ArgumentError.new('Script decode failed')
@@ -61,7 +61,7 @@ module Tapyrus
61
61
  end
62
62
 
63
63
  # Add color identifier to existing p2pkh or p2sh
64
- # @param [ColorIdentifier] color identifier
64
+ # @param [ColorIdentifier] color_id color identifier
65
65
  # @return [Script] CP2PKH or CP2SH script
66
66
  # @raise [ArgumentError] if color_id is nil or invalid
67
67
  # @raise [RuntimeError] if script is neither p2pkh nor p2sh
@@ -75,7 +75,6 @@ module Tapyrus
75
75
  end
76
76
 
77
77
  # Remove color identifier from cp2pkh or cp2sh
78
- # @param [ColorIdentifier] color identifier
79
78
  # @return [Script] P2PKH or P2SH script
80
79
  # @raise [RuntimeError] if script is neither cp2pkh nor cp2sh
81
80
  def remove_color
@@ -118,26 +117,18 @@ module Tapyrus
118
117
  # @param [String] addr address.
119
118
  # @return [Tapyrus::Script] parsed script.
120
119
  def self.parse_from_addr(addr)
121
- begin
122
- segwit_addr = Bech32::SegwitAddr.new(addr)
123
- raise 'Invalid hrp.' unless Tapyrus.chain_params.bech32_hrp == segwit_addr.hrp
124
- Tapyrus::Script.parse_from_payload(segwit_addr.to_script_pubkey.htb)
125
- rescue Exception => e
126
- hex, addr_version = Tapyrus.decode_base58_address(addr)
127
- case addr_version
128
- when Tapyrus.chain_params.address_version
129
- Tapyrus::Script.to_p2pkh(hex)
130
- when Tapyrus.chain_params.p2sh_version
131
- Tapyrus::Script.to_p2sh(hex)
132
- when Tapyrus.chain_params.cp2pkh_version
133
- color = Tapyrus::Color::ColorIdentifier.parse_from_payload(hex[0..65].htb)
134
- Tapyrus::Script.to_cp2pkh(color, hex[66..-1])
135
- when Tapyrus.chain_params.cp2sh_version
136
- color = Tapyrus::Color::ColorIdentifier.parse_from_payload(hex[0..65].htb)
137
- Tapyrus::Script.to_cp2sh(color, hex[66..-1])
138
- else
139
- throw e
140
- end
120
+ hex, addr_version = Tapyrus.decode_base58_address(addr)
121
+ case addr_version
122
+ when Tapyrus.chain_params.address_version
123
+ Tapyrus::Script.to_p2pkh(hex)
124
+ when Tapyrus.chain_params.p2sh_version
125
+ Tapyrus::Script.to_p2sh(hex)
126
+ when Tapyrus.chain_params.cp2pkh_version
127
+ color = Tapyrus::Color::ColorIdentifier.parse_from_payload(hex[0..65].htb)
128
+ Tapyrus::Script.to_cp2pkh(color, hex[66..-1])
129
+ when Tapyrus.chain_params.cp2sh_version
130
+ color = Tapyrus::Color::ColorIdentifier.parse_from_payload(hex[0..65].htb)
131
+ Tapyrus::Script.to_cp2sh(color, hex[66..-1])
141
132
  end
142
133
  end
143
134
 
@@ -189,6 +180,17 @@ module Tapyrus
189
180
  chunks.size == 0
190
181
  end
191
182
 
183
+ # Returns the address corresponding to this script. Return nil if there is no corresponding address.
184
+ # @return [String] address
185
+ def to_addr
186
+ return p2pkh_addr if p2pkh?
187
+ return p2sh_addr if p2sh?
188
+ return cp2pkh_addr if cp2pkh?
189
+ return cp2sh_addr if cp2sh?
190
+ nil
191
+ end
192
+
193
+ # @deprecated use #to_addr method.
192
194
  def addresses
193
195
  return [p2pkh_addr] if p2pkh?
194
196
  return [p2sh_addr] if p2sh?
@@ -1,3 +1,3 @@
1
1
  module Tapyrus
2
- VERSION = '0.2.13'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/tapyrus.rb CHANGED
@@ -156,22 +156,6 @@ module Tapyrus
156
156
  end
157
157
  end
158
158
 
159
- class ::Object
160
- def build_json
161
- self.is_a?(Array) ? "[#{self.map { |o| o.to_h.to_json }.join(',')}]" : to_h.to_json
162
- end
163
-
164
- def to_h
165
- return self if self.is_a?(String)
166
- instance_variables.inject({}) do |result, var|
167
- key = var.to_s
168
- key.slice!(0) if key.start_with?('@')
169
- value = instance_variable_get(var)
170
- value.is_a?(Array) ? result.update(key => value.map { |v| v.to_h }) : result.update(key => value)
171
- end
172
- end
173
- end
174
-
175
159
  class ::Integer
176
160
  def to_even_length_hex
177
161
  hex = to_s(16)
data/tapyrusrb.gemspec CHANGED
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_development_dependency 'leveldb-native'
37
37
 
38
38
  spec.add_development_dependency 'bundler'
39
- spec.add_development_dependency 'prettier'
39
+ spec.add_development_dependency 'prettier', '2.0.0'
40
40
  spec.add_development_dependency 'rake', '>= 12.3.3'
41
41
  spec.add_development_dependency 'rspec', '~> 3.0'
42
42
  spec.add_development_dependency 'timecop'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tapyrus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-03 00:00:00.000000000 Z
11
+ date: 2022-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ecdsa
@@ -210,16 +210,16 @@ dependencies:
210
210
  name: prettier
211
211
  requirement: !ruby/object:Gem::Requirement
212
212
  requirements:
213
- - - ">="
213
+ - - '='
214
214
  - !ruby/object:Gem::Version
215
- version: '0'
215
+ version: 2.0.0
216
216
  type: :development
217
217
  prerelease: false
218
218
  version_requirements: !ruby/object:Gem::Requirement
219
219
  requirements:
220
- - - ">="
220
+ - - '='
221
221
  - !ruby/object:Gem::Version
222
- version: '0'
222
+ version: 2.0.0
223
223
  - !ruby/object:Gem::Dependency
224
224
  name: rake
225
225
  requirement: !ruby/object:Gem::Requirement
@@ -425,7 +425,7 @@ homepage: https://github.com/chaintope/tapyrusrb
425
425
  licenses:
426
426
  - MIT
427
427
  metadata: {}
428
- post_install_message:
428
+ post_install_message:
429
429
  rdoc_options: []
430
430
  require_paths:
431
431
  - lib
@@ -440,8 +440,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
440
440
  - !ruby/object:Gem::Version
441
441
  version: '0'
442
442
  requirements: []
443
- rubygems_version: 3.2.22
444
- signing_key:
443
+ rubygems_version: 3.0.6
444
+ signing_key:
445
445
  specification_version: 4
446
446
  summary: The implementation of Tapyrus Protocol for Ruby.
447
447
  test_files: []