Skip to content

Commit

Permalink
Minibits wallet related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
minibits-cash committed Jul 15, 2024
1 parent 243a4a7 commit 9415955
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 28 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "@cashu/cashu-ts",
"version": "1.0.0-rc.9",
"version": "1.0.0-rc.9-minibits",
"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"
Expand Down
6 changes: 3 additions & 3 deletions src/CashuMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import type {
PostRestorePayload,
MeltQuotePayload,
MeltQuoteResponse
} from './model/types/index.js';
import request from './request.js';
import { isObj, joinUrls, sanitizeUrl } from './utils.js';
} from './model/types/index';
import request from './request';
import { isObj, joinUrls, sanitizeUrl } from './utils';

/**
* Class represents Cashu Mint API. This class contains Lower level functions that are implemented by CashuWallet.
Expand Down
28 changes: 18 additions & 10 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bytesToHex, randomBytes } from '@noble/hashes/utils';
import { CashuMint } from './CashuMint.js';
import { BlindedMessage } from './model/BlindedMessage.js';
import { CashuMint } from './CashuMint';
import { BlindedMessage } from './model/BlindedMessage';
import {
type AmountPreference,
type BlindedMessageData,
Expand All @@ -20,7 +20,7 @@ import {
type TokenEntry,
CheckStateEnum,
SerializedBlindedSignature
} from './model/types/index.js';
} from './model/types/index';
import {
bytesToNumber,
getDecodedToken,
Expand Down Expand Up @@ -134,8 +134,8 @@ class CashuWallet {
privkey: options?.privkey
});
return proofs;
} catch (error) {
throw new Error('Error when receiving');
} catch (error: any) {
throw new Error('Error when receiving: ' + error.message);
}
}

Expand Down Expand Up @@ -183,8 +183,8 @@ class CashuWallet {
keys
);
proofs.push(...newProofs);
} catch (error) {
throw new Error('Error receiving token entry');
} catch (error: any) {
throw new Error('Error receiving token entry: ' + error.message);
}
return proofs;
}
Expand Down Expand Up @@ -306,7 +306,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 || (keysetId !== undefined && this._keys.id !== keysetId)) {
const allKeys = await this.mint.getKeys(keysetId);
let keys;
Expand Down Expand Up @@ -577,7 +577,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) => hashToCurve(enc.encode(p.secret)).toHex(true));
const payload = {
Expand All @@ -586,11 +586,19 @@ 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
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CashuMint } from './CashuMint.js';
import { CashuWallet } from './CashuWallet.js';
import { setGlobalRequestOptions } from './request.js';
import { CashuMint } from './CashuMint';
import { CashuWallet } from './CashuWallet';
import { setGlobalRequestOptions } from './request';
import { generateNewMnemonic, deriveSeedFromMnemonic } from '@cashu/crypto/modules/client/NUT09';
import { getEncodedToken, getDecodedToken, deriveKeysetId } from './utils.js';
import { getEncodedToken, getDecodedToken, deriveKeysetId } from './utils';

export * from './model/types/index.js';
export * from './model/types/index';

export {
CashuMint,
Expand Down
2 changes: 1 addition & 1 deletion src/model/BlindedMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SerializedBlindedMessage } from './types/index.js';
import { SerializedBlindedMessage } from './types/index';
import { ProjPointType } from '@noble/curves/abstract/weierstrass';

class BlindedMessage {
Expand Down
2 changes: 1 addition & 1 deletion src/model/BlindedSignature.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/model/Split.ts
Original file line number Diff line number Diff line change
@@ -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>;
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down

0 comments on commit 9415955

Please sign in to comment.