-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check if safe address & chainId match
- Loading branch information
1 parent
dfb2406
commit 7cb56ed
Showing
5 changed files
with
64 additions
and
31 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
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
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
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
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 |
---|---|---|
@@ -1,25 +1,47 @@ | ||
import { addressEqual } from '@/helpers/utils'; | ||
import { GnosisSafe } from '../types'; | ||
import { isSafeFile } from './validators'; | ||
|
||
export async function parseGnosisSafeFile( | ||
file: File | ||
file: File, | ||
safe: GnosisSafe | null | ||
): Promise<GnosisSafe.BatchFile> { | ||
return new Promise((res, rej) => { | ||
const reader = new FileReader(); | ||
reader.readAsText(file); | ||
reader.onload = async () => { | ||
try { | ||
if (typeof reader.result !== 'string') { | ||
throw new Error('Buffer can not be parsed'); | ||
return rej(new Error('Buffer can not be parsed')); | ||
} | ||
const json = JSON.parse(reader.result); | ||
if (!isSafeFile(json)) { | ||
throw new Error('Not a Gnosis Safe transaction file!'); | ||
return rej(new Error('Not a valid Safe transaction file.')); | ||
} | ||
if (!isCreatedFromSafe(json, safe)) { | ||
return rej( | ||
new Error( | ||
"Safe file does not match the selected treasury's address or chain ID" | ||
) | ||
); | ||
} | ||
return res(json); | ||
} catch (err) { | ||
rej(err); | ||
return rej(new Error('Safe file corrupted. Please select another.')); | ||
} | ||
}; | ||
}); | ||
} | ||
|
||
function isCreatedFromSafe( | ||
batchFile: GnosisSafe.BatchFile, | ||
safe: GnosisSafe | null | ||
): boolean { | ||
if (safe && batchFile.meta.createdFromSafeAddress) { | ||
return ( | ||
safe.network === batchFile.chainId && | ||
addressEqual(safe.safeAddress, batchFile.meta.createdFromSafeAddress) | ||
); | ||
} | ||
return false; | ||
} |