From e034a8f979f4a634889edfcb5f16eda7161197d4 Mon Sep 17 00:00:00 2001 From: Luke Tchang Date: Sun, 18 Sep 2022 08:45:33 -0700 Subject: [PATCH] docs: add comment about safety of u8 -> u64 cast in signature (#1704) * docs: add comment about u8 -> u64 cast in signature * fix: format Co-authored-by: Georgios Konstantopoulos --- CHANGELOG.md | 1 + ethers-core/src/types/signature.rs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bbad810..40090899 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Unreleased +- Add comment about safety of u8 -> u64 cast in `ethers_core::types::Signature` - Stop defaulting to the `"latest"` block in `eth_estimateGas` params [#1657](https://github.com/gakonst/ethers-rs/pull/1657) - Fix geth trace types for debug_traceTransaction rpc - Fix RLP decoding of legacy `Transaction` diff --git a/ethers-core/src/types/signature.rs b/ethers-core/src/types/signature.rs index 8e2ff8e9..f0ef3685 100644 --- a/ethers-core/src/types/signature.rs +++ b/ethers-core/src/types/signature.rs @@ -215,6 +215,11 @@ impl From<&Signature> for [u8; 65] { sig[32..64].copy_from_slice(&s_bytes); // TODO: What if we try to serialize a signature where // the `v` is not normalized? + + // The u64 to u8 cast is safe because `sig.v` can only ever be 27 or 28 + // here. Regarding EIP-155, the modification to `v` happens during tx + // creation only _after_ the transaction is signed using + // `ethers_signers::to_eip155_v`. sig[64] = src.v as u8; sig }