|
|
|
<template>
|
|
|
|
<!-- QUICK NAV -->
|
|
|
|
<nav id="QuickNav" class="fixed bottom-0 left-0 right-0 bg-slate-200">
|
|
|
|
<ul class="flex text-2xl p-2 gap-2">
|
|
|
|
<!-- Home Feed -->
|
|
|
|
<li class="basis-1/5 rounded-md text-slate-500">
|
|
|
|
<router-link :to="{ name: 'home' }" class="block text-center py-3 px-1"
|
|
|
|
><fa icon="house-chimney" class="fa-fw"></fa
|
|
|
|
></router-link>
|
|
|
|
</li>
|
|
|
|
<!-- Search -->
|
|
|
|
<li class="basis-1/5 rounded-md text-slate-500">
|
|
|
|
<router-link
|
|
|
|
:to="{ name: 'discover' }"
|
|
|
|
class="block text-center py-3 px-1"
|
|
|
|
><fa icon="magnifying-glass" class="fa-fw"></fa
|
|
|
|
></router-link>
|
|
|
|
</li>
|
|
|
|
<!-- Contacts -->
|
|
|
|
<li class="basis-1/5 rounded-md text-slate-500">
|
|
|
|
<router-link
|
|
|
|
:to="{ name: 'projects' }"
|
|
|
|
class="block text-center py-3 px-1"
|
|
|
|
><fa icon="folder-open" class="fa-fw"></fa
|
|
|
|
></router-link>
|
|
|
|
</li>
|
|
|
|
<!-- Contacts -->
|
|
|
|
<li class="basis-1/5 rounded-md bg-slate-400 text-white">
|
|
|
|
<router-link
|
|
|
|
:to="{ name: 'contacts' }"
|
|
|
|
class="block text-center py-3 px-1"
|
|
|
|
><fa icon="users" class="fa-fw"></fa
|
|
|
|
></router-link>
|
|
|
|
</li>
|
|
|
|
<!-- Profile -->
|
|
|
|
<li class="basis-1/5 rounded-md text-slate-500">
|
|
|
|
<router-link
|
|
|
|
:to="{ name: 'account' }"
|
|
|
|
class="block text-center py-3 px-1"
|
|
|
|
><fa icon="circle-user" class="fa-fw"></fa
|
|
|
|
></router-link>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
<section id="Content" class="p-6 pb-24">
|
|
|
|
<!-- Heading -->
|
|
|
|
<h1 id="ViewHeading" class="text-4xl text-center font-light pt-4 mb-8">
|
|
|
|
My Contacts
|
|
|
|
</h1>
|
|
|
|
|
|
|
|
<!-- New Contact -->
|
|
|
|
<div class="mb-4 flex">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="DID, Name, Public Key"
|
|
|
|
class="block w-full rounded-l border border-r-0 border-slate-400 px-3 py-2"
|
|
|
|
v-model="contactInput"
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
class="px-4 rounded-r bg-slate-200 border border-l-0 border-slate-400"
|
|
|
|
@click="onClickNewContact()"
|
|
|
|
>
|
|
|
|
<fa icon="plus" class="fa-fw"></fa>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="flex justify-between" v-if="showGiveTotals">
|
|
|
|
<div class="w-full text-right">
|
|
|
|
Hours to Add:
|
|
|
|
<input
|
|
|
|
class="border border rounded border-slate-400 w-24 text-right"
|
|
|
|
type="text"
|
|
|
|
placeholder="1"
|
|
|
|
v-model="hourInput"
|
|
|
|
/>
|
|
|
|
<br />
|
|
|
|
<input
|
|
|
|
class="border border rounded border-slate-400 w-48"
|
|
|
|
type="text"
|
|
|
|
placeholder="Description"
|
|
|
|
v-model="hourDescriptionInput"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Results List -->
|
|
|
|
<ul class="">
|
|
|
|
<li
|
|
|
|
class="border-b border-slate-300"
|
|
|
|
v-for="contact in contacts"
|
|
|
|
:key="contact.did"
|
|
|
|
>
|
|
|
|
<div class="grow overflow-hidden">
|
|
|
|
<h2 class="text-base font-semibold">
|
|
|
|
{{ contact.name || "(no name)" }}
|
|
|
|
</h2>
|
|
|
|
<div class="text-sm truncate">{{ contact.did }}</div>
|
|
|
|
<div class="text-sm truncate" v-if="contact.publicKeyBase64">
|
|
|
|
Public Key (base 64): {{ contact.publicKeyBase64 }}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<button
|
|
|
|
v-if="contact.seesMe"
|
|
|
|
class="tooltip"
|
|
|
|
@click="setVisibility(contact, false)"
|
|
|
|
>
|
|
|
|
<fa icon="eye" class="text-slate-900 fa-fw ml-1" />
|
|
|
|
<span class="tooltiptext">Can see you</span>
|
|
|
|
</button>
|
|
|
|
<button v-else class="tooltip" @click="setVisibility(contact, true)">
|
|
|
|
<span class="tooltiptext">Cannot see you</span>
|
|
|
|
<fa icon="eye-slash" class="text-slate-900 fa-fw ml-1" />
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button class="tooltip" @click="checkVisibility(contact)">
|
|
|
|
<span class="tooltiptext">Check Visibility</span>
|
|
|
|
<fa icon="rotate" class="text-slate-900 fa-fw ml-1" />
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button v-if="contact.registered" class="tooltip">
|
|
|
|
<span class="tooltiptext">Registered</span>
|
|
|
|
<fa icon="person-circle-check" class="text-slate-900 fa-fw ml-1" />
|
|
|
|
</button>
|
|
|
|
<button v-else @click="register(contact)" class="tooltip">
|
|
|
|
<span class="tooltiptext">Maybe not registered</span>
|
|
|
|
<fa
|
|
|
|
icon="person-circle-question"
|
|
|
|
class="text-slate-900 fa-fw ml-1"
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button @click="deleteContact(contact)" class="px-9 tooltip">
|
|
|
|
<span class="tooltiptext">Delete!</span>
|
|
|
|
<fa icon="trash-can" class="text-red-600 fa-fw ml-1" />
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<div v-if="showGiveTotals" class="float-right">
|
|
|
|
<div class="float-right">
|
|
|
|
to: {{ givenByMeTotals[contact.did] || 0 }}
|
|
|
|
<button
|
|
|
|
class="text-md uppercase bg-slate-500 text-white px-1.5 py-2 rounded-md mb-6"
|
|
|
|
@click="onClickAddGive(identity.did, contact.did)"
|
|
|
|
>
|
|
|
|
+
|
|
|
|
</button>
|
|
|
|
by: {{ givenToMeTotals[contact.did] || 0 }}
|
|
|
|
<button
|
|
|
|
class="text-md uppercase bg-slate-500 text-white px-1.5 py-2 rounded-md mb-6"
|
|
|
|
@click="onClickAddGive(contact.did, identity.did)"
|
|
|
|
>
|
|
|
|
+
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</section>
|
|
|
|
<div v-bind:class="computedAlertClassNames()">
|
|
|
|
<button
|
|
|
|
class="close-button bg-slate-200 w-8 leading-loose rounded-full absolute top-2 right-2"
|
|
|
|
@click="onClickClose()"
|
|
|
|
>
|
|
|
|
<fa icon="xmark"></fa>
|
|
|
|
</button>
|
|
|
|
<h4 class="font-bold pr-5">{{ alertTitle }}</h4>
|
|
|
|
<p>{{ alertMessage }}</p>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { AxiosError } from "axios";
|
|
|
|
import * as didJwt from "did-jwt";
|
|
|
|
import * as R from "ramda";
|
|
|
|
import { IIdentifier } from "@veramo/core";
|
|
|
|
import { Options, Vue } from "vue-class-component";
|
|
|
|
|
|
|
|
import { AppString } from "@/constants/app";
|
|
|
|
import { accessToken, SimpleSigner } from "@/libs/crypto";
|
|
|
|
import { accountsDB, db } from "@/db";
|
|
|
|
import { Contact } from "@/db/tables/contacts";
|
|
|
|
import { MASTER_SETTINGS_KEY } from "@/db/tables/settings";
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
const Buffer = require("buffer/").Buffer;
|
|
|
|
|
|
|
|
const SERVICE_ID = "endorser.ch";
|
|
|
|
|
|
|
|
export interface GiveVerifiableCredential {
|
|
|
|
"@context": string;
|
|
|
|
"@type": string;
|
|
|
|
agent: { identifier: string };
|
|
|
|
description?: string;
|
|
|
|
object: { amountOfThisGood: number; unitCode: string };
|
|
|
|
recipient: { identifier: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RegisterVerifiableCredential {
|
|
|
|
"@context": string;
|
|
|
|
"@type": string;
|
|
|
|
agent: { identifier: string };
|
|
|
|
object: string;
|
|
|
|
recipient: { identifier: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
@Options({
|
|
|
|
components: {},
|
|
|
|
})
|
|
|
|
export default class ContactsView extends Vue {
|
|
|
|
contacts: Array<Contact> = [];
|
|
|
|
contactInput = "";
|
|
|
|
// { "did:...": amount } entry for each contact
|
|
|
|
givenByMeTotals: Record<string, number> = {};
|
|
|
|
// { "did:...": amount } entry for each contact
|
|
|
|
givenToMeTotals: Record<string, number> = {};
|
|
|
|
hourDescriptionInput = "";
|
|
|
|
hourInput = "0";
|
|
|
|
identity: IIdentifier | null = null;
|
|
|
|
showGiveTotals = false;
|
|
|
|
|
|
|
|
// 'created' hook runs when the Vue instance is first created
|
|
|
|
async created() {
|
|
|
|
await accountsDB.open();
|
|
|
|
const accounts = await accountsDB.accounts.toArray();
|
|
|
|
this.identity = JSON.parse(accounts[0].identity);
|
|
|
|
|
|
|
|
await db.open();
|
|
|
|
const settings = await db.settings.get(MASTER_SETTINGS_KEY);
|
|
|
|
this.showGiveTotals = !!settings?.showContactGivesInline;
|
|
|
|
if (this.showGiveTotals) {
|
|
|
|
this.loadGives();
|
|
|
|
}
|
|
|
|
const allContacts = await db.contacts.toArray();
|
|
|
|
this.contacts = R.sort(
|
|
|
|
(a: Contact, b) => (a.name || "").localeCompare(b.name || ""),
|
|
|
|
allContacts
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onClickNewContact(): Promise<void> {
|
|
|
|
let did = this.contactInput;
|
|
|
|
let name, publicKeyBase64;
|
|
|
|
const commaPos1 = this.contactInput.indexOf(",");
|
|
|
|
if (commaPos1 > -1) {
|
|
|
|
did = this.contactInput.substring(0, commaPos1).trim();
|
|
|
|
name = this.contactInput.substring(commaPos1 + 1).trim();
|
|
|
|
const commaPos2 = this.contactInput.indexOf(",", commaPos1 + 1);
|
|
|
|
if (commaPos2 > -1) {
|
|
|
|
name = this.contactInput.substring(commaPos1 + 1, commaPos2).trim();
|
|
|
|
publicKeyBase64 = this.contactInput.substring(commaPos2 + 1).trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// help with potential mistakes while this sharing requires copy-and-paste
|
|
|
|
if (publicKeyBase64 && /^[0-9A-Fa-f]{66}$/i.test(publicKeyBase64)) {
|
|
|
|
// it must be all hex (compressed public key), so convert
|
|
|
|
publicKeyBase64 = Buffer.from(publicKeyBase64, "hex").toString("base64");
|
|
|
|
}
|
|
|
|
const newContact = { did, name, publicKeyBase64 };
|
|
|
|
await db.contacts.add(newContact);
|
|
|
|
const allContacts = this.contacts.concat([newContact]);
|
|
|
|
this.contacts = R.sort(
|
|
|
|
(a: Contact, b) => (a.name || "").localeCompare(b.name || ""),
|
|
|
|
allContacts
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadGives() {
|
|
|
|
if (!this.identity) {
|
|
|
|
console.error(
|
|
|
|
"Attempted to load Give records with no identity available."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
|
|
|
|
|
|
|
|
// load all the time I have given
|
|
|
|
try {
|
|
|
|
const url =
|
|
|
|
endorserApiServer +
|
|
|
|
"/api/v2/report/gives?agentId=" +
|
|
|
|
encodeURIComponent(this.identity?.did);
|
|
|
|
const token = await accessToken(this.identity);
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
};
|
|
|
|
const resp = await this.axios.get(url, { headers });
|
|
|
|
//console.log("Server response", resp.status, resp.data);
|
|
|
|
if (resp.status === 200) {
|
|
|
|
const contactTotals: Record<string, number> = {};
|
|
|
|
for (const give of resp.data.data) {
|
|
|
|
if (give.unit == "HUR") {
|
|
|
|
const recipDid: string = give.recipientDid;
|
|
|
|
const prevAmount = contactTotals[recipDid] || 0;
|
|
|
|
contactTotals[recipDid] = prevAmount + give.amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//console.log("Done retrieving gives", contactTotals);
|
|
|
|
this.givenByMeTotals = contactTotals;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.alertTitle = "Error from Server";
|
|
|
|
this.alertMessage = error as string;
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load all the time I have received
|
|
|
|
try {
|
|
|
|
const url =
|
|
|
|
endorserApiServer +
|
|
|
|
"/api/v2/report/gives?recipientId=" +
|
|
|
|
encodeURIComponent(this.identity.did);
|
|
|
|
const token = await accessToken(this.identity);
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
};
|
|
|
|
const resp = await this.axios.get(url, { headers });
|
|
|
|
//console.log("Server response", resp.status, resp.data);
|
|
|
|
if (resp.status === 200) {
|
|
|
|
const contactTotals: Record<string, number> = {};
|
|
|
|
for (const give of resp.data.data) {
|
|
|
|
if (give.unit == "HUR") {
|
|
|
|
const prevAmount = contactTotals[give.agentDid] || 0;
|
|
|
|
contactTotals[give.agentDid] = prevAmount + give.amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//console.log("Done retrieving receipts", contactTotals);
|
|
|
|
this.givenToMeTotals = contactTotals;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.alertTitle = "Error from Server";
|
|
|
|
this.alertMessage = error as string;
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteContact(contact: Contact) {
|
|
|
|
if (
|
|
|
|
confirm(
|
|
|
|
"Are you sure you want to delete " +
|
|
|
|
this.nameForDid(this.contacts, contact.did) +
|
|
|
|
" with DID " +
|
|
|
|
contact.did +
|
|
|
|
" ?"
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
await db.open();
|
|
|
|
await db.contacts.delete(contact.did);
|
|
|
|
this.contacts = R.without([contact], this.contacts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async register(contact: Contact) {
|
|
|
|
if (
|
|
|
|
confirm(
|
|
|
|
"Are you sure you want to use one of your registrations for " +
|
|
|
|
this.nameForDid(this.contacts, contact.did) +
|
|
|
|
"?"
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
await accountsDB.open();
|
|
|
|
const accounts = await accountsDB.accounts.toArray();
|
|
|
|
const identity = JSON.parse(accounts[0].identity);
|
|
|
|
// Make a claim
|
|
|
|
const vcClaim: RegisterVerifiableCredential = {
|
|
|
|
"@context": "https://schema.org",
|
|
|
|
"@type": "RegisterAction",
|
|
|
|
agent: { identifier: identity.did },
|
|
|
|
object: SERVICE_ID,
|
|
|
|
recipient: { identifier: contact.did },
|
|
|
|
};
|
|
|
|
// Make a payload for the claim
|
|
|
|
const vcPayload = {
|
|
|
|
vc: {
|
|
|
|
"@context": ["https://www.w3.org/2018/credentials/v1"],
|
|
|
|
type: ["VerifiableCredential"],
|
|
|
|
credentialSubject: vcClaim,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// Create a signature using private key of identity
|
|
|
|
if (identity.keys[0].privateKeyHex !== null) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const privateKeyHex: string = identity.keys[0].privateKeyHex!;
|
|
|
|
const signer = await SimpleSigner(privateKeyHex);
|
|
|
|
const alg = undefined;
|
|
|
|
// Create a JWT for the request
|
|
|
|
const vcJwt: string = await didJwt.createJWT(vcPayload, {
|
|
|
|
alg: alg,
|
|
|
|
issuer: identity.did,
|
|
|
|
signer: signer,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the xhr request payload
|
|
|
|
const payload = JSON.stringify({ jwtEncoded: vcJwt });
|
|
|
|
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
|
|
|
|
const url = endorserApiServer + "/api/v2/claim";
|
|
|
|
const token = await accessToken(identity);
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const resp = await this.axios.post(url, payload, { headers });
|
|
|
|
//console.log("Got resp data:", resp.data);
|
|
|
|
if (resp.data?.success?.handleId) {
|
|
|
|
contact.registered = true;
|
|
|
|
db.contacts.update(contact.did, { registered: true });
|
|
|
|
|
|
|
|
this.alertTitle = "Registration Success";
|
|
|
|
this.alertMessage = contact.name + " has been registered.";
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
let userMessage = "There was an error. See logs for more info.";
|
|
|
|
const serverError = error as AxiosError;
|
|
|
|
if (serverError) {
|
|
|
|
if (serverError.message) {
|
|
|
|
userMessage = serverError.message; // Info for the user
|
|
|
|
} else {
|
|
|
|
userMessage = JSON.stringify(serverError.toJSON());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
userMessage = error as string;
|
|
|
|
}
|
|
|
|
// Now set that error for the user to see.
|
|
|
|
this.alertTitle = "Error with Server";
|
|
|
|
this.alertMessage = userMessage;
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async setVisibility(contact: Contact, visibility: boolean) {
|
|
|
|
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
|
|
|
|
const url =
|
|
|
|
endorserApiServer +
|
|
|
|
"/api/report/" +
|
|
|
|
(visibility ? "canSeeMe" : "cannotSeeMe");
|
|
|
|
await accountsDB.open();
|
|
|
|
const accounts = await accountsDB.accounts.toArray();
|
|
|
|
const identity = JSON.parse(accounts[0].identity);
|
|
|
|
const token = await accessToken(identity);
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
};
|
|
|
|
const payload = JSON.stringify({ did: contact.did });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const resp = await this.axios.post(url, payload, { headers });
|
|
|
|
if (resp.status === 200) {
|
|
|
|
contact.seesMe = visibility;
|
|
|
|
db.contacts.update(contact.did, { seesMe: visibility });
|
|
|
|
} else {
|
|
|
|
this.alertTitle = "Error from Server";
|
|
|
|
console.log("Bad response setting visibility: ", resp.data);
|
|
|
|
if (resp.data.error?.message) {
|
|
|
|
this.alertMessage = resp.data.error?.message;
|
|
|
|
} else {
|
|
|
|
this.alertMessage = "Bad server response of " + resp.status;
|
|
|
|
}
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
this.alertTitle = "Error from Server";
|
|
|
|
this.alertMessage = err as string;
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkVisibility(contact: Contact) {
|
|
|
|
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
|
|
|
|
const url =
|
|
|
|
endorserApiServer +
|
|
|
|
"/api/report/canDidExplicitlySeeMe?did=" +
|
|
|
|
encodeURIComponent(contact.did);
|
|
|
|
await accountsDB.open();
|
|
|
|
const accounts = await accountsDB.accounts.toArray();
|
|
|
|
const identity = JSON.parse(accounts[0].identity);
|
|
|
|
const token = await accessToken(identity);
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const resp = await this.axios.get(url, { headers });
|
|
|
|
if (resp.status === 200) {
|
|
|
|
const visibility = resp.data;
|
|
|
|
contact.seesMe = visibility;
|
|
|
|
db.contacts.update(contact.did, { seesMe: visibility });
|
|
|
|
|
|
|
|
this.alertTitle = "Refreshed";
|
|
|
|
this.alertMessage =
|
|
|
|
this.nameForContact(contact, true) +
|
|
|
|
" can " +
|
|
|
|
(visibility ? "" : "not ") +
|
|
|
|
"see your activity.";
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
} else {
|
|
|
|
this.alertTitle = "Error from Server";
|
|
|
|
if (resp.data.error?.message) {
|
|
|
|
this.alertMessage = resp.data.error?.message;
|
|
|
|
} else {
|
|
|
|
this.alertMessage = "Bad server response of " + resp.status;
|
|
|
|
}
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
this.alertTitle = "Error from Server";
|
|
|
|
this.alertMessage = err as string;
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// from https://stackoverflow.com/a/175787/845494
|
|
|
|
//
|
|
|
|
private isNumeric(str: string): boolean {
|
|
|
|
return !isNaN(+str);
|
|
|
|
}
|
|
|
|
|
|
|
|
private nameForDid(contacts: Array<Contact>, did: string): string {
|
|
|
|
const contact = R.find((con) => con.did == did, contacts);
|
|
|
|
return this.nameForContact(contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
private nameForContact(contact?: Contact, capitalize?: boolean): string {
|
|
|
|
return contact?.name || (capitalize ? "T" : "t") + "this unnamed user";
|
|
|
|
}
|
|
|
|
|
|
|
|
async onClickAddGive(fromDid: string, toDid: string): Promise<void> {
|
|
|
|
if (!this.isNumeric(this.hourInput)) {
|
|
|
|
this.alertTitle = "Input Error";
|
|
|
|
this.alertMessage =
|
|
|
|
"This is not a valid number of hours: " + this.hourInput;
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
} else if (!parseFloat(this.hourInput)) {
|
|
|
|
this.alertTitle = "Input Error";
|
|
|
|
this.alertMessage = "Giving 0 hours does nothing.";
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
} else if (!this.identity) {
|
|
|
|
this.alertTitle = "Status Error";
|
|
|
|
this.alertMessage = "No identity is available.";
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
} else {
|
|
|
|
let toFrom;
|
|
|
|
if (fromDid == this.identity?.did) {
|
|
|
|
toFrom = "from you to " + this.nameForDid(this.contacts, toDid);
|
|
|
|
} else {
|
|
|
|
toFrom = "from " + this.nameForDid(this.contacts, fromDid) + " to you";
|
|
|
|
}
|
|
|
|
let description;
|
|
|
|
if (this.hourDescriptionInput) {
|
|
|
|
description = " with description '" + this.hourDescriptionInput + "'";
|
|
|
|
} else {
|
|
|
|
description = " with no description";
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
confirm(
|
|
|
|
"Are you sure you want to record " +
|
|
|
|
this.hourInput +
|
|
|
|
" hours " +
|
|
|
|
toFrom +
|
|
|
|
description +
|
|
|
|
"?"
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
this.createAndSubmitGive(
|
|
|
|
this.identity,
|
|
|
|
fromDid,
|
|
|
|
toDid,
|
|
|
|
parseFloat(this.hourInput),
|
|
|
|
this.hourDescriptionInput
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async createAndSubmitGive(
|
|
|
|
identity: IIdentifier,
|
|
|
|
fromDid: string,
|
|
|
|
toDid: string,
|
|
|
|
amount: number,
|
|
|
|
description: string
|
|
|
|
): Promise<void> {
|
|
|
|
// Make a claim
|
|
|
|
const vcClaim: GiveVerifiableCredential = {
|
|
|
|
"@context": "https://schema.org",
|
|
|
|
"@type": "GiveAction",
|
|
|
|
agent: { identifier: fromDid },
|
|
|
|
object: { amountOfThisGood: amount, unitCode: "HUR" },
|
|
|
|
recipient: { identifier: toDid },
|
|
|
|
};
|
|
|
|
if (description) {
|
|
|
|
vcClaim.description = description;
|
|
|
|
}
|
|
|
|
// Make a payload for the claim
|
|
|
|
const vcPayload = {
|
|
|
|
vc: {
|
|
|
|
"@context": ["https://www.w3.org/2018/credentials/v1"],
|
|
|
|
type: ["VerifiableCredential"],
|
|
|
|
credentialSubject: vcClaim,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// Create a signature using private key of identity
|
|
|
|
if (identity.keys[0].privateKeyHex !== null) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
const privateKeyHex: string = identity.keys[0].privateKeyHex!;
|
|
|
|
const signer = await SimpleSigner(privateKeyHex);
|
|
|
|
const alg = undefined;
|
|
|
|
// Create a JWT for the request
|
|
|
|
const vcJwt: string = await didJwt.createJWT(vcPayload, {
|
|
|
|
alg: alg,
|
|
|
|
issuer: identity.did,
|
|
|
|
signer: signer,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make the xhr request payload
|
|
|
|
|
|
|
|
const payload = JSON.stringify({ jwtEncoded: vcJwt });
|
|
|
|
const endorserApiServer = AppString.DEFAULT_ENDORSER_API_SERVER;
|
|
|
|
const url = endorserApiServer + "/api/v2/claim";
|
|
|
|
const token = await accessToken(identity);
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: "Bearer " + token,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const resp = await this.axios.post(url, payload, { headers });
|
|
|
|
//console.log("Got resp data:", resp.data);
|
|
|
|
if (resp.data?.success?.handleId) {
|
|
|
|
this.alertTitle = "";
|
|
|
|
this.alertMessage = "";
|
|
|
|
if (fromDid === identity.did) {
|
|
|
|
this.givenByMeTotals[toDid] = this.givenByMeTotals[toDid] + amount;
|
|
|
|
// do this to update the UI (is there a better way?)
|
|
|
|
// eslint-disable-next-line no-self-assign
|
|
|
|
this.givenByMeTotals = this.givenByMeTotals;
|
|
|
|
} else {
|
|
|
|
this.givenToMeTotals[fromDid] =
|
|
|
|
this.givenToMeTotals[fromDid] + amount;
|
|
|
|
// do this to update the UI (is there a better way?)
|
|
|
|
// eslint-disable-next-line no-self-assign
|
|
|
|
this.givenToMeTotals = this.givenToMeTotals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
let userMessage = "There was an error. See logs for more info.";
|
|
|
|
const serverError = error as AxiosError;
|
|
|
|
if (serverError) {
|
|
|
|
if (serverError.message) {
|
|
|
|
userMessage = serverError.message; // Info for the user
|
|
|
|
} else {
|
|
|
|
userMessage = JSON.stringify(serverError.toJSON());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
userMessage = error as string;
|
|
|
|
}
|
|
|
|
// Now set that error for the user to see.
|
|
|
|
this.alertTitle = "Error with Server";
|
|
|
|
this.alertMessage = userMessage;
|
|
|
|
this.isAlertVisible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alertTitle = "";
|
|
|
|
alertMessage = "";
|
|
|
|
isAlertVisible = false;
|
|
|
|
|
|
|
|
public onClickClose() {
|
|
|
|
this.isAlertVisible = false;
|
|
|
|
this.alertTitle = "";
|
|
|
|
this.alertMessage = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
public computedAlertClassNames() {
|
|
|
|
return {
|
|
|
|
hidden: !this.isAlertVisible,
|
|
|
|
"dismissable-alert": true,
|
|
|
|
"bg-slate-100": true,
|
|
|
|
"p-5": true,
|
|
|
|
rounded: true,
|
|
|
|
"drop-shadow-lg": true,
|
|
|
|
absolute: true,
|
|
|
|
"top-3": true,
|
|
|
|
"inset-x-3": true,
|
|
|
|
"transition-transform": true,
|
|
|
|
"ease-in": true,
|
|
|
|
"duration-300": true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
/* Tooltip container */
|
|
|
|
.tooltip {
|
|
|
|
position: relative;
|
|
|
|
display: inline-block;
|
|
|
|
border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tooltip text */
|
|
|
|
.tooltip .tooltiptext {
|
|
|
|
visibility: hidden;
|
|
|
|
width: 200px;
|
|
|
|
background-color: black;
|
|
|
|
color: #fff;
|
|
|
|
text-align: center;
|
|
|
|
padding: 5px 0;
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
|
|
/* Position the tooltip text - see examples below! */
|
|
|
|
position: absolute;
|
|
|
|
z-index: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show the tooltip text when you mouse over the tooltip container */
|
|
|
|
.tooltip:hover .tooltiptext {
|
|
|
|
visibility: visible;
|
|
|
|
}
|
|
|
|
</style>
|