Skip to content

Commit

Permalink
Merge pull request #58 from cryptape/feature-sdk
Browse files Browse the repository at this point in the history
Using appchain.rb to make rpc calls and replace message decode
  • Loading branch information
ashchan authored Nov 7, 2018
2 parents eab20cb + 0066a2b commit 9928eb7
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 390 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ gem 'ethereum.rb', '~> 2.2'

gem 'health_check', '~> 3.0'

# appchain sdk
gem "appchain.rb", github: "cryptape/appchain.rb"

# Deployment
gem 'mina', require: false
gem 'mina-puma', require: false
Expand Down
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
GIT
remote: https://github.com/cryptape/appchain.rb.git
revision: 3a7e2288b57a5d2ef2bf850ab548e79fc3a8a6b7
specs:
appchain.rb (0.2.0)
activesupport (~> 5.2.1)
ciri-crypto (= 0.1.1)
faraday (~> 0.15.3)
google-protobuf (~> 3.6)
web3-eth (~> 0.2.16)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -199,6 +210,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
rlp (0.7.3)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
Expand Down Expand Up @@ -250,6 +262,9 @@ GEM
thread_safe (~> 0.1)
unicode-display_width (1.4.0)
url (0.3.2)
web3-eth (0.2.16)
digest-sha3 (~> 1.1.0)
rlp (~> 0.7.3)
webmock (3.4.2)
addressable (>= 2.3.6)
crack (>= 0.3.2)
Expand All @@ -266,6 +281,7 @@ PLATFORMS

DEPENDENCIES
active_model_serializers (~> 0.10.7)
appchain.rb!
awesome_print
bootsnap (>= 1.1.0)
byebug
Expand Down
36 changes: 7 additions & 29 deletions app/models/cita_sync/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,11 @@

module CitaSync
class Api
class << self
# New methods without prefix since CITA v0.16
# now upgrade to CITA v0.18
METHOD_NAMES = %w(
peerCount
blockNumber
sendRawTransaction
getBlockByHash
getBlockByNumber
getTransaction
getTransactionReceipt
getLogs
call
getTransactionCount
getCode
getAbi
getBalance
newFilter
newBlockFilter
uninstallFilter
getFilterChanges
getFilterLogs
getTransactionProof
getMetaData
getBlockHeader
getStateProof
).freeze
# New methods without prefix since CITA v0.16
# now upgrade to CITA v0.20
METHOD_NAMES = AppChain::RPC::METHOD_NAMES

class << self
METHOD_NAMES.each do |name|
define_method name.underscore do |*params|
call_rpc(name, params: params)
Expand All @@ -44,8 +21,9 @@ class << self
# @param id [Integer] id number
# @return [Hash, String, Array] json decode to hash
def call_rpc(method, params: [], jsonrpc: "2.0", id: 83)
resp = CitaSync::Http.post(method, params: params, jsonrpc: jsonrpc, id: id)
Oj.load(resp.body)
cita_url = ENV["CITA_URL"]
client = AppChain::Client.new(cita_url)
client.rpc.call_rpc(method, jsonrpc: jsonrpc, params: params, id: id)
end
end
end
Expand Down
51 changes: 0 additions & 51 deletions app/models/cita_sync/http.rb

This file was deleted.

100 changes: 11 additions & 89 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,100 +1,22 @@
# frozen_string_literal: true

require "blockchain_pb"
require "ciri/utils"
require "ciri/crypto"

class Message
attr_reader :original_data, :original_signature, :unverified_transaction
attr_reader :data, :signature, :value, :to, :from, :version, :chain_id
attr_reader :unverified_transaction, :data, :signature, :value, :to, :from, :version, :chain_id

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

@original_data = @unverified_transaction["transaction"]["data"]
@original_signature = @unverified_transaction["signature"]

@data = to_hex(@original_data)
@signature = to_hex(@original_signature)

@version = @unverified_transaction["transaction"]["version"]

@value = to_hex(@unverified_transaction["transaction"]["value"])
@to = "0x" + if @version.zero?
@unverified_transaction["transaction"]["to"]
elsif @version == 1
@unverified_transaction["transaction"]["to_v1"]&.unpack1("H*")
else
""
end

@chain_id = if @version.zero?
@unverified_transaction["transaction"]["chain_id"]
elsif @version == 1
chain_id_v1 = @unverified_transaction["transaction"]["chain_id_v1"]&.unpack1("H*")
"0x#{chain_id_v1}"
end

@from = get_from
end

# 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
transaction = unverified_transaction["transaction"]
msg = ProtoTransaction.encode(transaction)
tx_msg = Ciri::Utils.keccak(msg)
pubkey = Ciri::Crypto.ecdsa_recover(tx_msg, @original_signature)
address = Ciri::Utils.keccak(pubkey[1..-1])[-20..-1]
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.unpack1("H*")
return str if str.downcase.start_with?("0x")

"0x" + str
end

# 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

# 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*")
@data = AppChain::TransactionSigner.decode(content)
@unverified_transaction = @data[:unverified_transaction]
@transaction = @unverified_transaction[:transaction]
@from = @data.dig(:sender, :address)
@to = @transaction[:to]
@data = @transaction[:data]
@value = @transaction[:value]
@version = @transaction[:version]
@chain_id = @transaction[:chain_id]
@signature = @unverified_transaction[:signature]
end
end
101 changes: 0 additions & 101 deletions lib/blockchain.proto

This file was deleted.

Loading

0 comments on commit 9928eb7

Please sign in to comment.