Install forked cashu-ts lib

This commit is contained in:
minibits-cash
2024-07-15 15:59:09 +02:00
parent 463f3c8f83
commit 3013c83fdc
3 changed files with 5 additions and 388 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "minibits_wallet",
"version": "0.1.8-beta.35",
"version": "0.1.8-beta.36",
"private": true,
"scripts": {
"android:clean": "cd android && ./gradlew clean",
@@ -19,7 +19,7 @@
"postinstall": "patch-package --exclude 'nothing'"
},
"dependencies": {
"@cashu/cashu-ts": "1.0.0-rc.3",
"@cashu/cashu-ts": "minibits-cash/cashu-ts#v1.0.0-rc.9-minibits",
"@fortawesome/fontawesome-svg-core": "^6.5.2",
"@fortawesome/free-brands-svg-icons": "^6.5.2",
"@fortawesome/free-regular-svg-icons": "^6.5.2",
-382
View File
@@ -1,382 +0,0 @@
diff --git a/node_modules/@cashu/cashu-ts/package.json b/node_modules/@cashu/cashu-ts/package.json
index f2809bd..48176f1 100644
--- a/node_modules/@cashu/cashu-ts/package.json
+++ b/node_modules/@cashu/cashu-ts/package.json
@@ -2,8 +2,7 @@
"name": "@cashu/cashu-ts",
"version": "1.0.0-rc.3",
"description": "cashu library for communicating with a cashu mint",
- "main": "dist/lib/es5/index.js",
- "module": "dist/lib/es6/index.js",
+ "main": "src/index.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/cashubtc/cashu-ts"
diff --git a/node_modules/@cashu/cashu-ts/src/CashuMint.ts b/node_modules/@cashu/cashu-ts/src/CashuMint.ts
index 1491934..7852bd1 100644
--- a/node_modules/@cashu/cashu-ts/src/CashuMint.ts
+++ b/node_modules/@cashu/cashu-ts/src/CashuMint.ts
@@ -17,9 +17,9 @@ import type {
PostRestorePayload,
MeltQuotePayload,
MeltQuoteResponse
-} from './model/types/index.js';
-import request from './request.js';
-import { isObj, joinUrls } from './utils.js';
+} from './model/types/index';
+import request from './request';
+import { isObj, joinUrls } from './utils';
/**
* Class represents Cashu Mint API. This class contains Lower level functions that are implemented by CashuWallet.
@@ -81,6 +81,32 @@ class CashuMint {
async mintQuote(mintQuotePayload: MintQuotePayload): Promise<MintQuoteResponse> {
return CashuMint.mintQuote(this._mintUrl, mintQuotePayload, this._customRequest);
}
+ /**
+ * Retrieve if a mint quote has been paid
+ * @param mintUrl
+ * @param quote Quote ID.
+ * @param customRequest
+ * @returns MintQuoteResponse with paid boolean
+ */
+ public static async mintQuotePaid(
+ mintUrl: string,
+ quote: string,
+ customRequest?: typeof request
+ ): Promise<MintQuoteResponse & {paid: boolean}> {
+ const requestInstance = customRequest || request;
+ return requestInstance<MintQuoteResponse & {paid: boolean}>({
+ endpoint: joinUrls(mintUrl, '/v1/mint/quote/bolt11', quote)
+ });
+ }
+
+ /**
+ * Retrieve if a mint quote has been paid
+ * @param quote Quote ID.
+ * @returns MintQuoteResponse with paid boolean
+ */
+ async mintQuotePaid(quote: string): Promise<MintQuoteResponse & {paid: boolean}> {
+ return CashuMint.mintQuotePaid(this._mintUrl, quote, this._customRequest);
+ }
/**
* Requests the mint to perform token minting after the LN invoice has been paid
* @param mintUrl
diff --git a/node_modules/@cashu/cashu-ts/src/CashuWallet.ts b/node_modules/@cashu/cashu-ts/src/CashuWallet.ts
index 6d3a0b4..e4cd79b 100644
--- a/node_modules/@cashu/cashu-ts/src/CashuWallet.ts
+++ b/node_modules/@cashu/cashu-ts/src/CashuWallet.ts
@@ -1,7 +1,7 @@
import { bytesToHex, randomBytes } from '@noble/hashes/utils';
-import { CashuMint } from './CashuMint.js';
-import * as dhke from './DHKE.js';
-import { BlindedMessage } from './model/BlindedMessage.js';
+import { CashuMint } from './CashuMint';
+import * as dhke from './DHKE';
+import { BlindedMessage } from './model/BlindedMessage';
import {
type AmountPreference,
type BlindedMessageData,
@@ -21,15 +21,15 @@ import {
type Token,
type TokenEntry,
CheckStateEnum
-} from './model/types/index.js';
+} from './model/types/index';
import {
bytesToNumber,
cleanToken,
getDecodedToken,
getDefaultAmountPreference,
splitAmount
-} from './utils.js';
-import { deriveBlindingFactor, deriveSecret, deriveSeedFromMnemonic } from './secrets.js';
+} from './utils';
+import { deriveBlindingFactor, deriveSecret, deriveSeedFromMnemonic } from './secrets';
import { validateMnemonic } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
import { createP2PKsecret, getSignedProofs } from '@cashu/crypto/modules/client/NUT11';
@@ -106,6 +106,7 @@ class CashuWallet {
async receive(
token: string | Token,
options?: {
+ keysetId?: string;
preference?: Array<AmountPreference>;
counter?: number;
pubkey?: string;
@@ -113,6 +114,7 @@ class CashuWallet {
}
): Promise<ReceiveResponse> {
let decodedToken: Array<TokenEntry>;
+ let errors: string[] = []
if (typeof token === 'string') {
decodedToken = cleanToken(getDecodedToken(token)).token;
} else {
@@ -125,25 +127,27 @@ class CashuWallet {
continue;
}
try {
- const { proofs, proofsWithError } = await this.receiveTokenEntry(tokenEntry, {
+ const { proofs, proofsWithError, error } = await this.receiveTokenEntry(tokenEntry, {
+ keysetId: options?.keysetId,
preference: options?.preference,
counter: options?.counter,
pubkey: options?.pubkey,
privkey: options?.privkey
});
+ if(error) {errors.push(error);}
if (proofsWithError?.length) {
tokenEntriesWithError.push(tokenEntry);
continue;
}
tokenEntries.push({ mint: tokenEntry.mint, proofs: [...proofs] });
- } catch (error) {
- console.error(error);
- tokenEntriesWithError.push(tokenEntry);
+ } catch (error: any) {
+ errors.push(error)
}
}
return {
token: { token: tokenEntries },
- tokensWithErrors: tokenEntriesWithError.length ? { token: tokenEntriesWithError } : undefined
+ tokensWithErrors: tokenEntriesWithError.length ? { token: tokenEntriesWithError } : undefined,
+ errors
};
}
@@ -159,6 +163,7 @@ class CashuWallet {
async receiveTokenEntry(
tokenEntry: TokenEntry,
options?: {
+ keysetId?: string;
preference?: Array<AmountPreference>;
counter?: number;
pubkey?: string;
@@ -167,13 +172,14 @@ class CashuWallet {
): Promise<ReceiveTokenEntryResponse> {
const proofsWithError: Array<Proof> = [];
const proofs: Array<Proof> = [];
+ let err: string | undefined = undefined;
try {
const amount = tokenEntry.proofs.reduce((total, curr) => total + curr.amount, 0);
let preference = options?.preference;
if (!preference) {
preference = getDefaultAmountPreference(amount);
}
- const keys = await this.getKeys();
+ const keys = await this.getKeys(options?.keysetId);
const { payload, blindedMessages } = this.createSplitPayload(
amount,
tokenEntry.proofs,
@@ -192,12 +198,13 @@ class CashuWallet {
);
proofs.push(...newProofs);
} catch (error) {
- console.error(error);
+ err = String(error);
proofsWithError.push(...tokenEntry.proofs);
}
return {
proofs,
- proofsWithError: proofsWithError.length ? proofsWithError : undefined
+ proofsWithError: proofsWithError.length ? proofsWithError : undefined,
+ error: err
};
}
@@ -217,6 +224,7 @@ class CashuWallet {
amount: number,
proofs: Array<Proof>,
options?: {
+ keysetId?: string;
preference?: Array<AmountPreference>;
counter?: number;
pubkey?: string;
@@ -226,7 +234,7 @@ class CashuWallet {
if (options?.preference) {
amount = options?.preference?.reduce((acc, curr) => acc + curr.amount * curr.count, 0);
}
- const keyset = await this.getKeys();
+ const keyset = await this.getKeys(options?.keysetId);
let amountAvailable = 0;
const proofsToSend: Array<Proof> = [];
const proofsToKeep: Array<Proof> = [];
@@ -317,7 +325,7 @@ class CashuWallet {
/**
* Initialize the wallet with the mints public keys
*/
- private async getKeys(keysetId?: string, unit?: string): Promise<MintKeys> {
+ async getKeys(keysetId?: string, unit?: string): Promise<MintKeys> {
if (!this._keys || this._keys.id !== keysetId) {
const allKeys = await this.mint.getKeys(keysetId);
let keys;
@@ -564,7 +572,7 @@ class CashuWallet {
* @param proofs (only the 'Y' field is required)
* @returns
*/
- async checkProofsSpent<T extends { secret: string }>(proofs: Array<T>): Promise<Array<T>> {
+ async checkProofsSpent<T extends { secret: string }>(proofs: Array<T>): Promise<{spent: Array<T>, pending: Array<T>}> {
const enc = new TextEncoder();
const Ys = proofs.map((p) => dhke.hashToCurve(enc.encode(p.secret)).toHex(true));
const payload = {
@@ -573,11 +581,20 @@ class CashuWallet {
};
const { states } = await this.mint.check(payload);
- return proofs.filter((_, i) => {
+ const spent = proofs.filter((_, i) => {
const state = states.find((state) => state.Y === Ys[i]);
return state && state.state === CheckStateEnum.SPENT;
});
+
+ const pending = proofs.filter((_, i) => {
+ const state = states.find((state) => state.Y === Ys[i]);
+ return state && state.state === CheckStateEnum.PENDING;
+ });
+
+ return {spent, pending}
}
+
+
private splitReceive(
amount: number,
amountAvailable: number
diff --git a/node_modules/@cashu/cashu-ts/src/DHKE.ts b/node_modules/@cashu/cashu-ts/src/DHKE.ts
index da1dfe6..99f60a2 100644
--- a/node_modules/@cashu/cashu-ts/src/DHKE.ts
+++ b/node_modules/@cashu/cashu-ts/src/DHKE.ts
@@ -1,7 +1,7 @@
import { ProjPointType } from '@noble/curves/abstract/weierstrass';
import { secp256k1 } from '@noble/curves/secp256k1';
-import { MintKeys, Proof, SerializedBlindedSignature } from './model/types/index.js';
-import { bytesToNumber } from './utils.js';
+import { MintKeys, Proof, SerializedBlindedSignature } from './model/types/index';
+import { bytesToNumber } from './utils';
import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { Buffer } from 'buffer/';
diff --git a/node_modules/@cashu/cashu-ts/src/index.ts b/node_modules/@cashu/cashu-ts/src/index.ts
index f1c49f0..a8147a2 100644
--- a/node_modules/@cashu/cashu-ts/src/index.ts
+++ b/node_modules/@cashu/cashu-ts/src/index.ts
@@ -1,10 +1,10 @@
-import { CashuMint } from './CashuMint.js';
-import { CashuWallet } from './CashuWallet.js';
-import { setGlobalRequestOptions } from './request.js';
-import { generateNewMnemonic, deriveSeedFromMnemonic } from './secrets.js';
-import { getEncodedToken, getDecodedToken, deriveKeysetId } from './utils.js';
+import { CashuMint } from './CashuMint';
+import { CashuWallet } from './CashuWallet';
+import { setGlobalRequestOptions } from './request';
+import { generateNewMnemonic, deriveSeedFromMnemonic } from './secrets';
+import { getEncodedToken, getDecodedToken, deriveKeysetId } from './utils';
-export * from './model/types/index.js';
+export * from './model/types/index';
export {
CashuMint,
diff --git a/node_modules/@cashu/cashu-ts/src/model/BlindedMessage.ts b/node_modules/@cashu/cashu-ts/src/model/BlindedMessage.ts
index 9bd02f3..fe7e70b 100644
--- a/node_modules/@cashu/cashu-ts/src/model/BlindedMessage.ts
+++ b/node_modules/@cashu/cashu-ts/src/model/BlindedMessage.ts
@@ -1,4 +1,4 @@
-import { SerializedBlindedMessage } from './types/index.js';
+import { SerializedBlindedMessage } from './types/index';
import { ProjPointType } from '@noble/curves/abstract/weierstrass';
class BlindedMessage {
diff --git a/node_modules/@cashu/cashu-ts/src/model/BlindedSignature.ts b/node_modules/@cashu/cashu-ts/src/model/BlindedSignature.ts
index e998f32..16c4488 100644
--- a/node_modules/@cashu/cashu-ts/src/model/BlindedSignature.ts
+++ b/node_modules/@cashu/cashu-ts/src/model/BlindedSignature.ts
@@ -1,5 +1,5 @@
import { ProjPointType } from '@noble/curves/abstract/weierstrass';
-import { SerializedBlindedSignature } from './types/index.js';
+import { SerializedBlindedSignature } from './types/index';
class BlindedSignature {
id: string;
diff --git a/node_modules/@cashu/cashu-ts/src/model/Split.ts b/node_modules/@cashu/cashu-ts/src/model/Split.ts
index dab9010..ea21a04 100644
--- a/node_modules/@cashu/cashu-ts/src/model/Split.ts
+++ b/node_modules/@cashu/cashu-ts/src/model/Split.ts
@@ -1,5 +1,5 @@
-import { BlindedMessage } from './BlindedMessage.js';
-import { Proof } from './types/index.js';
+import { BlindedMessage } from './BlindedMessage';
+import { Proof } from './types/index';
class Split {
proofs: Array<Proof>;
diff --git a/node_modules/@cashu/cashu-ts/src/model/types/index.ts b/node_modules/@cashu/cashu-ts/src/model/types/index.ts
index 95f7d4e..03effa7 100644
--- a/node_modules/@cashu/cashu-ts/src/model/types/index.ts
+++ b/node_modules/@cashu/cashu-ts/src/model/types/index.ts
@@ -93,6 +93,10 @@ export type ReceiveTokenEntryResponse = {
* Proofs that could not be received. Doesn't throw an error, but if this field is populated it should be handled by the implementation accordingly
*/
proofsWithError: Array<Proof> | undefined;
+ /**
+ * Error
+ */
+ error?: string
};
/**
@@ -120,6 +124,10 @@ export type ReceiveResponse = {
* TokenEntries that had errors. No error will be thrown, but clients can choose to handle tokens with errors accordingly.
*/
tokensWithErrors: Token | undefined;
+ /**
+ * Errors
+ */
+ errors?: string[]
};
/**
@@ -384,6 +392,10 @@ export type Token = {
* token entries
*/
token: Array<TokenEntry>;
+ /**
+ * unit
+ */
+ unit?: string;
/**
* a message to send along with the token
*/
diff --git a/node_modules/@cashu/cashu-ts/src/request.ts b/node_modules/@cashu/cashu-ts/src/request.ts
index d74630a..dab9bf7 100644
--- a/node_modules/@cashu/cashu-ts/src/request.ts
+++ b/node_modules/@cashu/cashu-ts/src/request.ts
@@ -1,3 +1,4 @@
+import { isObj } from './utils';
import { HttpResponseError } from './model/Errors';
type RequestArgs = {
@@ -37,7 +38,8 @@ async function _request({
// expecting: { error: '', code: 0 }
// or: { detail: '' } (cashuBtc via pythonApi)
const { error, detail } = await response.json().catch(() => ({ error: 'bad response' }));
- throw new HttpResponseError(error || detail || 'bad response', response.status);
+
+ throw new HttpResponseError(isObj(error) ? JSON.stringify(error) : error || detail || 'bad response', response.status);
}
try {
diff --git a/node_modules/@cashu/cashu-ts/src/utils.ts b/node_modules/@cashu/cashu-ts/src/utils.ts
index 1729b99..5614b37 100644
--- a/node_modules/@cashu/cashu-ts/src/utils.ts
+++ b/node_modules/@cashu/cashu-ts/src/utils.ts
@@ -1,6 +1,6 @@
-import { encodeBase64ToJson, encodeJsonToBase64 } from './base64.js';
-import { AmountPreference, Keys, Proof, Token, TokenEntry, TokenV2 } from './model/types/index.js';
-import { TOKEN_PREFIX, TOKEN_VERSION } from './utils/Constants.js';
+import { encodeBase64ToJson, encodeJsonToBase64 } from './base64';
+import { AmountPreference, Keys, Proof, Token, TokenEntry, TokenV2 } from './model/types/index';
+import { TOKEN_PREFIX, TOKEN_VERSION } from './utils/Constants';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { sha256 } from '@noble/hashes/sha256';
+3 -4
View File
@@ -1202,10 +1202,9 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
"@cashu/cashu-ts@1.0.0-rc.3":
version "1.0.0-rc.3"
resolved "https://registry.yarnpkg.com/@cashu/cashu-ts/-/cashu-ts-1.0.0-rc.3.tgz#6e064952cea74e0fc219f5b182321ec42a52d9d5"
integrity sha512-WE2sDCepPDLV2kA+QSqyBnWfXxabfmw5YQxV7kMyur2LGVxIFufzmvKFy1LimuOG3ArXmBy2rlRdPx9LZ5d0Yw==
"@cashu/cashu-ts@minibits-cash/cashu-ts#v1.0.0-rc.9-minibits":
version "1.0.0-rc.9-minibits"
resolved "https://codeload.github.com/minibits-cash/cashu-ts/tar.gz/9415955cce68ef86c653757b0599c77634085281"
dependencies:
"@cashu/crypto" "^0.2.6"
"@noble/curves" "^1.3.0"