Skip to content

Commit

Permalink
[ERC-4337] Fix user operation hash mistake (#3228)
Browse files Browse the repository at this point in the history
* [ERC-4337] Fix user operation hash mistake

* Update TestBarz.kt

---------

Co-authored-by: hewigovens <[email protected]>
  • Loading branch information
rsrbk and hewigovens authored Jun 15, 2023
1 parent c6a0beb commit 4b3bacd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,18 @@ class TestBarz {

val output = AnySigner.sign(signingInput.build(), ETHEREUM, SigningOutput.parser())

assertEquals(Numeric.toHexString(output.preHash.toByteArray()), "0x731dd74cbf212bee883c6decbad07ad8980be89ca2e6c64de310af04e562d866");
assertEquals(Numeric.toHexString(output.preHash.toByteArray()), "0x2d37191a8688f69090451ed90a0a9ba69d652c2062ee9d023b3ebe964a3ed2ae");

assertEquals(output.encoded.toStringUtf8(), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"100000\",\"initCode\":\"0x\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"2\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0xb16db98b365b1f89191996942612b14f1da4bd5f\",\"signature\":\"0x3d29240a2decf2a1f47c88d1cb5923815ed50d24f8f01a9697e229ba69ce18d13779d2d96a5c93f5df29110677de62e75f4d751ab7baf5830276eb4ca257fb471b\",\"verificationGasLimit\":\"100000\"}");
assertEquals(output.encoded.toStringUtf8(), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"100000\",\"initCode\":\"0x\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"2\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0xb16db98b365b1f89191996942612b14f1da4bd5f\",\"signature\":\"0x80e84992ebf8d5f71180231163ed150a7557ed0aa4b4bcee23d463a09847e4642d0fbf112df2e5fa067adf4b2fa17fc4a8ac172134ba5b78e3ec9c044e7f28d71c\",\"verificationGasLimit\":\"100000\"}");
}

// https://testnet.bscscan.com/tx/0xea1f5cddc0653e116327cbcb3bc770360a642891176eff2ec69c227e46791c31
// https://testnet.bscscan.com/tx/0xea1f5cddc0653e116327cbcb3bc770360a642891176eff2ec69c227e46791c31
@Test
fun testSignR1TransferAccountNotDeployed() {
val factoryAddress = "0x3fC708630d85A3B5ec217E53100eC2b735d4f800"
val attestationObject = "0xa363666d74646e6f6e656761747453746d74a068617574684461746158981a70842af8c1feb7133b81e6a160a6a2be45ee057f0eb6d3f7f5126daa202e075d00000000000000000000000000000000000000000014c14f8a2dfd8f451581fad6e4e1c11821abcaacd6a5010203262001215820b173a6a812025c40c38bac46343646bd0a8137c807aae6e04aac238cc24d2ad2225820116ca14d23d357588ff2aabd7db29d5976f4ecc8037775db86f67e873a306b1f"
val verificationFacet = "0x5034534Efe9902779eD6eA6983F435c00f3bc510"

val signingInput = Ethereum.SigningInput.newBuilder()
signingInput.apply {
privateKey = ByteString.copyFrom(PrivateKey("3c90badc15c4d35733769093d3733501e92e7f16e101df284cee9a310d36c483".toHexByteArray()).data())
Expand Down Expand Up @@ -168,8 +168,8 @@ class TestBarz {

val output = AnySigner.sign(signingInput.build(), ETHEREUM, SigningOutput.parser())

assertEquals(Numeric.toHexString(output.preHash.toByteArray()), "0xb884c13821af6794e2fed1f5906f847251123adde02bd84723b50eda24fe2c93");
assertEquals(output.encoded.toStringUtf8(), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"2500000\",\"initCode\":\"0x3fc708630d85a3b5ec217e53100ec2b735d4f800296601cd0000000000000000000000005034534efe9902779ed6ea6983f435c00f3bc510000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040b173a6a812025c40c38bac46343646bd0a8137c807aae6e04aac238cc24d2ad2116ca14d23d357588ff2aabd7db29d5976f4ecc8037775db86f67e873a306b1f\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"0\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0x1392ae041bfbdbaa0cff9234a0c8f64df97b7218\",\"signature\":\"0x670d22e5361a75add1a90f2e86abf23a7e7b860795ca882e33efad17f0c6ff9a5c8e25260c89f1dd2aafba56ba0efdaec6cd976df399f848f443876fdb64867e1c\",\"verificationGasLimit\":\"3000000\"}");
assertEquals(Numeric.toHexString(output.preHash.toByteArray()), "0xb0920a70eee3309563cf9a83ad170cca7905e1170720c55c52c566b41f8daf83");

assertEquals(output.encoded.toStringUtf8(), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"2500000\",\"initCode\":\"0x3fc708630d85a3b5ec217e53100ec2b735d4f800296601cd0000000000000000000000005034534efe9902779ed6ea6983f435c00f3bc510000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040b173a6a812025c40c38bac46343646bd0a8137c807aae6e04aac238cc24d2ad2116ca14d23d357588ff2aabd7db29d5976f4ecc8037775db86f67e873a306b1f\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"0\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0x1392ae041bfbdbaa0cff9234a0c8f64df97b7218\",\"signature\":\"0xfcaecd5a66ef2d36a54437ce94185ef21a056dae4af66158e71cac84329050e570def91812915e059b08640cec070818aaef19fc8278e045d7b3e1f460869e581b\",\"verificationGasLimit\":\"3000000\"}");
}
}
4 changes: 1 addition & 3 deletions src/Ethereum/Transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ Data UserOperation::preHash(const uint256_t chainID) const {
std::make_shared<ABI::ParamUInt256>(chainID)});
Data encoded;
params.encode(encoded);
const auto hash = Hash::keccak256(encoded);
const auto hashStr = std::string(hash.begin(), hash.end());
return MessageSigner::generateMessage(hashStr);
return Hash::keccak256(encoded);
}

Data UserOperation::serialize([[maybe_unused]] const uint256_t chainID) const {
Expand Down
8 changes: 4 additions & 4 deletions swift/Tests/BarzTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class BarzTests: XCTestCase {
}
}
let output: EthereumSigningOutput = AnySigner.sign(input: input, coin: .ethereum)
XCTAssertEqual(output.preHash.hexString, "731dd74cbf212bee883c6decbad07ad8980be89ca2e6c64de310af04e562d866")
XCTAssertEqual(String(data: output.encoded, encoding: .utf8), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"100000\",\"initCode\":\"0x\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"2\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0xb16db98b365b1f89191996942612b14f1da4bd5f\",\"signature\":\"0x3d29240a2decf2a1f47c88d1cb5923815ed50d24f8f01a9697e229ba69ce18d13779d2d96a5c93f5df29110677de62e75f4d751ab7baf5830276eb4ca257fb471b\",\"verificationGasLimit\":\"100000\"}")
XCTAssertEqual(output.preHash.hexString, "2d37191a8688f69090451ed90a0a9ba69d652c2062ee9d023b3ebe964a3ed2ae")
XCTAssertEqual(String(data: output.encoded, encoding: .utf8), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"100000\",\"initCode\":\"0x\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"2\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0xb16db98b365b1f89191996942612b14f1da4bd5f\",\"signature\":\"0x80e84992ebf8d5f71180231163ed150a7557ed0aa4b4bcee23d463a09847e4642d0fbf112df2e5fa067adf4b2fa17fc4a8ac172134ba5b78e3ec9c044e7f28d71c\",\"verificationGasLimit\":\"100000\"}")
}

// https://testnet.bscscan.com/tx/0xea1f5cddc0653e116327cbcb3bc770360a642891176eff2ec69c227e46791c31
Expand Down Expand Up @@ -137,8 +137,8 @@ class BarzTests: XCTestCase {
}
}
let output: EthereumSigningOutput = AnySigner.sign(input: input, coin: .ethereum)
XCTAssertEqual(output.preHash.hexString, "b884c13821af6794e2fed1f5906f847251123adde02bd84723b50eda24fe2c93")
XCTAssertEqual(String(data: output.encoded, encoding: .utf8), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"2500000\",\"initCode\":\"0x3fc708630d85a3b5ec217e53100ec2b735d4f800296601cd0000000000000000000000005034534efe9902779ed6ea6983f435c00f3bc510000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040b173a6a812025c40c38bac46343646bd0a8137c807aae6e04aac238cc24d2ad2116ca14d23d357588ff2aabd7db29d5976f4ecc8037775db86f67e873a306b1f\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"0\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0x1392ae041bfbdbaa0cff9234a0c8f64df97b7218\",\"signature\":\"0x670d22e5361a75add1a90f2e86abf23a7e7b860795ca882e33efad17f0c6ff9a5c8e25260c89f1dd2aafba56ba0efdaec6cd976df399f848f443876fdb64867e1c\",\"verificationGasLimit\":\"3000000\"}")
XCTAssertEqual(output.preHash.hexString, "b0920a70eee3309563cf9a83ad170cca7905e1170720c55c52c566b41f8daf83")
XCTAssertEqual(String(data: output.encoded, encoding: .utf8), "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"2500000\",\"initCode\":\"0x3fc708630d85a3b5ec217e53100ec2b735d4f800296601cd0000000000000000000000005034534efe9902779ed6ea6983f435c00f3bc510000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040b173a6a812025c40c38bac46343646bd0a8137c807aae6e04aac238cc24d2ad2116ca14d23d357588ff2aabd7db29d5976f4ecc8037775db86f67e873a306b1f\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"0\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0x1392ae041bfbdbaa0cff9234a0c8f64df97b7218\",\"signature\":\"0xfcaecd5a66ef2d36a54437ce94185ef21a056dae4af66158e71cac84329050e570def91812915e059b08640cec070818aaef19fc8278e045d7b3e1f460869e581b\",\"verificationGasLimit\":\"3000000\"}")
}

let factory = "0x3fC708630d85A3B5ec217E53100eC2b735d4f800"
Expand Down
8 changes: 4 additions & 4 deletions tests/chains/Ethereum/BarzTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ TEST(Barz, SignK1TransferAccountDeployed) {
auto& transfer = *input.mutable_transaction()->mutable_transfer();
transfer.set_amount(amount.data(), amount.size());

std::string expected = "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"100000\",\"initCode\":\"0x\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"2\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0xb16db98b365b1f89191996942612b14f1da4bd5f\",\"signature\":\"0x3d29240a2decf2a1f47c88d1cb5923815ed50d24f8f01a9697e229ba69ce18d13779d2d96a5c93f5df29110677de62e75f4d751ab7baf5830276eb4ca257fb471b\",\"verificationGasLimit\":\"100000\"}";
std::string expected = "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"100000\",\"initCode\":\"0x\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"2\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0xb16db98b365b1f89191996942612b14f1da4bd5f\",\"signature\":\"0x80e84992ebf8d5f71180231163ed150a7557ed0aa4b4bcee23d463a09847e4642d0fbf112df2e5fa067adf4b2fa17fc4a8ac172134ba5b78e3ec9c044e7f28d71c\",\"verificationGasLimit\":\"100000\"}";

{
// sign test
TW::Ethereum::Proto::SigningOutput output;
ANY_SIGN(input, TWCoinTypeEthereum);

ASSERT_EQ(hexEncoded(output.pre_hash()), "0x731dd74cbf212bee883c6decbad07ad8980be89ca2e6c64de310af04e562d866");
ASSERT_EQ(hexEncoded(output.pre_hash()), "0x2d37191a8688f69090451ed90a0a9ba69d652c2062ee9d023b3ebe964a3ed2ae");
ASSERT_EQ(std::string(output.encoded()), expected);
}
}
Expand Down Expand Up @@ -237,14 +237,14 @@ TEST(Barz, SignR1TransferAccountNotDeployed) {
auto& transfer = *input.mutable_transaction()->mutable_transfer();
transfer.set_amount(amount.data(), amount.size());

std::string expected = "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"2500000\",\"initCode\":\"0x3fc708630d85a3b5ec217e53100ec2b735d4f800296601cd0000000000000000000000005034534efe9902779ed6ea6983f435c00f3bc510000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040b173a6a812025c40c38bac46343646bd0a8137c807aae6e04aac238cc24d2ad2116ca14d23d357588ff2aabd7db29d5976f4ecc8037775db86f67e873a306b1f\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"0\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0x1392ae041bfbdbaa0cff9234a0c8f64df97b7218\",\"signature\":\"0x670d22e5361a75add1a90f2e86abf23a7e7b860795ca882e33efad17f0c6ff9a5c8e25260c89f1dd2aafba56ba0efdaec6cd976df399f848f443876fdb64867e1c\",\"verificationGasLimit\":\"3000000\"}";
std::string expected = "{\"callData\":\"0xb61d27f600000000000000000000000061061fcae11fd5461535e134eff67a98cfff44e9000000000000000000000000000000000000000000000000002386f26fc1000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000\",\"callGasLimit\":\"2500000\",\"initCode\":\"0x3fc708630d85a3b5ec217e53100ec2b735d4f800296601cd0000000000000000000000005034534efe9902779ed6ea6983f435c00f3bc510000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040b173a6a812025c40c38bac46343646bd0a8137c807aae6e04aac238cc24d2ad2116ca14d23d357588ff2aabd7db29d5976f4ecc8037775db86f67e873a306b1f\",\"maxFeePerGas\":\"7033440745\",\"maxPriorityFeePerGas\":\"7033440745\",\"nonce\":\"0\",\"paymasterAndData\":\"0x\",\"preVerificationGas\":\"46856\",\"sender\":\"0x1392ae041bfbdbaa0cff9234a0c8f64df97b7218\",\"signature\":\"0xfcaecd5a66ef2d36a54437ce94185ef21a056dae4af66158e71cac84329050e570def91812915e059b08640cec070818aaef19fc8278e045d7b3e1f460869e581b\",\"verificationGasLimit\":\"3000000\"}";

{
// sign test
TW::Ethereum::Proto::SigningOutput output;
ANY_SIGN(input, TWCoinTypeEthereum);

ASSERT_EQ(hexEncoded(output.pre_hash()), "0xb884c13821af6794e2fed1f5906f847251123adde02bd84723b50eda24fe2c93");
ASSERT_EQ(hexEncoded(output.pre_hash()), "0xb0920a70eee3309563cf9a83ad170cca7905e1170720c55c52c566b41f8daf83");
ASSERT_EQ(std::string(output.encoded()), expected);
}
}
Expand Down

0 comments on commit 4b3bacd

Please sign in to comment.