openassets-ruby 0.4.9 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5daad66cb65aae696173dc3cba5afba5f8977dc8
4
- data.tar.gz: 69b6ede62314c4fa19159b12c32ddd69017184f9
3
+ metadata.gz: b72bda7683e85f0fab9de4bb661bf18126246157
4
+ data.tar.gz: d212bd8bcd3b784bd280c4f0f21c73a5e285113c
5
5
  SHA512:
6
- metadata.gz: cbf79b9c3896e27924fa445fddda472e44fd82288e84139dda121bad3fc238a1ee53ca6864d42b5d518dfe222e1137e2d9f9eca48c23cf81e8808c21a964e947
7
- data.tar.gz: 369a11d56c95c56f336353fed8ac5feed2e19015b7abc3d1237dff695f25a5f5afa60ac86097c2cd8bb0f619e09532284b04e6ea0c148f915868ca4883ef6dc2
6
+ metadata.gz: d8bde1f5913c9a701e57b48050cdf1ad2953c706daa66c186326ca7aaccb939a6504607c4e0946efef3eb7c7749e8af14d2d560743e867995b32c4cc6bec21ea
7
+ data.tar.gz: 72ed9e77591d9af4cfc8774580c1d866908fb1204e64e21b9a3b55f0e01ede935cf95181bb30ec9ec0b9830dbaf8c87a1914b4a5951db9e28862d2085065994a
@@ -58,7 +58,25 @@ module OpenAssets
58
58
  def self.parse_script(output_script)
59
59
  data = Bitcoin::Script.new(output_script).get_op_return_data
60
60
  return data if data.nil?
61
- data.start_with?(OAP_MARKER) ? data : nil
61
+ # check open assets marker
62
+ return nil unless data.start_with?(OAP_MARKER + VERSION)
63
+ # check asset quantity
64
+ offset = [OAP_MARKER + VERSION].pack('H*').length
65
+ count, offset = read_var_integer(data, offset)
66
+ return nil unless count
67
+ # check metadata
68
+ count.times do
69
+ quantity, length = read_leb128(data, offset)
70
+ return nil if quantity.nil? || (length - offset) > 9
71
+ offset = length
72
+ end
73
+ # check metadata
74
+ length, offset = read_var_integer(data, offset)
75
+ return nil unless length
76
+ puts [data].pack('H*').bytes.length
77
+ puts length + offset
78
+ return nil if [data].pack('H*').bytes.length < length + offset
79
+ data
62
80
  end
63
81
 
64
82
  # Creates an output script containing an OP_RETURN and a PUSHDATA from payload.
@@ -120,9 +120,53 @@ module OpenAssets
120
120
  pubkey_hash_to_asset_id(pubkey_hash)
121
121
  end
122
122
 
123
+ # read variable integer
124
+ # @param [String] data reading data
125
+ # @param [Integer] offset the position when reading from data.
126
+ # @return [[Integer, Integer]] decoded integer value and the reading byte length.
127
+ # https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
128
+ def read_var_integer(data, offset = 0)
129
+ raise ArgumentError, "data is nil." unless data
130
+ bytes = [data].pack('H*').bytes[offset..(offset + 9)] # 9 is variable integer max storage length.
131
+ first_byte = bytes[0]
132
+ if first_byte < 0xfd
133
+ [first_byte, offset + 1]
134
+ elsif first_byte == 0xfd
135
+ [calc_var_integer_val(bytes[1..2]), offset + 3]
136
+ elsif first_byte == 0xfe
137
+ [calc_var_integer_val(bytes[1..4]), offset + 5]
138
+ elsif first_byte == 0xff
139
+ [calc_var_integer_val(bytes[1..8]), offset + 9]
140
+ end
141
+ end
142
+
143
+ # read leb128 value
144
+ # @param [String] data reading data
145
+ # @param [Integer] offset start position when reading from data.
146
+ # @return [[Integer, Integer]] decoded integer value and the reading byte length.
147
+ def read_leb128(data, offset = 0)
148
+ bytes = [data].pack('H*').bytes
149
+ result = 0
150
+ shift = 0
151
+ while true
152
+ return [nil, offset] if bytes.length < 1 + offset
153
+ byte = bytes[offset..(offset + 1)][0]
154
+ result |= (byte & 0x7f) << shift
155
+ break if byte & 0x80 == 0
156
+ shift += 7
157
+ offset += 1
158
+ end
159
+ [result, offset + 1]
160
+ end
161
+
123
162
  private
124
163
  def oa_version_byte
125
164
  Bitcoin.network[:address_version] == "6f" ? OA_VERSION_BYTE_TESTNET : OA_VERSION_BYTE
126
165
  end
166
+
167
+ def calc_var_integer_val(byte_array)
168
+ byte_array.each_with_index.inject(0){|sum, pair| pair[1] == 0 ? pair[0] : sum + pair[0]*(256**pair[1])}
169
+ end
170
+
127
171
  end
128
172
  end
@@ -1,3 +1,3 @@
1
1
  module OpenAssets
2
- VERSION = '0.4.9'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openassets-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-05 00:00:00.000000000 Z
11
+ date: 2016-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bitcoin-ruby