borsh-rb 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/Gemfile.lock +8 -2
- data/README.md +31 -8
- data/bin/console +1 -1
- data/borsh-rb.gemspec +4 -4
- data/lib/borsh/argument_error.rb +5 -0
- data/lib/borsh/byte_string.rb +16 -0
- data/lib/borsh/integer.rb +34 -0
- data/lib/borsh/{rb/version.rb → serializer.rb} +2 -2
- data/lib/borsh/string.rb +16 -0
- data/lib/borsh/version.rb +5 -0
- data/lib/borsh.rb +46 -0
- metadata +38 -5
- data/lib/borsh/rb.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 625f3f0755640aaac127baccccb44ada20d439ef9987e4447e6bb0324a0a3f2c
|
4
|
+
data.tar.gz: '058eb504743730da0a37ba9015d2b3b6f0c59d61884afd062dfd0ad808b88f7e'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8c24a19398fa30b0a39ae8f5b8c56e2fd2c5a30d321683b9335f0525a2695cb1c4b8ee254f32550347281fbcbab144238989796ebc7a752051e380c7d5847a6
|
7
|
+
data.tar.gz: 42774fc35635cee53ae19014bef5427097dad647f2afff2d2bc93a18edb3483648f0cca94858b348aa86bbdfc3715aeacfe47ae3964a86d78bfced8b4d48e6b7
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
borsh-rb (0.1.
|
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.
|
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
|
-
|
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
|
-
|
23
|
+
include `Borsh` module and define serialisation schema
|
26
24
|
|
27
|
-
|
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
|
-
|
51
|
+
Serialize as: `User.new.to_borsh`
|
30
52
|
|
31
|
-
|
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/
|
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
data/borsh-rb.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "lib/borsh/
|
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::
|
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
|
-
|
31
|
-
|
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,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
|
data/lib/borsh/string.rb
ADDED
@@ -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
|
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.
|
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-
|
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
|
31
|
-
- lib/borsh/
|
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
|