beowulf-ruby 0.0.1 → 0.0.2

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: 3ef4850cb0d869fc8cdd9650b2eaa97e4c632685170515e98ab272bf1e427b8d
4
- data.tar.gz: db9a7bc76541043e0aacb559088e1a4cbc30673ce1ef6ad8d44c4458f8b82d0c
3
+ metadata.gz: d80b10af00162deb40c9d71f723e81a093de9404d02fb92e53388942a8d5091b
4
+ data.tar.gz: 281e7fbe7473dc501b5197fcb7e3a4c1400fab14899a55d1e5c56b7b1f7cb94a
5
5
  SHA512:
6
- metadata.gz: f9dfd423d37d99eeb4af29e647a68ba949f7ba75122a652089ed31f05c33d87472226ad71c7535adb253a398e8a04b2235b4e52eaec2831547f2e7f3f1b14525
7
- data.tar.gz: d5d2d7ea03c7d65b79079901fde8a86956db48fda59319bea17b070a5f58dde17d984b46b62024b3b224b2ffa1c3d8008e4d721cc2b7fd3c90445f961624a1c7
6
+ metadata.gz: 62467694a5be6efa7831f0d951cbecba2ba44186a7fe1936e8b5177b609eacd266fd4769b335d4091b489a4c9b479c20bf050020e942373d69aaa49846a1f8a5
7
+ data.tar.gz: f20991a1c2c2604bd8061d51c2ab8030b3b11dad2005397d7f388a2b6c945eed19d9ea120dae9d6e2c96a018e88fbd154138848f065b0a2f7404a28501a324fc
data/README.md CHANGED
@@ -7,7 +7,11 @@ beowulf-ruby is the official Beowulf library for Ruby.
7
7
  Add the gem to your Gemfile:
8
8
 
9
9
  ```ruby
10
+ # MainNet
10
11
  gem 'beowulf-ruby'
12
+
13
+ # TestNet
14
+ gem 'beowulf-ruby-testnet'
11
15
  ```
12
16
 
13
17
  Then:
data/lib/beowulf.rb CHANGED
@@ -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
@@ -68,6 +68,7 @@ module Beowulf
68
68
  if !(achs.eql? bnchs)
69
69
  puts achs, bnchs
70
70
  puts 'Invalid private key (checksum miss-match)'
71
+ raise WalletError, "Invalid private key (checksum miss-match): #{achs} != #{bnchs}"
71
72
  end
72
73
  # Create Public Key
73
74
  bk = Bitcoin::Key.new(hexlify(tpk[1..-1]), nil, {compressed: true})
@@ -83,12 +84,15 @@ module Beowulf
83
84
  # Validate
84
85
  if password == nil || password.length == 0
85
86
  puts "Password is not empty."
87
+ raise WalletError, "Password is not empty."
86
88
  end
87
89
  if password.length < 8
88
90
  puts "Password length >= 8 character."
91
+ raise WalletError, "Password length >= 8 character."
89
92
  end
90
93
  if @name == nil || @name.length == 0
91
94
  puts "name is not empty."
95
+ raise WalletError, "name is not empty."
92
96
  end
93
97
 
94
98
  # Encrypt data
@@ -138,9 +142,11 @@ module Beowulf
138
142
  # Validate inputs
139
143
  if wallet_path_file == nil || wallet_path_file.length == 0
140
144
  puts "Path file wallet is not empty."
145
+ raise WalletError, "Path file wallet is not empty."
141
146
  end
142
147
  if password == nil || password.length == 0
143
148
  puts "Password is not empty."
149
+ raise WalletError, "Password is not empty."
144
150
  end
145
151
  # Read file wallet
146
152
  file = File.read(wallet_path_file)
@@ -162,6 +168,7 @@ module Beowulf
162
168
  dechs = msg_json['checksum']
163
169
  if !(chs.eql? dechs)
164
170
  puts "Password wrong."
171
+ raise WalletError, "Password wrong."
165
172
  end
166
173
  keys = msg_json['keys']
167
174
  keys.each do |key, value|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beowulf-ruby
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