Fix: Ledger should have correct signature v value for chains with large chain ID (#1204)

* fix: Ledger should have correct signature v value

* format code

Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
Max 2022-05-03 01:53:11 +07:00 committed by GitHub
parent d5de795382
commit da38083013
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 1 deletions

View File

@ -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