ssz 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ssz.rb +12 -0
- data/lib/ssz/base.rb +9 -0
- data/lib/ssz/bit_list.rb +19 -0
- data/lib/ssz/bit_victor.rb +16 -0
- data/lib/ssz/boolean.rb +19 -0
- data/lib/ssz/byte_buffer.rb +44 -0
- data/lib/ssz/fixed_sized.rb +9 -0
- data/lib/ssz/uint.rb +29 -0
- data/lib/ssz/version.rb +3 -0
- data/lib/ssz/victor.rb +15 -0
- data/ssz.gemspec +26 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e90d345e757920b0e658c21302c92cc6bef8a8ca45dddc9cd05dfc4a979d2516
|
4
|
+
data.tar.gz: bcd166173f3156c77f882188d66ec7f53e2f978808238d3f8ba4582b55977d46
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6e26a1d8a49db7ccfcf597e82dfcda701a6dc5e9f7a39350ea6720354c5ded63f9c325507f48157717792b0743386a3cc5c7b14ba0bb79375a3d5457ee2c8dd
|
7
|
+
data.tar.gz: 722e879df0fe33a4499f45985a2fd0070e3c60a36f125de562d359d0d8d330cce1c401feb66255e5cd1e49642aadadcee0114e709a039ff3a83ea228235ecc75
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Kai Chen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# SSZ
|
2
|
+
|
3
|
+
**S**imple **S**eriali**Z**e implemented in Ruby programming language.
|
4
|
+
|
5
|
+
It's a data serialization format, to replace [RLP](https://github.com/ethereum/wiki/wiki/RLP), used in [eth2](https://github.com/ethereum/eth2.0-specs).
|
6
|
+
|
7
|
+
* <https://github.com/ethereum/eth2.0-specs/blob/dev/specs/simple-serialize.md>
|
8
|
+
* <https://ethresear.ch/t/replacing-ssz-with-rlp-zip-and-sha256/5706>
|
9
|
+
* <https://rauljordan.com/2019/07/02/go-lessons-from-writing-a-serialization-library-for-ethereum.html>
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'ssz-rb'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install ssz-rb
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
TODO: Write usage instructions here
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kaichen/ssz-rb.
|
40
|
+
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ssz/rb"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/ssz.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "ssz/version"
|
2
|
+
require_relative './ssz/boolean'
|
3
|
+
require_relative './ssz/uint'
|
4
|
+
require_relative './ssz/bit_victor'
|
5
|
+
require_relative './ssz/bit_list'
|
6
|
+
require_relative './ssz/byte_buffer'
|
7
|
+
require_relative './ssz/victor'
|
8
|
+
|
9
|
+
module Ssz
|
10
|
+
class Error < StandardError; end
|
11
|
+
# Your code goes here...
|
12
|
+
end
|
data/lib/ssz/base.rb
ADDED
data/lib/ssz/bit_list.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './base'
|
4
|
+
|
5
|
+
module Ssz
|
6
|
+
class BitList < Base
|
7
|
+
def initialize(bits, capacity:)
|
8
|
+
@capacity = capacity
|
9
|
+
@bits = bits
|
10
|
+
end
|
11
|
+
|
12
|
+
def serialize
|
13
|
+
bb = ByteBuffer.from_bit_array(@bits.map{|b| b ? 1 : 0})
|
14
|
+
bb.bytes << 0 if bb.bytes.size < (@bits.size / 8) + 1 # extend buffer size
|
15
|
+
bb.bytes[@bits.size / 8] |= 1 << (@bits.size % 8)
|
16
|
+
bb.serialize
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './base'
|
4
|
+
|
5
|
+
module Ssz
|
6
|
+
class BitVictor < Base
|
7
|
+
def initialize(bits, capacity:)
|
8
|
+
@capacity = capacity
|
9
|
+
@bits = bits
|
10
|
+
end
|
11
|
+
|
12
|
+
def serialize
|
13
|
+
ByteBuffer.from_bit_array(@bits.map{|b| b ? 1 : 0}).serialize
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/ssz/boolean.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './base'
|
4
|
+
|
5
|
+
module Ssz
|
6
|
+
class Boolean < Base
|
7
|
+
def initialize(bool)
|
8
|
+
@bool = bool
|
9
|
+
end
|
10
|
+
|
11
|
+
def serialize
|
12
|
+
Array(@bool ? 1 : 0).pack("C")
|
13
|
+
end
|
14
|
+
|
15
|
+
def size
|
16
|
+
1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ssz
|
4
|
+
# @example:
|
5
|
+
# ByteBuffer.from_bit_array([1, 0, 0, 0]).bytes #=> "\x01"
|
6
|
+
class ByteBuffer
|
7
|
+
attr_accessor :bytes
|
8
|
+
|
9
|
+
def initialize(n = 0)
|
10
|
+
@bytes = [0] * n
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_bit_array(bits)
|
14
|
+
byte_size = bits.size < 8 ? 1 : (bits.size / 8.0).ceil
|
15
|
+
bb = ByteBuffer.new(byte_size)
|
16
|
+
bb.bits = bits
|
17
|
+
bb
|
18
|
+
end
|
19
|
+
|
20
|
+
def bits=(bits)
|
21
|
+
byte_size = bits.size < 8 ? 1 : (bits.size / 8.0).ceil
|
22
|
+
# fill 0 to fix byte(8-bits)
|
23
|
+
while (byte_size * 8) != bits.size do
|
24
|
+
bits << 0
|
25
|
+
end
|
26
|
+
bits.each_with_index do |b, i|
|
27
|
+
@bytes[i / 8] |= b << (i % 8)
|
28
|
+
end
|
29
|
+
bits
|
30
|
+
end
|
31
|
+
|
32
|
+
def serialize
|
33
|
+
@bytes.pack("C*")
|
34
|
+
end
|
35
|
+
|
36
|
+
def length
|
37
|
+
@bytes.length
|
38
|
+
end
|
39
|
+
|
40
|
+
def bit_size
|
41
|
+
length * 8
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/ssz/uint.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './base'
|
4
|
+
|
5
|
+
module Ssz
|
6
|
+
class Uint < Base
|
7
|
+
def initialize(num, length: 8)
|
8
|
+
@num = num
|
9
|
+
@data_length = length
|
10
|
+
# TODO: validate length
|
11
|
+
end
|
12
|
+
|
13
|
+
def serialize
|
14
|
+
byte_size = (@data_length / 8)
|
15
|
+
# FIXME: better way convert integer to bytes
|
16
|
+
hex = @num.to_s(16)
|
17
|
+
hex = "0#{hex}" if hex.size % 2 != 0
|
18
|
+
bytes = hex.scan(/.{2}/)
|
19
|
+
if bytes.length < byte_size
|
20
|
+
bytes = ["0"] * (byte_size - bytes.length) + bytes
|
21
|
+
end
|
22
|
+
bytes.map { |str| str.to_i(16) }.pack("C" * byte_size).force_encoding("ASCII-8BIT")
|
23
|
+
end
|
24
|
+
|
25
|
+
def size
|
26
|
+
serialize.size
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/ssz/version.rb
ADDED
data/lib/ssz/victor.rb
ADDED
data/ssz.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'lib/ssz/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "ssz"
|
5
|
+
spec.version = Ssz::VERSION
|
6
|
+
spec.authors = ["Kai Chen"]
|
7
|
+
spec.email = ["kai@thekaiway.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Simple serialization implementation in Ruby}
|
10
|
+
spec.homepage = "https://github.com/kaichen/ssz-rb"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
spec.metadata["homepage_uri"] = "https://github.com/kaichen/ssz-rb"
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/kaichen/ssz-rb"
|
16
|
+
spec.metadata["changelog_uri"] = "https://github.com/kaichen/ssz-rb/blob/master/CHANGELOG.md"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ssz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kai Chen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- kai@thekaiway.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".travis.yml"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- lib/ssz.rb
|
29
|
+
- lib/ssz/base.rb
|
30
|
+
- lib/ssz/bit_list.rb
|
31
|
+
- lib/ssz/bit_victor.rb
|
32
|
+
- lib/ssz/boolean.rb
|
33
|
+
- lib/ssz/byte_buffer.rb
|
34
|
+
- lib/ssz/fixed_sized.rb
|
35
|
+
- lib/ssz/uint.rb
|
36
|
+
- lib/ssz/version.rb
|
37
|
+
- lib/ssz/victor.rb
|
38
|
+
- ssz.gemspec
|
39
|
+
homepage: https://github.com/kaichen/ssz-rb
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
homepage_uri: https://github.com/kaichen/ssz-rb
|
44
|
+
source_code_uri: https://github.com/kaichen/ssz-rb
|
45
|
+
changelog_uri: https://github.com/kaichen/ssz-rb/blob/master/CHANGELOG.md
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.3.0
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.0.3
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Simple serialization implementation in Ruby
|
65
|
+
test_files: []
|