Skip to content

Commit

Permalink
Merge branch 'development' into blinding
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Dec 4, 2024
2 parents c898683 + 02ea20e commit 2707a4b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cashu/cashu-ts",
"version": "2.0.0-rc3",
"version": "2.0.0-rc4",
"description": "cashu library for communicating with a cashu mint",
"main": "dist/lib/es5/index.js",
"module": "dist/lib/es6/index.js",
Expand Down
12 changes: 7 additions & 5 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { getSignedProofs } from '@cashu/crypto/modules/client/NUT11';
import { type Proof as NUT11Proof } from '@cashu/crypto/modules/common/index';
import { SubscriptionCanceller } from './model/types/wallet/websocket.js';
import { BlindingData, BlindingDataLike } from './model/BlindingData.js';
import { MintInfo } from './model/MintInfo.js';
/**
* The default number of proofs per denomination to keep in a wallet.
*/
Expand All @@ -53,7 +54,7 @@ class CashuWallet {
private _keysets: Array<MintKeyset> = [];
private _seed: Uint8Array | undefined = undefined;
private _unit = DEFAULT_UNIT;
private _mintInfo: GetInfoResponse | undefined = undefined;
private _mintInfo: MintInfo | undefined = undefined;
private _denominationTarget = DEFAULT_DENOMINATION_TARGET;

mint: CashuMint;
Expand Down Expand Up @@ -89,7 +90,7 @@ class CashuWallet {
if (keys) keys.forEach((key: MintKeys) => this._keys.set(key.id, key));
if (options?.unit) this._unit = options?.unit;
if (options?.keysets) this._keysets = options.keysets;
if (options?.mintInfo) this._mintInfo = options.mintInfo;
if (options?.mintInfo) this._mintInfo = new MintInfo(options.mintInfo);
if (options?.denominationTarget) {
this._denominationTarget = options.denominationTarget;
}
Expand Down Expand Up @@ -121,7 +122,7 @@ class CashuWallet {
get keysets(): Array<MintKeyset> {
return this._keysets;
}
get mintInfo(): GetInfoResponse {
get mintInfo(): MintInfo {
if (!this._mintInfo) {
throw new Error('Mint info not loaded');
}
Expand All @@ -132,8 +133,9 @@ class CashuWallet {
* Get information about the mint
* @returns mint info
*/
async getMintInfo(): Promise<GetInfoResponse> {
this._mintInfo = await this.mint.getInfo();
async getMintInfo(): Promise<MintInfo> {
const infoRes = await this.mint.getInfo();
this._mintInfo = new MintInfo(infoRes);
return this._mintInfo;
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CashuMint } from './CashuMint.js';
import { CashuWallet } from './CashuWallet.js';
import { PaymentRequest } from './model/PaymentRequest.js';
import { WSConnection } from './WSConnection.js';
import { setGlobalRequestOptions } from './request.js';
import {
getEncodedToken,
Expand All @@ -24,3 +23,5 @@ export {
deriveKeysetId,
setGlobalRequestOptions
};

export { injectWebSocketImpl } from './ws.js';
97 changes: 97 additions & 0 deletions src/model/MintInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { GetInfoResponse, MPPMethod, SwapMethod, WebSocketSupport } from './types';

export class MintInfo {
private readonly _mintInfo: GetInfoResponse;

constructor(info: GetInfoResponse) {
this._mintInfo = info;
}

isSupported(num: 4 | 5): { disabled: boolean; params: Array<SwapMethod> };
isSupported(num: 7 | 8 | 9 | 10 | 11 | 12 | 14): { supported: boolean };
isSupported(num: 17): { supported: boolean; params?: Array<WebSocketSupport> };
isSupported(num: 15): { supported: boolean; params?: Array<MPPMethod> };
isSupported(num: number) {
switch (num) {
case 4:
case 5: {
return this.checkMintMelt(num);
}
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 14: {
return this.checkGenericNut(num);
}
case 17: {
return this.checkNut17();
}
case 15: {
return this.checkNut15();
}
default: {
throw new Error('nut is not supported by cashu-ts');
}
}
}
private checkGenericNut(num: 7 | 8 | 9 | 10 | 11 | 12 | 14) {
if (this._mintInfo.nuts[num]?.supported) {
return { supported: true };
}
return { supported: false };
}
private checkMintMelt(num: 4 | 5) {
const mintMeltInfo = this._mintInfo.nuts[num];
if (mintMeltInfo && mintMeltInfo.methods.length > 0 && !mintMeltInfo.disabled) {
return { disabled: false, params: mintMeltInfo.methods };
}
return { disabled: true, params: mintMeltInfo.methods };
}
private checkNut17() {
if (this._mintInfo.nuts[17] && this._mintInfo.nuts[17].supported.length > 0) {
return { supported: true, params: this._mintInfo.nuts[17].supported };
}
return { supported: false };
}
private checkNut15() {
if (this._mintInfo.nuts[15] && this._mintInfo.nuts[15].methods.length > 0) {
return { supported: true, params: this._mintInfo.nuts[15].methods };
}
return { supported: false };
}

get contact() {
return this._mintInfo.contact;
}

get description() {
return this._mintInfo.description;
}

get description_long() {
return this._mintInfo.description_long;
}

get name() {
return this._mintInfo.name;
}

get pubkey() {
return this._mintInfo.pubkey;
}

get nuts() {
return this._mintInfo.nuts;
}

get version() {
return this._mintInfo.version;
}

get motd() {
return this._mintInfo.motd;
}
}
17 changes: 14 additions & 3 deletions test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { getDecodedToken } from '../src/utils.js';
import { Server, WebSocket } from 'mock-socket';
import { injectWebSocketImpl } from '../src/ws.js';
import { MintInfo } from '../src/model/MintInfo.js';

injectWebSocketImpl(WebSocket);

Expand Down Expand Up @@ -67,8 +68,18 @@ describe('test info', () => {
{ method: 'twitter', info: '@me' },
{ method: 'nostr', info: 'npub1337' }
]);
expect(info.nuts?.['17']).toEqual({
supported: [
expect(info.isSupported(10)).toEqual({ supported: true });
expect(info.isSupported(5)).toEqual({
disabled: false,
params: [
{ method: 'bolt11', unit: 'sat' },
{ method: 'bolt11', unit: 'usd' },
{ method: 'bolt11', unit: 'eur' }
]
});
expect(info.isSupported(17)).toEqual({
supported: true,
params: [
{
method: 'bolt11',
unit: 'sat',
Expand All @@ -86,7 +97,7 @@ describe('test info', () => {
}
]
});
expect(info).toEqual(mintInfoResp);
expect(info).toEqual(new MintInfo(mintInfoResp));
});
test('test info with deprecated contact field', async () => {
// mintInfoRespDeprecated is the same as mintInfoResp but with the contact field in the old format
Expand Down

0 comments on commit 2707a4b

Please sign in to comment.