ciri 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0fb4f5d7ae9d525c598590327ea9652617ca2d48eff3edd40c36307d9b140fbf
4
+ data.tar.gz: d45c159bc70ba765cae8d6b4150e3088ad8e43f55ec7e3f4cf691152604aa8ce
5
+ SHA512:
6
+ metadata.gz: 35f02c14a3401ab6bbcfae9617ce1b4872a71e9117b05d5ac99e5795c2027d7a8c7e448cce617b25a120cc3da7ecda24eea6fe7ea91689d88457840c0b2fc41b
7
+ data.tar.gz: d1867a3cc573020f8e5b1466d701029646598c748f1fdc405f34123120b77035ff369c9a6b92fa29e08072d1f5de9bc738f756c8071e24929afbc6f5868b2af1
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jjyruby@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}"}
4
+
5
+ # Specify your gem's dependencies in ciri.gemspec
6
+ gemspec
7
+
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ciri (0.0.0)
5
+ bitcoin-secp256k1 (~> 0.4.0)
6
+ concurrent-ruby (~> 1.0.5)
7
+ digest-sha3 (~> 1.1.0)
8
+ snappy (~> 0.0.17)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ bitcoin-secp256k1 (0.4.0)
14
+ ffi (>= 1.9.10)
15
+ concurrent-ruby (1.0.5)
16
+ diff-lcs (1.3)
17
+ digest-sha3 (1.1.0)
18
+ ffi (1.9.23)
19
+ rake (10.3.2)
20
+ rspec (3.7.0)
21
+ rspec-core (~> 3.7.0)
22
+ rspec-expectations (~> 3.7.0)
23
+ rspec-mocks (~> 3.7.0)
24
+ rspec-core (3.7.1)
25
+ rspec-support (~> 3.7.0)
26
+ rspec-expectations (3.7.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.7.0)
29
+ rspec-mocks (3.7.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.7.0)
32
+ rspec-support (3.7.1)
33
+ snappy (0.0.17)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.16)
40
+ ciri!
41
+ rake (~> 10.0)
42
+ rspec (~> 3.0)
43
+
44
+ BUNDLED WITH
45
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018, by Jiang Jinyang. <https://justjjy.com>
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,49 @@
1
+ Ciri
2
+ ===============
3
+
4
+ Ciri project intent to implement a full feature set ethereum client.
5
+
6
+ ### Check List
7
+
8
+ * [ ] RLPX
9
+ * [x] HandShake
10
+ * [ ] Server
11
+ * [ ] Node Discovery
12
+ * [x] RLP
13
+ * [ ] Eth Protocol
14
+ * [x] HandShake
15
+ * [ ] Ethereum Sub-protocol
16
+ * [ ] Block Chain
17
+ * [ ] Chain Syncing
18
+ * [ ] EVM
19
+ * [ ] Mining
20
+ * [ ] Consensus Algorithm
21
+ * [ ] POW
22
+ * [ ] POS
23
+ * [ ] Web3 RPC
24
+ * [ ] CLI
25
+
26
+ ### Install
27
+
28
+ ``` bash
29
+ gem install ciri
30
+ ```
31
+
32
+ As library
33
+
34
+ ``` ruby
35
+ require 'ciri'
36
+ puts Ciri::Version
37
+ ```
38
+
39
+ ### Command line
40
+
41
+ `ciri -h`
42
+
43
+ ### Documentation
44
+
45
+ [YARD documentation](https://www.rubydoc.info/github/ruby-ethereum/ciri/master)
46
+
47
+ ### Author
48
+
49
+ [Jiang Jinyang](https://justjjy.com)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ciri"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/ciri.gemspec ADDED
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ciri/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ciri"
8
+ spec.version = Ciri::VERSION
9
+ spec.authors = ["Jiang Jinyang"]
10
+ spec.email = ["jjyruby@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby implementation of the ethereum}
13
+ spec.description = %q{Ciri project intent to implement full feature set of ethereum in pure ruby, to provide both usable cli and well documented ruby library.}
14
+ spec.homepage = "https://github.com/ruby-ethereum/ethereum"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency 'digest-sha3', '~> 1.1.0'
25
+ spec.add_dependency 'bitcoin-secp256k1', '~> 0.4.0'
26
+ spec.add_dependency 'concurrent-ruby', '~> 1.0.5'
27
+ spec.add_dependency 'snappy', '~> 0.0.17'
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ end
data/lib/ciri.rb ADDED
@@ -0,0 +1,26 @@
1
+ # Copyright (c) 2018, by Jiang Jinyang. <https://justjjy.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+
22
+ require "ciri/version"
23
+
24
+ module Ciri
25
+ # Your code goes here...
26
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2018, by Jiang Jinyang. <https://justjjy.com>
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.
22
+
23
+
24
+ # this module include several methods translated from pydevp2p.devp2p.crypto
25
+
26
+ require 'openssl'
27
+ require 'ciri/utils'
28
+ require 'secp256k1'
29
+
30
+ module Ciri
31
+ module Crypto
32
+ extend self
33
+
34
+ class ECIESDecryptionError < StandardError
35
+ end
36
+
37
+ ECIES_CIPHER_NAME = 'aes-128-ctr'
38
+
39
+ def ecdsa_signature(key, data)
40
+ secp256k1_key = ensure_secp256k1_key(privkey: key)
41
+ signature, recid = secp256k1_key.ecdsa_recoverable_serialize(secp256k1_key.ecdsa_sign_recoverable(data, raw: true))
42
+ signature + Ciri::Utils.big_endian_encode(recid, "\x00")
43
+ end
44
+
45
+ def ecdsa_recover(msg, signature, return_raw_key: true)
46
+ pk = Secp256k1::PrivateKey.new(flags: Secp256k1::ALL_FLAGS)
47
+ sig, recid = signature[0..-2], Ciri::Utils.big_endian_decode(signature[-1])
48
+
49
+ recsig = pk.ecdsa_recoverable_deserialize(sig, recid)
50
+ pubkey = pk.ecdsa_recover(msg, recsig, raw: true)
51
+
52
+ key = Secp256k1::PublicKey.new(pubkey: pubkey)
53
+ return_raw_key ? key.serialize(compressed: false) : key
54
+ end
55
+
56
+ def ecies_encrypt(message, raw_pubkey, shared_mac_data = '')
57
+ pubkey = raw_pubkey.is_a?(OpenSSL::PKey::EC) ? raw_pubkey : ec_pkey_from_raw(raw_pubkey)
58
+
59
+ # compute keys
60
+ ephem_key = OpenSSL::PKey::EC.new('secp256k1')
61
+ ephem_key.generate_key
62
+ shared_secret = ephem_key.dh_compute_key(pubkey.public_key)
63
+ key = ecies_kdf(shared_secret, 32)
64
+ key_enc, key_mac = key[0...16], key[16..-1]
65
+
66
+ key_mac = Digest::SHA256.digest(key_mac)
67
+ ephem_raw_pubkey = ephem_key.public_key.to_bn.to_s(2)
68
+
69
+ cipher = OpenSSL::Cipher.new(ECIES_CIPHER_NAME)
70
+ cipher.encrypt
71
+ iv = cipher.random_iv
72
+ cipher.key = key_enc
73
+ cipher_text = cipher.update(message) + cipher.final
74
+ msg = ephem_raw_pubkey + iv + cipher_text
75
+ tag = hmac_sha256(key_mac, msg[ephem_raw_pubkey.size..-1] + shared_mac_data)
76
+ msg + tag
77
+ end
78
+
79
+ def ecies_decrypt(data, priv_key, shared_mac_data = '')
80
+ raise ECIESDecryptionError.new('invalid header') if data[0] != "\x04"
81
+
82
+ # compute shared_secret
83
+ ephem_raw_pubkey = data[0..64]
84
+ # add first byte tag
85
+ ephem_pubkey = ec_pkey_from_raw(ephem_raw_pubkey)
86
+ shared_secret = priv_key.dh_compute_key(ephem_pubkey.public_key)
87
+
88
+ key = ecies_kdf(shared_secret, 32)
89
+ key_enc, key_mac = key[0...16], key[16..-1]
90
+
91
+ # verify data
92
+ key_mac = Digest::SHA256.digest(key_mac)
93
+ tag = data[-32..-1]
94
+ unless Ciri::Utils.secret_compare(hmac_sha256(key_mac, data[65...-32] + shared_mac_data), tag)
95
+ raise ECIESDecryptionError.new("Fail to verify data")
96
+ end
97
+
98
+ # decrypt data
99
+ cipher = OpenSSL::Cipher.new(ECIES_CIPHER_NAME)
100
+
101
+ iv_start = 65
102
+ iv_end = iv_start + cipher.iv_len
103
+ iv = data[iv_start...iv_end]
104
+ ciphertext = data[iv_end...-32]
105
+
106
+ cipher.decrypt
107
+ cipher.key = key_enc
108
+ cipher.iv = iv
109
+ cipher.update(ciphertext) + cipher.final
110
+ end
111
+
112
+ def ensure_secp256k1_key(privkey:)
113
+ privkey.is_a?(Secp256k1::BaseKey) ? privkey : Secp256k1::PrivateKey.new(privkey: privkey)
114
+ end
115
+
116
+ private
117
+ def ecies_kdf(key_material, key_len)
118
+ s1 = ''.b
119
+ key = ''.b
120
+ hash_block_size = 64
121
+ reps = ((key_len + 7) * 8) / (hash_block_size * 8)
122
+ counter = 0
123
+ while counter <= reps
124
+ counter += 1
125
+ ctx = Digest::SHA256.new
126
+ ctx.update([counter].pack("I>*"))
127
+ ctx.update(key_material)
128
+ ctx.update(s1)
129
+ key += ctx.digest
130
+ end
131
+ key
132
+ end
133
+
134
+ def hmac_sha256(key, data)
135
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, data)
136
+ end
137
+
138
+ def ec_pkey_from_raw(raw_pubkey, raw_privkey: nil)
139
+ Ciri::Utils.create_ec_pk(raw_pubkey: raw_pubkey, raw_privkey: raw_privkey)
140
+ end
141
+
142
+ end
143
+ end