Skip to content

gem: run rufo #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

# ref: https://github.com/GemHQ/money-tree/issues/50
gem "money-tree", git: "https://github.com/GemHQ/money-tree.git"

source "https://rubygems.org"

gem 'money-tree', git: 'https://github.com/GemHQ/money-tree.git'
source 'https://rubygems.org'
# Specify your gem's dependencies in eth.gemspec
gemspec
56 changes: 28 additions & 28 deletions eth.gemspec
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
# frozen_string_literal: true
# coding: utf-8

lib = File.expand_path('lib', __dir__).freeze
lib = File.expand_path("lib", __dir__).freeze
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib

require 'eth/version'
require "eth/version"

Gem::Specification.new do |spec|
spec.name = "eth"
spec.version = Eth::VERSION
spec.authors = ["Steve Ellis", "Afri Schoedon"]
spec.email = ["[email protected]", "[email protected]"]
spec.name = "eth"
spec.version = Eth::VERSION
spec.authors = ["Steve Ellis", "Afri Schoedon"]
spec.email = ["[email protected]", "[email protected]"]

spec.summary = %q{Simple API to sign Ethereum transactions.}
spec.description = %q{Library to build, parse, and sign Ethereum transactions.}
spec.homepage = "https://github.com/se3000/ruby-eth"
spec.license = "MIT"
spec.summary = %q{Simple API to sign Ethereum transactions.}
spec.description = %q{Library to build, parse, and sign Ethereum transactions.}
spec.homepage = "https://github.com/se3000/ruby-eth"
spec.license = "MIT"

spec.metadata = {
'homepage_uri' => 'https://github.com/se3000/ruby-eth',
'source_code_uri' => 'https://github.com/se3000/ruby-eth',
'github_repo' => 'https://github.com/se3000/ruby-eth',
'bug_tracker_uri' => 'https://github.com/se3000/ruby-eth/issues',
"homepage_uri" => "https://github.com/se3000/ruby-eth",
"source_code_uri" => "https://github.com/se3000/ruby-eth",
"github_repo" => "https://github.com/se3000/ruby-eth",
"bug_tracker_uri" => "https://github.com/se3000/ruby-eth/issues",
}.freeze

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.test_files = spec.files.grep %r{^(test|spec|features)/}
spec.test_files = spec.files.grep %r{^(test|spec|features)/}

spec.add_dependency 'keccak', '~> 1.3'
spec.add_dependency 'ffi', '~> 1.15'
spec.add_dependency 'money-tree', '~> 0.11'
spec.add_dependency 'openssl', '~> 3.0'
spec.add_dependency 'rlp', '~> 0.7'
spec.add_dependency 'scrypt', '~> 3.0'
spec.add_dependency "keccak", "~> 1.3"
spec.add_dependency "ffi", "~> 1.15"
spec.add_dependency "money-tree", "~> 0.11"
spec.add_dependency "openssl", "~> 3.0"
spec.add_dependency "rlp", "~> 0.7"
spec.add_dependency "scrypt", "~> 3.0"

spec.platform = Gem::Platform::RUBY
spec.required_ruby_version = ">= 2.6", "< 4.0"

spec.add_development_dependency 'bundler', '~> 2.2'
spec.add_development_dependency 'pry', '~> 0.14'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.10'
spec.add_development_dependency "bundler", "~> 2.2"
spec.add_development_dependency "pry", "~> 0.14"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.10"
end
26 changes: 13 additions & 13 deletions lib/eth.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require 'digest/keccak'
require 'ffi'
require 'money-tree'
require 'rlp'
require "digest/keccak"
require "ffi"
require "money-tree"
require "rlp"

module Eth
BYTE_ZERO = "\x00".freeze
UINT_MAX = 2**256 - 1
UINT_MAX = 2 ** 256 - 1

autoload :Address, 'eth/address'
autoload :Gas, 'eth/gas'
autoload :Key, 'eth/key'
autoload :OpenSsl, 'eth/open_ssl'
autoload :Secp256k1, 'eth/secp256k1'
autoload :Sedes, 'eth/sedes'
autoload :Tx, 'eth/tx'
autoload :Utils, 'eth/utils'
autoload :Address, "eth/address"
autoload :Gas, "eth/gas"
autoload :Key, "eth/key"
autoload :OpenSsl, "eth/open_ssl"
autoload :Secp256k1, "eth/secp256k1"
autoload :Sedes, "eth/sedes"
autoload :Tx, "eth/tx"
autoload :Utils, "eth/utils"

class << self
def configure
Expand Down
3 changes: 0 additions & 3 deletions lib/eth/address.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Eth
class Address

def initialize(address)
@address = Utils.prefix_hex(address)
end
Expand All @@ -25,7 +24,6 @@ def checksummed
Utils.prefix_hex(cased.join)
end


private

attr_reader :address
Expand Down Expand Up @@ -57,6 +55,5 @@ def checksum
def unprefixed
Utils.remove_hex_prefix address
end

end
end
2 changes: 0 additions & 2 deletions lib/eth/gas.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module Eth
class Gas

GTXCOST = 21000 # TX BASE GAS COST
GTXDATANONZERO = 68 # TX DATA NON ZERO BYTE GAS COST
GTXDATAZERO = 4 # TX DATA ZERO BYTE GAS COST

end
end
13 changes: 6 additions & 7 deletions lib/eth/key.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Eth
class Key
autoload :Decrypter, 'eth/key/decrypter'
autoload :Encrypter, 'eth/key/encrypter'
autoload :Decrypter, "eth/key/decrypter"
autoload :Encrypter, "eth/key/encrypter"

attr_reader :private_key, :public_key

Expand All @@ -17,7 +17,7 @@ def self.decrypt(data, password)
end

def self.personal_recover(message, signature)
bin_signature = Utils.hex_to_bin(signature).bytes.rotate(-1).pack('c*')
bin_signature = Utils.hex_to_bin(signature).bytes.rotate(-1).pack("c*")
OpenSsl.recover_compact(Utils.keccak256(Utils.prefix_message(message)), bin_signature)
end

Expand All @@ -41,6 +41,7 @@ def public_hex
def address
Utils.public_key_to_address public_hex
end

alias_method :to_address, :address

def sign(message)
Expand All @@ -60,10 +61,9 @@ def verify_signature(message, signature)
end

def personal_sign(message)
Utils.bin_to_hex(sign(Utils.prefix_message(message)).bytes.rotate(1).pack('c*'))
Utils.bin_to_hex(sign(Utils.prefix_message(message)).bytes.rotate(1).pack("c*"))
end


private

def message_hash(message)
Expand All @@ -72,8 +72,7 @@ def message_hash(message)

def valid_s?(signature)
s_value = Utils.v_r_s_for(signature).last
s_value <= Secp256k1::N/2 && s_value != 0
s_value <= Secp256k1::N / 2 && s_value != 0
end

end
end
37 changes: 17 additions & 20 deletions lib/eth/key/decrypter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'json'
require 'scrypt'
require "json"
require "scrypt"

class Eth::Key::Decrypter
include Eth::Utils
Expand All @@ -19,16 +19,15 @@ def perform
bin_to_hex decrypted_data
end


private

attr_reader :data, :key, :password

def derive_key(password)
case kdf
when 'pbkdf2'
when "pbkdf2"
@key = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, digest)
when 'scrypt'
when "scrypt"
# OpenSSL 1.1 inclues OpenSSL::KDF.scrypt, but it is not available usually, otherwise we could do: OpenSSL::KDF.scrypt(password, salt: salt, N: n, r: r, p: p, length: key_length)
@key = SCrypt::Engine.scrypt(password, salt, n, r, p, key_length)
else
Expand All @@ -37,8 +36,8 @@ def derive_key(password)
end

def check_macs
mac1 = keccak256(key[(key_length/2), key_length] + ciphertext)
mac2 = hex_to_bin crypto_data['mac']
mac1 = keccak256(key[(key_length / 2), key_length] + ciphertext)
mac2 = hex_to_bin crypto_data["mac"]

if mac1 != mac2
raise "Message Authentications Codes do not match!"
Expand All @@ -50,11 +49,11 @@ def decrypted_data
end

def crypto_data
@crypto_data ||= data['crypto'] || data['Crypto']
@crypto_data ||= data["crypto"] || data["Crypto"]
end

def ciphertext
hex_to_bin crypto_data['ciphertext']
hex_to_bin crypto_data["ciphertext"]
end

def cipher_name
Expand All @@ -64,41 +63,41 @@ def cipher_name
def cipher
@cipher ||= OpenSSL::Cipher.new(cipher_name).tap do |cipher|
cipher.decrypt
cipher.key = key[0, (key_length/2)]
cipher.key = key[0, (key_length / 2)]
cipher.iv = iv
end
end

def iv
hex_to_bin crypto_data['cipherparams']['iv']
hex_to_bin crypto_data["cipherparams"]["iv"]
end

def salt
hex_to_bin crypto_data['kdfparams']['salt']
hex_to_bin crypto_data["kdfparams"]["salt"]
end

def iterations
crypto_data['kdfparams']['c'].to_i
crypto_data["kdfparams"]["c"].to_i
end

def kdf
crypto_data['kdf']
crypto_data["kdf"]
end

def key_length
crypto_data['kdfparams']['dklen'].to_i
crypto_data["kdfparams"]["dklen"].to_i
end

def n
crypto_data['kdfparams']['n'].to_i
crypto_data["kdfparams"]["n"].to_i
end

def r
crypto_data['kdfparams']['r'].to_i
crypto_data["kdfparams"]["r"].to_i
end

def p
crypto_data['kdfparams']['p'].to_i
crypto_data["kdfparams"]["p"].to_i
end

def digest
Expand All @@ -108,6 +107,4 @@ def digest
def digest_name
"sha256"
end


end
26 changes: 12 additions & 14 deletions lib/eth/key/encrypter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'json'
require 'securerandom'
require "json"
require "securerandom"

class Eth::Key::Encrypter
include Eth::Utils
Expand Down Expand Up @@ -48,7 +48,6 @@ def id
@id ||= options[:id] || SecureRandom.uuid
end


private

attr_reader :derived_key, :encrypted_key, :key, :options
Expand All @@ -57,7 +56,7 @@ def cipher
@cipher ||= OpenSSL::Cipher.new(cipher_name).tap do |cipher|
cipher.encrypt
cipher.iv = iv
cipher.key = derived_key[0, (key_length/2)]
cipher.key = derived_key[0, (key_length / 2)]
end
end

Expand All @@ -74,7 +73,7 @@ def encrypt
end

def mac
keccak256(derived_key[(key_length/2), key_length] + encrypted_key)
keccak256(derived_key[(key_length / 2), key_length] + encrypted_key)
end

def cipher_name
Expand Down Expand Up @@ -107,22 +106,21 @@ def iterations

def salt
@salt ||= if options[:salt]
hex_to_bin options[:salt]
else
SecureRandom.random_bytes(salt_length)
end
hex_to_bin options[:salt]
else
SecureRandom.random_bytes(salt_length)
end
end

def iv
@iv ||= if options[:iv]
hex_to_bin options[:iv]
else
SecureRandom.random_bytes(iv_length)
end
hex_to_bin options[:iv]
else
SecureRandom.random_bytes(iv_length)
end
end

def address
Eth::Key.new(priv: key).address
end

end
Loading