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: Ledger should have correct signature v value for chains with large chain ID #1204

Merged
merged 3 commits into from
May 2, 2022
Merged
Changes from all commits
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
29 changes: 28 additions & 1 deletion ethers-signers/src/ledger/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,34 @@ impl LedgerEthereum {
}
let mut payload = Self::path_to_bytes(&self.derivation);
payload.extend_from_slice(tx_with_chain.rlp().as_ref());
self.sign_payload(INS::SIGN, payload).await

let mut signature = self.sign_payload(INS::SIGN, payload).await?;

// modify `v` value of signature to match EIP-155 for chains with large chain ID
// The logic is derived from Ledger's library
// https://github.com/LedgerHQ/ledgerjs/blob/e78aac4327e78301b82ba58d63a72476ecb842fc/packages/hw-app-eth/src/Eth.ts#L300
let eip155_chain_id = self.chain_id * 2 + 35;
if eip155_chain_id + 1 > 255 {
let one_byte_chain_id = eip155_chain_id % 256;
let ecc_parity = if signature.v > one_byte_chain_id {
signature.v - one_byte_chain_id
} else {
one_byte_chain_id - signature.v
};

signature.v = match tx {
TypedTransaction::Eip2930(_) | TypedTransaction::Eip1559(_) => {
if ecc_parity % 2 == 1 {
0
} else {
1
}
}
TypedTransaction::Legacy(_) => eip155_chain_id + ecc_parity,
};
}

Ok(signature)
}

/// Signs an ethereum personal message
Expand Down