Skip to content

Commit

Permalink
sendPreference, keepPreference
Browse files Browse the repository at this point in the history
  • Loading branch information
lollerfirst committed Jun 18, 2024
1 parent a5e4cd2 commit 4d51bf9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
type SplitPayload,
type Token,
type TokenEntry,
CheckStateEnum
CheckStateEnum,
Preferences
} from './model/types/index.js';
import {
bytesToNumber,
Expand Down Expand Up @@ -178,11 +179,12 @@ class CashuWallet {
if (!preference) {
preference = getDefaultAmountPreference(amount, keys);
}
let pref: Preferences = {sendPreference: preference};
const { payload, blindedMessages } = this.createSplitPayload(
amount,
tokenEntry.proofs,
keys,
preference,
pref,
options?.counter,
options?.pubkey,
options?.privkey
Expand Down Expand Up @@ -222,14 +224,14 @@ class CashuWallet {
proofs: Array<Proof>,
options?: {
keysetId?: string;
preference?: Array<AmountPreference>;
preference?: Preferences;
counter?: number;
pubkey?: string;
privkey?: string;
}
): Promise<SendResponse> {
if (options?.preference) {
amount = options?.preference?.reduce((acc, curr) => acc + curr.amount * curr.count, 0);
amount = options?.preference?.sendPreference.reduce((acc, curr) => acc + curr.amount * curr.count, 0);
}
const keyset = await this.getKeys(options?.keysetId);
let amountAvailable = 0;
Expand Down Expand Up @@ -508,7 +510,7 @@ class CashuWallet {
amount: number,
proofsToSend: Array<Proof>,
keyset: MintKeys,
preference?: Array<AmountPreference>,
preference?: Preferences,
counter?: number,
pubkey?: string,
privkey?: string
Expand All @@ -520,7 +522,7 @@ class CashuWallet {
const keepBlindedMessages = this.createRandomBlindedMessages(
totalAmount - amount,
keyset,
undefined,
preference?.keepPreference,
counter
);
if (this._seed && counter) {
Expand All @@ -529,7 +531,7 @@ class CashuWallet {
const sendBlindedMessages = this.createRandomBlindedMessages(
amount,
keyset,
preference,
preference?.sendPreference,
counter,
pubkey
);
Expand Down
5 changes: 5 additions & 0 deletions src/model/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ export type AmountPreference = {
count: number;
};

export type Preferences = {
sendPreference: Array<AmountPreference>;
keepPreference?: Array<AmountPreference>;
}

export type InvoiceData = {
paymentRequest: string;
amountInSats?: number;
Expand Down
4 changes: 2 additions & 2 deletions test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ describe('send', () => {
C: '034268c0bd30b945adf578aca2dc0d1e26ef089869aaf9a08ba3a6da40fda1d8be'
}
];
const result = await wallet.send(4, overpayProofs, { preference: [{ amount: 1, count: 4 }] });
const result = await wallet.send(4, overpayProofs, { preference: { sendPreference: [{ amount: 1, count: 4 }] }});

expect(result.send).toHaveLength(4);
expect(result.send[0]).toMatchObject({ amount: 1, id: '009a1f293253e41e' });
Expand Down Expand Up @@ -520,7 +520,7 @@ describe('send', () => {
C: '034268c0bd30b945adf578aca2dc0d1e26ef089869aaf9a08ba3a6da40fda1d8be'
}
];
const result = await wallet.send(4, overpayProofs, { preference: [{ amount: 1, count: 3 }] });
const result = await wallet.send(4, overpayProofs, { preference: { sendPreference: [{ amount: 1, count: 3 }]} });

expect(result.send).toHaveLength(3);
expect(result.send[0]).toMatchObject({ amount: 1, id: '009a1f293253e41e' });
Expand Down

0 comments on commit 4d51bf9

Please sign in to comment.