This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
forked from 5afe/safe-core-protocol-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix formatting and enforce linting in workflow (5afe#11)
- Loading branch information
Showing
4 changed files
with
49 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import { Addressable, BaseContract } from "ethers"; | ||
import { BaseContract } from "ethers"; | ||
import hre, { deployments } from "hardhat"; | ||
import { SamplePlugin } from "../../typechain-types"; | ||
|
||
export const getInstance = async<T extends BaseContract>(name: string, address: string): Promise<T> => { | ||
export const getInstance = async <T extends BaseContract>(name: string, address: string): Promise<T> => { | ||
// TODO: this typecasting should be refactored | ||
return (await hre.ethers.getContractAt(name, address) as unknown) as T; | ||
return (await hre.ethers.getContractAt(name, address)) as unknown as T; | ||
}; | ||
|
||
export const getSingleton = async<T extends BaseContract>(name: string): Promise<T> => { | ||
export const getSingleton = async <T extends BaseContract>(name: string): Promise<T> => { | ||
const deployment = await deployments.get(name); | ||
return getInstance<T>(name, deployment.address) | ||
return getInstance<T>(name, deployment.address); | ||
}; | ||
|
||
export const getSamplePlugin = () => getSingleton<SamplePlugin>("SamplePlugin") | ||
export const getSamplePlugin = () => getSingleton<SamplePlugin>("SamplePlugin"); |
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,64 +1,55 @@ | ||
import { AbiCoder, ParamType, isHexString, keccak256 } from "ethers" | ||
import { BasePlugin, MetaDataProvider, MetaDataProvider__factory } from "../../typechain-types" | ||
import { AbiCoder, isHexString, keccak256 } from "ethers"; | ||
import { BasePlugin, MetaDataProvider } from "../../typechain-types"; | ||
import { getInstance } from "./contracts"; | ||
|
||
interface PluginMetaData { | ||
name: string, | ||
version: string, | ||
requiresRootAccess: boolean, | ||
iconUrl: string, | ||
appUrl: string | ||
name: string; | ||
version: string; | ||
requiresRootAccess: boolean; | ||
iconUrl: string; | ||
appUrl: string; | ||
} | ||
|
||
const ProviderType_IPFS = 0n; | ||
const ProviderType_URL = 1n; | ||
// const ProviderType_IPFS = 0n; | ||
// const ProviderType_URL = 1n; | ||
const ProviderType_Contract = 2n; | ||
const ProviderType_Event = 3n; | ||
// const ProviderType_Event = 3n; | ||
|
||
const PluginMetaDataType: string[] = [ | ||
"string name", | ||
"string version", | ||
"bool requiresRootAccess", | ||
"string iconUrl", | ||
"string appUrl" | ||
] | ||
const PluginMetaDataType: string[] = ["string name", "string version", "bool requiresRootAccess", "string iconUrl", "string appUrl"]; | ||
|
||
const loadPluginMetaDataFromContract = async (provider: string, metaDataHash: string): Promise<string> => { | ||
const providerInstance = await getInstance<MetaDataProvider>("MetaDataProvider", provider) | ||
return await providerInstance.retrieveMetaData(metaDataHash) | ||
} | ||
const providerInstance = await getInstance<MetaDataProvider>("MetaDataProvider", provider); | ||
return await providerInstance.retrieveMetaData(metaDataHash); | ||
}; | ||
|
||
const loadRawMetaData = async(plugin: BasePlugin, metaDataHash: string): Promise<string> => { | ||
const [type, source] = await plugin.metaProvider() | ||
switch(type) { | ||
case ProviderType_Contract: | ||
return loadPluginMetaDataFromContract(AbiCoder.defaultAbiCoder().decode(["address"], source)[0], metaDataHash) | ||
const loadRawMetaData = async (plugin: BasePlugin, metaDataHash: string): Promise<string> => { | ||
const [type, source] = await plugin.metaProvider(); | ||
switch (type) { | ||
case ProviderType_Contract: | ||
return loadPluginMetaDataFromContract(AbiCoder.defaultAbiCoder().decode(["address"], source)[0], metaDataHash); | ||
default: | ||
throw Error("Unsupported MetaDataProviderType") | ||
throw Error("Unsupported MetaDataProviderType"); | ||
} | ||
} | ||
}; | ||
|
||
export const loadPluginMetaData = async (plugin: BasePlugin): Promise<PluginMetaData> => { | ||
const metaDataHash = await plugin.metaDataHash() | ||
const metaData = await loadRawMetaData(plugin, metaDataHash) | ||
if (metaDataHash !== keccak256(metaData)) throw Error("Invalid meta data retrieved!") | ||
return decodePluginMetaData(metaData) | ||
} | ||
const metaDataHash = await plugin.metaDataHash(); | ||
const metaData = await loadRawMetaData(plugin, metaDataHash); | ||
if (metaDataHash !== keccak256(metaData)) throw Error("Invalid meta data retrieved!"); | ||
return decodePluginMetaData(metaData); | ||
}; | ||
|
||
export const decodePluginMetaData = (data: string): PluginMetaData => { | ||
if (!isHexString(data)) throw Error("Invalid data format"); | ||
const format = data.slice(2, 6) | ||
const format = data.slice(2, 6); | ||
if (format !== "0000") throw Error("Unsupported format or format version"); | ||
const metaData = data.slice(6) | ||
const decoded = AbiCoder.defaultAbiCoder().decode( | ||
PluginMetaDataType, | ||
"0x" + metaData | ||
) | ||
const metaData = data.slice(6); | ||
const decoded = AbiCoder.defaultAbiCoder().decode(PluginMetaDataType, "0x" + metaData); | ||
return { | ||
name: decoded[0], | ||
version: decoded[1], | ||
requiresRootAccess: decoded[2], | ||
iconUrl: decoded[3], | ||
appUrl: decoded[4] | ||
} | ||
} | ||
appUrl: decoded[4], | ||
}; | ||
}; |