borsh-rb 0.1.0 → 0.1.3

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: c3a38414ba8064cc0a4a90dec06b813df3ac2c4e32b5c4149549241e2a27219f
4
- data.tar.gz: 2e6b833cd730150c00220aa5744d5809f0f522bceb098575a1336d7bedefa75f
3
+ metadata.gz: ea49443bda65351efb2ff82517e0cfa3659e37bcc6037cc97039422eff6d32fc
4
+ data.tar.gz: 459b4eee287216c01fef51d05ff438fb08e3f68c5bae49ff23785c6ffb2d471a
5
5
  SHA512:
6
- metadata.gz: 629392b88fe0415ffa6d8c23bfc75a553f38d126124306dff1bae6e45ac54a58a6c3aaefaaafd5f7434f2940552722cf03346f0a476bfc3964ced3339b4ccc19
7
- data.tar.gz: c81268f0de8fa467af9dd808d826e2616aff387c8d8919377d68cb626771f3e539cc42bf97c592317148d935f05d8b632c4aa97cd197b76c76ae80150864f980
6
+ metadata.gz: 7049e920545c7c5f166258eeec49cee85a6e35e63b4f0ae2c1826eea7f45e3c88d221407bc0c0d9169fefaf22859eb132bee907a209549b47589edcfb294c31c
7
+ data.tar.gz: c6aad3cfe4175b5a8f181623eb204d9fcd396b9a0a12d6462f9ffc490f31e4d0cf7aec60535aeedfb42e5df5f1e6f437c65965ed686e2ea4e047f7c3969074ea
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.3)
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,22 +1,22 @@
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
 
11
11
  spec.summary = "https://borsh.io implementation for ruby"
12
12
  spec.description = ""
13
- spec.homepage = "https://github.com/2rba/borsh-rb"
13
+ spec.homepage = "https://github.com/near-rails-guide/borsh-rb"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = ">= 2.4.0"
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/2rba/borsh-rb"
19
- spec.metadata["changelog_uri"] = "https://github.com/2rba/borsh-rb"
18
+ spec.metadata["source_code_uri"] = "https://github.com/near-rails-guide/borsh-rb"
19
+ spec.metadata["changelog_uri"] = "https://github.com/near-rails-guide/borsh-rb"
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
22
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -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 Borsh::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 & 0xFFFFFFFFFFFFFFFF
19
+ higher = (value >> 64) & 0xFFFFFFFFFFFFFFFF
20
+ return [lower, higher].pack('Q<*')
21
+ else
22
+ raise Borsh::ArgumentError, "unknown type #{type}"
23
+ end
24
+
25
+ result = [value].pack(pack_arg)
26
+ raise Borsh::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 Borsh::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.3"
5
+ end
data/lib/borsh.rb ADDED
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'borsh/version'
4
+ require_relative 'borsh/argument_error'
5
+ require_relative 'borsh/string'
6
+ require_relative 'borsh/integer'
7
+ require_relative 'borsh/byte_string'
8
+
9
+ module Borsh
10
+ def self.included(base)
11
+ base.extend ParentClassMethods
12
+ end
13
+
14
+ def to_borsh
15
+ to_borsh_schema(self, self.class.instance_variable_get(:@borsh_schema))
16
+ end
17
+
18
+ private
19
+
20
+ def to_borsh_schema(value, schema)
21
+ case schema
22
+ when :string
23
+ String.new(value).to_borsh
24
+ when :u8, :u16, :u32, :u64, :u128
25
+ Integer.new(value, schema).to_borsh
26
+ when ::Integer
27
+ Borsh::ByteString.new(value, schema).to_borsh
28
+ when ::Hash
29
+ schema.map do |entry_source, entry_schema|
30
+ to_borsh_schema(value.send(entry_source), entry_schema)
31
+ rescue Borsh::ArgumentError => e
32
+ raise Borsh::ArgumentError.new("#{entry_source} => #{e.message}")
33
+ end.join
34
+ when ::Array
35
+ Integer.new(value.count, :u32).to_borsh + \
36
+ value.flat_map do |item|
37
+ schema.map{ |entry_schema| to_borsh_schema(item, entry_schema) }
38
+ end.join
39
+ when :borsh
40
+ value.to_borsh
41
+ else
42
+ raise ArgumentError, "unknown serializer #{schema}, supported serializers: :string, :u8, :u16, :u32, :u64, :u128, :borsh"
43
+ end
44
+ end
45
+
46
+ module ParentClassMethods
47
+ def borsh(schema)
48
+ @borsh_schema = schema
49
+ end
50
+ end
51
+ 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.3
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: 2022-02-23 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,15 +55,20 @@ 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
32
- homepage: https://github.com/2rba/borsh-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
65
+ homepage: https://github.com/near-rails-guide/borsh-rb
33
66
  licenses:
34
67
  - MIT
35
68
  metadata:
36
- homepage_uri: https://github.com/2rba/borsh-rb
37
- source_code_uri: https://github.com/2rba/borsh-rb
38
- changelog_uri: https://github.com/2rba/borsh-rb
69
+ homepage_uri: https://github.com/near-rails-guide/borsh-rb
70
+ source_code_uri: https://github.com/near-rails-guide/borsh-rb
71
+ changelog_uri: https://github.com/near-rails-guide/borsh-rb
39
72
  post_install_message:
40
73
  rdoc_options: []
41
74
  require_paths:
@@ -51,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
84
  - !ruby/object:Gem::Version
52
85
  version: '0'
53
86
  requirements: []
54
- rubygems_version: 3.2.22
87
+ rubygems_version: 3.2.3
55
88
  signing_key:
56
89
  specification_version: 4
57
90
  summary: https://borsh.io implementation for ruby
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