Skip to content

Commit

Permalink
call npm run format again, to apply on TS files
Browse files Browse the repository at this point in the history
  • Loading branch information
prusnak committed Nov 12, 2024
1 parent 64cd350 commit b3eb590
Show file tree
Hide file tree
Showing 17 changed files with 1,125 additions and 631 deletions.
2 changes: 1 addition & 1 deletion src/components/SendPaymentRequest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default defineComponent({
clickPaymentRequest: function () {
this.parseAndPayPaymentRequest(
this.sendData.paymentRequest,
this.sendData.tokensBase64
this.sendData.tokensBase64,
);
},
getPaymentRequestTransportType: function (request) {
Expand Down
25 changes: 16 additions & 9 deletions src/js/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const errorTypes = {
500: "negative",
} as StatusMap;

async function notifyApiError(error: Error, caption: string = "", position = "top" as QNotifyCreateOptions["position"]) {
async function notifyApiError(
error: Error,
caption: string = "",
position = "top" as QNotifyCreateOptions["position"],
) {
try {
Notify.create({
timeout: 5000,
Expand All @@ -20,7 +24,7 @@ async function notifyApiError(error: Error, caption: string = "", position = "to
{
icon: "close",
color: "white",
handler: () => { },
handler: () => {},
},
],
});
Expand All @@ -31,7 +35,7 @@ async function notifyApiError(error: Error, caption: string = "", position = "to

async function notifySuccess(
message: string,
position = "top" as QNotifyCreateOptions["position"]
position = "top" as QNotifyCreateOptions["position"],
) {
Notify.create({
timeout: 5000,
Expand All @@ -43,7 +47,7 @@ async function notifySuccess(
{
icon: "close",
color: "white",
handler: () => { },
handler: () => {},
},
],
});
Expand All @@ -60,7 +64,7 @@ async function notifyError(message: string, caption?: string) {
{
icon: "close",
color: "white",
handler: () => { },
handler: () => {},
},
],
});
Expand All @@ -69,7 +73,7 @@ async function notifyError(message: string, caption?: string) {
async function notifyWarning(
message: string,
caption?: string,
timeout = 5000
timeout = 5000,
) {
Notify.create({
timeout: timeout,
Expand All @@ -82,13 +86,16 @@ async function notifyWarning(
{
icon: "close",
color: "black",
handler: () => { },
handler: () => {},
},
],
});
}

async function notify(message: string, position = "top" as QNotifyCreateOptions["position"]) {
async function notify(
message: string,
position = "top" as QNotifyCreateOptions["position"],
) {
// failure
Notify.create({
timeout: 5000,
Expand All @@ -100,7 +107,7 @@ async function notify(message: string, position = "top" as QNotifyCreateOptions[
{
icon: "close",
color: "white",
handler: () => { },
handler: () => {},
},
],
});
Expand Down
4 changes: 1 addition & 3 deletions src/js/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ function decode(encoded_token: string) {
* Returns a list of proofs from a decoded token
*/
function getProofs(decoded_token: Token): WalletProof[] {
if (
!(decoded_token.proofs.length > 0)
) {
if (!(decoded_token.proofs.length > 0)) {
throw new Error("Token format wrong");
}
const proofs = decoded_token.proofs.flat();
Expand Down
122 changes: 85 additions & 37 deletions src/stores/mints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import { defineStore } from "pinia";
import { useLocalStorage } from "@vueuse/core";
import { useWorkersStore } from "./workers";
import { notifyApiError, notifyError, notifySuccess } from "src/js/notify";
import { CashuMint, MintKeys, MintAllKeysets, MintActiveKeys, Proof, SerializedBlindedSignature, MintKeyset, GetInfoResponse } from "@cashu/cashu-ts";
import {
CashuMint,
MintKeys,
MintAllKeysets,
MintActiveKeys,
Proof,
SerializedBlindedSignature,
MintKeyset,
GetInfoResponse,
} from "@cashu/cashu-ts";
import { useUiStore } from "./ui";
export type Mint = {
url: string;
Expand All @@ -23,7 +32,9 @@ export class MintClass {
}
get proofs() {
const mintStore = useMintsStore();
return mintStore.proofs.filter((p) => this.mint.keysets.map((k) => k.id).includes(p.id));
return mintStore.proofs.filter((p) =>
this.mint.keysets.map((k) => k.id).includes(p.id),
);
}
// get balance() {
// const proofs = this.proofs;
Expand All @@ -44,7 +55,9 @@ export class MintClass {
}

get units() {
return this.mint.keysets.map((k) => k.unit).filter((value, index, self) => self.indexOf(value) === index);
return this.mint.keysets
.map((k) => k.unit)
.filter((value, index, self) => self.indexOf(value) === index);
}

unitKeysets(unit: string): MintKeyset[] {
Expand All @@ -53,7 +66,9 @@ export class MintClass {

unitProofs(unit: string) {
const unitKeysets = this.unitKeysets(unit);
return this.proofs.filter((p) => unitKeysets.map((k) => k.id).includes(p.id));
return this.proofs.filter((p) =>
unitKeysets.map((k) => k.id).includes(p.id),
);
}

unitBalance(unit: string) {
Expand All @@ -69,7 +84,6 @@ export type Balances = {
[unit: string]: number;
};


type BlindSignatureAudit = {
signature: SerializedBlindedSignature;
amount: number;
Expand All @@ -90,7 +104,10 @@ export const useMintsStore = defineStore("mints", {
mints: useLocalStorage("cashu.mints", [] as Mint[]),
proofs: useLocalStorage("cashu.proofs", [] as WalletProof[]),
spentProofs: useLocalStorage("cashu.spentProofs", [] as WalletProof[]),
blindSignatures: useLocalStorage("cashu.blindSignatures", [] as BlindSignatureAudit[]),
blindSignatures: useLocalStorage(
"cashu.blindSignatures",
[] as BlindSignatureAudit[],
),
// balances: useLocalStorage("cashu.balances", {} as Balances),
showAddMintDialog: false,
addMintBlocking: false,
Expand All @@ -101,30 +118,39 @@ export const useMintsStore = defineStore("mints", {
},
getters: {
activeProofs({ activeMintUrl, activeUnit }): WalletProof[] {
const unitKeysets = this.mints.find((m) => m.url === activeMintUrl)?.keysets?.filter((k) => k.unit === activeUnit);
const unitKeysets = this.mints
.find((m) => m.url === activeMintUrl)
?.keysets?.filter((k) => k.unit === activeUnit);
if (!unitKeysets) {
return [];
}
return this.proofs.filter((p) =>
unitKeysets.map((k) => k.id).includes(p.id)
unitKeysets.map((k) => k.id).includes(p.id),
);
},
activeBalance({ activeUnit }): number {
const allUnitKeysets = this.mints.map((m) => m.keysets).flat().filter((k) => k.unit === activeUnit);
const balance = this.proofs.filter((p) =>
allUnitKeysets.map((k) => k.id).includes(p.id)
).reduce((sum, p) => sum + p.amount, 0);
return balance
const allUnitKeysets = this.mints
.map((m) => m.keysets)
.flat()
.filter((k) => k.unit === activeUnit);
const balance = this.proofs
.filter((p) => allUnitKeysets.map((k) => k.id).includes(p.id))
.reduce((sum, p) => sum + p.amount, 0);
return balance;
},
activeKeysets({ activeMintUrl, activeUnit }): MintKeyset[] {
const unitKeysets = this.mints.find((m) => m.url === activeMintUrl)?.keysets?.filter((k) => k.unit === activeUnit);
const unitKeysets = this.mints
.find((m) => m.url === activeMintUrl)
?.keysets?.filter((k) => k.unit === activeUnit);
if (!unitKeysets) {
return [];
}
return unitKeysets;
},
activeKeys({ activeMintUrl, activeUnit }): MintKeys[] {
const unitKeys = this.mints.find((m) => m.url === activeMintUrl)?.keys?.filter((k) => k.unit === activeUnit);
const unitKeys = this.mints
.find((m) => m.url === activeMintUrl)
?.keys?.filter((k) => k.unit === activeUnit);
if (!unitKeys) {
return [];
}
Expand Down Expand Up @@ -163,8 +189,10 @@ export const useMintsStore = defineStore("mints", {
return new MintClass(mint);
} else {
if (this.mints.length) {
console.error("No active mint. This should not happen. switching to first one.")
this.activateMintUrl(this.mints[0].url, false, true)
console.error(
"No active mint. This should not happen. switching to first one.",
);
this.activateMintUrl(this.mints[0].url, false, true);
return new MintClass(this.mints[0]);
}
throw new Error("No active mint");
Expand Down Expand Up @@ -202,7 +230,12 @@ export const useMintsStore = defineStore("mints", {
});
this.spentProofs = this.spentProofs.concat(walletProofs);
},
appendBlindSignatures(signature: SerializedBlindedSignature, amount: number, secret: Uint8Array, r: Uint8Array) {
appendBlindSignatures(
signature: SerializedBlindedSignature,
amount: number,
secret: Uint8Array,
r: Uint8Array,
) {
const audit: BlindSignatureAudit = {
signature: signature,
amount: amount,
Expand All @@ -215,8 +248,11 @@ export const useMintsStore = defineStore("mints", {
toggleActiveUnitForMint(mint: Mint) {
// method to set the active unit to one that is supported by `mint`
const mintClass = new MintClass(mint);
if (!this.activeUnit || mintClass.allBalances[this.activeUnit] == undefined) {
this.activeUnit = mintClass.units[0]
if (
!this.activeUnit ||
mintClass.allBalances[this.activeUnit] == undefined
) {
this.activeUnit = mintClass.units[0];
}
},
updateMint(oldMint: Mint, newMint: Mint) {
Expand All @@ -236,21 +272,30 @@ export const useMintsStore = defineStore("mints", {
throw new Error("Mint not found");
}
},
addMint: async function (addMintData: { url: string, nickname: string }, verbose = false) {
addMint: async function (
addMintData: { url: string; nickname: string },
verbose = false,
) {
let url = addMintData.url;
this.addMintBlocking = true;
try {
// sanitize url
const sanitizeUrl = (url: string): string => {
let cleanedUrl = url.trim().replace(/\/+$/, '')
if (!/^[a-z]+:\/\//.test(cleanedUrl)) { // Check for any protocol followed by "://"
cleanedUrl = 'https://' + cleanedUrl;
let cleanedUrl = url.trim().replace(/\/+$/, "");
if (!/^[a-z]+:\/\//.test(cleanedUrl)) {
// Check for any protocol followed by "://"
cleanedUrl = "https://" + cleanedUrl;
}
return cleanedUrl;
};
url = sanitizeUrl(url);

const mintToAdd: Mint = { url: url, keys: [], keysets: [], nickname: addMintData.nickname };
const mintToAdd: Mint = {
url: url,
keys: [],
keysets: [],
nickname: addMintData.nickname,
};

// we have no mints at all
if (this.mints.length === 0) {
Expand Down Expand Up @@ -279,7 +324,12 @@ export const useMintsStore = defineStore("mints", {
this.addMintBlocking = false;
}
},
activateMintUrl: async function (url: string, verbose = false, force = false, unit: string | undefined = undefined) {
activateMintUrl: async function (
url: string,
verbose = false,
force = false,
unit: string | undefined = undefined,
) {
const mint = this.mints.filter((m) => m.url === url)[0];
if (mint) {
await this.activateMint(mint, verbose, force);
Expand Down Expand Up @@ -312,7 +362,6 @@ export const useMintsStore = defineStore("mints", {
worker.clearAllWorkers();
},
activateMint: async function (mint: Mint, verbose = false, force = false) {

if (mint.url === this.activeMintUrl && !force) {
return;
}
Expand All @@ -334,10 +383,7 @@ export const useMintsStore = defineStore("mints", {
if (verbose) {
await notifySuccess("Mint activated.");
}
console.log(
"### activateMint: Mint activated: ",
this.activeMintUrl,
);
console.log("### activateMint: Mint activated: ", this.activeMintUrl);
} catch (error: any) {
// restore previous values because the activation errored
this.activeMintUrl = previousUrl;
Expand All @@ -360,7 +406,7 @@ export const useMintsStore = defineStore("mints", {
console.error(error);
try {
notifyApiError(error, "Could not get mint info");
} catch { }
} catch {}
throw error;
}
},
Expand Down Expand Up @@ -388,7 +434,9 @@ export const useMintsStore = defineStore("mints", {
if (!mint.keys.find((k) => k.id === keyset.id)) {
const keys = await mintClass.api.getKeys(keyset.id);
// store keys in mint and update local storage
this.mints.filter((m) => m.url === mint.url)[0].keys.push(keys.keysets[0]);
this.mints
.filter((m) => m.url === mint.url)[0]
.keys.push(keys.keysets[0]);
}
}

Expand All @@ -398,12 +446,12 @@ export const useMintsStore = defineStore("mints", {
// this.mints.filter((m) => m.url === mint.url)[0].keys = keys.keysets;

// return the mint with keys set
return this.mints.filter((m) => m.url === mint.url)[0]
return this.mints.filter((m) => m.url === mint.url)[0];
} catch (error: any) {
console.error(error);
try {
notifyApiError(error, "Could not get mint keys");
} catch { }
} catch {}
throw error;
}
},
Expand All @@ -417,7 +465,7 @@ export const useMintsStore = defineStore("mints", {
console.error(error);
try {
notifyApiError(error, "Could not get mint keysets");
} catch { }
} catch {}
throw error;
}
},
Expand Down Expand Up @@ -458,5 +506,5 @@ export const useMintsStore = defineStore("mints", {
// }
// return null
// }
}
},
});
Loading

0 comments on commit b3eb590

Please sign in to comment.