Skip to content

Commit

Permalink
add comments to message class
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jul 13, 2018
1 parent e7b3d83 commit 826db33
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Message
attr_reader :original_data, :original_signature
attr_reader :data, :signature, :value, :to, :from

# initialize the object and values...
#
# @param content [String] hex number string of transaction content
# @return [void]
def initialize(content)
@unverified_transaction = decode(content)

Expand All @@ -21,12 +25,17 @@ def initialize(content)
@from = get_from
end

# decode
# unserialize the transaction content
#
# @param content [String] hex number string of transaction content
# @return [UnverifiedTransaction] an object of google protobuf file in lib dir.
def decode(content)
binary_str = hex_to_binary_str(content)
::UnverifiedTransaction.decode(binary_str)
end

# get from value from UnverifiedTransaction
# @return [String] an address of hex number string with prefix "0x"
def get_from
digest_data = Ciri::Utils.sha3(@original_data)
pubkey = Ciri::Crypto.ecdsa_recover(digest_data, @original_signature)
Expand All @@ -35,24 +44,36 @@ def get_from
Ciri::Utils.to_hex(address)
end

# a method to convert hex byte string to hex number string with prefix "0x"
# @return [String] an hex string of address
def to_hex(hex)
str = hex.unpack("H*").first
return str if str.downcase.start_with?("0x")
"0x" + str
end

# remove 0x with hex string
# remove "0x" prefix if have
#
# @param [String] hex number string
# @return [String]
def filter_hex_str(hex)
return hex[2..-1] if hex.start_with?("0x")
hex
end

# convert hex string to byte code array
#
# @param hex [String] hex number string
# @return [Array<Integer>] byte code array
def hex_to_buffer(hex)
hex_str = filter_hex_str(hex)
[hex_str].pack('H*').bytes.to_a
end

# deserialization
# convert hex string to binary string
#
# @param hex [String] hex number string
# @return [String] binary string
def hex_to_binary_str(hex)
hex_to_buffer(hex).pack("c*")
end
Expand Down

0 comments on commit 826db33

Please sign in to comment.