Created
August 25, 2020 20:42
GnosisSafe-2.tsx
This file contains hidden or 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
import { useCallback } from "react"; | |
import EIP712Domain from "eth-typed-data"; | |
import BigNumber from "bignumber.js"; | |
import { SimpleSigner } from "did-jwt"; | |
import { ethers } from "ethers"; | |
const { utils } = ethers; | |
import { useGnosisApi, useGnosisEstimateTransaction } from "../../session"; | |
export default function useGnosisWithdraw() { | |
const gnosis = useGnosisApi(); | |
const { gnosisEstimateTransaction } = useGnosisEstimateTransaction(); | |
const gnosisWithdraw = useCallback( | |
async ({ safe, privateKey }) => { | |
const safeDomain = new EIP712Domain({ | |
verifyingContract: safe, | |
}); | |
const SafeTx = safeDomain.createType('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 to = utils.getAddress("0x0ebd146ffd9e20bf74e374e5f3a5a567a798177e"); | |
const baseTxn = { | |
to, | |
value: "1000", | |
data: utils.hexlify([]), | |
nonce: "0", | |
operation: "0", | |
}; | |
const { safeTxGas, baseGas, dataGas, gasPrice, gasToken, refundReceiver } = await gnosisEstimateTransaction(safe, baseTxn); | |
const txn = { | |
...baseTxn, | |
safeTxGas: safeTxGas, //0, | |
baseGas: baseGas, //0, | |
gasPrice: gasPrice, //0, | |
gasToken: gasToken, // "0x0000000000000000000000000000000000000000", | |
refundReceiver: refundReceiver, //"0x0000000000000000000000000000000000000000" | |
}; | |
const safeTx = new SafeTx(txn); | |
console.log(safeDomain.domainSeparator.toString('hex')) // expected: 941d7660b44bfb2cd573ec60520eedf2265e16609a8737bb1cf10adc58994cdf | |
console.log(SafeTx.typeHash().toString('hex')) // expected: bb8310d486368db6bd6f849402fdd73ad53d316b5a4b2644ad6efe0f941286d8 | |
console.log(safeTx.signHash().toString('hex')) // expected: ba252606abd1024b666c8f4a0f8eb2230323eb1d67b7ff1de23a290dcca52ea3 | |
// XXX: I've tried with the 0x prefix and without. :P | |
const signer = new SimpleSigner(`0x${privateKey}`); | |
const { r, s, recoveryParam: v } = await safeTx.sign(signer); | |
console.log({ r, s, v }); | |
const signature = Object.freeze({ | |
r: new BigNumber(`0x${r}`, 16).toString(10), | |
s: new BigNumber(`0x${s}`, 16).toString(10), | |
v, | |
}); | |
const { data: x } = await gnosis({ | |
url: `/v1/safes/${safe}/transactions/`, | |
method: "post", | |
data: { | |
...txn, | |
dataGas: dataGas, | |
signatures: [signature], | |
}, | |
}); | |
console.log({ signature }); | |
}, | |
[gnosis] | |
); | |
return { gnosisWithdraw }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment