Skip to content

Commit

Permalink
Merge branch 'blinding' into v2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Egge21M committed Jan 14, 2025
2 parents ec88b31 + f519e24 commit 0d65b03
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/CashuWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class CashuWallet {
* @param options.mintInfo mint info from the mint (will be fetched from mint if not provided)
* @param options.denominationTarget target number proofs per denomination (default: see @constant DEFAULT_DENOMINATION_TARGET)
* @param options.bip39seed BIP39 seed for deterministic secrets.
* @param options.keepFactory A function that will be used by all parts of the library that produce proofs to be kept (change, etc.).
* This can lead to poor performance, in which case the seed should be directly provided
*/
constructor(
Expand Down Expand Up @@ -1090,7 +1091,7 @@ class CashuWallet {
outputAmounts
);
} else if (p2pk) {
outputData = OutputData.createP2PKData(p2pk, amount, keyset);
outputData = OutputData.createP2PKData(p2pk, amount, keyset, outputAmounts);
} else if (factory) {
const amounts = splitAmount(amount, keyset.keys);
outputData = amounts.map((a) => factory(a, keyset));
Expand Down
4 changes: 2 additions & 2 deletions src/model/OutputData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export class OutputData implements OutputDataLike {
customSplit?: Array<number>
): Array<OutputData> {
const amounts = splitAmount(amount, keyset.keys, customSplit);
return amounts.map((_, i) =>
this.createSingleDeterministicData(amount, seed, (counter = i), keyset.id)
return amounts.map((a, i) =>
this.createSingleDeterministicData(a, seed, counter + i, keyset.id)
);
}

Expand Down
24 changes: 12 additions & 12 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const unit = 'sat';

injectWebSocketImpl(ws);

function expectProofsSecretToEqual(p: Array<Proof>, s: string) {
function expectNUT10SecretDataToEqual(p: Array<Proof>, s: string) {
p.forEach((p) => {
const parsedSecret = JSON.parse(p.secret);
expect(parsedSecret[1].data).toBe(s);
Expand Down Expand Up @@ -497,7 +497,7 @@ describe('Custom Outputs', () => {
return OutputData.createSingleP2PKData({ pubkey: hexPk }, a, k.id);
}
const mint = new CashuMint(mintUrl);
// We then pass out factory to the CashuWallet constructor
// We then pass our factory to the CashuWallet constructor
const wallet = new CashuWallet(mint, { keepFactory: p2pkFactory });

// Lets mint some fresh proofs
Expand All @@ -506,7 +506,7 @@ describe('Custom Outputs', () => {
const proofs = await wallet.mintProofs(32, quoteRes.quote);

// Because of the keepFactory we expect these proofs to be locked to our public key
expectProofsSecretToEqual(proofs, hexPk);
expectNUT10SecretDataToEqual(proofs, hexPk);

// Lets melt some of these proofs to pay an invoice
const meltQuote = await wallet.createMeltQuote(invoice);
Expand All @@ -517,15 +517,15 @@ describe('Custom Outputs', () => {
includeFees: true
});
// Again the change we get from the swap are expected to be locked to our public key
expectProofsSecretToEqual(meltKeep, hexPk);
expectNUT10SecretDataToEqual(meltKeep, hexPk);

// We then pay the melt. In this case no private key is required, as our factory only applies to keep Proofs, not send Proofs
const meltRes = await wallet.meltProofs(meltQuote, meltSend);
// Even the change we receive from the fee reserve is expected to be locked
if (meltRes.change && meltRes.change.length > 0) {
expectProofsSecretToEqual(meltRes.change, hexPk);
expectNUT10SecretDataToEqual(meltRes.change, hexPk);
}
// Finally we want to check wheter received token are locked as well
// Finally we want to check whether received token are locked as well
const restAmount = sumProofs(meltKeep) - wallet.getFeesForProofs(meltKeep);
// First we unlock all the proofs that we have left
const unlockedProofs = await wallet.send(restAmount, meltKeep, {
Expand All @@ -537,7 +537,7 @@ describe('Custom Outputs', () => {
{ outputData: (a, k) => OutputData.createSingleP2PKData({ pubkey: 'testKey' }, a, k.id) }
);
// Our factory also applies to the receive method, so we expect all received proofs to be locked
expectProofsSecretToEqual(newProofs, 'testKey');
expectNUT10SecretDataToEqual(newProofs, 'testKey');
}, 15000);
test('Manual Factory Mint', async () => {
function createFactory(pubkey: string): OutputDataFactory {
Expand All @@ -555,7 +555,7 @@ describe('Custom Outputs', () => {
const proofs = await wallet.mintProofs(21, quote.quote, {
outputData: createFactory('mintTest')
});
expectProofsSecretToEqual(proofs, 'mintTest');
expectNUT10SecretDataToEqual(proofs, 'mintTest');
});
test('Manual Factory Send', async () => {
function createFactory(pubkey: string): OutputDataFactory {
Expand All @@ -575,8 +575,8 @@ describe('Custom Outputs', () => {
const { send, keep } = await wallet.send(amount, proofs, {
outputData: { send: createFactory('send'), keep: createFactory('keep') }
});
expectProofsSecretToEqual(send, 'send');
expectProofsSecretToEqual(keep, 'keep');
expectNUT10SecretDataToEqual(send, 'send');
expectNUT10SecretDataToEqual(keep, 'keep');
});
test('Manual BlindingData', async () => {
const mint = new CashuMint(mintUrl);
Expand All @@ -593,8 +593,8 @@ describe('Custom Outputs', () => {
});
const key1Sends = send.slice(0, data1.length);
const key2Sends = send.slice(data1.length);
expectProofsSecretToEqual(key1Sends, 'key1');
expectProofsSecretToEqual(key2Sends, 'key2');
expectNUT10SecretDataToEqual(key1Sends, 'key1');
expectNUT10SecretDataToEqual(key2Sends, 'key2');
});
});
describe('Keep Vector and Reordering', () => {
Expand Down

0 comments on commit 0d65b03

Please sign in to comment.