Skip to content

Commit

Permalink
Merge pull request #8 from blockchain-lab-um/dev
Browse files Browse the repository at this point in the history
SSI Snap v1.1.4
  • Loading branch information
andyv09 authored Aug 9, 2022
2 parents 4cbe3fb + b093472 commit 2333990
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.vscode/settings.json

tsconfig.tsbuildinfo
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,36 @@ The SSI Snap is a MetaMask Snap that enhances MetaMask with functionality to man

## [Documentation](https://blockchain-lab-um.github.io/ssi-snap-docs/)

### Testing SSI Snap

#### Metamask

First, build and install beta release of Metamask extension:

- checkout to [this branch](https://github.com/MetaMask/metamask-extension/tree/snaps-stable-nov-21) (chrome recommended)
- build metamask using `yarn start --build-type flask` or `yarn dist`
- go to [chrome://extensions/](chrome://extensions/)
- enable "Developer mode"
- click "Load unpacked" and point to chrome directory

##### Live demo

## [Demo](https://blockchain-lab-um.github.io/course-dapp/)

#### Demo Snapshot

##### Running SSI Snap locally

Build and test locally

- `yarn install`
- `yarn start`

Demo should open on localhost:8081

## License

This project is dual-licensed under Apache 2.0 and MIT terms:

- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
13 changes: 7 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@ <h1>Hello, Snaps!</h1>
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'saveVC',
params: [ {
"vc": {
params: {
"verifiableCredential": {
"credentialSchema": {
"id": "https://beta.api.schemas.serto.id/v1/public/program-completion-certificate/1.0/json-schema.json",
"type": "JsonSchemaValidator2018"
}
},
},
"credentialSubject": {
"accomplishmentType": "Developer Certificate",
"learnerName": "a",
Expand All @@ -117,12 +116,12 @@ <h1>Hello, Snaps!</h1>
"https://www.w3.org/2018/credentials/v1",
"https://beta.api.schemas.serto.id/v1/public/program-completion-certificate/1.0/ld-context.json"
],
"issuanceDate": "2022-05-13T12:08:10.000Z",
"issuanceDate": "2022-06-13T12:08:10.000Z",
"proof": {
"type": "JwtProof2020",
"jwt": "eyJhbGciOiJFUzI1NksiLCJ0eXAiOiJKV1QifQ.eyJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSIsImh0dHBzOi8vYmV0YS5hcGkuc2NoZW1hcy5zZXJ0by5pZC92MS9wdWJsaWMvcHJvZ3JhbS1jb21wbGV0aW9uLWNlcnRpZmljYXRlLzEuMC9sZC1jb250ZXh0Lmpzb24iXSwidHlwZSI6WyJWZXJpZmlhYmxlQ3JlZGVudGlhbCIsIlByb2dyYW1Db21wbGV0aW9uQ2VydGlmaWNhdGUiXSwiY3JlZGVudGlhbFN1YmplY3QiOnsiYWNjb21wbGlzaG1lbnRUeXBlIjoiRGV2ZWxvcGVyIENlcnRpZmljYXRlIiwibmFtZSI6ImEiLCJhY2hpZXZlbWVudCI6IkNlcnRpZmllZCBTb2xpZGl0eSBEZXZlbG9wZXIgMiIsImNvdXJzZVByb3ZpZGVyIjoiVU0gRkVSSSJ9LCJjcmVkZW50aWFsU2NoZW1hIjp7ImlkIjoiaHR0cHM6Ly9iZXRhLmFwaS5zY2hlbWFzLnNlcnRvLmlkL3YxL3B1YmxpYy9wcm9ncmFtLWNvbXBsZXRpb24tY2VydGlmaWNhdGUvMS4wL2pzb24tc2NoZW1hLmpzb24iLCJ0eXBlIjoiSnNvblNjaGVtYVZhbGlkYXRvcjIwMTgifX0sInN1YiI6ImRpZDpldGhyOnJpbmtlYnk6MHg2QTI0Njg3NjIxY0REMUM3N0JiNmFDYkJFRTkxMGQwQzUxN2VCNDQzIiwibmJmIjoxNjUyNDQzNjkwLCJpc3MiOiJkaWQ6ZXRocjpyaW5rZWJ5OjB4MDI0MWFiZDY2MmRhMDZkMGFmMmYwMTUyYTgwYmMwMzdmNjVhN2Y5MDExNjBjZmUxZWIzNWVmM2YwYzUzMmEyYTRkIn0.Z4q7kn4vKdFI5QfAyQmqtWa0icAv91HqxSEwn-AMr4_bY3vfD_WeD3W9hgqf9tsUJPx2ru5gY3tLpAx04nk0RQ"
}
}]
}}
}]
})
} catch (err) {
Expand All @@ -136,6 +135,8 @@ <h1>Hello, Snaps!</h1>
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'getVCs',
//params: {querry: {}}
params: {querry: {issuer: {id: 'did:ethr:rinkeby:0x0241abd662da06d0af2f0152a80bc037f65a7f901160cfe1eb35ef3f0c532a2a4d'}, issuanceDate: "2022-05-13T12:08:10.000Z"}}
}]
})
console.log(response)
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blockchain-lab-um/ssi-snap",
"version": "1.1.3",
"version": "1.1.4",
"description": "Snap for managing VCs and VPs in MetaMask",
"keywords": [
"MetaMask",
Expand All @@ -18,7 +18,7 @@
"type": "git",
"url": "https://github.com/blockchain-lab-um/ssi-snap.git"
},
"license": "ISC",
"license": "(Apache-2.0 AND MIT)",
"author": {
"name": "Blockchain Lab:UM",
"email": "blockchain-lab@um.si",
Expand Down Expand Up @@ -49,7 +49,8 @@
},
"dependencies": {
"@babel/runtime": "^7.17.2",
"@blockchain-lab-um/veramo-vc-manager": "1.0.3",
"@blockchain-lab-um/ssi-snap-types": "^1.0.2",
"@blockchain-lab-um/veramo-vc-manager": "^1.0.5",
"@veramo/core": "^3.1.6-next.146",
"@veramo/credential-eip712": "^3.1.6-next.146",
"@veramo/credential-w3c": "3.1.6-next.146",
Expand Down
4 changes: 2 additions & 2 deletions snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"version": "1.1.3",
"version": "1.1.4",
"description": "Snap for managing VCs and VPs in MetaMask",
"proposedName": "ssi-snap",
"repository": {
"type": "git",
"url": "https://github.com/blockchain-lab-um/ssi-snap.git"
},
"source": {
"shasum": "uv8cEU8WbYFQHHgi3ptwiRJ8FW3GCP9Rbwz3srsJGxw=",
"shasum": "npRKAbqvGCtzSKiHX/a0Ge0tFAzS8LlRaJ8pYvuwAgs=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
39 changes: 18 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { VerifiableCredential } from "@veramo/core";
import { OnRpcRequestHandler } from "@metamask/snap-types";
import { togglePopups, changeInfuraToken } from "./rpc/configure";
import { getVCs } from "./rpc/getVCs";
import { getVP } from "./rpc/getVP";
import { saveVC } from "./rpc/saveVC";

let vc_id: string;
let vc: VerifiableCredential;
let challenge: string;
let domain: string;
let infuraToken: string;
let querry: any;
import {
isValidChangeInfuraTokenRequest,
isValidGetVCsRequest,
isValidGetVPRequest,
isValidSaveVCRequest,
} from "./utils/params";

export const onRpcRequest: OnRpcRequestHandler = async ({
origin,
Expand All @@ -24,22 +22,21 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
console.log("Hello World!!!");
return { data: "vcr" };
case "getVCs":
console.log("querry before");
if (request.params) {
querry = (request as any).params[0];
}
return await getVCs(querry);
isValidGetVCsRequest(request.params);
return await getVCs(request.params.querry);
case "saveVC":
vc = (request as any).params[0];
return await saveVC(vc);
isValidSaveVCRequest(request.params);
return await saveVC(request.params.verifiableCredential);
case "getVP":
vc_id = (request as any).params[0];
domain = (request as any).params[1];
challenge = (request as any).params[2];
return await getVP(vc_id, domain, challenge);
isValidGetVPRequest(request.params);
return await getVP(
request.params.vc_id,
request.params.domain,
request.params.challenge
);
case "changeInfuraToken":
infuraToken = (request as any).params[0];
return await changeInfuraToken(infuraToken);
isValidChangeInfuraTokenRequest(request.params);
return await changeInfuraToken(request.params.infuraToken);
case "togglePopups":
return await togglePopups();
default:
Expand Down
7 changes: 1 addition & 6 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface SSISnapConfig {
*/
infuraToken: string;
/**
*
* dApp settings
*/
};
dApp: {
Expand Down Expand Up @@ -86,8 +86,3 @@ export interface SSIAccountState {
*/
vcs: Record<string, VerifiableCredential>;
}

export interface Response {
error?: string;
data?: any;
}
16 changes: 7 additions & 9 deletions src/rpc/configure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/restrict-plus-operands */
import { Response } from "../interfaces";
import {
_changeInfuraToken,
_togglePopups,
Expand All @@ -9,7 +8,7 @@ import {
} from "../utils/snap_utils";
import { getConfig } from "../utils/state_utils";

export async function togglePopups(): Promise<Response> {
export async function togglePopups(): Promise<boolean> {
const config = await getConfig();

const result =
Expand All @@ -31,13 +30,12 @@ export async function togglePopups(): Promise<Response> {
}));
if (result) {
await _togglePopups();
return { data: true };
} else {
return { data: false, error: "Request declined" };
return true;
}
return false;
}

export async function changeInfuraToken(token?: string): Promise<Response> {
export async function changeInfuraToken(token?: string): Promise<boolean> {
if (token != null && token != "") {
const config = await getConfig();
const result = await wallet.request({
Expand All @@ -58,11 +56,11 @@ export async function changeInfuraToken(token?: string): Promise<Response> {
});
if (result) {
await _changeInfuraToken(token);
return { data: true };
return true;
} else {
return { data: false, error: "Request declined" };
return false;
}
} else {
return { error: "Missing parameter: infuraToken" };
return false;
}
}
11 changes: 7 additions & 4 deletions src/rpc/getVCs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { list_vcs } from "../utils/veramo_utils";
import { Response } from "../interfaces";
import { getConfig } from "../utils/state_utils";
import { VerifiableCredential } from "@veramo/core";
import { VCQuerry } from "@blockchain-lab-um/ssi-snap-types";

export async function getVCs(querry?: any): Promise<Response> {
export async function getVCs(
querry?: VCQuerry
): Promise<VerifiableCredential[]> {
console.log("querry", querry);
const vcs = await list_vcs(querry);
const config = await getConfig();
Expand All @@ -23,8 +26,8 @@ export async function getVCs(querry?: any): Promise<Response> {
],
}));
if (result) {
return { data: { vcs: vcs } };
return vcs;
} else {
return { error: "User rejected" };
return [];
}
}
16 changes: 8 additions & 8 deletions src/rpc/getVP.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { create_vp } from "../utils/veramo_utils";
import { VerifiablePresentation } from "@veramo/core";

export async function getVP(vc_id: string, domain: string, challenge: string) {
if (vc_id) {
const vp = await create_vp(vc_id, challenge, domain);
return { data: vp };
} else {
console.log("Missing parameters: vc_id");
return { error: "Missing parameter: vc_id" };
}
export async function getVP(
vc_id: string,
domain?: string,
challenge?: string
): Promise<VerifiablePresentation | null> {
const vp = await create_vp(vc_id, challenge, domain);
return vp;
}
42 changes: 42 additions & 0 deletions src/utils/params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { VCQuerry } from "@blockchain-lab-um/ssi-snap-types";
import { VerifiableCredential } from "@veramo/core";

export function isValidGetVCsRequest(
params: unknown
): asserts params is { querry?: VCQuerry } {
if (!(params != null && typeof params == "object" && "querry" in params)) {
throw new Error("Invalid GetVCs request");
}
}

export function isValidSaveVCRequest(
params: unknown
): asserts params is { verifiableCredential: VerifiableCredential } {
if (
!(
params != null &&
typeof params == "object" &&
"verifiableCredential" in params
)
) {
throw new Error("Invalid SaveVC request");
}
}

export function isValidGetVPRequest(
params: unknown
): asserts params is { vc_id: string; domain?: string; challenge?: string } {
if (!(params != null && typeof params == "object" && "vc_id" in params)) {
throw new Error("Invalid GetVP request");
}
}

export function isValidChangeInfuraTokenRequest(
params: unknown
): asserts params is { infuraToken: string } {
if (
!(params != null && typeof params == "object" && "infuraToken" in params)
) {
throw new Error("Invalid ChangeInfuraToken request");
}
}
11 changes: 8 additions & 3 deletions src/utils/veramo_utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VCQuerry } from "@blockchain-lab-um/ssi-snap-types";
import {
IIdentifier,
MinimalImportableKey,
Expand Down Expand Up @@ -38,16 +39,20 @@ export async function save_vc(vc: VerifiableCredential) {
* Get a list of VCs of the curently selected MetaMask account.
* @returns {Promise<VerifiableCredential[]>} Array of saved VCs.
*/
export async function list_vcs(querry?: any): Promise<VerifiableCredential[]> {
export async function list_vcs(
querry?: VCQuerry
): Promise<VerifiableCredential[]> {
const agent = await getAgent();
const vcs = await agent.listVCS(querry);
const vcs = await agent.listVCS({ querry: querry });
console.log("VCS", vcs);
return vcs.vcs as VerifiableCredential[];
}

/**
* Create a VP from a specific VC (if it exists), that is stored in MetaMask state under the currently selected MetaMask account.
* @param {number} vc_id - index of the VC
* @param {string} vc_id - index of the VC
* @param {string} domain - domain of the VP
* @param {string} challenge - challenge of the VP
* @returns {Promise<VerifiablePresentation | null>} - generated VP
* */
export async function create_vp(
Expand Down
3 changes: 2 additions & 1 deletion src/veramo/plugins/snapDataStore/snapDataStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VCQuerry } from "@blockchain-lab-um/ssi-snap-types";
import { IKey, RequireOnly, IIdentifier } from "@veramo/core";
import { AbstractKeyStore } from "@veramo/key-manager";
import {
Expand Down Expand Up @@ -226,7 +227,7 @@ export class SnapVCStore extends AbstractVCStore {
return true;
}

async list(args: { querry?: any }): Promise<VerifiableCredential[]> {
async list(): Promise<VerifiableCredential[]> {
const ssiAccountState = await getVCAccount();

const result: VerifiableCredential[] = [];
Expand Down
1 change: 0 additions & 1 deletion tsconfig.tsbuildinfo

This file was deleted.

Loading

0 comments on commit 2333990

Please sign in to comment.