borsh-rb 0.1.0 → 0.1.1

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: c3a38414ba8064cc0a4a90dec06b813df3ac2c4e32b5c4149549241e2a27219f
4
- data.tar.gz: 2e6b833cd730150c00220aa5744d5809f0f522bceb098575a1336d7bedefa75f
3
+ metadata.gz: 625f3f0755640aaac127baccccb44ada20d439ef9987e4447e6bb0324a0a3f2c
4
+ data.tar.gz: '058eb504743730da0a37ba9015d2b3b6f0c59d61884afd062dfd0ad808b88f7e'
5
5
  SHA512:
6
- metadata.gz: 629392b88fe0415ffa6d8c23bfc75a553f38d126124306dff1bae6e45ac54a58a6c3aaefaaafd5f7434f2940552722cf03346f0a476bfc3964ced3339b4ccc19
7
- data.tar.gz: c81268f0de8fa467af9dd808d826e2616aff387c8d8919377d68cb626771f3e539cc42bf97c592317148d935f05d8b632c4aa97cd197b76c76ae80150864f980
6
+ metadata.gz: f8c24a19398fa30b0a39ae8f5b8c56e2fd2c5a30d321683b9335f0525a2695cb1c4b8ee254f32550347281fbcbab144238989796ebc7a752051e380c7d5847a6
7
+ data.tar.gz: 42774fc35635cee53ae19014bef5427097dad647f2afff2d2bc93a18edb3483648f0cca94858b348aa86bbdfc3715aeacfe47ae3964a86d78bfced8b4d48e6b7
data/Gemfile CHANGED
@@ -6,5 +6,3 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
-
10
- gem "rspec", "~> 3.0"
data/Gemfile.lock CHANGED
@@ -1,12 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- borsh-rb (0.1.0)
4
+ borsh-rb (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ coderay (1.1.3)
9
10
  diff-lcs (1.4.4)
11
+ method_source (1.0.0)
12
+ pry (0.14.1)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
10
15
  rake (13.0.6)
11
16
  rspec (3.10.0)
12
17
  rspec-core (~> 3.10.0)
@@ -27,8 +32,9 @@ PLATFORMS
27
32
 
28
33
  DEPENDENCIES
29
34
  borsh-rb!
35
+ pry
30
36
  rake (~> 13.0)
31
- rspec (~> 3.0)
37
+ rspec (~> 3.10)
32
38
 
33
39
  BUNDLED WITH
34
40
  2.2.25
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Borsh::Rb
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/borsh/rb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Unofficial implementation of Borsh serializer https://borsh.io/
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,17 +20,42 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ include `Borsh` module and define serialisation schema
26
24
 
27
- ## Development
25
+ ```
26
+ class User
27
+ include Borsh
28
+
29
+ borsh id: :string, type: :u8, key: 32, admin: :borsh
30
+
31
+ def id
32
+ 'test'
33
+ end
34
+
35
+ def type
36
+ 1
37
+ end
38
+
39
+ def key
40
+ 'abcd'
41
+ end
42
+
43
+ admin
44
+ return unless admin?
45
+
46
+ User.new
47
+ end
48
+ end
49
+ ```
28
50
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
51
+ Serialize as: `User.new.to_borsh`
30
52
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
53
+ Supported types: `:string, :u8, :u16, :u32, :u64, :u128, Integer, Array, :borsh`
54
+ Integer as a type is supported to validate preserialized value length (for ex. public key)
32
55
 
33
56
  ## Contributing
34
57
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/borsh-rb.
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/2rba/borsh-rb.
36
59
 
37
60
  ## License
38
61
 
data/bin/console CHANGED
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require "bundler/setup"
5
- require "borsh/rb"
5
+ require "borsh"
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
data/borsh-rb.gemspec CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/borsh/rb/version"
3
+ require_relative "lib/borsh/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "borsh-rb"
7
- spec.version = Borsh::Rb::VERSION
7
+ spec.version = Borsh::VERSION
8
8
  spec.authors = ["Serg Tyatin"]
9
9
  spec.email = ["700@2rba.com"]
10
10
 
@@ -27,8 +27,8 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- # Uncomment to register a new dependency of your gem
31
- # spec.add_dependency "example-gem", "~> 1.0"
30
+ spec.add_development_dependency "rspec", "~> 3.10"
31
+ spec.add_development_dependency "pry"
32
32
 
33
33
  # For more information and examples about making a new gem, checkout our
34
34
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Borsh
4
+ class ArgumentError < ::ArgumentError; end
5
+ end
@@ -0,0 +1,16 @@
1
+ class Borsh::ByteString
2
+ def initialize(value, length)
3
+ @value = value
4
+ @length = length
5
+ end
6
+
7
+ def to_borsh
8
+ raise ArgumentError, 'ByteString length mismatch' if value.size != length
9
+
10
+ value
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :value, :length
16
+ end
@@ -0,0 +1,34 @@
1
+ class Borsh::Integer
2
+ def initialize(value, type)
3
+ @value = value
4
+ @type = type
5
+ end
6
+
7
+ def to_borsh
8
+ pack_arg = case type
9
+ when :u8
10
+ 'C'
11
+ when :u16
12
+ 'S<'
13
+ when :u32
14
+ 'L<'
15
+ when :u64
16
+ 'Q<'
17
+ when :u128
18
+ lower = value & 0xFFFFFFFF
19
+ higher = (value >> 64) & 0xFFFFFFFF
20
+ return [lower, higher].pack('Q<*')
21
+ else
22
+ raise ArgumentError, "unknown type #{type}"
23
+ end
24
+
25
+ result = [value].pack(pack_arg)
26
+ raise ArgumentError, 'Integer serialization failure' if result.unpack(pack_arg) != [value]
27
+
28
+ result
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :value, :type
34
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Borsh
4
- module Rb
5
- VERSION = "0.1.0"
4
+ class Serializer
5
+
6
6
  end
7
7
  end
@@ -0,0 +1,16 @@
1
+ class Borsh::String
2
+ def initialize(value)
3
+ @value = value
4
+ end
5
+
6
+ def to_borsh
7
+ length = [value.length].pack('V')
8
+ raise ArgumentError, 'string too long' if length.unpack('V') != [value.length]
9
+
10
+ length + value
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :value
16
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Borsh
4
+ VERSION = "0.1.1"
5
+ end
data/lib/borsh.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'borsh/version'
4
+ require_relative 'borsh/string'
5
+ require_relative 'borsh/integer'
6
+ require_relative 'borsh/byte_string'
7
+
8
+ module Borsh
9
+ def self.included(base)
10
+ base.extend ParentClassMethods
11
+ end
12
+
13
+ def to_borsh
14
+ to_borsh_schema(self, self.class.instance_variable_get(:@borsh_schema))
15
+ end
16
+
17
+ private
18
+
19
+ def to_borsh_schema(value, schema)
20
+ case schema
21
+ when :string
22
+ String.new(value).to_borsh
23
+ when :u8, :u16, :u32, :u64, :u128
24
+ Integer.new(value, schema).to_borsh
25
+ when ::Integer
26
+ Borsh::ByteString.new(value, schema).to_borsh
27
+ when ::Hash
28
+ schema.map{ |entry_source, entry_schema| to_borsh_schema(value.send(entry_source), entry_schema) }.join
29
+ when ::Array
30
+ Integer.new(value.count, :u32).to_borsh + \
31
+ value.flat_map do |item|
32
+ schema.map{ |entry_schema| to_borsh_schema(item, entry_schema) }
33
+ end.join
34
+ when :borsh
35
+ value.to_borsh
36
+ else
37
+ raise ArgumentError, "unknown serializer #{schema}, supported serializers: :string, :u8, :u16, :u32, :u64, :u128, :borsh"
38
+ end
39
+ end
40
+
41
+ module ParentClassMethods
42
+ def borsh(schema)
43
+ @borsh_schema = schema
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: borsh-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serg Tyatin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-11 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: ''
14
42
  email:
15
43
  - 700@2rba.com
@@ -27,8 +55,13 @@ files:
27
55
  - bin/console
28
56
  - bin/setup
29
57
  - borsh-rb.gemspec
30
- - lib/borsh/rb.rb
31
- - lib/borsh/rb/version.rb
58
+ - lib/borsh.rb
59
+ - lib/borsh/argument_error.rb
60
+ - lib/borsh/byte_string.rb
61
+ - lib/borsh/integer.rb
62
+ - lib/borsh/serializer.rb
63
+ - lib/borsh/string.rb
64
+ - lib/borsh/version.rb
32
65
  homepage: https://github.com/2rba/borsh-rb
33
66
  licenses:
34
67
  - MIT
data/lib/borsh/rb.rb DELETED
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "rb/version"
4
-
5
- module Borsh
6
- module Rb
7
- class Error < StandardError; end
8
- # Your code goes here...
9
- end
10
- end