Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use sendOption type in function argument #212

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 35 additions & 87 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
import {
blindMessage,
constructProofFromPromise,
serializeProof
} from '@cashu/crypto/modules/client';
import { deriveBlindingFactor, deriveSecret } from '@cashu/crypto/modules/client/NUT09';
import { createP2PKsecret, getSignedProofs } from '@cashu/crypto/modules/client/NUT11';
import { verifyDLEQProof_reblind } from '@cashu/crypto/modules/client/NUT12';
import { hashToCurve, pointFromHex } from '@cashu/crypto/modules/common';
import { DLEQ, type Proof as NUT11Proof } from '@cashu/crypto/modules/common/index';
import { bytesToHex, hexToBytes, randomBytes } from '@noble/hashes/utils';
import { CashuMint } from './CashuMint.js';
import { BlindedMessage } from './model/BlindedMessage.js';
import {
BlindingData,
GetInfoResponse,
MeltQuoteState,
MintQuoteResponse,
MintQuoteState,
OutputAmounts,
ProofState,
SerializedBlindedSignature,
SerializedDLEQ,
type MeltPayload,
type MeltProofsResponse,
type MeltQuotePayload,
type MeltQuoteResponse,
type MintKeys,
type MintKeyset,
type MeltProofsResponse,
type MintPayload,
type Proof,
type MintQuotePayload,
type MeltQuotePayload,
type Proof,
type SendResponse,
type SerializedBlindedMessage,
type SwapPayload,
type Token,
SerializedBlindedSignature,
GetInfoResponse,
OutputAmounts,
ProofState,
BlindingData,
MintQuoteResponse,
MintQuoteState,
MeltQuoteState,
SerializedDLEQ
type Token
} from './model/types/index.js';
import { SubscriptionCanceller } from './model/types/wallet/websocket.js';
import {
bytesToNumber,
getDecodedToken,
splitAmount,
sumProofs,
getKeepAmounts,
numberToHexPadded64,
hasValidDleq,
stripDleq
numberToHexPadded64,
SendOptions,
splitAmount,
stripDleq,
sumProofs
} from './utils.js';
import { hashToCurve, pointFromHex } from '@cashu/crypto/modules/common';
import {
blindMessage,
constructProofFromPromise,
serializeProof
} from '@cashu/crypto/modules/client';
import { deriveBlindingFactor, deriveSecret } from '@cashu/crypto/modules/client/NUT09';
import { createP2PKsecret, getSignedProofs } from '@cashu/crypto/modules/client/NUT11';
import { type Proof as NUT11Proof, DLEQ } from '@cashu/crypto/modules/common/index';
import { SubscriptionCanceller } from './model/types/wallet/websocket.js';
import { verifyDLEQProof_reblind } from '@cashu/crypto/modules/client/NUT12';
/**
* The default number of proofs per denomination to keep in a wallet.
*/
Expand Down Expand Up @@ -254,18 +255,7 @@ class CashuWallet {
* @param options.requireDleq? will check each proof for DLEQ proofs. Reject the token if any one of them can't be verified.
* @returns New token with newly created proofs, token entries that had errors
*/
async receive(
token: string | Token,
options?: {
keysetId?: string;
outputAmounts?: OutputAmounts;
proofsWeHave?: Array<Proof>;
counter?: number;
pubkey?: string;
privkey?: string;
requireDleq?: boolean;
}
): Promise<Array<Proof>> {
async receive(token: string | Token, options?: SendOptions): Promise<Array<Proof>> {
if (typeof token === 'string') {
token = getDecodedToken(token);
}
Expand Down Expand Up @@ -310,21 +300,7 @@ class CashuWallet {
* @param options.includeDleq? optionally include DLEQ proof in the proofs to send.
* @returns {SendResponse}
*/
async send(
amount: number,
proofs: Array<Proof>,
options?: {
outputAmounts?: OutputAmounts;
proofsWeHave?: Array<Proof>;
counter?: number;
pubkey?: string;
privkey?: string;
keysetId?: string;
offline?: boolean;
includeFees?: boolean;
includeDleq?: boolean;
}
): Promise<SendResponse> {
async send(amount: number, proofs: Array<Proof>, options?: SendOptions): Promise<SendResponse> {
if (options?.includeDleq) {
proofs = proofs.filter((p: Proof) => p.dleq != undefined);
}
Expand Down Expand Up @@ -489,19 +465,7 @@ class CashuWallet {
* @param options.privkey? will create a signature on the @param proofs secrets if set
* @returns promise of the change- and send-proofs
*/
async swap(
amount: number,
proofs: Array<Proof>,
options?: {
outputAmounts?: OutputAmounts;
proofsWeHave?: Array<Proof>;
counter?: number;
pubkey?: string;
privkey?: string;
keysetId?: string;
includeFees?: boolean;
}
): Promise<SendResponse> {
async swap(amount: number, proofs: Array<Proof>, options?: SendOptions): Promise<SendResponse> {
if (!options) options = {};
const keyset = await this.getKeys(options.keysetId);
const proofsToSend = proofs;
Expand Down Expand Up @@ -604,9 +568,7 @@ class CashuWallet {
async restore(
start: number,
count: number,
options?: {
keysetId?: string;
}
options?: SendOptions
): Promise<{ proofs: Array<Proof> }> {
const keys = await this.getKeys(options?.keysetId);
if (!this._seed) {
Expand Down Expand Up @@ -669,17 +631,7 @@ class CashuWallet {
* @param options.pubkey? optionally locks ecash to pubkey. Will not be deterministic, even if counter is set!
* @returns proofs
*/
async mintProofs(
amount: number,
quote: string,
options?: {
keysetId?: string;
outputAmounts?: OutputAmounts;
proofsWeHave?: Array<Proof>;
counter?: number;
pubkey?: string;
}
): Promise<Array<Proof>> {
async mintProofs(amount: number, quote: string, options?: SendOptions): Promise<Array<Proof>> {
const keyset = await this.getKeys(options?.keysetId);
if (!options?.outputAmounts && options?.proofsWeHave) {
options.outputAmounts = {
Expand Down Expand Up @@ -745,11 +697,7 @@ class CashuWallet {
async meltProofs(
meltQuote: MeltQuoteResponse,
proofsToSend: Array<Proof>,
options?: {
keysetId?: string;
counter?: number;
privkey?: string;
}
options?: SendOptions
): Promise<MeltProofsResponse> {
const keys = await this.getKeys(options?.keysetId);
const { blindedMessages, secrets, blindingFactors } = this.createBlankOutputs(
Expand Down
30 changes: 24 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { verifyDLEQProof_reblind } from '@cashu/crypto/modules/client/NUT12';
import { DLEQ, pointFromHex } from '@cashu/crypto/modules/common';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { sha256 } from '@noble/hashes/sha256';
import {
encodeBase64ToJson,
encodeBase64toUint8,
encodeJsonToBase64,
encodeUint8toBase64Url
} from './base64.js';
import { decodeCBOR, encodeCBOR } from './cbor.js';
import { PaymentRequest } from './model/PaymentRequest.js';
import {
DeprecatedToken,
Keys,
Expand All @@ -17,12 +23,6 @@ import {
V4ProofTemplate
} from './model/types/index.js';
import { TOKEN_PREFIX, TOKEN_VERSION } from './utils/Constants.js';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { sha256 } from '@noble/hashes/sha256';
import { decodeCBOR, encodeCBOR } from './cbor.js';
import { PaymentRequest } from './model/PaymentRequest.js';
import { DLEQ, pointFromHex } from '@cashu/crypto/modules/common';
import { verifyDLEQProof_reblind } from '@cashu/crypto/modules/client/NUT12';

/**
* Splits the amount into denominations of the provided @param keyset
Expand Down Expand Up @@ -521,3 +521,21 @@ export function hasValidDleq(proof: Proof, keyset: MintKeys): boolean {

return true;
}

type OutputAmounts = {
sendAmounts: Array<number>;
keepAmounts?: Array<number>;
};

export type SendOptions = {
outputAmounts?: OutputAmounts;
proofsWeHave?: Array<Proof>;
counter?: number;
pubkey?: string;
privkey?: string;
keysetId?: string;
offline?: boolean;
includeFees?: boolean;
includeDleq?: boolean;
requireDleq?: boolean;
};