beowulf-ruby-testnet 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: fa825486188eacb7d78874e1df62ddc66ea95fc0b34c9afef73957d991e34896
4
- data.tar.gz: 521d4eabe5ec1d841b94ada6acd8d74779981357e710182a1892449a8e0d3c11
3
+ metadata.gz: 051760a12035fd7cec40d4580668e0a83d417f185df42bad78af8f182c6c4cfc
4
+ data.tar.gz: 26b1c49130ebe45f5dfe568b47b91526409ccfdfabcf7d87c5afd7da1ce29589
5
5
  SHA512:
6
- metadata.gz: 7a87dfef370a330650476c3fd5b03d9fe2b89162fff816c5605598f65572f98c845fd0d054e22e91f07adc82d8748f19389e5f10cde87b95ab3e5f8a2e551190
7
- data.tar.gz: 10bee63887ef219c547978d7cf2b22062ad02b804b25b43fb8d24b320607d17030264a4f0bf4687303ff1ee583640faef8d7e122f33a5c114dc6e5c1bf3b22fb
6
+ metadata.gz: a1727b552c1ad5a1bca03d122720f6dab9f307422bc8521f4a40f16f7625b6a1b33d049f1c7be67fcf8f5a2cc134c1043d20c374f76ea09f4e78653ec66f5948
7
+ data.tar.gz: 20fd619bf000ebe4ff86e254c8506fc5302ca9bbc1b465e9c0629ca8d89177015efd346230b390ac26623d85ed1ec8fb483df20312732f74c387b2a4f4c3080e
@@ -16,6 +16,10 @@ module Beowulf
16
16
  require 'beowulf/type/future'
17
17
  require 'beowulf/type/authority'
18
18
  require 'beowulf/type/authority_update'
19
+ require 'beowulf/type/extension_ids'
20
+ require 'beowulf/type/extension_json'
21
+ require 'beowulf/type/extension_types'
22
+ require 'beowulf/type/extension'
19
23
  require 'beowulf/logger'
20
24
  require 'beowulf/chain_config'
21
25
  require 'beowulf/api'
@@ -19,3 +19,5 @@ module Beowulf; class ApiError < BaseError; end; end
19
19
  module Beowulf; class TypeError < BaseError; end; end
20
20
  module Beowulf; class OperationError < BaseError; end; end
21
21
  module Beowulf; class TransactionError < BaseError; end; end
22
+ module Beowulf; class WalletError < BaseError; end; end
23
+ module Beowulf; class ExtensionError < BaseError; end; end
@@ -71,7 +71,9 @@ module Beowulf
71
71
 
72
72
  [@type, params]
73
73
  end
74
+
74
75
  private
76
+
75
77
  def self.broadcast_operations_json_path
76
78
  @broadcast_operations_json_path ||= "#{File.dirname(__FILE__)}/broadcast_operations.json"
77
79
  end
@@ -6,6 +6,7 @@ module Beowulf
6
6
  class Transaction
7
7
  include ChainConfig
8
8
  include Utils
9
+ include Type
9
10
 
10
11
  VALID_OPTIONS = %w(
11
12
  wif private_key ref_block_num ref_block_prefix expiration
@@ -54,6 +55,7 @@ module Beowulf
54
55
  @ref_block_num ||= nil
55
56
  @ref_block_prefix ||= nil
56
57
  @expiration ||= nil
58
+ @extensions ||= []
57
59
  @created_time ||= Time.now.utc.to_i
58
60
  @immutable_expiration = !!@expiration
59
61
 
@@ -132,6 +134,19 @@ module Beowulf
132
134
  @operations = operations
133
135
  end
134
136
 
137
+ def extensions
138
+ @extensions = @extensions.map do |ex|
139
+ case ex
140
+ when Extension then ex
141
+ else; Extension.new(ex)
142
+ end
143
+ end
144
+ end
145
+
146
+ def extensions=(extensions)
147
+ @extensions = extensions
148
+ end
149
+
135
150
  def shutdown
136
151
  @api.shutdown if !!@api
137
152
  @network_broadcast_api.shutdown if !!@network_broadcast_api
@@ -177,7 +192,7 @@ module Beowulf
177
192
  ref_block_num: @ref_block_num,
178
193
  ref_block_prefix: @ref_block_prefix,
179
194
  operations: operations.map { |op| op.payload },
180
- extensions: [],
195
+ extensions: extensions.map { |ex| ex.payload },
181
196
  created_time: @created_time,
182
197
  signatures: [hexlify(signature)]
183
198
  }
@@ -250,7 +265,15 @@ module Beowulf
250
265
  bytes << op.to_bytes # n-bit ...
251
266
  end
252
267
 
253
- bytes << 0x0000 # extensions # 16-bit, 4 Hex
268
+ if extensions.empty?
269
+ bytes << 0x0000 # extensions # 16-bit, 4 Hex
270
+ else
271
+ bytes << pakC(extensions.size) # 8-bit, 2 Hex
272
+ extensions.each do |ex|
273
+ bytes << ex.to_bytes
274
+ end
275
+ end
276
+
254
277
  bytes << pakQ(@created_time.to_i) # 64-bit, 16 Hex
255
278
 
256
279
  puts "Transaction.to_bytes:", hexlify(bytes)
@@ -0,0 +1,100 @@
1
+ require 'json'
2
+
3
+ module Beowulf
4
+ module Type
5
+ class Extension
6
+ include ExtensionIds
7
+ include ExtensionTypes
8
+ include Utils
9
+
10
+ def initialize(options = {})
11
+ opt = options.dup
12
+ @type = opt.delete(:type)
13
+
14
+ opt.each do |k, v|
15
+ instance_variable_set("@#{k}", type(@type, k, v))
16
+ end
17
+
18
+ unless Extension::known_extension_names.include? @type
19
+ raise ExtensionError, "Unsupported extension type: #{@type}"
20
+ end
21
+ end
22
+
23
+ def to_bytes
24
+ bytes = [id(@type.to_sym)].pack('C')
25
+
26
+ Extension::param_names(@type.to_sym).each do |p|
27
+ next unless defined? p
28
+ # puts p
29
+ v = instance_variable_get("@#{p}")
30
+ # puts v
31
+ bytes += v.to_bytes and next if v.respond_to? :to_bytes
32
+
33
+ bytes += case v
34
+ when Symbol then pakStr(v.to_s)
35
+ when String then pakStr(v)
36
+ when Integer then paks(v)
37
+ when TrueClass then pakC(1)
38
+ when FalseClass then pakC(0)
39
+ when ::Array then pakArr(v)
40
+ when ::Hash then pakHash(v)
41
+ when ExtensionJson then v.to_bytes
42
+ when NilClass then next
43
+ else
44
+ raise ExtensionError, "Unsupported type: #{v.class}"
45
+ end
46
+ end
47
+
48
+ bytes
49
+ end
50
+
51
+ def payload
52
+ params = {}
53
+ params["type"] = @type.to_s
54
+ Extension::param_names(@type.to_sym).each do |p|
55
+ next unless defined? p
56
+ # puts p
57
+ v = instance_variable_get("@#{p}")
58
+ # puts v
59
+ next if v.nil?
60
+ next if v.class == Beowulf::Type::Future
61
+
62
+ params[p] = case v
63
+ when Beowulf::Type::ExtensionJson
64
+ v.to_s
65
+ else; v
66
+ end
67
+ end
68
+
69
+ params
70
+ end
71
+
72
+ private
73
+
74
+ def self.list_extensions
75
+ data_exts = '[
76
+ {
77
+ "extension": "extension_json_type",
78
+ "params": [
79
+ "value"
80
+ ]
81
+ }
82
+ ]'
83
+ @list_extensions = JSON.parse(data_exts)
84
+ end
85
+
86
+ def self.known_extension_names
87
+ list_extensions.map { |ex| ex["extension"].to_sym }
88
+ end
89
+
90
+ def self.param_names(type)
91
+ list_extensions.each do |ex|
92
+ if ex['extension'].to_sym == type.to_sym
93
+ return ex['params'].map(&:to_sym)
94
+ end
95
+ end
96
+ end
97
+
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,13 @@
1
+ module Beowulf
2
+ module ExtensionIds
3
+ IDS = [
4
+ :void_t,
5
+ :extension_json_type
6
+ ]
7
+
8
+ def id(op)
9
+ IDS.find_index op
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+
3
+ module Beowulf
4
+ module Type
5
+ class ExtensionJson
6
+ include Utils
7
+
8
+ def initialize(options = {})
9
+ @data = options[:data] || ""
10
+ end
11
+
12
+ def to_bytes
13
+ pakStr(@data)
14
+ end
15
+
16
+ def to_s
17
+ {"data" => @data}
18
+ end
19
+
20
+ def to_json(options)
21
+ JSON.dump ({
22
+ :data => @data
23
+ })
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module Beowulf
2
+ module ExtensionTypes
3
+ TYPES = {
4
+ void_t: {
5
+ },
6
+ extension_json_type: {
7
+ value: Type::ExtensionJson
8
+ }
9
+ }
10
+
11
+ def type(key, param, value)
12
+ return if value.nil?
13
+ t = TYPES[key] or return value
14
+ p = t[param] or return value
15
+ p.new(value)
16
+ end
17
+ end
18
+ end
19
+
@@ -1,4 +1,4 @@
1
1
  module Beowulf
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  AGENT_ID = "beowulf/#{VERSION}"
4
4
  end
@@ -74,6 +74,7 @@ module Beowulf
74
74
  if !(achs.eql? bnchs)
75
75
  puts achs, bnchs
76
76
  puts 'Invalid private key (checksum miss-match)'
77
+ raise WalletError, "Invalid private key (checksum miss-match): #{achs} != #{bnchs}"
77
78
  end
78
79
  # Create Public Key
79
80
  bk = Bitcoin::Key.new(hexlify(tpk[1..-1]), nil, {compressed: true})
@@ -92,12 +93,15 @@ module Beowulf
92
93
  # Validate
93
94
  if password == nil || password.length == 0
94
95
  puts "Password is not empty."
96
+ raise WalletError, "Password is not empty."
95
97
  end
96
98
  if password.length < 8
97
99
  puts "Password length >= 8 character."
100
+ raise WalletError, "Password length >= 8 character."
98
101
  end
99
102
  if @name == nil || @name.length == 0
100
103
  puts "name is not empty."
104
+ raise WalletError, "name is not empty."
101
105
  end
102
106
 
103
107
  # Encrypt data
@@ -156,9 +160,11 @@ module Beowulf
156
160
  # Validate inputs
157
161
  if wallet_path_file == nil || wallet_path_file.length == 0
158
162
  puts "Path file wallet is not empty."
163
+ raise WalletError, "Path file wallet is not empty."
159
164
  end
160
165
  if password == nil || password.length == 0
161
166
  puts "Password is not empty."
167
+ raise WalletError, "Password is not empty."
162
168
  end
163
169
  # Read file wallet
164
170
  file = File.read(wallet_path_file)
@@ -186,6 +192,7 @@ module Beowulf
186
192
  dechs = msg_json['checksum']
187
193
  if !(chs.eql? dechs)
188
194
  puts "Password wrong."
195
+ raise WalletError, "Password wrong."
189
196
  end
190
197
  keys = msg_json['keys']
191
198
  keys.each do |key, value|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beowulf-ruby-testnet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - NghiaTC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-07 00:00:00.000000000 Z
11
+ date: 2020-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -426,6 +426,10 @@ files:
426
426
  - lib/beowulf/type/array.rb
427
427
  - lib/beowulf/type/authority.rb
428
428
  - lib/beowulf/type/authority_update.rb
429
+ - lib/beowulf/type/extension.rb
430
+ - lib/beowulf/type/extension_ids.rb
431
+ - lib/beowulf/type/extension_json.rb
432
+ - lib/beowulf/type/extension_types.rb
429
433
  - lib/beowulf/type/future.rb
430
434
  - lib/beowulf/type/hash.rb
431
435
  - lib/beowulf/type/permission.rb