Skip to content

Commit

Permalink
Update Transactions with Multiple Outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Sep 28, 2022
1 parent 861c391 commit 621d286
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 9 additions & 0 deletions wallet/transaction.js
Expand Up @@ -26,6 +26,15 @@ class Transaction {
};
}

update({ senderWallet, recipient, amount }) {
this.outputMap[recipient] = amount;

this.outputMap[senderWallet.publicKey] =
this.outputMap[senderWallet.publicKey] - amount;

this.input = this.createInput({ senderWallet, outputMap: this.outputMap });
}

static validTransaction(transaction) {
const { input: { address, amount, signature }, outputMap } = transaction;

Expand Down
37 changes: 36 additions & 1 deletion wallet/transaction.test.js
Expand Up @@ -94,6 +94,41 @@ describe('Transaction', () => {
});
});
});
})
});

describe('update()', () => {
let originalSignature, originalSenderOutput, nextRecipient, nextAmount;

beforeEach(() => {
originalSignature = transaction.input.signature;
originalSenderOutput = transaction.outputMap[senderWallet.publicKey];
nextRecipient = 'next-recipient';
nextAmount = 50;

transaction.update({
senderWallet, recipient: nextRecipient, amount: nextAmount
});
});

it('outputs the amount to the next recipient', () => {
expect(transaction.outputMap[nextRecipient]).toEqual(nextAmount);
});

it('subtracts the amount from the original sender output amount', () => {
expect(transaction.outputMap[senderWallet.publicKey])
.toEqual(originalSenderOutput - nextAmount);
});

it('maintains a total output that matches the input amount', () => {
expect(
Object.values(transaction.outputMap)
.reduce((total, outputAmount) => total+outputAmount)
).toEqual(transaction.input.amount);
});

it('re-signs the transaction', () => {
expect(transaction.input.signature).not.toEqual(originalSignature);
});
});
});

0 comments on commit 621d286

Please sign in to comment.