ethereum-base 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ethereum-base.gemspec +28 -0
- data/lib/ethereum.rb +8 -0
- data/lib/ethereum/base.rb +7 -0
- data/lib/ethereum/base/version.rb +5 -0
- data/lib/ethereum/utils.rb +153 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de5876380603e16b6768ec297a5d9c7462def5ac
|
4
|
+
data.tar.gz: 0b8bcb8c6f1c0b38eb816b5f2baf3f4f7cffd6d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c090fde521b3d910eedb928c1b0675ab968643cb81b64362a363f747754d2b4d7d9ff38451f39947ae86f2c3c72cac1eebfb25d902f36fa9762ae9bb78a4ac6
|
7
|
+
data.tar.gz: af77dac8a601cd9760a1e1d71587bd342ee9f955662c51b835dd60efe004d8b4cf3040e6a6dd32f84603631e1bc248611893011bebf4dca2f6a14af5bbdad230
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Jan Xie
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Ethereum::Base
|
2
|
+
|
3
|
+
A gem to provide some basic utilities for common Ethereum functions.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ethereum-base'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ethereum-base
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
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.
|
28
|
+
|
29
|
+
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).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/janx/ruby-ethereum-base.
|
34
|
+
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
39
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ethereum/base"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ethereum/base/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ethereum-base"
|
8
|
+
spec.version = Ethereum::Base::VERSION
|
9
|
+
spec.authors = ["Jan Xie", "Steve Ellis"]
|
10
|
+
spec.email = ["jan.h.xie@gmail.com", "email@steveell.is"]
|
11
|
+
|
12
|
+
spec.summary = %q{Utilities for common Ethereum functions.}
|
13
|
+
spec.description = %q{A base set of utilities for common Ethereum functions.}
|
14
|
+
spec.homepage = "https://github.com/janx/ruby-ethereum-base"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency 'digest-sha3', '~> 1.1'
|
23
|
+
spec.add_dependency 'rlp', '~> 0.6'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
26
|
+
spec.add_development_dependency 'minitest', '5.8.3'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
end
|
data/lib/ethereum.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
module Ethereum
|
2
|
+
module Utils
|
3
|
+
|
4
|
+
def sha256(x)
|
5
|
+
Digest::SHA256.digest x
|
6
|
+
end
|
7
|
+
|
8
|
+
def keccak256(x)
|
9
|
+
Digest::SHA3.new(256).digest(x)
|
10
|
+
end
|
11
|
+
|
12
|
+
def keccak512(x)
|
13
|
+
Digest::SHA3.new(512).digest(x)
|
14
|
+
end
|
15
|
+
|
16
|
+
def keccak256_rlp(x)
|
17
|
+
keccak256 RLP.encode(x)
|
18
|
+
end
|
19
|
+
|
20
|
+
def encode_hex(b)
|
21
|
+
RLP::Utils.encode_hex b
|
22
|
+
end
|
23
|
+
|
24
|
+
def decode_hex(s)
|
25
|
+
RLP::Utils.decode_hex s
|
26
|
+
end
|
27
|
+
|
28
|
+
def big_endian_to_int(s)
|
29
|
+
RLP::Sedes.big_endian_int.deserialize s.sub(/\A(\x00)+/, '')
|
30
|
+
end
|
31
|
+
|
32
|
+
def int_to_big_endian(n)
|
33
|
+
RLP::Sedes.big_endian_int.serialize n
|
34
|
+
end
|
35
|
+
|
36
|
+
def remove_0x_head(s)
|
37
|
+
s[0,2] == '0x' ? s[2..-1] : s
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_int_or_hex(s)
|
41
|
+
if s.is_a?(Numeric)
|
42
|
+
s
|
43
|
+
elsif s[0,2] == '0x'
|
44
|
+
big_endian_to_int decode_hex(normalize_hex_without_prefix(s))
|
45
|
+
else
|
46
|
+
s.to_i
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def normalize_hex_without_prefix(s)
|
51
|
+
if s[0,2] == '0x'
|
52
|
+
(s.size % 2 == 1 ? '0' : '') + s[2..-1]
|
53
|
+
else
|
54
|
+
s
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def ripemd160(x)
|
59
|
+
Digest::RMD160.digest x
|
60
|
+
end
|
61
|
+
|
62
|
+
def hash160(x)
|
63
|
+
ripemd160 sha256(x)
|
64
|
+
end
|
65
|
+
|
66
|
+
def hash160_hex(x)
|
67
|
+
encode_hex hash160(x)
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_signed(i)
|
71
|
+
i > Constant::INT_MAX ? (i-Constant::TT256) : i
|
72
|
+
end
|
73
|
+
|
74
|
+
def ceil32(x)
|
75
|
+
x % 32 == 0 ? x : (x + 32 - x%32)
|
76
|
+
end
|
77
|
+
|
78
|
+
def zpad(x, l)
|
79
|
+
lpad x, BYTE_ZERO, l
|
80
|
+
end
|
81
|
+
|
82
|
+
def zunpad(x)
|
83
|
+
x.sub /\A\x00+/, ''
|
84
|
+
end
|
85
|
+
|
86
|
+
def zpad_int(n, l=32)
|
87
|
+
zpad encode_int(n), l
|
88
|
+
end
|
89
|
+
|
90
|
+
def zpad_hex(s, l=32)
|
91
|
+
zpad decode_hex(s), l
|
92
|
+
end
|
93
|
+
|
94
|
+
def int_to_addr(x)
|
95
|
+
zpad_int x, 20
|
96
|
+
end
|
97
|
+
|
98
|
+
def encode_int(n)
|
99
|
+
raise ArgumentError, "Integer invalid or out of range: #{n}" unless n.is_a?(Integer) && n >= 0 && n <= UINT_MAX
|
100
|
+
int_to_big_endian n
|
101
|
+
end
|
102
|
+
|
103
|
+
def decode_int(v)
|
104
|
+
raise ArgumentError, "No leading zero bytes allowed for integers" if v.size > 0 && (v[0] == Constant::BYTE_ZERO || v[0] == 0)
|
105
|
+
big_endian_to_int v
|
106
|
+
end
|
107
|
+
|
108
|
+
def bytearray_to_int(arr)
|
109
|
+
o = 0
|
110
|
+
arr.each {|x| o = (o << 8) + x }
|
111
|
+
o
|
112
|
+
end
|
113
|
+
|
114
|
+
def int_array_to_bytes(arr)
|
115
|
+
arr.pack('C*')
|
116
|
+
end
|
117
|
+
|
118
|
+
def bytes_to_int_array(bytes)
|
119
|
+
bytes.unpack('C*')
|
120
|
+
end
|
121
|
+
|
122
|
+
def coerce_to_int(x)
|
123
|
+
if x.is_a?(Numeric)
|
124
|
+
x
|
125
|
+
elsif x.size == 40
|
126
|
+
big_endian_to_int decode_hex(x)
|
127
|
+
else
|
128
|
+
big_endian_to_int x
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def coerce_to_bytes(x)
|
133
|
+
if x.is_a?(Numeric)
|
134
|
+
int_to_big_endian x
|
135
|
+
elsif x.size == 40
|
136
|
+
decode_hex(x)
|
137
|
+
else
|
138
|
+
x
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def coerce_addr_to_hex(x)
|
143
|
+
if x.is_a?(Numeric)
|
144
|
+
encode_hex zpad(int_to_big_endian(x), 20)
|
145
|
+
elsif x.size == 40 || x.size == 0
|
146
|
+
x
|
147
|
+
else
|
148
|
+
encode_hex zpad(x, 20)[-20..-1]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ethereum-base
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Xie
|
8
|
+
- Steve Ellis
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-06-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: digest-sha3
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.1'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.1'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rlp
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0.6'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0.6'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.12'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.12'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: minitest
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 5.8.3
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 5.8.3
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rake
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '10.0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '10.0'
|
84
|
+
description: A base set of utilities for common Ethereum functions.
|
85
|
+
email:
|
86
|
+
- jan.h.xie@gmail.com
|
87
|
+
- email@steveell.is
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- ethereum-base.gemspec
|
102
|
+
- lib/ethereum.rb
|
103
|
+
- lib/ethereum/base.rb
|
104
|
+
- lib/ethereum/base/version.rb
|
105
|
+
- lib/ethereum/utils.rb
|
106
|
+
homepage: https://github.com/janx/ruby-ethereum-base
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.5.1
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Utilities for common Ethereum functions.
|
130
|
+
test_files: []
|
131
|
+
has_rdoc:
|