Skip to content

Commit

Permalink
Merge pull request cashubtc#36 from cashubtc/cashu-ts
Browse files Browse the repository at this point in the history
Cashu ts
  • Loading branch information
ebrakke authored May 11, 2023
2 parents 22ab724 + 85ac92a commit f69c7a4
Show file tree
Hide file tree
Showing 10 changed files with 3,189 additions and 1,830 deletions.
4,661 changes: 3,040 additions & 1,621 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@capacitor/clipboard": "^4.1.0",
"@cashu/cashu-ts": "^0.8.0-rc.3",
"@chenfengyuan/vue-qrcode": "^2.0.0",
"@noble/hashes": "^1.3.0",
"@noble/secp256k1": "^1.7.1",
Expand Down
2 changes: 1 addition & 1 deletion quasar.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = configure(function (/* ctx */) {
// app boot file (/src/boot)
// --> boot files are part of "main.js"
// https://v2.quasar.dev/quasar-cli/boot-files
boot: ["base", "global-components"],
boot: ["base", "global-components", "cashu"],

// https://v2.quasar.dev/quasar-cli-vite/quasar-config-js#css
css: ["app.scss", "base.scss"],
Expand Down
11 changes: 11 additions & 0 deletions src/boot/cashu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Configures the Cashu-ts library axios client
*/
import { setupAxios } from "@cashu/cashu-ts";

export default () => {
setupAxios({
// Default timeout for any interaction using the cashu-ts library to interact with a mint
timeout: 15 * 1000, // 15 seconds
});
};
32 changes: 22 additions & 10 deletions src/js/notify.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Notify } from 'quasar'
import { Notify } from "quasar";
import axios, { AxiosError } from "axios";

function notifyApiError(error) {
let types = {
400: "warning",
401: "warning",
500: "negative",
};
if (axios.isAxiosError(error)) {
notifyAxiosError(error);
return;
}
Notify.create({
timeout: 5000,
type: types[error.response.status] || "warning",
message:
error.response.data.message || error.response.data.detail || null,
message: error.response.data.message || error.response.data.detail || null,
caption:
[error.response.status, " ", error.response.statusText]
.join("")
Expand All @@ -19,6 +23,20 @@ function notifyApiError(error) {
});
}

/**
* Cashu-TS will return axios errors when certain calls fail, so we should handle those
* @param {AxiosError} error
*/
function notifyAxiosError(error) {
Notify.create({
timeout: 5000,
type: types[error.status] || "warning",
message: error.message,
caption: error.code,
icon: null,
});
}

async function notifySuccess(message, position = "top") {
Notify.create({
timeout: 5000,
Expand Down Expand Up @@ -96,10 +114,4 @@ async function notify(
});
}

export {
notifyApiError,
notifySuccess,
notifyError,
notifyWarning,
notify,
}
export { notifyApiError, notifySuccess, notifyError, notifyWarning, notify };
81 changes: 2 additions & 79 deletions src/js/token.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDecodedToken } from "@cashu/cashu-ts";
/**
* Functions related to cashu tokens
* @typedef {{C: string, amount: number, id: number, secret: number}} Proof
Expand All @@ -13,11 +14,7 @@ export default { decode, getProofs, getMint };
*/
function decode(encoded_token) {
if (!encoded_token || encoded_token === "") return "";
encoded_token = cropPrefixes(encoded_token);
if (isV2Token(encoded_token)) return parseV2Token(encoded_token);
if (isV3Token(encoded_token)) return parseV3Token(encoded_token);

throw new Error("Unknown token format");
return getDecodedToken(encoded_token);
}

/**
Expand Down Expand Up @@ -49,77 +46,3 @@ function getMint(decoded_token) {
return "";
}
}

/**
* @param {string} token
* @returns {boolean}
*/
function isV2Token(token) {
return token.startsWith("eyJwcm9");
}

/**
*
* @param {string} token
* @returns {boolean}
*/
function isV3Token(token) {
return token.startsWith("cashuA");
}

/**
* @param {string} encoded_token
* @returns {{token: Token[]}}
*/
function parseV3Token(encoded_token) {
let prefix = "cashuA";
let token_parsed = encoded_token.slice(prefix.length);
let tokenJson = getTokenJSON(token_parsed);
if (
!(tokenJson.token.length > 0) ||
!(tokenJson.token[0].proofs.length > 0)
) {
throw new Error("No proofs in encoded token");
}
return tokenJson;
}

/**
* @param {string} encoded_token
* @returns {{token: Token[]}}
*/
function parseV2Token(encoded_token) {
let tokenV2 = getTokenJSON(encoded_token);
let newToken = {
token: [
{
proofs: tokenV2.proofs,
mint: tokenV2.mints[0].url,
},
],
};
return newToken;
}

/**
* @param {string} token
* @returns {{token: Token[]}}
*/
function getTokenJSON(token) {
return JSON.parse(atob(token));
}

/**
*
* @param {string} token
* @returns {string}
*/
function cropPrefixes(token) {
let uriPrefixes = ["web+cashu://", "cashu://", "cashu:"];
uriPrefixes.forEach((prefix) => {
if (token.startsWith(prefix)) {
token = token.slice(prefix.length);
}
});
return token;
}
7 changes: 6 additions & 1 deletion src/js/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { date } from "quasar";
import * as nobleSecp256k1 from "@noble/secp256k1";

function splitAmount(value) {
Expand All @@ -24,4 +25,8 @@ function hexToNumber(hex) {
return BigInt(`0x${hex}`);
}

export { splitAmount, bytesToNumber, bigIntStringify };
function currentDateStr() {
return date.formatDate(new Date(), "YYYY-MM-DD HH:mm:ss");
}

export { splitAmount, bytesToNumber, bigIntStringify, currentDateStr };
Loading

0 comments on commit f69c7a4

Please sign in to comment.