Skip to content

Commit

Permalink
Merge pull request #14 from bide-dev/migrate-to-snaps-sdk
Browse files Browse the repository at this point in the history
Replace deprecated packages with `@metamask/snaps-sdk`
  • Loading branch information
piotr-roslaniec authored Jan 21, 2024
2 parents 5141f50 + 31ea73e commit 06923c7
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 133 deletions.
4 changes: 2 additions & 2 deletions examples/site/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "0.2.2",
"version": "0.3.0",
"description": "Aleph Zero Snap",
"proposedName": "Aleph Zero Snap",
"repository": {
"type": "git",
"url": "https://github.com/bide-dev/azero-snap"
},
"source": {
"shasum": "DlXYVMnP6QYJse0e9gjiUMLMebwQV7Z+RKjV15MHQJ4=",
"shasum": "236mLJWVNOm0vYEJEysapcLsDcLmn1sUpTKQ0cUt+mA=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azero-wallet-adapter",
"version": "0.2.0",
"version": "0.3.0",
"description": "Access Aleph Zero Wallet snap through a typed API",
"keywords": [
"aleph",
Expand Down Expand Up @@ -43,7 +43,7 @@
},
"dependencies": {
"@metamask/providers": "^14.0.2",
"azero-wallet-types": "^0.2.0"
"azero-wallet-types": "^0.3.0"
},
"devDependencies": {
"replace-in-file": "^6.3.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { sendSnapMethod } from './metamask';
export const getAccount = async (): Promise<Result<GetAccountResult>> =>
sendSnapMethod({
method: 'getAccount',
params: undefined,
params: [],
});

/**
Expand Down
11 changes: 5 additions & 6 deletions packages/adapter/src/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ const walletRequest = async (requestArgs: RequestArguments): Promise<any> => {
* @returns The result of the request.
* @throws If fails to send the request.
*/
export async function sendSnapMethod<TData>(
export const sendSnapMethod = async <TData>(
request: AlephRPCRequest,
): Promise<Result<TData>> {
return walletRequest({
): Promise<Result<TData>> =>
walletRequest({
method: 'wallet_invokeSnap',
params: {
snapId: getSnapId(),
request,
},
});
}
}).then(JSON.parse);

/**
* Connect to snap. Attempts to install the snap if needed.
Expand All @@ -37,7 +36,7 @@ export async function sendSnapMethod<TData>(
*/
export const connect = async (
snapId: string = getSnapId(),
params: Record<'version' | string, unknown> = {},
params?: Record<'version' | string, unknown>,
): Promise<void> => {
await walletRequest({
method: 'wallet_requestSnaps',
Expand Down
12 changes: 6 additions & 6 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azero-wallet",
"version": "0.2.2",
"version": "0.3.0",
"description": "Aleph Zero Wallet snap",
"keywords": [
"aleph",
Expand All @@ -24,9 +24,10 @@
"README.md"
],
"scripts": {
"build": "pnpm clean && pnpm build:snap && pnpm build:fix-bundle",
"build": "pnpm clean && pnpm build:tsc && pnpm build:snap && pnpm build:fix-bundle",
"build:fix-bundle": "node scripts/fix-bundle.mjs && mm-snap manifest --fix && mm-snap eval --verboseErrors --bundle dist/snap.js && cp snap.manifest.json ../../examples/site/",
"build:snap": "rollup -c",
"build:tsc": "tsc -p tsconfig.build.json",
"clean": "rimraf dist",
"lint": "run-p lint:exports lint:eslint",
"lint:eslint": "eslint .",
Expand All @@ -41,15 +42,14 @@
"@metamask/approval-controller": "^5.0.0",
"@metamask/browser-passworder": "^4.3.0",
"@metamask/key-tree": "^9.0.0",
"@metamask/snaps-types": "^3.1.0",
"@metamask/snaps-ui": "^3.1.0",
"@metamask/snaps-sdk": "^1.4.0",
"@polkadot/api": "^9.9.4",
"@polkadot/keyring": "^10.1.14",
"@polkadot/types": "^10.9.1",
"@polkadot/util": "^10.1.14",
"@polkadot/util-crypto": "^10.1.14",
"@polkadot/wasm-crypto": "^6.3.1",
"azero-wallet-types": "^0.2.0",
"azero-wallet-types": "^0.3.0",
"eth-rpc-errors": "^4.0.3",
"sha3": "^2.1.4"
},
Expand All @@ -63,6 +63,7 @@
"@metamask/providers": "^14.0.2",
"@metamask/snaps-cli": "^3.0.5",
"@metamask/snaps-rollup-plugin": "^3.0.1",
"@metamask/utils": "^8.3.0",
"@rollup/plugin-commonjs": "^25.0.5",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-sucrase": "^5.0.2",
Expand All @@ -79,7 +80,6 @@
"metamask-testing-tools": "^1.1.10",
"nodemon": "^2.0.16",
"rollup": "^4.0.2",
"rollup-plugin-terser": "^7.0.2",
"standard-version": "^9.5.0",
"ts-node": "^10.9.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "0.2.2",
"version": "0.3.0",
"description": "Aleph Zero Snap",
"proposedName": "Aleph Zero Snap",
"repository": {
"type": "git",
"url": "https://github.com/bide-dev/azero-snap"
},
"source": {
"shasum": "DlXYVMnP6QYJse0e9gjiUMLMebwQV7Z+RKjV15MHQJ4=",
"shasum": "236mLJWVNOm0vYEJEysapcLsDcLmn1sUpTKQ0cUt+mA=",
"location": {
"npm": {
"filePath": "dist/snap.js",
Expand Down
14 changes: 9 additions & 5 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { OnRpcRequestHandler } from '@metamask/snaps-types';
import type { Json, OnRpcRequestHandler } from '@metamask/snaps-sdk';
import type { JsonRpcRequest } from '@metamask/utils';
import { initWasm } from '@polkadot/wasm-crypto/initOnlyAsm';
import type { RequestMethod, RequestParameters } from 'azero-wallet-types';
import { ResultObject } from 'azero-wallet-types';
Expand All @@ -15,9 +16,12 @@ initWasm().catch(console.error);
export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
request,
}): Promise<ResultObject> => {
}: {
origin: string;
request: JsonRpcRequest;
}): Promise<Json> => {
try {
console.log({ origin, request });
console.log({ origin, request: JSON.stringify(request) });
const { method, params } = request;

console.log('Initiating StorageService');
Expand All @@ -32,8 +36,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
method as RequestMethod,
params as RequestParameters,
);
).then(JSON.stringify);
} catch (error) {
return ResultObject.error((error as Error).toString());
return JSON.stringify(ResultObject.error((error as Error).toString()));
}
};
4 changes: 2 additions & 2 deletions packages/snap/src/metamask/ui.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Panel } from '@metamask/snaps-ui';
import { divider, heading, panel, text } from '@metamask/snaps-ui';
import type { Panel } from '@metamask/snaps-sdk';
import { divider, heading, panel, text } from '@metamask/snaps-sdk';
import type { SignerPayloadJSON } from '@polkadot/types/types';

export const prettyJson = (record: Record<string, unknown>): string =>
Expand Down
13 changes: 6 additions & 7 deletions packages/snap/test/e2e/getAccount.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { SnapsGlobalObject } from '@metamask/snaps-types';
import type { Result } from 'azero-wallet-types';
import { isError } from 'azero-wallet-types';

import { onRpcRequest } from '../../src';
Expand All @@ -21,20 +20,20 @@ describe('getAccount', () => {
});

it('should return an account', async () => {
const res = (await onRpcRequest({
const result = await onRpcRequest({
origin: 'localhost',
request: {
id: 'test-id',
jsonrpc: '2.0',
method: 'getAccount',
params: {},
},
})) as Result<unknown>;
}).then(JSON.parse);

if (isError(res)) {
throw new Error(res.error);
if (isError(result)) {
throw new Error(result.error);
}
expect(res.success).toBe(true);
expect(res.data).toBeDefined();
expect(result.success).toBe(true);
expect(result.data).toBeDefined();
});
});
6 changes: 3 additions & 3 deletions packages/snap/test/e2e/setRpcUrl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SnapsGlobalObject } from '@metamask/snaps-types';
import type { Result, SetRpcUrlRequestParams } from 'azero-wallet-types';
import type { SetRpcUrlRequestParams } from 'azero-wallet-types';
import { isError } from 'azero-wallet-types';

import { onRpcRequest } from '../../src';
Expand Down Expand Up @@ -31,15 +31,15 @@ describe('setRpcUrl', () => {
const requestParams: SetRpcUrlRequestParams = {
rpcUrl: 'wss://rpc.polkadot.io',
};
const res = (await onRpcRequest({
const res = await onRpcRequest({
origin,
request: {
id: 'test-id',
jsonrpc: '2.0',
method: 'setRpcUrl',
params: requestParams,
},
})) as Result<unknown>;
}).then(JSON.parse);

expect(polkadotInitSpy).toHaveBeenCalledTimes(2); // Once for snap init, and once for setRpcUrl request
expect(setRpcUrlSpy).toHaveBeenCalled();
Expand Down
9 changes: 3 additions & 6 deletions packages/snap/test/e2e/signAndSendTransactionPayload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { SnapsGlobalObject } from '@metamask/snaps-types';
import type {
Result,
SignAndSendTransactionRequestParams,
} from 'azero-wallet-types';
import type { SignAndSendTransactionRequestParams } from 'azero-wallet-types';
import { isError } from 'azero-wallet-types';

import { onRpcRequest } from '../../src';
Expand Down Expand Up @@ -40,15 +37,15 @@ describe('signAndSendTransaction', () => {
const requestParams: SignAndSendTransactionRequestParams = {
payload: fakeTransactionPayload,
};
const res = (await onRpcRequest({
const res = await onRpcRequest({
origin: 'localhost',
request: {
id: 'test-id',
jsonrpc: '2.0',
method: 'signAndSendTransaction',
params: requestParams,
},
})) as Result<unknown>;
}).then(JSON.parse);

expect(polkadotInitSpy).toHaveBeenCalled();
expect(polkadotSignPayload).toHaveBeenCalled();
Expand Down
5 changes: 2 additions & 3 deletions packages/snap/test/e2e/signSignerPayloadJSON.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Json, SnapsGlobalObject } from '@metamask/snaps-types';
import type { Result } from 'azero-wallet-types';
import { isError } from 'azero-wallet-types';

import { onRpcRequest } from '../../src';
Expand Down Expand Up @@ -35,15 +34,15 @@ describe('signSignerPayload', () => {
.spyOn(PolkadotService, 'sendTransactionWithSignature')
.mockImplementation(async () => Promise.resolve(fakeTransactionInfo));

const res = (await onRpcRequest({
const res = await onRpcRequest({
origin: 'localhost',
request: {
id: 'test-id',
jsonrpc: '2.0',
method: 'signAndSendTransaction',
params: fakeTransactionPayload as unknown as Json,
},
})) as Result<unknown>;
}).then(JSON.parse);

expect(polkadotInitSpy).toHaveBeenCalled();
expect(polkadotSignPayload).toHaveBeenCalled();
Expand Down
5 changes: 2 additions & 3 deletions packages/snap/test/e2e/transferNativeAsset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { JsonRpcRequest, SnapsGlobalObject } from '@metamask/snaps-types';
import {
isError,
type Result,
type TransferNativeAssetRequestParams,
} from 'azero-wallet-types';

Expand Down Expand Up @@ -37,15 +36,15 @@ describe('transferNativeAsset', () => {
recipient: '5FjvBzjJq6x2',
amount: '0x0000000',
};
const res = (await onRpcRequest({
const res = await onRpcRequest({
origin: 'localhost',
request: {
id: 'test-id',
jsonrpc: '2.0',
method: 'transferNativeAsset',
params: requestParams,
} as JsonRpcRequest<TransferNativeAssetRequestParams>,
})) as Result<unknown>;
}).then(JSON.parse);

expect(polkadotInitSpy).toHaveBeenCalled();
expect(makeTransferTxSpy).toHaveBeenCalled();
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azero-wallet-types",
"version": "0.2.0",
"version": "0.3.0",
"description": "Types for azero-wallet-snap",
"keywords": [
"aleph",
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {

export type GetAccountRequest = {
method: 'getAccount';
params: undefined;
params: [];
};

export type SignSignerPayloadRequest = {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const isSuccess = <TData>(

export class ResultObject {
static success<TData>(data: TData): Result<TData> {
console.log('data: ', data);
return { success: true, data };
}

Expand Down
Loading

0 comments on commit 06923c7

Please sign in to comment.