Skip to content

Instantly share code, notes, and snippets.

@Alirun
Created October 26, 2020 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alirun/1e7f917902054047eb2cec02aede7985 to your computer and use it in GitHub Desktop.
Save Alirun/1e7f917902054047eb2cec02aede7985 to your computer and use it in GitHub Desktop.
How to verify your Ledger signature of Gnosis Safe transaction
const { TypedDataUtils } = require('eth-sig-util')
const { sha256 } = require('ethereumjs-util')
function generateSafeTxHash(safeAddress, txArgs) {
const messageTypes = {
EIP712Domain: [{ type: 'address', name: 'verifyingContract' }],
SafeTx: [
{ type: 'address', name: 'to' },
{ type: 'uint256', name: 'value' },
{ type: 'bytes', name: 'data' },
{ type: 'uint8', name: 'operation' },
{ type: 'uint256', name: 'safeTxGas' },
{ type: 'uint256', name: 'baseGas' },
{ type: 'uint256', name: 'gasPrice' },
{ type: 'address', name: 'gasToken' },
{ type: 'address', name: 'refundReceiver' },
{ type: 'uint256', name: 'nonce' },
],
}
const primaryType = 'SafeTx'
const typedData = {
types: messageTypes,
domain: {
verifyingContract: safeAddress,
},
primaryType,
message: txArgs,
}
return TypedDataUtils.sign(typedData)
}
const safeTxHashBuffer = generateSafeTxHash(
'0x0000000000000000000000000000000000000000', // Safe address
{
to: '0x0000000000000000000000000000000000000000',
value: '0',
data: '0x',
operation: '0',
safeTxGas: '0',
baseGas: '0',
gasPrice: '0',
gasToken: '0x0000000000000000000000000000000000000000',
refundReceiver: '0x0000000000000000000000000000000000000000',
nonce: 0,
}
)
const dataHashBuf = sha256(safeTxHashBuffer)
console.log(`safeTxHash: 0x${safeTxHashBuffer.toString('hex')}`)
console.log(`ledgerSignature: 0x${dataHashBuf.toString('hex')}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment