Skip to content

Commit

Permalink
added on proofStateUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Nov 4, 2024
1 parent 4c315df commit 71e2e47
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,27 @@ class CashuWallet {
};
}

async onProofStateUpdates(
proofs: Array<Proof>,
callback: (payload: ProofState) => void,
errorCallback: (e: Error) => void
): Promise<SubscriptionCanceller> {
await this.mint.connectWebSocket();
if (!this.mint.webSocketConnection) {
throw new Error('failed to establish WebSocket connection.');
}
const enc = new TextEncoder();
const ys = proofs.map((p: Proof) => hashToCurve(enc.encode(p.secret)).toHex(true));
const subId = this.mint.webSocketConnection.createSubscription(
{ kind: 'proof_state', filters: ys },
callback,
errorCallback
);
return () => {
this.mint.webSocketConnection?.cancelSubscription(subId, callback);
};
}

/**
* Creates blinded messages for a given amount
* @param amount amount to create blinded messages for
Expand Down
36 changes: 35 additions & 1 deletion test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
CheckStateEnum,
MeltQuoteState,
MintQuotePayload,
MintQuoteState
MintQuoteResponse,
MintQuoteState,
ProofState
} from '../src/model/types/index.js';
import ws from 'ws';
import { injectWebSocketImpl } from '../src/ws.js';
Expand Down Expand Up @@ -321,4 +323,36 @@ describe('mint api', () => {
expect(callbackRef).toHaveBeenCalledTimes(4);
expect(mint.webSocketConnection?.activeSubscriptions.length).toBe(0);
});
test('websocket proof state + mint quote updates', async () => {
const mint = new CashuMint(mintUrl);
const wallet = new CashuWallet(mint);

const quote = await wallet.createMintQuote(63);
await new Promise<void>((res) => {
function handleUpdate(p: MintQuoteResponse) {
if (p.state === MintQuoteState.PAID) {
res();
}
}
wallet.onMintQuoteUpdates([quote.quote], handleUpdate, (e) => {
console.log(e);
});
});
const { proofs } = await wallet.mintProofs(63, quote.quote);
const data = await new Promise<ProofState>((res) => {
wallet.onProofStateUpdates(
proofs,
(p) => {
if (p.state === CheckStateEnum.SPENT) {
res(p);
}
},
(e) => {
console.log(e);
}
);
wallet.swap(63, proofs);
});
console.log('final ws boss', data);
});
});

0 comments on commit 71e2e47

Please sign in to comment.