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

feat: display SafeMessage message hash when signing off-chain #4687

Merged
merged 6 commits into from
Dec 19, 2024

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Dec 18, 2024

⚠️ Branches from #4621.

What it solves

Resolves missing message hash when signing off-chain messages

How this PR fixes it

We currently display the domain/message hashes for SafeTxs, but not SafeMessages. This adds the latter.

Note: this also moves the domain/SafeTx hash helpers (and their tests) to a centralised file.

How to test it

Sign off-chain and observe the new domain/message hashes that match that being signed on hardware wallets.

Screenshots

image

Checklist

  • I've tested the branch on mobile 📱
  • I've documented how it affects the analytics (if at all) 📊
  • I've written a unit/e2e test for it (if applicable) 🧑‍💻

Copy link

github-actions bot commented Dec 18, 2024

Branch preview

⏳ Deploying a preview site...

Comment on lines +6 to +119
const NEW_SAFE_TX_TYPEHASH = '0xbb8310d486368db6bd6f849402fdd73ad53d316b5a4b2644ad6efe0f941286d8'

// Not versioned (>= 0.1.0)
// keccak256("SafeMessage(bytes message)");
const SAFE_MESSAGE_TYPEHASH = '0x60b3cbf8b4a223d68d641b3b6ddf9a298e7f33710cf3d3a9d1146b5a6150fbca'

describe('getDomainHash', () => {
it.each(['1.0.0' as const, '1.1.1' as const, '1.2.0' as const])(
'should return the domain hash without chain ID for version %s',
(version) => {
const chainId = faker.string.numeric()
const safeAddress = faker.finance.ethereumAddress()

const result = getDomainHash({ chainId, safeAddress, safeVersion: version })

expect(result).toEqual(
keccak256(AbiCoder.defaultAbiCoder().encode(['bytes32', 'address'], [OLD_DOMAIN_TYPEHASH, safeAddress])),
)
},
)

it.each(['1.3.0' as const, '1.4.1' as const])(
'should return the domain hash with chain ID for version %s',
(version) => {
const chainId = faker.string.numeric()
const safeAddress = faker.finance.ethereumAddress()

const result = getDomainHash({ chainId, safeAddress, safeVersion: version })

expect(result).toEqual(
keccak256(
AbiCoder.defaultAbiCoder().encode(
['bytes32', 'uint256', 'address'],
[NEW_DOMAIN_TYPEHASH, chainId, safeAddress],
),
),
)
},
)
})

describe('getSafeTxMessageHash', () => {
it.each([
['0.1.0' as SafeVersion, OLD_SAFE_TX_TYPEHASH],
['1.0.0' as const, NEW_SAFE_TX_TYPEHASH],
['1.1.1' as const, NEW_SAFE_TX_TYPEHASH],
['1.2.0' as const, NEW_SAFE_TX_TYPEHASH],
['1.3.0' as const, NEW_SAFE_TX_TYPEHASH],
['1.4.1' as const, NEW_SAFE_TX_TYPEHASH],
])('should return the message hash for version %s', (version, typehash) => {
const SafeTx: SafeTransactionData = {
to: faker.finance.ethereumAddress(),
value: faker.string.numeric(),
data: faker.string.hexadecimal({ length: 30 }),
operation: faker.number.int({ min: 0, max: 1 }),
safeTxGas: faker.string.numeric(),
baseGas: faker.string.numeric(), // <1.0.0 is dataGas
gasPrice: faker.string.numeric(),
gasToken: faker.finance.ethereumAddress(),
refundReceiver: faker.finance.ethereumAddress(),
nonce: faker.number.int({ min: 0, max: 69 }),
}

const result = getSafeTxMessageHash({ safeVersion: version, safeTxData: SafeTx })

expect(result).toEqual(
keccak256(
AbiCoder.defaultAbiCoder().encode(
[
'bytes32',
'address', // to
'uint256', // value
'bytes32', // data
'uint8', // operation
'uint256', // safeTxGas
'uint256', // dataGas/baseGas
'uint256', // gasPrice
'address', // gasToken
'address', // refundReceiver
'uint256', // nonce
],
[
typehash,
SafeTx.to,
SafeTx.value,
// EIP-712 expects data to be hashed
keccak256(SafeTx.data),
SafeTx.operation,
SafeTx.safeTxGas,
SafeTx.baseGas,
SafeTx.gasPrice,
SafeTx.gasToken,
SafeTx.refundReceiver,
SafeTx.nonce,
],
),
),
)
})
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are unchanged.

Comment on lines +8 to +48
const NEW_DOMAIN_TYPE_HASH_VERSION = '>=1.3.0'
const NEW_SAFE_TX_TYPE_HASH_VERSION = '>=1.0.0'

export function getDomainHash({
chainId,
safeAddress,
safeVersion,
}: {
chainId: string
safeAddress: string
safeVersion: SafeVersion
}): string {
const includeChainId = semverSatisfies(safeVersion, NEW_DOMAIN_TYPE_HASH_VERSION)
return TypedDataEncoder.hashDomain({
...(includeChainId && { chainId }),
verifyingContract: safeAddress,
})
}

export function getSafeTxMessageHash({
safeVersion,
safeTxData,
}: {
safeVersion: SafeVersion
safeTxData: SafeTransactionData
}): string {
const usesBaseGas = semverSatisfies(safeVersion, NEW_SAFE_TX_TYPE_HASH_VERSION)
const SafeTx = getEip712TxTypes(safeVersion).SafeTx

// Clone to not modify the original
const tx: any = { ...safeTxData }

if (!usesBaseGas) {
tx.dataGas = tx.baseGas
delete tx.baseGas

SafeTx[5].name = 'dataGas'
}

return TypedDataEncoder.hashStruct('SafeTx', { SafeTx }, tx)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are unchanged.

Copy link

@iamacook iamacook requested a review from tmjssz December 18, 2024 08:30
Copy link

📦 Next.js Bundle Analysis for safe-wallet-web

This analysis was generated by the Next.js Bundle Analysis action. 🤖

⚠️ Global Bundle Size Increased

Page Size (compressed)
global 1017.11 KB (🟡 +782 B)
Details

The global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

If you want further insight into what is behind the changes, give @next/bundle-analyzer a try!

Eight Pages Changed Size

The following pages changed size from the code in this PR compared to its base branch:

Page Size (compressed) First Load
/apps/open 53.07 KB (🟢 -582 B) 1.05 MB
/home 58.7 KB (🟡 +1 B) 1.05 MB
/transactions 96.09 KB (🟢 -802 B) 1.09 MB
/transactions/history 96.05 KB (🟢 -802 B) 1.09 MB
/transactions/messages 57.83 KB (🟢 -582 B) 1.05 MB
/transactions/msg 53.99 KB (🟢 -582 B) 1.05 MB
/transactions/queue 46.95 KB (🟢 -582 B) 1.04 MB
/transactions/tx 46.21 KB (🟢 -582 B) 1.04 MB
Details

Only the gzipped size is provided here based on an expert tip.

First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If next/link is used, subsequent page loads would only need to download that page's bundle (the number in the "Size" column), since the global bundle has already been downloaded.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 20% or more, there will be a red status indicator applied, indicating that special attention should be given to this.

Copy link

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
73.93% (+0.01% 🔼)
14417/19501
🔴 Branches 51.45% 3438/6682
🔴 Functions
56.88% (+0.01% 🔼)
2046/3597
🟡 Lines
75.46% (+0.01% 🔼)
13077/17329
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🟢
... / safe-hashes.ts
100% 100% 100% 100%
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🔴
... / index.tsx
50% (-30.77% 🔻)
0% (-50% 🔻)
0% (-66.67% 🔻)
55.56% (-28.44% 🔻)

Test suite run success

1730 tests passing in 232 suites.

Report generated by 🧪jest coverage report action from 6e8c19f

@iamacook iamacook marked this pull request as ready for review December 18, 2024 08:36
@iamacook iamacook self-assigned this Dec 18, 2024
Base automatically changed from old-safetx-hash to dev December 19, 2024 15:22
@katspaugh katspaugh merged commit cb19058 into dev Dec 19, 2024
7 of 14 checks passed
@katspaugh katspaugh deleted the safemessage-message-hashes branch December 19, 2024 15:27
@github-actions github-actions bot locked and limited conversation to collaborators Dec 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants