Skip to content

Commit

Permalink
Merge pull request #760 from matter-labs/dvush/accept-eth-signatures-…
Browse files Browse the repository at this point in the history
…btc-v

Dvush/accept eth signatures btc v
  • Loading branch information
dvush authored Jun 23, 2020
2 parents e8ca026 + 8ba40e8 commit da0670e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 13 additions & 4 deletions core/models/src/node/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,13 @@ impl Serialize for EIP1271Signature {
///
/// Some notes on implementation of methods of this structure:
///
/// Ethereum signed messages expect v parameter to be 27 + recovery_id(0,1,2,3)
/// Ethereum signed message produced by most clients contains v where v = 27 + recovery_id(0,1,2,3),
/// but for some clients v = recovery_id(0,1,2,3).
/// Library that we use for signature verification (written for bitcoin) expects v = recovery_id
///
/// That is why:
/// 1) when we create this structure by deserialization of message produced by user
/// we subtract 27 from v in `ETHSignature` and store it in ETHSignature structure this way.
/// we subtract 27 from v in `ETHSignature` if necessary and store it in the `ETHSignature` structure this way.
/// 2) When we serialize/create this structure we add 27 to v in `ETHSignature`.
///
/// This way when we have methods that consumes &self we can be sure that ETHSignature::recover_signer works
Expand All @@ -863,8 +864,14 @@ impl PackedEthSignature {

pub fn deserialize_packed(bytes: &[u8]) -> Result<Self, failure::Error> {
ensure!(bytes.len() == 65, "eth signature length should be 65 bytes");
// assumes v = recover + 27
Ok(PackedEthSignature(ETHSignature::from_electrum(&bytes)))
let mut bytes_array = [0u8; 65];
bytes_array.copy_from_slice(&bytes);

if bytes_array[64] >= 27 {
bytes_array[64] -= 27;
}

Ok(PackedEthSignature(ETHSignature::from(bytes_array)))
}

/// Signs message using ethereum private key, results are identical to signature created
Expand Down Expand Up @@ -1168,6 +1175,8 @@ mod test {
("0x8a91dc2d28b689474298d91899f0c1baf62cb85b", "0x", "0xd98f51c2ee0fd589e421348002dffec5d1b38e5bef9a41a699030456dc39298d12698158dc2a814b5f9ac6d433009dec87484a4579107be3f8f33907e92938291b"),
// this example has v = 28, unlike others
("0x8a91dc2d28b689474298d91899f0c1baf62cb85b", "0x14", "0xd288b623af654c9d805e132812edf09ce244040376ca49112e91d437ecceed7c518690d4ae14149cd533f1ca4f081e6d2252c980fccc63de4d6bb818f1b668921c"),
// same as first, but v is just recovery id
("0x8a91dc2d28b689474298d91899f0c1baf62cb85b", "0xdead", "0x13c34c76ffb42d97da67ddc5d275e92d758d1b48b5ee4b3bacd800cbeec3baff043a5ee63fea55485e1ee5d6f8b088daabd095f2ebbdc80a33806528b44bfccc01"),
];

for (address, msg, signature) in examples {
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
version: '3.2'
services:
postgres:
image: "postgres:10.4"
image: "postgres:12"
ports:
- "5432:5432"
volumes:
- type: bind
source: ./volumes/postgres
target: /var/lib/postgresql/data
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
geth:
image: "matterlabs/geth:latest"
ports:
Expand Down

0 comments on commit da0670e

Please sign in to comment.