Skip to content

Commit

Permalink
abstracted strip util + changed conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Nov 13, 2024
1 parent 06fdaab commit af68d57
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
sumProofs,
getKeepAmounts,
numberToHexPadded64,
hasValidDleq
hasValidDleq,
stripDleq
} from './utils.js';
import { hashToCurve, pointFromHex } from '@cashu/crypto/modules/common';
import {
Expand Down Expand Up @@ -320,7 +321,6 @@ class CashuWallet {
}
): Promise<SendResponse> {
if (options?.includeDleq) {
// only pick the ones with a DLEQ proof
proofs = proofs.filter((p: Proof) => p.dleq != undefined);
}
if (sumProofs(proofs) < amount) {
Expand Down Expand Up @@ -352,11 +352,8 @@ class CashuWallet {
let { keep, send } = await this.swap(amount, sendProofs, options);
keep = keepProofsSelect.concat(keep);

// strip dleq if explicitly told so
if (options?.includeDleq === false) {
send = send.map((p: Proof) => {
return { ...p, dleq: undefined };
});
if (!options?.includeDleq) {
send = stripDleq(send);
}

return { keep, send };
Expand All @@ -366,11 +363,8 @@ class CashuWallet {
throw new Error('Not enough funds available to send');
}

// strip dleq if explicitly told so
if (!options?.includeDleq) {
sendProofOffline.forEach((p: Proof) => {
p.dleq = undefined;
});
return { keep: keepProofsOffline, send: stripDleq(sendProofOffline) };
}

return { keep: keepProofsOffline, send: sendProofOffline };
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,19 @@ export function decodePaymentRequest(paymentRequest: string) {
return PaymentRequest.fromEncodedRequest(paymentRequest);
}

/**
* Removes all traces of DLEQs from a list of proofs
* @param proofs The list of proofs that dleq should be stripped from
*/
export function stripDleq(proofs: Array<Proof>): Array<Omit<Proof, 'dleq' | 'dleqValid'>> {
return proofs.map((p) => {
const newP = { ...p };
delete newP['dleq'];
delete newP['dleqValid'];
return newP;
});
}

/**
* Checks that the proof has a valid DLEQ proof according to
* keyset `keys`
Expand Down

0 comments on commit af68d57

Please sign in to comment.