Skip to content

Commit

Permalink
added type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Dec 27, 2024
1 parent 3910250 commit 468d433
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
63 changes: 44 additions & 19 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
type SerializedBlindedMessage,
type SwapPayload,
type Token,
LockedMintQuote
LockedMintQuoteReponse
} from './model/types/index.js';
import { SubscriptionCanceller } from './model/types/wallet/websocket.js';
import {
Expand Down Expand Up @@ -260,7 +260,7 @@ class CashuWallet {
* @returns New token with newly created proofs, token entries that had errors
*/
async receive(token: string | Token, options?: ReceiveOptions): Promise<Array<Proof>> {
let { requireDleq, keysetId, outputAmounts, counter, pubkey, privkey } = options || {};
const { requireDleq, keysetId, outputAmounts, counter, pubkey, privkey } = options || {};

if (typeof token === 'string') {
token = getDecodedToken(token);
Expand Down Expand Up @@ -299,7 +299,7 @@ class CashuWallet {
* @returns {SendResponse}
*/
async send(amount: number, proofs: Array<Proof>, options?: SendOptions): Promise<SendResponse> {
let {
const {
proofsWeHave,
offline,
includeFees,
Expand Down Expand Up @@ -572,7 +572,7 @@ class CashuWallet {
count: number,
options?: RestoreOptions
): Promise<{ proofs: Array<Proof> }> {
let { keysetId } = options || {};
const { keysetId } = options || {};
const keys = await this.getKeys(keysetId);
if (!this._seed) {
throw new Error('CashuWallet must be initialized with a seed to use restore');
Expand Down Expand Up @@ -623,7 +623,11 @@ class CashuWallet {
* @returns the mint will return a mint quote with a Lightning invoice for minting tokens of the specified amount and unit.
* The quote will be locked to the specified `pubkey`.
*/
async createLockedMintQuote(amount: number, pubkey: string, description?: string) {
async createLockedMintQuote(
amount: number,
pubkey: string,
description?: string
): Promise<LockedMintQuoteReponse> {
const { supported } = (await this.getMintInfo()).isSupported(20);
if (!supported) {
throw new Error('Mint does not support NUT-20');
Expand All @@ -634,7 +638,11 @@ class CashuWallet {
description: description,
pubkey: pubkey
};
return await this.mint.createMintQuote(mintQuotePayload);
const res = await this.mint.createMintQuote(mintQuotePayload);
if (!res.pubkey) {
throw new Error('Mint returned unlocked mint quote');
}
return res as LockedMintQuoteReponse;
}

/**
Expand All @@ -656,10 +664,20 @@ class CashuWallet {
*/
async mintProofs(
amount: number,
quote: string | LockedMintQuote,
quote: MintQuoteResponse,
options: MintProofOptions & { privateKey: string }
): Promise<Array<Proof>>;
async mintProofs(
amount: number,
quote: string,
options?: MintProofOptions
): Promise<Array<Proof>>;
async mintProofs(
amount: number,
quote: string | MintQuoteResponse,
options?: MintProofOptions & { privateKey?: string }
): Promise<Array<Proof>> {
let { keysetId, proofsWeHave, outputAmounts, counter, pubkey } = options || {};
let { keysetId, proofsWeHave, outputAmounts, counter, pubkey, privateKey } = options || {};
const keyset = await this.getKeys(keysetId);
if (!outputAmounts && proofsWeHave) {
outputAmounts = {
Expand All @@ -674,16 +692,23 @@ class CashuWallet {
counter,
pubkey
);
const mintQuoteSignature =
typeof quote === 'string'
? undefined
: signMintQuote(quote.privkey, quote.id, blindedMessages);
const quoteId = typeof quote === 'string' ? quote : quote.id;
const mintPayload: MintPayload = {
outputs: blindedMessages,
quote: quoteId,
signature: mintQuoteSignature
};
let mintPayload: MintPayload;
if (typeof quote !== 'string') {
if (!privateKey) {
throw new Error('Can not sign locked quote without private key');
}
const mintQuoteSignature = signMintQuote(privateKey, quote.quote, blindedMessages);
mintPayload = {
outputs: blindedMessages,
quote: quote.quote,
signature: mintQuoteSignature
};
} else {
mintPayload = {
outputs: blindedMessages,
quote: quote
};
}
const { signatures } = await this.mint.mint(mintPayload);
return this.constructProofs(signatures, blindingFactors, secrets, keyset);
}
Expand Down Expand Up @@ -725,7 +750,7 @@ class CashuWallet {
proofsToSend: Array<Proof>,
options?: MeltProofOptions
): Promise<MeltProofsResponse> {
let { keysetId, counter, privkey } = options || {};
const { keysetId, counter, privkey } = options || {};
const keys = await this.getKeys(keysetId);
const { blindedMessages, secrets, blindingFactors } = this.createBlankOutputs(
sumProofs(proofsToSend) - meltQuote.amount,
Expand Down
2 changes: 2 additions & 0 deletions src/model/types/mint/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ export type MintQuoteResponse = {
pubkey?: string;
} & ApiError;

export type LockedMintQuoteReponse = MintQuoteResponse & { pubkey: string };

/**
* Response from the mint after requesting a mint
*/
Expand Down

0 comments on commit 468d433

Please sign in to comment.