-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add message model to process transaction content and get original value
- Loading branch information
1 parent
10db761
commit 4bcd15f
Showing
5 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require "blockchain_pb" | ||
require "ciri/utils" | ||
require "ciri/crypto" | ||
|
||
class Message | ||
attr_reader :original_data, :original_signature | ||
attr_reader :data, :signature, :value, :to, :from | ||
|
||
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) | ||
|
||
@value = to_hex(@unverified_transaction["transaction"]["value"]) | ||
@to = "0x" + @unverified_transaction["transaction"]["to"] | ||
|
||
@from = get_from | ||
end | ||
|
||
# decode | ||
def decode(content) | ||
binary_str = hex_to_binary_str(content) | ||
::UnverifiedTransaction.decode(binary_str) | ||
end | ||
|
||
def get_from | ||
digest_data = Ciri::Utils.sha3(@original_data) | ||
pubkey = Ciri::Crypto.ecdsa_recover(digest_data, @original_signature) | ||
# address = Ciri::Utils.sha3(pubkey[1..-1]).unpack("H*").first.downcase[-40..-1] | ||
address = Ciri::Utils.sha3(pubkey[1..-1])[-20..-1] | ||
Ciri::Utils.to_hex(address) | ||
end | ||
|
||
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 | ||
def filter_hex_str(hex) | ||
return hex[2..-1] if hex.start_with?("0x") | ||
hex | ||
end | ||
|
||
def hex_to_buffer(hex) | ||
hex_str = filter_hex_str(hex) | ||
[hex_str].pack('H*').bytes.to_a | ||
end | ||
|
||
# deserialization | ||
def hex_to_binary_str(hex) | ||
hex_to_buffer(hex).pack("c*") | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
syntax = "proto3"; | ||
|
||
enum ProofType { | ||
AuthorityRound = 0; | ||
Raft = 1; | ||
Tendermint = 2; | ||
} | ||
|
||
message Proof { | ||
bytes content = 1; | ||
ProofType type = 2; | ||
} | ||
|
||
message BlockHeader { | ||
bytes prevhash = 1; | ||
uint64 timestamp = 2; | ||
uint64 height = 3; | ||
bytes state_root = 4; | ||
bytes transactions_root = 5; | ||
bytes receipts_root = 6; | ||
uint64 gas_used = 7; | ||
uint64 gas_limit = 8; | ||
Proof proof = 9; | ||
bytes proposer = 10; | ||
} | ||
|
||
message Status { | ||
bytes hash = 1; | ||
uint64 height = 2; | ||
} | ||
|
||
message AccountGasLimit { | ||
uint64 common_gas_limit = 1; | ||
map<string,uint64> specific_gas_limit = 2; | ||
} | ||
|
||
message RichStatus { | ||
bytes hash = 1; | ||
uint64 height = 2; | ||
repeated bytes nodes = 3; | ||
uint64 interval = 4; | ||
} | ||
|
||
enum Crypto { | ||
SECP = 0; | ||
SM2 = 1; | ||
} | ||
|
||
message ProtoTransaction { | ||
string to = 1; | ||
string nonce = 2; | ||
uint64 quota = 3; | ||
uint64 valid_until_block = 4; | ||
bytes data = 5; | ||
bytes value = 6; | ||
uint32 chain_id = 7; | ||
uint32 version = 8; | ||
} | ||
|
||
message UnverifiedTransaction { | ||
ProtoTransaction transaction = 1; | ||
bytes signature = 2; | ||
Crypto crypto = 3; | ||
} | ||
|
||
message SignedTransaction { | ||
UnverifiedTransaction transaction_with_sig = 1; | ||
// SignedTransaction hash | ||
bytes tx_hash = 2; | ||
// public key | ||
bytes signer = 3; | ||
} | ||
|
||
// data precompile API | ||
|
||
message BlockBody { | ||
repeated SignedTransaction transactions = 1; | ||
} | ||
|
||
message Block { | ||
uint32 version = 1; | ||
BlockHeader header = 2; | ||
BlockBody body = 3; | ||
} | ||
|
||
message BlockWithProof { | ||
Block blk = 1; | ||
Proof proof = 2; | ||
} | ||
|
||
message BlockTxs { | ||
uint64 height = 1; | ||
BlockBody body = 3; | ||
} | ||
|
||
message BlackList { | ||
// black list of address, the account that sent the transaction does not have enough gas | ||
repeated bytes black_list = 1; | ||
// clear list of address | ||
repeated bytes clear_list = 2; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.