Created
October 26, 2020 19:04
-
-
Save Alirun/1e7f917902054047eb2cec02aede7985 to your computer and use it in GitHub Desktop.
How to verify your Ledger signature of Gnosis Safe transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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