Skip to content

Commit

Permalink
Migrating to signer only
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox committed Jun 17, 2024
1 parent b13a233 commit 3470443
Show file tree
Hide file tree
Showing 23 changed files with 329 additions and 417 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "root",
"version": "0.1.0",
"private": true,
"type": "module",
"description": "",
"homepage": "https://github.com/MetaMask/template-snap-monorepo#readme",
"bugs": {
Expand All @@ -14,6 +13,7 @@
},
"license": "(MIT-0 OR Apache-2.0)",
"author": "",
"type": "module",
"workspaces": [
"packages/*"
],
Expand Down
88 changes: 46 additions & 42 deletions packages/site/package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
{
"name": "site",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check . && eslint .",
"format": "prettier --write .",
"allow-scripts": "yarn workspace root allow-scripts",
"start": "vite dev"
},
"dependencies": {
"@metamask/providers": "^17.0.0"
},
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-browser": "^12.1.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@types/eslint": "^8.56.7",
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.36.0",
"globals": "^15.0.0",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"typescript-eslint": "^8.0.0-alpha.20",
"vite": "^5.0.3"
},
"type": "module"
"name": "site",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check . && eslint .",
"format": "prettier --write .",
"allow-scripts": "yarn workspace root allow-scripts",
"start": "vite dev"
},
"dependencies": {
"@metamask/providers": "^17.0.0",
"@wharfkit/session": "next",
"@wharfkit/transact-plugin-resource-provider": "^1.1.1",
"@wharfkit/wallet-plugin-metamask": "next",
"@wharfkit/web-renderer": "next"
},
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-browser": "^12.1.0",
"@metamask/eslint-config-jest": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@types/eslint": "^8.56.7",
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.36.0",
"globals": "^15.0.0",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"typescript-eslint": "^8.0.0-alpha.20",
"vite": "^5.0.3"
},
"type": "module"
}
13 changes: 9 additions & 4 deletions packages/site/src/components/CreateAccount.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { connectAccount } from '$lib/rpc-methods';
import { getPublicKey } from '$lib/rpc-methods';
import { onMount } from 'svelte';
let account = { name: '', publicKey: '' };
Expand All @@ -24,10 +25,14 @@
if (typeof name !== 'undefined') {
console.log(`Account ${name} created`);
connectAccount();
}
}
onMount(async () => {
const publicKey = await getPublicKey();
account.publicKey = publicKey;
});
async function createAccount(accountData: AccountData, chainUrl: string) {
const data = {
accountName: accountData.accountName,
Expand Down Expand Up @@ -66,8 +71,8 @@
<input type="text" id="account" name="account" bind:value={account.name} />
</div>
<div>
<label for="account">PublicKey:</label>
<input type="text" id="account" name="publicKey" bind:value={account.publicKey} />
<label for="account">PublicKey from Metamask:</label>
<input readonly type="text" id="account" name="publicKey" bind:value={account.publicKey} />
</div>
<button type="submit">Submit</button>
</form>
7 changes: 0 additions & 7 deletions packages/site/src/lib/account.ts

This file was deleted.

25 changes: 6 additions & 19 deletions packages/site/src/lib/rpc-methods.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { accountName } from './account';
import { Chains } from '@wharfkit/common';
import { invokeSnap } from './snap';

export async function connectAccount() {
console.log('connectAccount');
const result = (await invokeSnap({ method: 'eos_connectAccount', params: {} })) as string;
console.log('accountName', result);
accountName.set(result);
}

export async function getConnectedAccount() {
const account = (await invokeSnap({ method: 'eos_getConnectedAccount' })) as string;
console.log('account', account);
accountName.set(account);
// accountPublicKey.set(account.publicKey);
// accountPermission.set(account.permission);
}

export async function testTransaction() {
const result = await invokeSnap({ method: 'eos_signTransaction' });
console.log('result', result);
export async function getPublicKey(): Promise<string> {
return (await invokeSnap({
method: 'antelope_getPublicKey',
params: { chainId: String(Chains.Jungle4.id) }
})) as string;
}
Empty file removed packages/site/src/lib/utils.ts
Empty file.
2 changes: 2 additions & 0 deletions packages/site/src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ssr = false;
export const prerender = false;
90 changes: 73 additions & 17 deletions packages/site/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,84 @@
import { onMount } from 'svelte';
import { snapProvider, isFlask, isMetaMaskReady, snapsDetected, installedSnap } from '$store';
import type { MetaMaskInpageProvider } from '@metamask/providers';
import { setSnap, requestSnap, invokeSnap } from '$lib/snap';
import { setSnap, requestSnap } from '$lib/snap';
import flask_fox from '../assets/flask_fox.svg';
import { accountName } from '$lib/account';
import CreateAccount from '../components/CreateAccount.svelte';
import { connectAccount, getConnectedAccount, testTransaction } from '$lib/rpc-methods';
// import type {RpcMethodTypes} from '@greymass/eos-snap';
import SessionKit, { Chains, Session } from '@wharfkit/session';
import WebRenderer from '@wharfkit/web-renderer';
import { type Writable, writable } from 'svelte/store';
import { TransactPluginResourceProvider } from '@wharfkit/transact-plugin-resource-provider';
import { WalletPluginMetaMask } from '@wharfkit/wallet-plugin-metamask';
let provider: MetaMaskInpageProvider;
const session: Writable<Session | undefined> = writable();
$: isSnapInstalled = $installedSnap !== null;
const kit = new SessionKit(
{
appName: 'Metamask Snap Demo',
chains: [Chains.Jungle4],
ui: new WebRenderer(),
walletPlugins: [new WalletPluginMetaMask()]
},
{
transactPlugins: [new TransactPluginResourceProvider()]
}
);
onMount(async () => {
snapProvider.set(await getSnapsProvider());
if ($snapProvider !== null) {
provider = $snapProvider; // gotta be a better way of narrowing this type
isFlask.set(await checkIsFlask(provider));
setSnap();
getConnectedAccount();
}
const restored = await kit.restore();
if (restored) {
session.set(restored);
}
});
async function login() {
console.log('calling login');
const result = await kit.login();
session.set(result.session);
}
async function logout() {
console.log('calling logout');
session.set(undefined);
await kit.logout();
}
async function test() {
if ($session) {
const action = {
account: 'eosio.token',
name: 'transfer',
authorization: [
{
actor: $session.permissionLevel.actor,
permission: $session.permissionLevel.permission
}
],
data: {
from: $session.permissionLevel.actor,
to: 'teamgreymass',
quantity: '0.0001 EOS',
memo: 'test with metamask'
}
};
$session.transact({
actions: [action]
});
}
}
</script>

<h1>
Welcome to SvelteKit
{#if $accountName}
{$accountName}
{/if}
</h1>
<p>Snaps detected: {$snapsDetected}</p>
<p>Is flask: {$isFlask}</p>
<p>Is metamask ready: {$isMetaMaskReady}</p>
Expand All @@ -45,11 +94,18 @@

<p>The snap will need to be re-installed after any changes to the code.</p>

<button on:click={connectAccount} disabled={!$isMetaMaskReady}> Connect EOS Account </button>

<p>We disable the connection button when an account is already connected.</p>
{#if isSnapInstalled}
{#if $session}
<p>Connected account: {String($session.permissionLevel)}</p>
<button on:click={test} disabled={!$isMetaMaskReady || !$session}
>Test Signing Transaction</button
>
<button on:click={logout}>Logout</button>
{:else}
<button on:click={login} disabled={!$isMetaMaskReady}>Login</button>
{/if}

<button on:click={testTransaction} disabled={!$isMetaMaskReady}>Test Signing Transaction</button>
<hr />
<hr />

<CreateAccount />
<CreateAccount />
{/if}
46 changes: 23 additions & 23 deletions packages/site/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler",
"isolatedModules": true,
"references": [
{
"path": "../snap"
}
]
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
// except $lib which is handled by https://kit.svelte.dev/docs/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler",
"isolatedModules": true,
"references": [
{
"path": "../snap"
}
]
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
// except $lib which is handled by https://kit.svelte.dev/docs/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}
6 changes: 2 additions & 4 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@greymass/eos-snap",
"version": "0.1.0",
"name": "@greymass/test-snap",
"version": "0.0.1",
"description": "The 'Hello, world!' of MetaMask Snaps, now written in TypeScript.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -31,8 +31,6 @@
"@metamask/snaps-sdk": "^4.0.0",
"@wharfkit/antelope": "^1.0.7",
"@wharfkit/common": "^1.2.2",
"@wharfkit/session": "^1.3.1",
"@wharfkit/wallet-plugin-privatekey": "^1.1.0",
"buffer": "^6.0.3"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 3470443

Please sign in to comment.