Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andyv09 committed Aug 5, 2022
2 parents 640dd61 + 0eab034 commit 0e65b08
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 190 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Publish

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16.15.1
- run: yarn install
- run: yarn build
publish:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16.15.1
registry-url: https://registry.npmjs.org/
- run: yarn install
- run: yarn build
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
39 changes: 39 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ <h1>Hello, Snaps!</h1>
<button class="saveVC">Save VC</button>
<button class="getVCs">Get VCS</button>
<button class="getVCAddr">Get VC Addr</button>
<button class="togglePopups">TogglePopups</button>
<button class="changeInfuraToken">ChangeInfuraToken</button>
</body>

<script>
Expand All @@ -44,6 +46,8 @@ <h1>Hello, Snaps!</h1>
const getAddrBtn = document.querySelector('button.getVCAddr')
const initializeBtn = document.querySelector('button.init')
const isInitializedBtn = document.querySelector('button.isInit')
const popups = document.querySelector('button.togglePopups')
const infura = document.querySelector('button.changeInfuraToken')


connectButton.addEventListener('click', connect)
Expand All @@ -53,6 +57,8 @@ <h1>Hello, Snaps!</h1>
getVCBtn.addEventListener('click', getVCs)
initializeBtn.addEventListener('click', getVp)
isInitializedBtn.addEventListener('click', isInit)
popups.addEventListener('click', togglePopups)
infura.addEventListener('click', changeInfuraToken)

// here we get permissions to interact with and install the snap
async function connect () {
Expand Down Expand Up @@ -184,5 +190,38 @@ <h1>Hello, Snaps!</h1>
alert('Problem happened: ' + err.message || err)
}
}

async function changeInfuraToken() {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'changeInfuraToken',
params: ["newToken"]
}]
})
console.log(response)
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}

async function togglePopups() {
try {
const response = await ethereum.request({
method: 'wallet_invokeSnap',
params: [snapId, {
method: 'togglePopups',
params: ["newToken"]
}]
})
console.log(response)
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}

</script>
</html>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
},
"devDependencies": {
"@metamask/snaps-cli": "^0.18.1",
"@metamask/providers": "^9.0.0",
"@types/node": "^17.0.18",
"rimraf": "^3.0.2",
"ts-node": "^10.5.0",
Expand Down
2 changes: 1 addition & 1 deletion snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/blockchain-lab-um/ssi-snap.git"
},
"source": {
"shasum": "KeyOvYHaNPZj76J1WrMKvCNdiB+/sJ0DlMDbPNkxSEU=",
"shasum": "EBoU/jgioFgxClhncL9EE8q3PJB2rqV23ylYTYxgT7o=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
99 changes: 10 additions & 89 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Wallet } from "./interfaces";
import { VerifiableCredential } from "@veramo/core";
import { get_id, list_vcs, save_vc, create_vp } from "./utils/veramo_utils";
import { changeInfuraToken } from "./utils/snap_utils";
import { getConfig } from "./utils/state_utils";
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";

declare let wallet: Wallet;
let vc_id: string;
let vc: VerifiableCredential;
let challenge: string;
Expand All @@ -29,98 +28,20 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
if (request.params) {
querry = (request as any).params[0];
}
console.log("querry", querry);
let vcs = await list_vcs(querry);
let num = vcs.length;

//TODO display specific VCs
const result = await wallet.request({
method: "snap_confirm",
params: [
{
prompt: `Send VCs`,
description: "Are you sure you want to send VCs to the dApp?",
textAreaContent:
"Some dApps are less secure than others and could save data from VCs against your will. Be careful where you send your private VCs! Number of VCs submitted is " +
num,
},
],
});
if (result) {
return { data: vcs };
} else {
return { error: "User rejected" };
}
case "getDIDAddress":
let did = await get_id();
if (did != null) {
return { data: did.did.split(":")[3] };
}
return { data: false, error: "Failed to fetch addres" };
return await getVCs(querry);
case "saveVC":
vc = (request as any).params[0];
if (vc) {
const result = await wallet.request({
method: "snap_confirm",
params: [
{
prompt: `Save VC`,
description: "Would you like to save the following VC?",
textAreaContent: JSON.stringify(vc.credentialSubject),
},
],
});
if (result) {
await save_vc(vc);
return { data: true };
} else {
return { data: false, error: "Request declined" };
}
} else {
console.log("Missing parameters: vc");
return { error: "Missing parameter: vc" };
}
return await saveVC(vc);
case "getVP":
vc_id = (request as any).params[0];
domain = (request as any).params[1];
challenge = (request as any).params[2];
console.log(vc_id, domain, challenge);
if (vc_id) {
let vp = await create_vp(vc_id, challenge, domain);
return { data: vp };
} else {
console.log("Missing parameters: vc_id");
return { error: "Missing parameter: vc_id" };
}
return await getVP(vc_id, domain, challenge);
case "changeInfuraToken":
infuraToken = (request as any).params[0];
if (infuraToken != null && infuraToken != "") {
const config = await getConfig();
const result = await wallet.request({
method: "snap_confirm",
params: [
{
prompt: `Change Infura Token`,
description:
"Would you like to change the infura token to following?",
textAreaContent:
"Current token: " +
config.infuraToken +
"\n" +
"New token: " +
infuraToken,
},
],
});
if (result) {
await changeInfuraToken(infuraToken);
return { data: true };
} else {
return { data: false, error: "Request declined" };
}
} else {
return { error: "Missing parameter: infuraToken" };
}
return await changeInfuraToken(infuraToken);
case "togglePopups":
return await togglePopups();
default:
throw new Error("Method not found.");
}
Expand Down
36 changes: 17 additions & 19 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import {
SnapPrivateKeyStore,
} from "./veramo/plugins/snapDataStore/snapDataStore";

/**
* MetaMask Wallet interface
*/
export interface Wallet {
registerApiRequestHandler: (origin: unknown) => unknown;
registerRpcMessageHandler: (origin: unknown) => unknown;
request: (origin: any) => unknown;
send(options: { method: string; params: unknown[] }): unknown;
getAppKey(): Promise<string>;
}

/**
* MetaMask State
*/
Expand Down Expand Up @@ -57,14 +46,23 @@ export type ExtendedVerifiableCredential = VerifiableCredential & {
};

export interface SSISnapConfig {
/**
* Type of store, 'snap' by default
*/
store: string;
/**
* Infura token, used by Veramo agent.
*/
infuraToken: string;
veramo: {
/**
* Type of store, 'snap' by default
*/
store: string;
/**
* Infura token, used by Veramo agent.
*/
infuraToken: string;
/**
*
*/
};
dApp: {
disablePopups: boolean;
friendlyDapps: Array<string>;
};
}

/**
Expand Down
67 changes: 67 additions & 0 deletions src/rpc/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Response } from "./../interfaces";
import {
_changeInfuraToken,
_togglePopups,
_addFriendlyDapp,
_getFriendlyDapps,
_removeFriendlyDapp,
} from "./../utils/snap_utils";
import { getConfig } from "./../utils/state_utils";

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

const result =
config.dApp.disablePopups ||
(await wallet.request({
method: "snap_confirm",
params: [
{
prompt: `Toggle Popups`,
description: "Would you like to toggle the popups to following?",
textAreaContent:
"Current setting: " +
config.dApp.disablePopups +
"\n" +
"New setting: " +
!config.dApp.disablePopups,
},
],
}));
if (result) {
await await _togglePopups();
return { data: true };
} else {
return { data: false, error: "Request declined" };
}
}

export async function changeInfuraToken(token?: string): Promise<Response> {
if (token != null && token != "") {
const config = await getConfig();
const result = await wallet.request({
method: "snap_confirm",
params: [
{
prompt: `Change Infura Token`,
description:
"Would you like to change the infura token to following?",
textAreaContent:
"Current token: " +
config.veramo.infuraToken +
"\n" +
"New token: " +
token,
},
],
});
if (result) {
await _changeInfuraToken(token);
return { data: true };
} else {
return { data: false, error: "Request declined" };
}
} else {
return { error: "Missing parameter: infuraToken" };
}
}
30 changes: 30 additions & 0 deletions src/rpc/getVCs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { list_vcs } from "../utils/veramo_utils";
import { Response } from "../interfaces";
import { getConfig } from "../utils/state_utils";

export async function getVCs(querry?: any): Promise<Response> {
console.log("querry", querry);
let vcs = await list_vcs(querry);
const config = await getConfig();
console.log("VCs: ", vcs);

const result =
config.dApp.disablePopups ||
(await wallet.request({
method: "snap_confirm",
params: [
{
prompt: `Send VCs`,
description: "Are you sure you want to send VCs to the dApp?",
textAreaContent:
"Some dApps are less secure than others and could save data from VCs against your will. Be careful where you send your private VCs! Number of VCs submitted is " +
vcs.length,
},
],
}));
if (result) {
return { data: { vcs: vcs } };
} else {
return { error: "User rejected" };
}
}
11 changes: 11 additions & 0 deletions src/rpc/getVP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { create_vp } from "../utils/veramo_utils";

export async function getVP(vc_id: string, domain: string, challenge: string) {
if (vc_id) {
let vp = await create_vp(vc_id, challenge, domain);
return { data: vp };
} else {
console.log("Missing parameters: vc_id");
return { error: "Missing parameter: vc_id" };
}
}
Loading

0 comments on commit 0e65b08

Please sign in to comment.