Skip to content

Commit

Permalink
parent 6a881c3
Browse files Browse the repository at this point in the history
author misovan <[email protected]> 1696800460 +0200
committer misovan <[email protected]> 1705581162 +0100

Add custom request option

Prettier

Fix method injection

Temp fix of build inside Minibits wallet

Temp fix of build of Minibits wallet

Revert temp build changes

Add customRequest param to method comments

Prettier

Fix method injection

Temp fix of build inside Minibits wallet

Temp fix of build of Minibits wallet

Revert temp build changes

Add customRequest param to method comments

Prettier
  • Loading branch information
misovan committed Jan 18, 2024
1 parent 6a881c3 commit 2565a88
Showing 1 changed file with 81 additions and 31 deletions.
112 changes: 81 additions & 31 deletions src/CashuMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,49 @@ import { isObj, joinUrls } from './utils.js';
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) {}
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): Promise<GetInfoResponse> {
return request<GetInfoResponse>({ endpoint: joinUrls(mintUrl, 'info') });
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);
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): Promise<RequestMintResponse> {
return request<RequestMintResponse>({
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}`
});
}
Expand All @@ -58,21 +74,24 @@ class CashuMint {
* @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);
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
hash: string,
customRequest?: typeof request
) {
const data = await request<{ promises: Array<SerializedBlindedSignature> }>({
const requestInstance = customRequest || request;
const data = await requestInstance<{ promises: Array<SerializedBlindedSignature> }>({
endpoint: `${joinUrls(mintUrl, 'mint')}?hash=${hash}`,
method: 'POST',
requestBody: payloads
Expand All @@ -91,20 +110,26 @@ class CashuMint {
* @returns serialized blinded signatures
*/
async mint(payloads: { outputs: Array<SerializedBlindedMessage> }, hash: string) {
return CashuMint.mint(this._mintUrl, payloads, hash);
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): Promise<MintKeys> {
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, '-');
}
return request<MintKeys>({
const requestInstance = customRequest || request;
return requestInstance<MintKeys>({
endpoint: keysetId ? joinUrls(mintUrl, 'keys', keysetId) : joinUrls(mintUrl, 'keys')
});
}
Expand All @@ -114,33 +139,44 @@ class CashuMint {
* @returns the mints public keys
*/
async getKeys(keysetId?: string): Promise<MintKeys> {
return CashuMint.getKeys(this._mintUrl, keysetId);
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): Promise<{ keysets: Array<string> }> {
return request<{ keysets: Array<string> }>({ endpoint: joinUrls(mintUrl, '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);
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): Promise<SplitResponse> {
const data = await request<SplitResponse>({
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
Expand All @@ -158,16 +194,22 @@ class CashuMint {
* @returns split tokens
*/
async split(splitPayload: SplitPayload): Promise<SplitResponse> {
return CashuMint.split(this._mintUrl, splitPayload);
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): Promise<MeltResponse> {
const data = await request<MeltResponse>({
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
Expand All @@ -189,19 +231,22 @@ class CashuMint {
* @returns
*/
async melt(meltPayload: MeltPayload): Promise<MeltResponse> {
return CashuMint.melt(this._mintUrl, meltPayload);
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 }
checkfeesPayload: { pr: string },
customRequest?: typeof request
): Promise<{ fee: number }> {
const data = await request<{ fee: number }>({
const requestInstance = customRequest || request;
const data = await requestInstance<{ fee: number }>({
endpoint: joinUrls(mintUrl, 'checkfees'),
method: 'POST',
requestBody: checkfeesPayload
Expand All @@ -220,19 +265,22 @@ class CashuMint {
* @returns estimated Fee
*/
async checkFees(checkfeesPayload: { pr: string }): Promise<{ fee: number }> {
return CashuMint.checkFees(this._mintUrl, checkfeesPayload);
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
checkPayload: CheckSpendablePayload,
customRequest?: typeof request
): Promise<CheckSpendableResponse> {
const data = await request<CheckSpendableResponse>({
const requestInstance = customRequest || request;
const data = await requestInstance<CheckSpendableResponse>({
endpoint: joinUrls(mintUrl, 'check'),
method: 'POST',
requestBody: checkPayload
Expand All @@ -250,14 +298,16 @@ class CashuMint {
* @returns redeemed and unredeemed ordered list of booleans
*/
async check(checkPayload: CheckSpendablePayload): Promise<CheckSpendableResponse> {
return CashuMint.check(this._mintUrl, checkPayload);
return CashuMint.check(this._mintUrl, checkPayload, this._customRequest);
}

public static async restore(
mintUrl: string,
restorePayload: { outputs: Array<SerializedBlindedMessage> }
restorePayload: { outputs: Array<SerializedBlindedMessage> },
customRequest?: typeof request
): Promise<PostRestoreResponse> {
const data = await request<PostRestoreResponse>({
const requestInstance = customRequest || request;
const data = await requestInstance<PostRestoreResponse>({
endpoint: joinUrls(mintUrl, 'restore'),
method: 'POST',
requestBody: restorePayload
Expand All @@ -273,7 +323,7 @@ class CashuMint {
async restore(restorePayload: {
outputs: Array<SerializedBlindedMessage>;
}): Promise<PostRestoreResponse> {
return CashuMint.restore(this._mintUrl, restorePayload);
return CashuMint.restore(this._mintUrl, restorePayload, this._customRequest);
}
}

Expand Down

0 comments on commit 2565a88

Please sign in to comment.