All files / src CashuMint.ts

78.82% Statements 67/85
63.75% Branches 51/80
65.21% Functions 15/23
78.48% Lines 62/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331                            2x 2x         2x           3x 3x     2x 5x               2x                   2x                   2x                               2x                     2x           2x 2x           2x 1x     1x               2x 2x                 2x         16x   2x   16x 16x                 2x 16x               2x                       2x                     2x         11x 11x           9x 1x     8x             2x 5x                 2x         3x 3x           3x               3x             2x 3x                 2x         7x 7x           7x 1x     6x               2x 7x                 2x         1x 1x           1x       1x             2x 1x     2x                                     2x         2x   2x  
import {
	CheckSpendablePayload,
	CheckSpendableResponse,
	GetInfoResponse,
	MeltPayload,
	MeltResponse,
	MintKeys,
	PostRestoreResponse,
	RequestMintResponse,
	SerializedBlindedMessage,
	SerializedBlindedSignature,
	SplitPayload,
	SplitResponse
} from './model/types/index.js';
import request from './request.js';
import { isObj, joinUrls } from './utils.js';
 
/**
 * Class represents Cashu Mint API. This class contains Lower level functions that are implemented by CashuWallet.
 */
class CashuMint {
	/**
	 * @param _mintUrl requires mint URL to create this object
	 * @param _customRequest if passed, use custom request implementation for network communication with the mint
	 */
	constructor(
		private _mintUrl: string,
		private _customRequest?: typeof request
	) {}
 
	get mintUrl() {
		return this._mintUrl;
	}
 
	/**
	 * fetches mints info at the /info endpoint
	 * @param mintUrl
	 * @param customRequest
	 */
	public static async getInfo(
		mintUrl: string,
		customRequest?: typeof request
	): Promise<GetInfoResponse> {
		const requestInstance = customRequest || request;
		return requestInstance<GetInfoResponse>({ endpoint: joinUrls(mintUrl, 'info') });
	}
	/**
	 * fetches mints info at the /info endpoint
	 */
	async getInfo(): Promise<GetInfoResponse> {
		return CashuMint.getInfo(this._mintUrl, this._customRequest);
	}
	/**
	 * Starts a minting process by requesting an invoice from the mint
	 * @param mintUrl
	 * @param amount Amount requesting for mint.
	 * @param customRequest
	 * @returns the mint will create and return a Lightning invoice for the specified amount
	 */
	public static async requestMint(
		mintUrl: string,
		amount: number,
		customRequest?: typeof request
	): Promise<RequestMintResponse> {
		const requestInstance = customRequest || request;
		return requestInstance<RequestMintResponse>({
			endpoint: `${joinUrls(mintUrl, 'mint')}?amount=${amount}`
		});
	}
 
	/**
	 * Starts a minting process by requesting an invoice from the mint
	 * @param amount Amount requesting for mint.
	 * @returns the mint will create and return a Lightning invoice for the specified amount
	 */
	async requestMint(amount: number): Promise<RequestMintResponse> {
		return CashuMint.requestMint(this._mintUrl, amount, this._customRequest);
	}
	/**
	 * Requests the mint to perform token minting after the LN invoice has been paid
	 * @param mintUrl
	 * @param payloads outputs (Blinded messages) that can be written
	 * @param hash hash (id) used for by the mint to keep track of wether the invoice has been paid yet
	 * @param customRequest
	 * @returns serialized blinded signatures
	 */
	public static async mint(
		mintUrl: string,
		payloads: { outputs: Array<SerializedBlindedMessage> },
		hash: string,
		customRequest?: typeof request
	) {
		const requestInstance = customRequest || request;
		const data = await requestInstance<{ promises: Array<SerializedBlindedSignature> }>({
			endpoint: `${joinUrls(mintUrl, 'mint')}?hash=${hash}`,
			method: 'POST',
			requestBody: payloads
		});
 
		if (!isObj(data) || !Array.isArray(data?.promises)) {
			throw new Error('bad response');
		}
 
		return data;
	}
	/**
	 * Requests the mint to perform token minting after the LN invoice has been paid
	 * @param payloads outputs (Blinded messages) that can be written
	 * @param hash hash (id) used for by the mint to keep track of wether the invoice has been paid yet
	 * @returns serialized blinded signatures
	 */
	async mint(payloads: { outputs: Array<SerializedBlindedMessage> }, hash: string) {
		return CashuMint.mint(this._mintUrl, payloads, hash, this._customRequest);
	}
	/**
	 * Get the mints public keys
	 * @param mintUrl
	 * @param keysetId optional param to get the keys for a specific keyset. If not specified, the keys from the active keyset are fetched
	 * @param customRequest
	 * @returns
	 */
	public static async getKeys(
		mintUrl: string,
		keysetId?: string,
		customRequest?: typeof request
	): Promise<MintKeys> {
		if (keysetId) {
			// make the keysetId url safe
			keysetId = keysetId.replace(/\//g, '_').replace(/\+/g, '-');
		}
		const requestInstance = customRequest || request;
		return requestInstance<MintKeys>({
			endpoint: keysetId ? joinUrls(mintUrl, 'keys', keysetId) : joinUrls(mintUrl, 'keys')
		});
	}
	/**
	 * Get the mints public keys
	 * @param keysetId optional param to get the keys for a specific keyset. If not specified, the keys from the active keyset are fetched
	 * @returns the mints public keys
	 */
	async getKeys(keysetId?: string): Promise<MintKeys> {
		return CashuMint.getKeys(this._mintUrl, keysetId, this._customRequest);
	}
	/**
	 * Get the mints keysets in no specific order
	 * @param mintUrl
	 * @param customRequest
	 * @returns all the mints past and current keysets.
	 */
	public static async getKeySets(
		mintUrl: string,
		customRequest?: typeof request
	): Promise<{ keysets: Array<string> }> {
		const requestInstance = customRequest || request;
		return requestInstance<{ keysets: Array<string> }>({ endpoint: joinUrls(mintUrl, 'keysets') });
	}
 
	/**
	 * Get the mints keysets in no specific order
	 * @returns all the mints past and current keysets.
	 */
	async getKeySets(): Promise<{ keysets: Array<string> }> {
		return CashuMint.getKeySets(this._mintUrl, this._customRequest);
	}
 
	/**
	 * Ask mint to perform a split operation
	 * @param mintUrl
	 * @param splitPayload data needed for performing a token split
	 * @param customRequest
	 * @returns split tokens
	 */
	public static async split(
		mintUrl: string,
		splitPayload: SplitPayload,
		customRequest?: typeof request
	): Promise<SplitResponse> {
		const requestInstance = customRequest || request;
		const data = await requestInstance<SplitResponse>({
			endpoint: joinUrls(mintUrl, 'split'),
			method: 'POST',
			requestBody: splitPayload
		});
 
		if (!isObj(data) || !Array.isArray(data?.promises)) {
			throw new Error('bad response');
		}
 
		return data;
	}
	/**
	 * Ask mint to perform a split operation
	 * @param splitPayload data needed for performing a token split
	 * @returns split tokens
	 */
	async split(splitPayload: SplitPayload): Promise<SplitResponse> {
		return CashuMint.split(this._mintUrl, splitPayload, this._customRequest);
	}
	/**
	 * Ask mint to perform a melt operation. This pays a lightning invoice and destroys tokens matching its amount + fees
	 * @param mintUrl
	 * @param meltPayload
	 * @param customRequest
	 * @returns
	 */
	public static async melt(
		mintUrl: string,
		meltPayload: MeltPayload,
		customRequest?: typeof request
	): Promise<MeltResponse> {
		const requestInstance = customRequest || request;
		const data = await requestInstance<MeltResponse>({
			endpoint: joinUrls(mintUrl, 'melt'),
			method: 'POST',
			requestBody: meltPayload
		});
 
		Iif (
			!isObj(data) ||
			typeof data?.paid !== 'boolean' ||
			(data?.preimage !== null && typeof data?.preimage !== 'string')
		) {
			throw new Error('bad response');
		}
 
		return data;
	}
	/**
	 * Ask mint to perform a melt operation. This pays a lightning invoice and destroys tokens matching its amount + fees
	 * @param meltPayload
	 * @returns
	 */
	async melt(meltPayload: MeltPayload): Promise<MeltResponse> {
		return CashuMint.melt(this._mintUrl, meltPayload, this._customRequest);
	}
	/**
	 * Estimate fees for a given LN invoice
	 * @param mintUrl
	 * @param checkfeesPayload Payload containing LN invoice that needs to get a fee estimate
	 * @param customRequest
	 * @returns estimated Fee
	 */
	public static async checkFees(
		mintUrl: string,
		checkfeesPayload: { pr: string },
		customRequest?: typeof request
	): Promise<{ fee: number }> {
		const requestInstance = customRequest || request;
		const data = await requestInstance<{ fee: number }>({
			endpoint: joinUrls(mintUrl, 'checkfees'),
			method: 'POST',
			requestBody: checkfeesPayload
		});
 
		if (!isObj(data) || typeof data?.fee !== 'number') {
			throw new Error('bad response');
		}
 
		return data;
	}
	/**
	 * Estimate fees for a given LN invoice
	 * @param mintUrl
	 * @param checkfeesPayload Payload containing LN invoice that needs to get a fee estimate
	 * @returns estimated Fee
	 */
	async checkFees(checkfeesPayload: { pr: string }): Promise<{ fee: number }> {
		return CashuMint.checkFees(this._mintUrl, checkfeesPayload, this._customRequest);
	}
	/**
	 * Checks if specific proofs have already been redeemed
	 * @param mintUrl
	 * @param checkPayload
	 * @param customRequest
	 * @returns redeemed and unredeemed ordered list of booleans
	 */
	public static async check(
		mintUrl: string,
		checkPayload: CheckSpendablePayload,
		customRequest?: typeof request
	): Promise<CheckSpendableResponse> {
		const requestInstance = customRequest || request;
		const data = await requestInstance<CheckSpendableResponse>({
			endpoint: joinUrls(mintUrl, 'check'),
			method: 'POST',
			requestBody: checkPayload
		});
 
		Iif (!isObj(data) || !Array.isArray(data?.spendable)) {
			throw new Error('bad response');
		}
 
		return data;
	}
	/**
	 * Checks if specific proofs have already been redeemed
	 * @param checkPayload
	 * @returns redeemed and unredeemed ordered list of booleans
	 */
	async check(checkPayload: CheckSpendablePayload): Promise<CheckSpendableResponse> {
		return CashuMint.check(this._mintUrl, checkPayload, this._customRequest);
	}
 
	public static async restore(
		mintUrl: string,
		restorePayload: { outputs: Array<SerializedBlindedMessage> },
		customRequest?: typeof request
	): Promise<PostRestoreResponse> {
		const requestInstance = customRequest || request;
		const data = await requestInstance<PostRestoreResponse>({
			endpoint: joinUrls(mintUrl, 'restore'),
			method: 'POST',
			requestBody: restorePayload
		});
 
		Iif (!isObj(data) || !Array.isArray(data?.outputs) || !Array.isArray(data?.promises)) {
			throw new Error('bad response');
		}
 
		return data;
	}
 
	async restore(restorePayload: {
		outputs: Array<SerializedBlindedMessage>;
	}): Promise<PostRestoreResponse> {
		return CashuMint.restore(this._mintUrl, restorePayload, this._customRequest);
	}
}
 
export { CashuMint };