Skip to content

Commit

Permalink
Simplifying chain integration
Browse files Browse the repository at this point in the history
  • Loading branch information
aaroncox authored and deansallinen committed Jun 17, 2024
1 parent fd502fc commit 5c02e87
Show file tree
Hide file tree
Showing 8 changed files with 2,840 additions and 2,810 deletions.
5 changes: 3 additions & 2 deletions packages/site/src/lib/rpc-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { accountName } from './account';
import { invokeSnap } from './snap';

export async function connectAccount() {
const result = await invokeSnap({ method: 'eos_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' });
const account = (await invokeSnap({ method: 'eos_getConnectedAccount' })) as string;
console.log('account', account);
accountName.set(account);
// accountPublicKey.set(account.publicKey);
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@metamask/key-tree": "^9.1.1",
"@metamask/snaps-sdk": "^4.0.0",
"@wharfkit/antelope": "^1.0.7",
"@wharfkit/common": "^1.2.0",
"@wharfkit/common": "^1.2.2",
"@wharfkit/session": "^1.3.1",
"@wharfkit/wallet-plugin-privatekey": "^1.1.0",
"buffer": "^6.0.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/template-snap-monorepo.git"
},
"source": {
"shasum": "jyxS5srKby/XVNf3aoW+WrKPwt1e6NPo9aRhJyaY+E8=",
"shasum": "8umaH6PfjEyYlBUhmvZMVC0qBkwbhIp7NFH4hSB1ERc=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
33 changes: 0 additions & 33 deletions packages/snap/src/lib/chains.ts

This file was deleted.

34 changes: 17 additions & 17 deletions packages/snap/src/lib/keyDeriver.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { getBIP44AddressKeyDeriver } from '@metamask/key-tree';
import * as Antelope from '@wharfkit/antelope';
import type { Chain } from 'src/models';
import { Bytes, KeyType, PrivateKey } from '@wharfkit/antelope';

import { Chains } from '@wharfkit/common';

// export const chain = Chains.EOS;
export const chain = Chains.Jungle4;

/**
* Get the key deriver for the given coin type.
*
* @param coinType - The SLIP-0044 registered coin type for BIP-0044.
* @returns The key deriver.
*/
async function getKeyDeriver(coinType = 194) {
async function getKeyDeriver() {
if (!chain.coinType) {
throw new Error('ChainDefinition does not contain coinType value.');
}

const networkNode = await snap.request({
method: 'snap_getBip44Entropy',
params: { coinType },
params: { coinType: chain.coinType },
});

return getBIP44AddressKeyDeriver(networkNode);
Expand All @@ -20,35 +27,28 @@ async function getKeyDeriver(coinType = 194) {
/**
* Derive an Antelope public key from the key tree at the given address index.
*
* @param chain - The chain to derive the key for.
* @param addressIndex - The index of the address to derive.
* @returns The public key.
* @throws If the key tree is not initialized.
*/
export async function derivePublicKey(chain: Chain, addressIndex = 0) {
const privateKey = await derivePrivateKey(chain, addressIndex);
return privateKey.toPublic();
export async function derivePublicKey(addressIndex = 0) {
return (await derivePrivateKey(addressIndex)).toPublic();
}

/**
* Derive an Antelope private key from the key tree at the given address index.
*
* @param chain - The chain to derive the key for.
* @param addressIndex - The index of the address to derive.
* @returns The private key.
* @throws If the key tree is not initialized.
*/
export async function derivePrivateKey(chain: Chain, addressIndex = 0) {
const { coinType } = chain;
const keyDeriver = await getKeyDeriver(coinType);
export async function derivePrivateKey(addressIndex = 0) {
const keyDeriver = await getKeyDeriver();
const derived = await keyDeriver(addressIndex);

if (!derived.privateKeyBytes) {
throw new Error('Private key not found');
}

return new Antelope.PrivateKey(
Antelope.KeyType.K1,
Antelope.Bytes.from(derived.privateKeyBytes),
);
return new PrivateKey(KeyType.K1, Bytes.from(derived.privateKeyBytes));
}
33 changes: 0 additions & 33 deletions packages/snap/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
import { Checksum256 } from '@wharfkit/antelope';

export class Account {
constructor(
public name: string,
public permission: string,
public publicKey: string,
) {}
}

export class Chain {
id: Checksum256;
url: string;
coinType: number;

constructor({
id,
url,
coinType,
}: {
id: string;
url: string;
coinType: string;
}) {
this.id = Checksum256.from(id);
this.url = url;
this.coinType = parseInt(coinType, 10);
}

static from(json: string): Chain {
console.log(json);
const parsed = JSON.parse(json);
console.log(parsed);
return new Chain({
id: parsed.id,
url: parsed.url,
coinType: parsed.coinType,
});
}
}
17 changes: 8 additions & 9 deletions packages/snap/src/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { assert } from '@metamask/snaps-sdk';
import { getCurrentChain, SupportedChain, supportedChains } from './lib/chains';

import { Session } from '@wharfkit/session';
import { WalletPluginPrivateKey } from '@wharfkit/wallet-plugin-privatekey';

import { chain, derivePrivateKey, derivePublicKey } from './lib/keyDeriver';
import { ApiClient } from './api';
import { derivePrivateKey, derivePublicKey } from './lib/keyDeriver';
import { StateManager } from './lib/manageState';
import { makeMockTransaction } from './lib/mockTransfer';
import {
Expand All @@ -12,9 +13,9 @@ import {
userConfirmedTransaction,
} from './ui';

export async function connectAccount(chainName: SupportedChain = 'Jungle4') {
const chain = supportedChains[chainName];
const publicKey = await derivePublicKey(chain);
export async function connectAccount() {
const publicKey = await derivePublicKey();
console.log(publicKey);
const api = new ApiClient(chain.url);
const account = await api.fetchAccountByKey(publicKey);
console.log(JSON.stringify(account));
Expand All @@ -26,7 +27,6 @@ export async function connectAccount(chainName: SupportedChain = 'Jungle4') {
const state = new StateManager();
await state.set({
account: JSON.stringify(account),
currentChain: JSON.stringify(chain),
});
return account.name;
} else {
Expand All @@ -38,9 +38,8 @@ export async function connectAccount(chainName: SupportedChain = 'Jungle4') {
// TODO: will need params
export async function signTransaction() {
console.log('signTransaction');
const chain = await getCurrentChain();
const api = new ApiClient(chain.url);
const publicKey = await derivePublicKey(chain);
const publicKey = await derivePublicKey();
const account = await api.fetchAccountByKey(publicKey);

assert(account, 'Account not found');
Expand All @@ -61,7 +60,7 @@ export async function signTransaction() {
const confirmed = await userConfirmedTransaction(transferObject);

if (confirmed) {
const privateKey = await derivePrivateKey(chain);
const privateKey = await derivePrivateKey();
console.log(privateKey);
assert(privateKey, 'Private key not found');
const sessionArgs = {
Expand Down
Loading

0 comments on commit 5c02e87

Please sign in to comment.