Skip to content

Commit

Permalink
forked aleo-snap initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Chen authored and Frank Chen committed Mar 12, 2024
0 parents commit a590e9b
Show file tree
Hide file tree
Showing 114 changed files with 19,080 additions and 0 deletions.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# AleoSnap - MetaMask Snap for Aleo ecosystem

## Table of Contents

1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Getting Started](#getting-started)
4. [Building and Running the Snap](#building-and-running-the-snap)
5. [Testing](#testing)
6. [Linting and Formatting](#linting-and-formatting)

<a name="introduction"></a>
## Introduction

AleoSnap is a MetaMask Snap that enables MetaMask users to interact with Aleo blockchain.

This project is a monorepo containing the MetaMask Snap package, managed by [Yarn 3 Workspaces](https://yarnpkg.com/features/workspaces). The goal of this project is to provide native interaction with the Aleo blockchain inside MetaMask using the Snaps system.

<a name="prerequisites"></a>
## Prerequisites

Before you can start working with the project, make sure you have the following software installed:

- [nvm](https://github.com/nvm-sh/nvm) (or you can use [Node.js](https://nodejs.org/) 18)
- [corepack](https://github.com/nodejs/corepack)
- will enable [yarn](https://yarnpkg.com/) 3.x

<a name="getting-started"></a>
## Getting Started

To get started with the project:

1. Clone the repository:
```shell
git clone https://github.com/ChainSafe/aleo-snap.git
```

2. Navigate to the project root:
```shell
cd aleo-snap
```

3. Set node to compatible version of project _(skip if you use manual approach)_
```shell
nvm use
```

4. Utilize corepack
```shell
corepack enable
```

5. Install the dependencies
```shell
yarn install
```

<a name="building-and-running-the-snap"></a>
## Building and Running the Snap

To build and run the project, follow these steps:

1. Build all packages:

```shell
yarn build
```

2. Run the MetaMask Snap:

```shell
yarn start
```

<a name="testing"></a>
## Testing

To run tests for all packages:

```shell
yarn test
```

<a name="linting-and-formatting"></a>
## Linting and Formatting

This project uses [ESLint](https://eslint.org/) to enforce code style and formatting. To check code quality with the linter, run:

```shell
yarn lint
```

To lint and format the code, run:

```shell
yarn lint:style:fix
```
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "root",
"private": true,
"author": "ChainSafe <[email protected]>",
"license": "(MIT)",
"packageManager": "[email protected]",
"workspaces": [
"packages/*"
],
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"build": "yarn workspaces foreach -vpt run build",
"start": "yarn workspaces foreach -vpt run start",
"lint": "yarn workspaces foreach -vpt run lint",
"lint:style:fix": "yarn workspaces foreach -vp run lint:style:fix",
"test:unit": "yarn workspaces foreach -vpt run test:unit",
"test:integrations": "yarn workspaces foreach -vpt run test:integrations"
},
"devDependencies": {
"@chainsafe/eslint-config": "^1.1.0",
"@rushstack/eslint-patch": "^1.2.0",
"@types/eslint": "^8.37.0",
"@types/node": "18.15.11",
"eslint": "8.37.0",
"typescript": "^5.0.3"
}
}
3 changes: 3 additions & 0 deletions packages/adapter/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: "../../.eslintrc.js",
}
19 changes: 19 additions & 0 deletions packages/adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog

## [1.1.1](https://github.com/ChainSafe/aleo-snap/compare/aleo-snap-adapter-v1.1.0...aleo-snap-adapter-v1.1.1) (2023-10-18)


### Bug Fixes

* adapter and examole flask flag fix, snap readme ([b1603a1](https://github.com/ChainSafe/aleo-snap/commit/b1603a1e00d5d09b1ad3bc87b09fbc1f3a7ba66e))
* adapter and example flask flag fix, snap readme ([#61](https://github.com/ChainSafe/aleo-snap/issues/61)) ([b1603a1](https://github.com/ChainSafe/aleo-snap/commit/b1603a1e00d5d09b1ad3bc87b09fbc1f3a7ba66e))

## [1.1.0](https://github.com/ChainSafe/aleo-snap/compare/aleo-snap-adapter-v1.0.5...aleo-snap-adapter-v1.1.0) (2023-07-25)


### Features

* aleo-snap-adapter ([#40](https://github.com/ChainSafe/aleo-snap/issues/40)) ([254f723](https://github.com/ChainSafe/aleo-snap/commit/254f7233e2255302d8c7ce0f1c55787ccb65f104))
* Sign, Verify in example ([#46](https://github.com/ChainSafe/aleo-snap/issues/46)) ([e70d0c4](https://github.com/ChainSafe/aleo-snap/commit/e70d0c418a5ce3fc5eefcc1d2b754a9fc7d0b1ed))
* snap web ([#31](https://github.com/ChainSafe/aleo-snap/issues/31)) ([93cbbe9](https://github.com/ChainSafe/aleo-snap/commit/93cbbe9158be4b4283cb14be82d5dc21b7b2069c))
* Transfer page ([#48](https://github.com/ChainSafe/aleo-snap/issues/48)) ([7e80273](https://github.com/ChainSafe/aleo-snap/commit/7e8027327cf106ff785ae6787390d82633f09d96))
24 changes: 24 additions & 0 deletions packages/adapter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@chainsafe/aleo-snap-adapter",
"version": "1.1.1",
"main": "dist/index.js",
"module": "dist/index.js",
"typings": "dist/index.d.ts",
"license": "MIT",
"scripts": {
"build": "tsc --build",
"lint": "eslint --color --ext .ts src/",
"lint:fix": "yarn run lint --fix",
"test:unit": "echo",
"test:integrations": "echo"
},
"devDependencies": {
"@types/eslint": "^8.37.0",
"@types/node": "18.15.11",
"eslint": "^8.37.0",
"typescript": "^5.0.3"
},
"dependencies": {
"@chainsafe/aleo-snap-shared": "workspace:^"
}
}
49 changes: 49 additions & 0 deletions packages/adapter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { isMetaMaskAvailable, isSnapInstalled } from "./utils";
import { AleoSnap } from "./snap";

export { AleoSnap } from "./snap";

const defaultSnapOrigin = "npm:@chainsafe/aleo-snap";

export { isMetaMaskAvailable, isSnapInstalled } from "./utils";

export type SnapInstallationParamNames = "version" | string;

/**
* Install and enable Aleo snap
*
* Checks for existence of Metamask and version compatibility with snaps before installation.
*
* @param snapOrigin
*
* @return AleoSnap - adapter object that exposes snap API
*/
export async function enableAleoSnap(
snapOrigin?: string,
snapInstallationParams: Record<SnapInstallationParamNames, unknown> = {}
): Promise<AleoSnap> {
const snapId = snapOrigin ?? defaultSnapOrigin;

// check for metamask
if (!(await isMetaMaskAvailable())) {
throw new Error("Metamask is not installed");
}
// check for snap
const isInstalled = await isSnapInstalled(snapId);

if (!isInstalled) {
// // enable snap
await window.ethereum.request({
method: "wallet_requestSnaps",
params: {
[snapId]: { ...snapInstallationParams },
},
});
}

// create snap describer
const snap = new AleoSnap(snapOrigin || defaultSnapOrigin);

// return snap object
return snap;
}
85 changes: 85 additions & 0 deletions packages/adapter/src/methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Balance, Methods, Records } from "@chainsafe/aleo-snap-shared";
import { AleoSnap } from "./snap";

async function sendSnapMethod<T>(
request: { method: string; params?: unknown },
snapId: string
): Promise<T> {
return await window.ethereum.request({
method: "wallet_invokeSnap",
params: {
request,
snapId,
},
});
}

export async function getAccount(this: AleoSnap): Promise<string> {
return await sendSnapMethod({ method: Methods.GetAccount }, this.snapId);
}

export async function getViewKey(this: AleoSnap): Promise<string> {
return await sendSnapMethod({ method: Methods.GetViewKey }, this.snapId);
}

export async function getPrivateKey(this: AleoSnap): Promise<string> {
return await sendSnapMethod({ method: Methods.GetPrivateKey }, this.snapId);
}

export async function decrypt(
this: AleoSnap,
cipherText: string,
viewKey?: string
): Promise<string> {
return await sendSnapMethod(
{
method: Methods.Decrypt,
params: {
viewKey,
cipherText,
},
},
this.snapId
);
}

export async function sign(this: AleoSnap, message: string): Promise<string> {
return await sendSnapMethod(
{
method: Methods.Sign,
params: {
message,
},
},
this.snapId
);
}

export async function verify(
this: AleoSnap,
message: string,
signature: string
): Promise<boolean> {
return await sendSnapMethod(
{
method: Methods.Verify,
params: {
message,
signature,
},
},
this.snapId
);
}

export async function syncRecords(this: AleoSnap): Promise<null> {
return await sendSnapMethod({ method: Methods.SyncRecords }, this.snapId);
}

export async function getBalance(this: AleoSnap): Promise<Balance> {
return await sendSnapMethod({ method: Methods.GetBalance }, this.snapId);
}

export async function getRecords(this: AleoSnap): Promise<Records> {
return await sendSnapMethod({ method: Methods.GetRecords }, this.snapId);
}
37 changes: 37 additions & 0 deletions packages/adapter/src/snap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AleoSnapApi } from "@chainsafe/aleo-snap-shared";
import {
getAccount,
getViewKey,
decrypt,
sign,
verify,
syncRecords,
getBalance,
getRecords,
getPrivateKey,
} from "./methods";

export class AleoSnap {
protected readonly snapOrigin: string;
protected readonly snapId: string;

public constructor(snapOrigin: string) {
this.snapOrigin = snapOrigin;
this.snapId = this.snapOrigin;
}

// eslint-disable-next-line @typescript-eslint/require-await
public getAleoSnapApi = async (): Promise<AleoSnapApi> => {
return {
getAccount: getAccount.bind(this),
getViewKey: getViewKey.bind(this),
getPrivateKey: getPrivateKey.bind(this),
decrypt: decrypt.bind(this),
sign: sign.bind(this),
verify: verify.bind(this),
syncRecords: syncRecords.bind(this),
getBalance: getBalance.bind(this),
getRecords: getRecords.bind(this),
};
};
}
22 changes: 22 additions & 0 deletions packages/adapter/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SnapRpcMethodRequest } from "@chainsafe/aleo-snap-shared";

export type GetSnapsResponse = {
[k: string]: {
permissionName?: string;
id?: string;
version?: string;
initialPermissions?: { [k: string]: unknown };
};
};

declare global {
interface Window {
ethereum: {
isMetaMask: boolean;
isUnlocked: Promise<boolean>;
request: <T>(
request: SnapRpcMethodRequest | { method: string; params?: any }
) => Promise<T>;
};
}
}
Loading

0 comments on commit a590e9b

Please sign in to comment.