forked from ChainSafe/metamask-snap-polkadot
-
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.
Merge pull request ChainSafe#135 from ChainSafe/irubido/snapChanges
Snap changes
- Loading branch information
Showing
47 changed files
with
566 additions
and
615 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,31 +1,51 @@ | ||
import '@polkadot/types-augment'; | ||
import {injectExtension} from '@polkadot/extension-inject'; | ||
import {MetamaskPolkadotSnap} from "./snap"; | ||
import {SnapConfig} from "@chainsafe/metamask-polkadot-types"; | ||
import {hasMetaMask} from "./utils"; | ||
import {hasMetaMask, isMetamaskSnapsSupported, isPolkadotSnapInstalled} from "./utils"; | ||
|
||
const defaultOrigin = "https://bafybeih426v3jpdwnltjfmeefyt4isrogvgzg2wxvryu6itodvb4vzvuma.ipfs.infura-ipfs.io/"; | ||
const defaultSnapOrigin = "https://bafybeih426v3jpdwnltjfmeefyt4isrogvgzg2wxvryu6itodvb4vzvuma.ipfs.infura-ipfs.io/"; | ||
|
||
/** | ||
* | ||
* @param network | ||
* @param config | ||
* @param pluginOrigin url to package.json | ||
*/ | ||
export function injectMetamaskPolkadotSnapProvider( | ||
network: "westend"|"kusama", | ||
config?: SnapConfig, | ||
pluginOrigin?: string | ||
): void { | ||
if(!hasMetaMask()) { | ||
return; | ||
export type SnapInstallationParamNames = "version" | string; | ||
|
||
export async function enablePolkadotSnap( | ||
config: SnapConfig, | ||
snapOrigin?: string, | ||
snapInstallationParams: Record<SnapInstallationParamNames, unknown> = {} | ||
): Promise<MetamaskPolkadotSnap> { | ||
|
||
const snapId = snapOrigin ?? defaultSnapOrigin; | ||
|
||
// check all conditions | ||
if (!hasMetaMask()) { | ||
throw new Error("Metamask is not installed"); | ||
} | ||
if (!(await isMetamaskSnapsSupported())) { | ||
throw new Error("Current Metamask version doesn't support snaps"); | ||
} | ||
const polkadotSnap = new MetamaskPolkadotSnap( | ||
pluginOrigin || defaultOrigin, | ||
config || {networkName: network} | ||
); | ||
injectExtension( | ||
async () => await polkadotSnap.enableSnap(), | ||
{name: 'metamask-polkadot-snap', version: '1.0.0'} | ||
); | ||
if (!config.networkName) { | ||
throw new Error("Configuration must at least define network type"); | ||
} | ||
|
||
const isInstalled = await isPolkadotSnapInstalled(snapId); | ||
console.log("isInstalled", isInstalled); | ||
if (!isInstalled) { | ||
// // enable snap | ||
await window.ethereum.request({ | ||
method: "wallet_enable", | ||
params: [ | ||
{ | ||
[`wallet_snap_${snapId}`]: { | ||
...snapInstallationParams, | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
// create snap describer | ||
const snap = new MetamaskPolkadotSnap(snapOrigin || defaultSnapOrigin, config); | ||
// set initial configuration | ||
await (await snap.getMetamaskSnapApi()).setConfiguration(config); | ||
// return snap object | ||
return snap; | ||
} |
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
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
Oops, something went wrong.