rlp-ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +82 -0
- data/LICENSE.txt +20 -0
- data/README.md +41 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/core_ext/array.rb +45 -0
- data/lib/core_ext/fixnum.rb +26 -0
- data/lib/core_ext/string.rb +5 -0
- data/lib/encode.rb +20 -0
- data/lib/rlp-ruby.rb +82 -0
- data/test/helper.rb +34 -0
- data/test/test_rlp-ruby.rb +105 -0
- metadata +144 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "rdoc", "~> 3.12"
|
11
|
+
gem "bundler", "~> 1.0"
|
12
|
+
gem "jeweler", "~> 2.0.1"
|
13
|
+
gem "simplecov", ">= 0"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (4.0.4)
|
5
|
+
i18n (~> 0.6, >= 0.6.9)
|
6
|
+
minitest (~> 4.2)
|
7
|
+
multi_json (~> 1.3)
|
8
|
+
thread_safe (~> 0.1)
|
9
|
+
tzinfo (~> 0.3.37)
|
10
|
+
addressable (2.3.6)
|
11
|
+
atomic (1.1.16)
|
12
|
+
builder (3.2.2)
|
13
|
+
descendants_tracker (0.0.4)
|
14
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
15
|
+
docile (1.1.3)
|
16
|
+
faraday (0.9.0)
|
17
|
+
multipart-post (>= 1.2, < 3)
|
18
|
+
git (1.2.6)
|
19
|
+
github_api (0.11.3)
|
20
|
+
addressable (~> 2.3)
|
21
|
+
descendants_tracker (~> 0.0.1)
|
22
|
+
faraday (~> 0.8, < 0.10)
|
23
|
+
hashie (>= 1.2)
|
24
|
+
multi_json (>= 1.7.5, < 2.0)
|
25
|
+
nokogiri (~> 1.6.0)
|
26
|
+
oauth2
|
27
|
+
hashie (2.0.5)
|
28
|
+
highline (1.6.21)
|
29
|
+
i18n (0.6.9)
|
30
|
+
jeweler (2.0.1)
|
31
|
+
builder
|
32
|
+
bundler (>= 1.0)
|
33
|
+
git (>= 1.2.5)
|
34
|
+
github_api
|
35
|
+
highline (>= 1.6.15)
|
36
|
+
nokogiri (>= 1.5.10)
|
37
|
+
rake
|
38
|
+
rdoc
|
39
|
+
json (1.8.1)
|
40
|
+
jwt (0.1.11)
|
41
|
+
multi_json (>= 1.5)
|
42
|
+
mini_portile (0.5.3)
|
43
|
+
minitest (4.7.5)
|
44
|
+
multi_json (1.9.2)
|
45
|
+
multi_xml (0.5.5)
|
46
|
+
multipart-post (2.0.0)
|
47
|
+
nokogiri (1.6.1)
|
48
|
+
mini_portile (~> 0.5.0)
|
49
|
+
oauth2 (0.9.3)
|
50
|
+
faraday (>= 0.8, < 0.10)
|
51
|
+
jwt (~> 0.1.8)
|
52
|
+
multi_json (~> 1.3)
|
53
|
+
multi_xml (~> 0.5)
|
54
|
+
rack (~> 1.2)
|
55
|
+
rack (1.5.2)
|
56
|
+
rake (10.2.2)
|
57
|
+
rdoc (3.12.2)
|
58
|
+
json (~> 1.4)
|
59
|
+
shoulda (3.5.0)
|
60
|
+
shoulda-context (~> 1.0, >= 1.0.1)
|
61
|
+
shoulda-matchers (>= 1.4.1, < 3.0)
|
62
|
+
shoulda-context (1.1.6)
|
63
|
+
shoulda-matchers (2.5.0)
|
64
|
+
activesupport (>= 3.0.0)
|
65
|
+
simplecov (0.8.2)
|
66
|
+
docile (~> 1.1.0)
|
67
|
+
multi_json
|
68
|
+
simplecov-html (~> 0.8.0)
|
69
|
+
simplecov-html (0.8.0)
|
70
|
+
thread_safe (0.3.1)
|
71
|
+
atomic (>= 1.1.7, < 2)
|
72
|
+
tzinfo (0.3.39)
|
73
|
+
|
74
|
+
PLATFORMS
|
75
|
+
ruby
|
76
|
+
|
77
|
+
DEPENDENCIES
|
78
|
+
bundler (~> 1.0)
|
79
|
+
jeweler (~> 2.0.1)
|
80
|
+
rdoc (~> 3.12)
|
81
|
+
shoulda
|
82
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Maran
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# rlp-ruby
|
2
|
+
|
3
|
+
rlp-ruby is a ruby implementation of the [recursive length prefix](https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP) (RLP) that can be found in [Ethereum](http://
|
4
|
+
ethereum.org).
|
5
|
+
|
6
|
+
rlp-ruby works by extending the ruby primitve classes with rlp oriented functions.
|
7
|
+
|
8
|
+
## Encoding to RLP
|
9
|
+
```
|
10
|
+
>"dog".to_rlp
|
11
|
+
=> [131, 100, 111, 103]
|
12
|
+
>["c", "cat", "dog"].to_rlp
|
13
|
+
=> [201, 99, 131, 99, 97, 116, 131, 100, 111, 103]
|
14
|
+
```
|
15
|
+
|
16
|
+
## Decoding from RLP
|
17
|
+
```
|
18
|
+
>rlp_array = RLP.decoder([201, 99, 131, 99, 97, 116, 131, 100, 111, 103])
|
19
|
+
>ba.get(0).as_string
|
20
|
+
=>"c"
|
21
|
+
>ba.get(1).as_string
|
22
|
+
=>"cat"
|
23
|
+
```
|
24
|
+
|
25
|
+
Please review the tests for more examples.
|
26
|
+
|
27
|
+
### Contributing to rlp-ruby
|
28
|
+
|
29
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
30
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
31
|
+
* Fork the project.
|
32
|
+
* Start a feature/bugfix branch.
|
33
|
+
* Commit and push until you are happy with your contribution.
|
34
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
35
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
36
|
+
|
37
|
+
### Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2014 Maran. See LICENSE.txt for
|
40
|
+
further details.
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
|
+
gem.name = "rlp-ruby"
|
18
|
+
gem.homepage = "http://github.com/maran/rlp-ruby"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A recursive length prefix implementation in ruby.}
|
21
|
+
gem.description = %Q{With this library it's possible to encode ruby objects into RLP arrays.}
|
22
|
+
gem.email = "maran.hidskes@gmail.com"
|
23
|
+
gem.authors = ["Maran"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
desc "Code coverage detail"
|
36
|
+
task :simplecov do
|
37
|
+
ENV['COVERAGE'] = "true"
|
38
|
+
Rake::Task['test'].execute
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rdoc/task'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "rlp-ruby #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module RLP
|
2
|
+
module Extensions
|
3
|
+
module Array
|
4
|
+
def get(position)
|
5
|
+
self[position]
|
6
|
+
end
|
7
|
+
|
8
|
+
def as_string
|
9
|
+
self.collect{|x| x.as_string}.join
|
10
|
+
end
|
11
|
+
|
12
|
+
def as_int
|
13
|
+
self.reverse_bytes
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_rlp
|
17
|
+
return [0xc0] if self == []
|
18
|
+
|
19
|
+
result = self.collect do |item|
|
20
|
+
rlp = item.to_rlp
|
21
|
+
if rlp.is_a?(Array)
|
22
|
+
rlp.to_a.collect{|x| x.chr}
|
23
|
+
else
|
24
|
+
rlp.chr
|
25
|
+
end
|
26
|
+
end.flatten
|
27
|
+
|
28
|
+
result = result.join.to_rlp(true)
|
29
|
+
return result
|
30
|
+
end
|
31
|
+
|
32
|
+
def reverse_bytes
|
33
|
+
bytes = self.collect do |item|
|
34
|
+
item.chr
|
35
|
+
end
|
36
|
+
(8 - bytes.length).times do
|
37
|
+
bytes.insert(0, 0.chr)
|
38
|
+
end
|
39
|
+
bytes.join.unpack("q>").first
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
Array.send(:include, RLP::Extensions::Array)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RLP
|
2
|
+
module Extensions
|
3
|
+
module Fixnum
|
4
|
+
include RLP::Encode
|
5
|
+
|
6
|
+
def as_int
|
7
|
+
return self
|
8
|
+
end
|
9
|
+
|
10
|
+
def as_string
|
11
|
+
return self.chr
|
12
|
+
end
|
13
|
+
|
14
|
+
def bytesize
|
15
|
+
self.bytes.length
|
16
|
+
end
|
17
|
+
|
18
|
+
# This makes sure for isntance 1024 becomes [0,0,0,0,0,0,4,0] and in turn [4,0]
|
19
|
+
def bytes
|
20
|
+
[self].pack("q>").bytes.to_a.drop_while{|x| x == 0}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Fixnum.send(:include, RLP::Extensions::Fixnum)
|
data/lib/encode.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module RLP
|
2
|
+
module Encode;
|
3
|
+
def to_rlp(is_array = false)
|
4
|
+
return [0x80] if self == ""
|
5
|
+
|
6
|
+
length = self.bytes.to_a.length
|
7
|
+
|
8
|
+
offset = [0x80, 0xb7]
|
9
|
+
offset = [0xc0, 0xf7] if is_array
|
10
|
+
|
11
|
+
if length == 1 && !is_array && self.bytes.first <= 0x7f
|
12
|
+
return [self.bytes.first]
|
13
|
+
elsif length <= 55
|
14
|
+
return [(offset[0]+length), *self.bytes]
|
15
|
+
elsif length > 55
|
16
|
+
return [(offset[1]+length.bytesize),*length.bytes, *self.bytes]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/rlp-ruby.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require_relative 'encode'
|
2
|
+
require_relative 'core_ext/array'
|
3
|
+
require_relative 'core_ext/fixnum'
|
4
|
+
String.send(:include, RLP::Encode)
|
5
|
+
|
6
|
+
module RLP
|
7
|
+
def self.log(message)
|
8
|
+
if false
|
9
|
+
puts(message)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.decoder(data)
|
14
|
+
return self.decode(data,0)[0]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.decode(data, pos)
|
18
|
+
value = data[pos]
|
19
|
+
RLP.log("Data: #{data}")
|
20
|
+
result = []
|
21
|
+
|
22
|
+
# Reading a single byte
|
23
|
+
if value <= 0x7f
|
24
|
+
RLP.log("<= 0x7f, reading 1 byte")
|
25
|
+
RLP.log("=> Read: #{value}")
|
26
|
+
return value, pos+1
|
27
|
+
# Reading a string up to and including 55 bytes long
|
28
|
+
elsif value <= 0xb7
|
29
|
+
start_at = pos + 1
|
30
|
+
read_length = value - 0x80
|
31
|
+
RLP.log("<= 0xb7, starting reading at the #{start_at}th byte and reading #{read_length} bytes")
|
32
|
+
read_data = data[start_at..(start_at+read_length-1)]
|
33
|
+
RLP.log("=> Read: #{read_data}")
|
34
|
+
return read_data, (start_at + read_length)
|
35
|
+
# Reading a string that is longer then 55 bytes
|
36
|
+
elsif value <= 0xbf
|
37
|
+
start_at = pos + 1
|
38
|
+
read_length = value - 0xb7
|
39
|
+
|
40
|
+
read_length_length = data[start_at..(start_at+read_length-1)].first
|
41
|
+
RLP.log("<= 0xbf, starting reading at the #{start_at}th byte and reading #{read_length_length} bytes")
|
42
|
+
read_data = data[(start_at+1)..read_length_length]
|
43
|
+
RLP.log("=> Read: #{read_data}")
|
44
|
+
|
45
|
+
return read_data, read_length_length
|
46
|
+
# Reading a list that is up to (and including) 55 bytes long
|
47
|
+
elsif value <= 0xf7
|
48
|
+
amount = value - 0xc0
|
49
|
+
RLP.log("<= 0x7f, starting reading at the #{start_at}th byte and reading #{amount} bytes")
|
50
|
+
pos += 1
|
51
|
+
i = 0
|
52
|
+
while i < amount do
|
53
|
+
decoded, prev_pos = decode(data,pos)
|
54
|
+
result << decoded
|
55
|
+
i += (prev_pos - pos)
|
56
|
+
pos = prev_pos
|
57
|
+
end
|
58
|
+
RLP.log("=> Read: #{result}")
|
59
|
+
|
60
|
+
return result,pos
|
61
|
+
# Reading a list that is longer then 55 bytes
|
62
|
+
elsif value <= 0xff
|
63
|
+
length = value - 0xf7
|
64
|
+
start_at = pos + 1
|
65
|
+
read_length_length = data[start_at..start_at+length-1]
|
66
|
+
|
67
|
+
pos = length + start_at
|
68
|
+
RLP.log("<= 0xff, starting reading at the #{start_at}th byte and reading #{read_length_length} bytes")
|
69
|
+
prev_pos = read_length_length
|
70
|
+
i = 0
|
71
|
+
while i < read_length_length.reverse_bytes do
|
72
|
+
decoded,prev_pos = decode(data,pos)
|
73
|
+
result << decoded
|
74
|
+
i += (prev_pos - pos)
|
75
|
+
pos = prev_pos
|
76
|
+
end
|
77
|
+
|
78
|
+
RLP.log("=> Read: #{result}")
|
79
|
+
return result, pos
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
module SimpleCov::Configuration
|
4
|
+
def clean_filters
|
5
|
+
@filters = []
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
SimpleCov.configure do
|
10
|
+
clean_filters
|
11
|
+
load_adapter 'test_frameworks'
|
12
|
+
end
|
13
|
+
|
14
|
+
ENV["COVERAGE"] && SimpleCov.start do
|
15
|
+
add_filter "/.rvm/"
|
16
|
+
end
|
17
|
+
require 'rubygems'
|
18
|
+
require 'bundler'
|
19
|
+
begin
|
20
|
+
Bundler.setup(:default, :development)
|
21
|
+
rescue Bundler::BundlerError => e
|
22
|
+
$stderr.puts e.message
|
23
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
24
|
+
exit e.status_code
|
25
|
+
end
|
26
|
+
require 'test/unit'
|
27
|
+
require 'shoulda'
|
28
|
+
|
29
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
30
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
31
|
+
require 'rlp-ruby'
|
32
|
+
|
33
|
+
class Test::Unit::TestCase
|
34
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'helper'
|
2
|
+
class TestRlpRuby < Test::Unit::TestCase
|
3
|
+
context "decoding raw rlp values" do
|
4
|
+
should "decode simple byte arrays" do
|
5
|
+
assert_equal [[100,111,103],4], RLP.decode([131, 100, 111, 103],0)
|
6
|
+
end
|
7
|
+
|
8
|
+
should "decode array to a byte arrays" do
|
9
|
+
assert_equal [[99, [99, 97, 116], [100, 111, 103]], 10], RLP.decode([201, 99, 131, 99, 97, 116, 131, 100, 111, 103],0)
|
10
|
+
assert_equal [[[100, 111, 103], 15, [[99, 97, 116], [99, 97, 116], []], [4, 0], [116, 97, 99, 104, 105, 107, 111, 109, 97]], 29], RLP.decode([220,131, 100, 111, 103, 15, 201, 131, 99, 97, 116, 131, 99, 97, 116, 192, 130, 4, 0, 137, 116, 97, 99, 104, 105, 107, 111, 109, 97],0)
|
11
|
+
end
|
12
|
+
|
13
|
+
should "decode > 55 byte arrays" do
|
14
|
+
assert_equal [[[116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 118, 101, 114, 121, 32, 108, 111, 110, 103, 32, 108, 105, 115, 116],[121, 111, 117, 32, 110, 101, 118, 101, 114, 32, 103, 117, 101, 115, 115, 32, 104, 111, 119, 32, 108, 111, 110, 103, 32, 105, 116, 32, 105, 115],[105, 110, 100, 101, 101, 100, 44, 32, 104, 111, 119, 32, 100, 105, 100, 32, 121, 111, 117, 32, 107, 110, 111, 119, 32, 105, 116, 32, 119, 97, 115, 32, 116, 104, 105, 115, 32, 108, 111, 110, 103],[103, 111, 111, 100, 32, 106, 111, 98, 44, 32, 116, 104, 97, 116, 32, 73, 32, 99, 97, 110, 32, 116, 101, 108, 108, 32, 121, 111, 117, 32, 105, 110, 32, 104, 111, 110, 101, 115, 116, 108, 121, 121, 121, 121, 121]], 146], RLP.decode([248, 144, 152, 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 118, 101, 114, 121, 32, 108, 111, 110, 103, 32, 108, 105, 115, 116, 158, 121, 111, 117, 32, 110, 101, 118, 101, 114, 32, 103, 117, 101, 115, 115, 32, 104, 111, 119, 32, 108, 111, 110, 103, 32, 105, 116, 32, 105, 115, 169, 105, 110, 100, 101, 101, 100, 44, 32, 104, 111, 119, 32, 100, 105, 100, 32, 121, 111, 117, 32, 107, 110, 111, 119, 32, 105, 116, 32, 119, 97, 115, 32, 116, 104, 105, 115, 32, 108, 111, 110, 103, 173, 103, 111, 111, 100, 32, 106, 111, 98, 44, 32, 116, 104, 97, 116, 32, 73, 32, 99, 97, 110, 32, 116, 101, 108, 108, 32, 121, 111, 117, 32, 105, 110, 32, 104, 111, 110, 101, 115, 116, 108, 121, 121, 121, 121, 121],0)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "decoding to primatives" do
|
19
|
+
should "decode back to a string" do
|
20
|
+
ba = RLP.decoder([131, 100, 111, 103])
|
21
|
+
assert_equal "dog", ba.as_string
|
22
|
+
end
|
23
|
+
|
24
|
+
should "decode back to a character" do
|
25
|
+
ba = RLP.decoder([99])
|
26
|
+
assert_equal "c", ba.as_string
|
27
|
+
end
|
28
|
+
|
29
|
+
should "decode back to a small int" do
|
30
|
+
ba = RLP.decoder([15])
|
31
|
+
assert_equal 15, ba.as_int
|
32
|
+
end
|
33
|
+
|
34
|
+
should "decode back to a large int" do
|
35
|
+
ba = RLP.decoder([0x82, 0x04, 0x00])
|
36
|
+
assert_equal 1024, ba.as_int
|
37
|
+
end
|
38
|
+
|
39
|
+
should "decode a 1-dimensional list" do
|
40
|
+
ba = RLP.decoder([201, 99, 131, 99, 97, 116, 131, 100, 111, 103])
|
41
|
+
assert_equal "c", ba.get(0).as_string
|
42
|
+
assert_equal "cat", ba.get(1).as_string
|
43
|
+
assert_equal "dog", ba.get(2).as_string
|
44
|
+
end
|
45
|
+
|
46
|
+
should "decode a very long list" do
|
47
|
+
ba = RLP.decoder(["this is a very long list", "you never guess how long it is", "indeed, how did you know it was this long", "good job, that I can tell you in honestlyyyyy"].to_rlp)
|
48
|
+
|
49
|
+
assert_equal "this is a very long list", ba.get(0).as_string
|
50
|
+
assert_equal "you never guess how long it is", ba.get(1).as_string
|
51
|
+
assert_equal "indeed, how did you know it was this long", ba.get(2).as_string
|
52
|
+
end
|
53
|
+
|
54
|
+
should "decode mixed byte arrays" do
|
55
|
+
ba = RLP.decoder(["dog", 15, ["cat", "cat", []], 1024, "tachikoma"].to_rlp)
|
56
|
+
assert_equal "dog", ba.get(0).as_string
|
57
|
+
assert_equal 15, ba.get(1).as_int
|
58
|
+
assert_equal "cat", ba.get(2).get(0).as_string
|
59
|
+
assert_equal [], ba.get(2).get(2)
|
60
|
+
assert_equal 1024, ba.get(3).as_int
|
61
|
+
assert_equal "tachikoma", ba.get(4).as_string
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "encoding rlp values" do
|
66
|
+
should "encode short (<56 bytes) strings" do
|
67
|
+
assert_equal "dog".to_rlp, [131, 100, 111, 103]
|
68
|
+
assert_equal "cat".to_rlp, [131, 99, 97, 116]
|
69
|
+
end
|
70
|
+
|
71
|
+
should "encode long (>55 bytes) strings" do
|
72
|
+
assert_equal "Lorem ipsum dolor sit amet, consectetur adipisicing elit".to_rlp, [184, 0x38, 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 32, 100, 111, 108, 111, 114, 32, 115, 105, 116, 32, 97, 109, 101, 116, 44, 32, 99, 111, 110, 115, 101, 99, 116, 101, 116, 117, 114, 32, 97, 100, 105, 112, 105, 115, 105, 99, 105, 110, 103, 32, 101, 108, 105, 116]
|
73
|
+
end
|
74
|
+
|
75
|
+
should "encode simple characters" do
|
76
|
+
assert_equal "d".to_rlp, [100]
|
77
|
+
end
|
78
|
+
|
79
|
+
should "encode short arrays" do
|
80
|
+
assert_equal [ [], [[]], [ [], [[]] ] ].to_rlp,[199, 192, 193, 192, 195, 192, 193, 192]
|
81
|
+
assert_equal ["cat", "dog"].to_rlp, [200, 131, 99, 97, 116, 131, 100, 111, 103]
|
82
|
+
assert_equal ["c", "cat", "dog"].to_rlp, [201, 99, 131, 99, 97, 116, 131, 100, 111, 103]
|
83
|
+
end
|
84
|
+
|
85
|
+
should "encode long arrays" do
|
86
|
+
assert_equal ["this is a very long list", "you never guess how long it is", "indeed, how did you know it was this long", "good job, that I can tell you in honestlyyyyy"].to_rlp, [248, 144, 152, 116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 118, 101, 114, 121, 32, 108, 111, 110, 103, 32, 108, 105, 115, 116, 158, 121, 111, 117, 32, 110, 101, 118, 101, 114, 32, 103, 117, 101, 115, 115, 32, 104, 111, 119, 32, 108, 111, 110, 103, 32, 105, 116, 32, 105, 115, 169, 105, 110, 100, 101, 101, 100, 44, 32, 104, 111, 119, 32, 100, 105, 100, 32, 121, 111, 117, 32, 107, 110, 111, 119, 32, 105, 116, 32, 119, 97, 115, 32, 116, 104, 105, 115, 32, 108, 111, 110, 103, 173, 103, 111, 111, 100, 32, 106, 111, 98, 44, 32, 116, 104, 97, 116, 32, 73, 32, 99, 97, 110, 32, 116, 101, 108, 108, 32, 121, 111, 117, 32, 105, 110, 32, 104, 111, 110, 101, 115, 116, 108, 121, 121, 121, 121, 121]
|
87
|
+
end
|
88
|
+
|
89
|
+
should "encode integer" do
|
90
|
+
assert_equal 1024.to_rlp, [0x82, 0x04, 0x00]
|
91
|
+
assert_equal 15.to_rlp, [0x0f]
|
92
|
+
assert_equal 128.to_rlp, [129, 128]
|
93
|
+
end
|
94
|
+
|
95
|
+
should "encode empty structures" do
|
96
|
+
assert_equal "".to_rlp, [0x80]
|
97
|
+
assert_equal [].to_rlp, [0xc0]
|
98
|
+
end
|
99
|
+
|
100
|
+
should "encode mixing everything togther" do
|
101
|
+
assert_equal ["dog", 15, ["cat", "cat", []], 1024, "tachikoma"].to_rlp, [220,131, 100, 111, 103, 15, 201, 131, 99, 97, 116, 131, 99, 97, 116, 192, 130, 4, 0, 137, 116, 97, 99, 104, 105, 107, 111, 109, 97]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
metadata
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rlp-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Maran
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: shoulda
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rdoc
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.12'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.12'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: jeweler
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.0.1
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.0.1
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: simplecov
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: With this library it's possible to encode ruby objects into RLP arrays.
|
95
|
+
email: maran.hidskes@gmail.com
|
96
|
+
executables: []
|
97
|
+
extensions: []
|
98
|
+
extra_rdoc_files:
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
files:
|
102
|
+
- .document
|
103
|
+
- Gemfile
|
104
|
+
- Gemfile.lock
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- VERSION
|
109
|
+
- lib/core_ext/array.rb
|
110
|
+
- lib/core_ext/fixnum.rb
|
111
|
+
- lib/core_ext/string.rb
|
112
|
+
- lib/encode.rb
|
113
|
+
- lib/rlp-ruby.rb
|
114
|
+
- test/helper.rb
|
115
|
+
- test/test_rlp-ruby.rb
|
116
|
+
homepage: http://github.com/maran/rlp-ruby
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
segments:
|
130
|
+
- 0
|
131
|
+
hash: 3155010614310884218
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ! '>='
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 1.8.24
|
141
|
+
signing_key:
|
142
|
+
specification_version: 3
|
143
|
+
summary: A recursive length prefix implementation in ruby.
|
144
|
+
test_files: []
|