Skip to content

Commit

Permalink
new createLockedMintQuote method and LockedMintQuote type for `mi…
Browse files Browse the repository at this point in the history
…ntProofs`
  • Loading branch information
lollerfirst committed Dec 26, 2024
1 parent 5367a91 commit 9d1da94
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
51 changes: 35 additions & 16 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import {
type SendResponse,
type SerializedBlindedMessage,
type SwapPayload,
type Token
type Token,
LockedMintQuote
} from './model/types/index.js';
import { SubscriptionCanceller } from './model/types/wallet/websocket.js';
import {
Expand Down Expand Up @@ -605,12 +606,27 @@ class CashuWallet {
* @param pubkey optional public key to lock the quote to
* @returns the mint will return a mint quote with a Lightning invoice for minting tokens of the specified amount and unit
*/
async createMintQuote(amount: number, description?: string, pubkey?: string) {
if (pubkey) {
const { supported } = (await this.getMintInfo()).isSupported(20);
if (!supported) {
throw new Error('Mint does not support NUT-20');
}
async createMintQuote(amount: number, description?: string) {
const mintQuotePayload: MintQuotePayload = {
unit: this._unit,
amount: amount,
description: description,
};
return await this.mint.createMintQuote(mintQuotePayload);
}

/**
* Requests a mint quote from the mint that is locked to a public key.
* @param amount Amount requesting for mint.
* @param pubkey public key to lock the quote to
* @param description optional description for the mint quote
* @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) {
const { supported } = (await this.getMintInfo()).isSupported(20);
if (!supported) {
throw new Error('Mint does not support NUT-20');
}
const mintQuotePayload: MintQuotePayload = {
unit: this._unit,
Expand All @@ -619,7 +635,7 @@ class CashuWallet {
pubkey: pubkey
};
return await this.mint.createMintQuote(mintQuotePayload);
}
}

/**
* Gets an existing mint quote from the mint.
Expand All @@ -633,37 +649,40 @@ class CashuWallet {
/**
* Mint proofs for a given mint quote
* @param amount amount to request
* @param quote ID of mint quote
* @param {string} quote - ID of mint quote (when quote is a string)
* @param {LockedMintQuote} quote - containing the quote ID and unlocking private key (when quote is a LockedMintQuote)
* @param {MintProofOptions} [options] - Optional parameters for configuring the Mint Proof operation
* @returns proofs
*/
async mintProofs(
amount: number,
quote: string,
quote: string | LockedMintQuote,
options?: MintProofOptions
): Promise<Array<Proof>> {
let { keysetId, proofsWeHave, outputAmounts, counter, pubkey, quotePrivkey } = options || {};
let { keysetId, proofsWeHave, outputAmounts, counter, pubkey } = options || {};
const keyset = await this.getKeys(keysetId);
if (!outputAmounts && proofsWeHave) {
outputAmounts = {
keepAmounts: getKeepAmounts(proofsWeHave, amount, keyset.keys, this._denominationTarget),
sendAmounts: []
};
}

const { blindedMessages, secrets, blindingFactors } = this.createRandomBlindedMessages(
amount,
keyset,
outputAmounts?.keepAmounts,
counter,
pubkey
);
const mintQuoteSignature = quotePrivkey
? signMintQuote(quotePrivkey, quote, blindedMessages)
: undefined;
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: quote,
quote: quoteId,
signature: mintQuoteSignature
};
const { signatures } = await this.mint.mint(mintPayload);
Expand Down
7 changes: 5 additions & 2 deletions src/model/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type OutputAmounts = {
keepAmounts?: Array<number>;
};

export type LockedMintQuote = {
id: string;
privkey: string;
};

/**
* @param {ReceiveOptions} [options] - Optional configuration for token processing:
* - `keysetId`: Override the default keyset ID with a custom one fetched from the `/keysets` endpoint.
Expand Down Expand Up @@ -85,15 +90,13 @@ export type RestoreOptions = {
* - `counter`: optionally set counter to derive secret deterministically. CashuWallet class must be initialized with seed phrase to take effect
* - `proofsWeHave`: optionally provide all currently stored proofs of this mint. Cashu-ts will use them to derive the optimal output amounts
* - `pubkey`: optionally locks ecash to pubkey. Will not be deterministic, even if counter is set!
* - `privkey`: optional private key to unlock the mint quote. Generates a schnorr signature of the payload.
*/
export type MintProofOptions = {
keysetId?: string;
outputAmounts?: OutputAmounts;
proofsWeHave?: Array<Proof>;
counter?: number;
pubkey?: string;
quotePrivkey?: string;
};

/**
Expand Down

0 comments on commit 9d1da94

Please sign in to comment.