Skip to content
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

feat: tron sign message support v2 version[R2D2-11852] #131

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions imkey-core/ikc-proto/src/tron.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ message TronTxOutput {
}

message TronMessageInput {
string message = 2;
bool is_hex =4;
bool is_tron_header=5;
string message = 1;
bool is_hex = 2;
bool is_tron_header = 3;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个命名太有歧义了。感觉叫做 header 分别为"none", "eth", "tron" 这种更明显一些

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已处理,修改is_tron_header字段为header,并改为string类型,通过"none", "eth", "tron" 三个参数标注message签名的头信息。

string version = 4;
}

message TronMessageOutput {
Expand Down
5 changes: 4 additions & 1 deletion imkey-core/ikc-wallet/coin-tron/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ impl TronSigner {

// this code is from tron wallet
let header = match input.is_tron_header {
true => "\x19TRON Signed Message:\n32".as_bytes(),
true => match input.version.to_uppercase().as_str() {
"V2" => "\x19TRON Signed Message:\n".as_bytes(),
_ => "\x19TRON Signed Message:\n32".as_bytes(),
},
false => "\x19Ethereum Signed Message:\n32".as_bytes(),
};
let mut msg_with_header = Vec::new();
Expand Down
8 changes: 5 additions & 3 deletions imkey-core/ikc-wallet/coin-tron/src/tronapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ pub struct TronTxOutput {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TronMessageInput {
#[prost(string, tag = "2")]
#[prost(string, tag = "1")]
pub message: ::prost::alloc::string::String,
#[prost(bool, tag = "4")]
#[prost(bool, tag = "2")]
pub is_hex: bool,
#[prost(bool, tag = "5")]
#[prost(bool, tag = "3")]
pub is_tron_header: bool,
#[prost(string, tag = "4")]
pub version: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
1 change: 1 addition & 0 deletions token-core/tcx-proto/src/tron.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ message TronTxOutput {
message TronMessageInput {
string value = 1;
bool isTronHeader = 2;
string version = 3;
}

message TronMessageOutput {
Expand Down
5 changes: 4 additions & 1 deletion token-core/tcx-tron/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ impl TraitMessageSigner<TronMessageInput, TronMessageOutput> for Keystore {
let data = Vec::from_hex_auto(&message.value)?;

let header = match message.is_tron_header {
true => "\x19TRON Signed Message:\n32".as_bytes(),
true => match message.version.to_uppercase().as_str() {
"V2" => "\x19TRON Signed Message:\n".as_bytes(),
_ => "\x19TRON Signed Message:\n32".as_bytes(),
},
false => "\x19Ethereum Signed Message:\n32".as_bytes(),
};
let to_hash = [header, &data].concat();
Expand Down
2 changes: 2 additions & 0 deletions token-core/tcx-tron/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct TronMessageInput {
pub value: ::prost::alloc::string::String,
#[prost(bool, tag = "2")]
pub is_tron_header: bool,
#[prost(string, tag = "3")]
pub version: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
38 changes: 38 additions & 0 deletions token-core/tcx/tests/sign_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,19 @@ fn test_tron_sign_message() {
value: "0x645c0b7b58158babbfa6c6cd5a48aa7340a8749176b120e8516216787a13dc76"
.to_string(),
is_tron_header: true,
version: "V1".to_string(),
}, "0x16417c6489da3a88ef980bf0a42551b9e76181d03e7334548ab3cb36e7622a484482722882a29e2fe4587b95c739a68624ebf9ada5f013a9340d883f03fcf9af1b"),
(TronMessageInput {
value: "645c0b7b58158babbfa6c6cd5a48aa7340a8749176b120e8516216787a13dc76"
.to_string(),
is_tron_header: true,
version: "V1".to_string(),
}, "0x16417c6489da3a88ef980bf0a42551b9e76181d03e7334548ab3cb36e7622a484482722882a29e2fe4587b95c739a68624ebf9ada5f013a9340d883f03fcf9af1b"),
(TronMessageInput {
value: "abcdef"
.to_string(),
is_tron_header: true,
version: "V1".to_string(),
}, "0x13e407627e584c821ba527d23d64163d458447dfea1c3bfc92be660aa8d093ee5cfa3881870c4c51f157828eb9d4f7fad8112761f3b51cf76c7a4a3f241033d51b"),
];
for (input, expected) in input_expects {
Expand All @@ -541,6 +544,40 @@ fn test_tron_sign_message() {
});
}

#[test]
#[serial]
fn test_tron_sign_message_v2() {
run_test(|| {
let wallet = import_default_wallet();

let input = TronMessageInput {
value: "0x645c0b7b58158babbfa6c6cd5a48aa7340a8749176b120e8516216787a13dc76"
.to_string(),
is_tron_header: true,
version: "V2".to_string(),
};

let tx = SignParam {
id: wallet.id.to_string(),
key: Some(Key::Password(TEST_PASSWORD.to_string())),
chain_type: "TRON".to_string(),
path: "m/44'/195'/0'/0/0".to_string(),
curve: "secp256k1".to_string(),
network: "".to_string(),
seg_wit: "".to_string(),
input: Some(::prost_types::Any {
type_url: "imtoken".to_string(),
value: encode_message(input).unwrap(),
}),
};

let sign_result = call_api("sign_msg", tx).unwrap();
let ret: TronMessageOutput = TronMessageOutput::decode(sign_result.as_slice()).unwrap();
assert_eq!("0x9e7a691647c02fad5fe939a50df0351a58be67b3cdd87619c37f316b913d0be92ecf190f5e0c3640d54d9be731e8ab4bea4894ca9e7267b6c86d852e5c5dd71d1c", ret.signature);

});
}

#[test]
#[serial]
fn test_bitcoin_sign_message() {
Expand Down Expand Up @@ -589,6 +626,7 @@ fn test_sign_by_dk_hd_store() {
let input = TronMessageInput {
value: "0x645c0b7b58158babbfa6c6cd5a48aa7340a8749176b120e8516216787a13dc76".to_string(),
is_tron_header: true,
version: "V1".to_string()
};

let dk_param = WalletKeyParam {
Expand Down
Loading