-
Notifications
You must be signed in to change notification settings - Fork 468
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
Conversation
Branch preview⏳ Deploying a preview site... |
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, | ||
], | ||
), | ||
), | ||
) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are unchanged.
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are unchanged.
📦 Next.js Bundle Analysis for safe-wallet-webThis analysis was generated by the Next.js Bundle Analysis action. 🤖
|
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.
Coverage report
Show new covered files 🐣
Show files with reduced coverage 🔻
Test suite run success1730 tests passing in 232 suites. Report generated by 🧪jest coverage report action from 6e8c19f |
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
SafeTx
s, but notSafeMessage
s. 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
Checklist