-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: calculate the colony creation salt and validate it
- Loading branch information
Showing
4 changed files
with
1,868 additions
and
3,071 deletions.
There are no files selected for viewing
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
71 changes: 71 additions & 0 deletions
71
apps/main-chain/src/handlers/colonies/helpers/validateCreationSalt.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { verbose } from '@joincolony/utils'; | ||
import { utils } from 'ethers'; | ||
import rpcProvider from '~provider'; | ||
import networkClient from '~networkClient'; | ||
import { getTransactionSignerAddress } from '~utils/transactions'; | ||
|
||
const ContractCreationEvents = { | ||
Create3ProxyContractCreation: 'Create3ProxyContractCreation(address,bytes32)', | ||
}; | ||
|
||
export const getColonyCreationSalt = async ( | ||
blockNumber: number, | ||
transactionHash: string, | ||
): Promise<string | null> => { | ||
const create3ProxyLogs = await rpcProvider.getProviderInstance().getLogs({ | ||
fromBlock: blockNumber, | ||
toBlock: blockNumber, | ||
topics: [utils.id(ContractCreationEvents.Create3ProxyContractCreation)], | ||
}); | ||
|
||
if (create3ProxyLogs.length === 0) { | ||
verbose(`Couldn't fetch colony proxy contract creation events`); | ||
return null; | ||
} | ||
|
||
const create3ProxySalt = create3ProxyLogs[0].topics[2]; | ||
|
||
if (!create3ProxySalt) { | ||
verbose( | ||
`The Create3ProxyContractCreation log doesn't have the salt data: ${JSON.stringify(create3ProxyLogs[0], null, 2)}`, | ||
); | ||
return null; | ||
} | ||
|
||
const transaction = await rpcProvider | ||
.getProviderInstance() | ||
.getTransaction(transactionHash); | ||
|
||
const signerAddress = getTransactionSignerAddress(transaction); | ||
|
||
if (!signerAddress) { | ||
verbose( | ||
`Couldn't find the signer for transaction with txHash: ${transactionHash}`, | ||
); | ||
return null; | ||
} | ||
|
||
const generatedColonySalt = await networkClient.getColonyCreationSalt({ | ||
blockTag: blockNumber, | ||
from: signerAddress, | ||
}); | ||
|
||
const guardedSalt = utils.keccak256( | ||
utils.defaultAbiCoder.encode( | ||
['bytes32', 'bytes32'], | ||
[ | ||
utils.hexZeroPad(networkClient.address, 32), | ||
generatedColonySalt, // Actual salt | ||
], | ||
), | ||
); | ||
|
||
if (guardedSalt !== create3ProxySalt) { | ||
verbose( | ||
`The network salt doesn't match the salt used when creating the colony!`, | ||
); | ||
return null; | ||
} | ||
|
||
return generatedColonySalt; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { utils, Transaction } from 'ethers'; | ||
|
||
const MetatransactionInterface = new utils.Interface([ | ||
'function executeMetaTransaction(address userAddress, bytes memory payload, bytes32 sigR, bytes32 sigS, uint8 sigV) external payable returns (bytes memory)', | ||
]); | ||
|
||
export const getTransactionSignerAddress = ( | ||
transaction: Transaction, | ||
): string | undefined => { | ||
let signerAddress = transaction.from; | ||
|
||
try { | ||
const metaTx = MetatransactionInterface.parseTransaction({ | ||
data: transaction.data, | ||
value: transaction.value, | ||
}); | ||
signerAddress = metaTx.args[0]; | ||
} catch (error) { | ||
// if it's an error, it just means it's not a metatransaction | ||
} | ||
|
||
return signerAddress; | ||
}; |
Oops, something went wrong.