Skip to content

Commit

Permalink
Fix melt to return whole change
Browse files Browse the repository at this point in the history
  • Loading branch information
minibits-cash committed Nov 19, 2024
1 parent 1a60b78 commit 8dcac7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
bytesToNumber,
getDecodedToken,
getDefaultAmountPreference,
splitAmount
splitAmount,
sumProofs
} from './utils';
import { validateMnemonic } from '@scure/bip39';
import { wordlist } from '@scure/bip39/wordlists/english';
Expand Down Expand Up @@ -443,7 +444,7 @@ class CashuWallet {
const keys = await this.getKeys(options?.keysetId);

const { blindedMessages, secrets, rs } = this.createBlankOutputs(
meltQuote.fee_reserve,
sumProofs(proofsToSend) - meltQuote.amount,
keys.id,
options?.counter
);
Expand All @@ -463,6 +464,8 @@ class CashuWallet {
};
}



/**
* Helper function that pays a Lightning invoice directly without having to create a melt quote before
* The combined amount of Proofs must match the payment amount including fees.
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { TOKEN_PREFIX, TOKEN_VERSION } from './utils/Constants';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { sha256 } from '@noble/hashes/sha256';

export function sumProofs(proofs: Array<Proof>) {
return proofs.reduce((acc: number, proof: Proof) => acc + proof.amount, 0);
}

function splitAmount(value: number, amountPreference?: Array<AmountPreference>): Array<number> {
const chunks: Array<number> = [];
Expand Down

0 comments on commit 8dcac7c

Please sign in to comment.