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

fix: sign eth empty address #73

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions imkey-core/ikc/src/ethereum_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ pub fn sign_eth_transaction(data: &[u8], sign_param: &SignParam) -> Result<Vec<u
hex::decode(&input.data).unwrap()
};

let mut to = input.to;
if to.starts_with("0x") {
to = to[2..].to_string();
}
let to = if input.to.is_empty() || input.to == "0x" {
"0000000000000000000000000000000000000000"
} else if input.to.starts_with("0x") {
&input.to[2..]
} else {
&input.to
};

let eth_tx = if input.r#type.to_lowercase() == "0x02"
|| input.r#type.to_lowercase() == "0x2"
Expand Down
41 changes: 40 additions & 1 deletion token-core/tcx-eth/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ impl TryFrom<&EthTxInput> for Transaction {
type Error = anyhow::Error;

fn try_from(tx: &EthTxInput) -> std::result::Result<Self, Self::Error> {
let to_addr = if tx.to.is_empty() || tx.to == "0x" {
"0x0000000000000000000000000000000000000000"
} else {
&tx.to
};
Ok(Transaction {
to: Address::from_slice(&Vec::from_0x_hex(&tx.to)?),
to: Address::from_slice(&Vec::from_hex_auto(to_addr)?),
nonce: parse_u64(&tx.nonce)?,
gas: parse_u256(&tx.gas_limit)?,
gas_price: parse_u256(&tx.gas_price)?,
Expand Down Expand Up @@ -199,6 +204,40 @@ mod test {
assert_eq!(tx_output.signature, "f86d8283ca85012a05f20082c350946031564e7b2f5cc33737807b2e58daff870b590b870228108d99bd318078a09d56ef5b7ba4d6e2c4b9367ab263beb6bc2926bb9170ff2f42f0e25cbdec9aa7a062b6e1b702b1a34e887d6a3f693bdc8fbcd92e2963c12dfcffd255022d892fbe");
}

#[test]
fn test_create_contract() {
let tx = EthTxInput {
nonce: "33738".to_string(),
gas_price: "5000000000".to_string(),
gas_limit: "50000".to_string(),
to: "".to_string(),
value: "607001513671985".to_string(),
data: "0x11111111".to_string(),
chain_id: "42".to_string(),
tx_type: "00".to_string(),
max_fee_per_gas: "".to_string(),
max_priority_fee_per_gas: "".to_string(),
access_list: vec![],
};
let mut keystore =
private_key_store("cce64585e3b15a0e4ee601a467e050c9504a0db69a559d7ec416fa25ad3410c2");
let params = SignatureParameters {
curve: CurveType::SECP256k1,
derivation_path: "m/44'/60'/0'/0/0".to_string(),
chain_type: "ETHEREUM".to_string(),
network: "".to_string(),
seg_wit: "".to_string(),
};

let tx_output = keystore.sign_transaction(&params, &tx).unwrap();

assert_eq!(
tx_output.tx_hash,
"0x5830b35ec8b19b8a55b39d18d8533a97a7dfb8dba1b99e79e055099fc349602f"
);
assert_eq!(tx_output.signature, "f8718283ca85012a05f20082c350940000000000000000000000000000000000000000870228108d99bd31841111111178a0d1e3efb80e9338cf63a05b6c5209ffa3417cbbc7bdfd0eddc1ded150ad5db96ca008d395b3de0484f9ee64546b8f3de2b545b0897e9e5e63b951d56cb68accf970");
}

#[test]
fn test_etc_transaction() {
let tx = EthTxInput {
Expand Down