Skip to content

Commit

Permalink
Rubocop resolved warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nduplessis committed Jul 22, 2021
1 parent e771a17 commit 7eaf9e1
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 30 deletions.
12 changes: 7 additions & 5 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

Expand All @@ -15,7 +17,7 @@
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# NOTE: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
Expand All @@ -24,11 +26,11 @@
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: 'rspec' do
guard :rspec, cmd: "rspec" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/flow_clients(.+)\.rb$}) { |m| "spec/lib/flow_client/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/flow_clients(.+)\.rb$}) { |m| "spec/lib/flow_client/#{m[1]}_spec.rb" }
watch("spec/spec_helper.rb") { "spec" }
end

# guard :rspec, cmd: "bundle exec rspec" do
Expand Down
4 changes: 2 additions & 2 deletions flow_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
spec.add_dependency "digest-sha3"
spec.add_dependency "ecdsa"
spec.add_dependency "grpc"
spec.add_dependency "grpc-tools"
spec.add_dependency "json"
spec.add_dependency "openssl"
spec.add_dependency "rlp"
spec.add_dependency "rspec"
spec.add_dependency "digest-sha3"
spec.add_dependency "ecdsa"

# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
12 changes: 7 additions & 5 deletions lib/flow_client/crypto.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true

require "openssl"

module FlowClient
class Crypto
def self.sign(data, key)
digest = OpenSSL::Digest.digest('SHA3-256', data)
digest = OpenSSL::Digest.digest("SHA3-256", data)
asn = key.dsa_sign_asn1(digest)
asn1 = OpenSSL::ASN1.decode(asn)
r, s = asn1.value
combined_bytes = Utils.left_pad_bytes([r.value.to_s(16)].pack("H*").unpack("C*"), 32) +
Utils.left_pad_bytes([s.value.to_s(16)].pack("H*").unpack("C*"), 32)
Utils.left_pad_bytes([s.value.to_s(16)].pack("H*").unpack("C*"), 32)
combined_bytes.pack("C*")
end

Expand All @@ -19,13 +21,13 @@ def self.key_from_hex_keys(private_hex, public_hex)
asn1 = OpenSSL::ASN1::Sequence(
[
OpenSSL::ASN1::Integer(1),
OpenSSL::ASN1::OctetString([private_hex].pack('H*')),
OpenSSL::ASN1::OctetString([private_hex].pack("H*")),
OpenSSL::ASN1::ObjectId("prime256v1", 0, :EXPLICIT),
OpenSSL::ASN1::BitString([public_hex].pack('H*'), 1, :EXPLICIT)
OpenSSL::ASN1::BitString([public_hex].pack("H*"), 1, :EXPLICIT)
]
)

OpenSSL::PKey::EC.new(asn1.to_der)
end
end
end
end
7 changes: 4 additions & 3 deletions lib/flow_client/transaction.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require "flow/entities/transaction_pb"
require "openssl"
require "rlp"
Expand Down Expand Up @@ -37,7 +38,7 @@ def payload_canonical_form
[
@script,
@arguments,
[@reference_block_id].pack('H*'),
[@reference_block_id].pack("H*"),
@gas_limit,
padded_address(@proposer_address),
@proposer_key_index,
Expand Down Expand Up @@ -99,7 +100,7 @@ def envelope_message
end

def add_envelope_signature(signer_address, key_index, key)
domain_tagged_payload = (Transaction.padded_transaction_domain_tag.bytes + envelope_message.bytes).pack('C*')
domain_tagged_payload = (Transaction.padded_transaction_domain_tag.bytes + envelope_message.bytes).pack("C*")

@envelope_signatures << Entities::Transaction::Signature.new(
address: padded_address(signer_address),
Expand Down Expand Up @@ -135,7 +136,7 @@ def to_protobuf_message
protected

def padded_address(address_hex_string)
Utils.left_pad_bytes([address_hex_string].pack('H*').bytes, 8).pack("C*")
Utils.left_pad_bytes([address_hex_string].pack("H*").bytes, 8).pack("C*")
end
end
end
8 changes: 5 additions & 3 deletions lib/flow_client/utils.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

module FlowClient
module Utils
def self.left_pad_bytes(byte_array, length)
required_pad_count = length - byte_array.count
padding = []
for i in 1..required_pad_count
(1..required_pad_count).each do |_i|
padding << 0
end
padding + byte_array
Expand All @@ -12,10 +14,10 @@ def self.left_pad_bytes(byte_array, length)
def self.right_pad_bytes(byte_array, length)
required_pad_count = length - byte_array.count
padding = []
for i in 1..required_pad_count
(1..required_pad_count).each do |_i|
padding << 0
end
byte_array + padding
end
end
end
end
13 changes: 7 additions & 6 deletions spec/lib/flow_client/crypto_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

RSpec.describe FlowClient::Transaction do
let(:key) {
let(:key) do
FlowClient::Crypto.key_from_hex_keys(
'81c9655ca2affbd3421c90a1294260b62f1fd4e9aaeb70da4b9185ebb4f4a26b',
'041c3e4980f2e7d733a7b023b6f9b9f5c0ff8116869492fd3b813597f9d17f826130c2e68fee90fc8beeabcb05c2bffa4997166ba5ab86942b03c8c86ab13e50d8'
"81c9655ca2affbd3421c90a1294260b62f1fd4e9aaeb70da4b9185ebb4f4a26b",
"041c3e4980f2e7d733a7b023b6f9b9f5c0ff8116869492fd3b813597f9d17f826130c2e68fee90fc8beeabcb05c2bffa4997166ba5ab86942b03c8c86ab13e50d8"
)
}
end
it "imports a key a hex string key pair for prime256v1" do

expect(key.private?).to eq(true)
end

Expand All @@ -18,4 +19,4 @@
data = "hello world!"
sig = FlowClient::Crypto.sign(data, key)
end
end
end
10 changes: 5 additions & 5 deletions spec/lib/flow_client/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
end
end

context 'the generated payload' do
context "the generated payload" do
it "correctly packages the script" do
ref_block_id = "7bc42fe85d32ca513769a74f97f7e1a7bad6c9407f0d934c2aa645ef9cf613c7"

# transaction = FlowClient::Transaction.new
# transaction.script = %{
# transaction {
# transaction {
# prepare(signer: AuthAccount) { log(signer.address) }
# }
# }
Expand All @@ -73,8 +73,8 @@
ref_block_id = "7bc42fe85d32ca513769a74f97f7e1a7bad6c9407f0d934c2aa645ef9cf613c7"

key = FlowClient::Crypto.key_from_hex_keys(
'81c9655ca2affbd3421c90a1294260b62f1fd4e9aaeb70da4b9185ebb4f4a26b',
'041c3e4980f2e7d733a7b023b6f9b9f5c0ff8116869492fd3b813597f9d17f826130c2e68fee90fc8beeabcb05c2bffa4997166ba5ab86942b03c8c86ab13e50d8'
"81c9655ca2affbd3421c90a1294260b62f1fd4e9aaeb70da4b9185ebb4f4a26b",
"041c3e4980f2e7d733a7b023b6f9b9f5c0ff8116869492fd3b813597f9d17f826130c2e68fee90fc8beeabcb05c2bffa4997166ba5ab86942b03c8c86ab13e50d8"
)

transaction = FlowClient::Transaction.new
Expand All @@ -87,7 +87,7 @@
transaction.reference_block_id = ref_block_id
transaction.proposer_address = "f8d6e0586b0a20c7"
transaction.proposer_key_index = 0
transaction.arguments = [ { type: "String", value: "Hello world!" }.to_json ]
transaction.arguments = [{ type: "String", value: "Hello world!" }.to_json]
transaction.proposer_key_sequence_number = 0
transaction.payer_address = "f8d6e0586b0a20c7"
transaction.authorizer_addresses = ["f8d6e0586b0a20c7"]
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/flow_client/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
bytes = [0x41]
expect(FlowClient::Utils.right_pad_bytes(bytes, 2)).to eq([0x41, 0x00])
end
end
end

0 comments on commit 7eaf9e1

Please sign in to comment.