Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Lykhoyda committed Nov 4, 2024
1 parent b48c504 commit fc90d86
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 201 deletions.
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ dist
.idea
.vscode

# Wasm package
packages/wasm/lib

# Demo Wallet package
packages/demo-wallet/dist/*
!packages/demo-wallet/dist/_headers
!/packages/demo-wallet/dist/_headers

# e2e Tests package
packages/e2e-tests/dist/*
!packages/e2e-tests/dist/serve.json
!/packages/e2e-tests/dist/serve.json

3 changes: 3 additions & 0 deletions packages/demo-wallet/dist/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
14 changes: 7 additions & 7 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@
"@metamask/eslint-config-jest": "^14.0.0",
"@metamask/eslint-config-nodejs": "^14.0.0",
"@metamask/eslint-config-typescript": "^14.0.0",
"@metamask/snaps-cli": "^6.5.0",
"@metamask/snaps-cli": "^6.5.2",
"@metamask/snaps-jest": "^8.6.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^5.42.1",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "~2.26.0",
"eslint": "^9.14.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "~2.31.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jsdoc": "^41.1.2",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.5.0",
"prettier": "^2.7.1",
"prettier-plugin-packagejson": "^2.2.11",
"prettier": "^3.3.3",
"prettier-plugin-packagejson": "^2.5.3",
"rimraf": "^3.0.2",
"ts-jest": "^29.1.0",
"typescript": "^4.7.4"
Expand Down
5 changes: 1 addition & 4 deletions 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": "tXFgovVFCyp5iM5sZS1YkRj/XpZlw/9yu2Gbjk9EYq4=",
"shasum": "TlMTqyWhemCoj+B1qchLHNGm+nQgvjnS7ZL/MaoE7Kg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand All @@ -17,9 +17,6 @@
}
},
"initialPermissions": {
"endowment:transaction-insight": {
"maxRequestTime": 180000
},
"snap_dialog": {},
"endowment:webassembly": {},
"endowment:rpc": {
Expand Down
19 changes: 7 additions & 12 deletions packages/snap/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getViewingKey } from './rpc/getViewingKey';
import { InitOutput, initSync } from '@webzjs/webz-keys';
// This import will be the wasm as a base64 encoded string because we are using the
// asset/inline bundler option in the snap.config.ts
import wasmDataBase64 from '@webzjs/webz-keys/webz_keys_bg.wasm';
import { InitOutput } from '@webzjs/webz-keys';
import { initialiseWasm } from './utils/initialiseWasm';

let wasm: InitOutput;

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
Expand All @@ -18,7 +18,9 @@ export const onRpcRequest: ({
}: {
request: any;
}) => Promise<string> = async ({ request }) => {
initializeWasm();
if (!wasm) {
wasm = initialiseWasm();
}

switch (request.method) {
case 'getViewingKey':
Expand All @@ -27,10 +29,3 @@ export const onRpcRequest: ({
throw new Error('Method not found.');
}
};

function initializeWasm() {
// remove the data url prefix
const base64 = (wasmDataBase64 as any as string).split(',')[1] || '';
const wasmData = Buffer.from(base64, 'base64');
initSync(wasmData);
}
11 changes: 5 additions & 6 deletions packages/snap/src/rpc/getViewingKey.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { getSeed } from '../utils/getSeed';
import { UnifiedSpendingKey } from '@webzjs/webz-keys';
import { getSeed } from '../utils/getSeed';

type Network = 'main' | 'test';

export async function getViewingKey() {
export async function getViewingKey(
network: Network = 'main',
accountIndex: number = 0,
) {
try {
// Retrieve the BIP-44 entropy from MetaMask
const seed = await getSeed();

// Define the account index and network
const accountIndex = 0;
const network = 'main' as Network;

// Generate the UnifiedSpendingKey and obtain the Viewing Key
let spendingKey = new UnifiedSpendingKey(network, seed, accountIndex);
let viewingKey = spendingKey.to_unified_full_viewing_key();
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/utils/getSeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hexStringToUint8Array } from './hexStringToUint8Array';
export async function getSeed(): Promise<Uint8Array> {
const entropyNode = await snap.request({
method: 'snap_getBip44Entropy',
params: { coinType: 133 }, // 133 is the coin type for Zcash
params: { coinType: 133 }, // 133 is the coin type for Zcash https://github.com/satoshilabs/slips/blob/master/slip-0044.md
});

if (
Expand Down
17 changes: 17 additions & 0 deletions packages/snap/src/utils/initialiseWasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { InitOutput, initSync } from '@webzjs/webz-keys';
import wasmDataBase64 from '@webzjs/webz-keys/webz_keys_bg.wasm';

export function initialiseWasm(): InitOutput {
const base64String = wasmDataBase64 as any as string;
// Check if the imported data is a data URL
const base64Formatted = base64String.startsWith('data:')
? base64String.split(',')[1]
: base64String;

if (!base64Formatted) {
throw new Error('Invalid WASM data');
}

const wasmData = Buffer.from(base64Formatted, 'base64');
return initSync({ module: wasmData });
}
Loading

0 comments on commit fc90d86

Please sign in to comment.