Skip to content

Commit

Permalink
Throw if outputs.length > 15 (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunify authored Jul 24, 2019
1 parent f2dad7a commit 940b73a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
27 changes: 15 additions & 12 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class Transaction {

let noSigs = Buffer.alloc(raw.length, 0);
if (type === Type.TRANSFER) {

let offset = 2;

// copy type and lengths
Expand Down Expand Up @@ -401,6 +401,9 @@ export default class Transaction {
if (inputsLength > 15) {
throw new Error('Too many inputs (>15)');
}
if (outputsLength > 15) {
throw new Error('Too many outputs (>15)');
}
payload.writeUInt8((16 * inputsLength) + outputsLength, 1);
}
if (this.type === Type.DEPOSIT) {
Expand Down Expand Up @@ -464,7 +467,7 @@ export default class Transaction {
return Buffer.concat([payload, Util.arrayToRaw(inputs), Util.arrayToRaw(this.outputs)]);
}
/* eslint-enable prefer-destructuring */


toJSON() {
const json = {
Expand Down Expand Up @@ -646,56 +649,56 @@ export default class Transaction {
);

if (exact) return [new Input(exact.outpoint)];

const inputs = [];
let sum = BigInt(0);
for (let i = 0; i < myUnspent.length; i += 1) {
inputs.push(new Input(myUnspent[i].outpoint));
sum = add(sum, BigInt(myUnspent[i].output.value));

if (greaterThanOrEqual(sum, BigInt(amount))) {
break;
}
}

if (lessThan(sum, BigInt(amount))) {
throw new Error('Not enough inputs');
}

return inputs;
}

// ToDo: handle inputs from different accounts
// ToDo: handle different input colors
static calcOutputs(unspent, inputs, from, to, amount, color) {
if (unspent.length === 0) {
throw new Error('Unspent is empty');
}

const inInputs = u =>
inputs.findIndex(input => u.outpoint.equals(input.prevout)) > -1;
const inputUtxos = unspent.filter(inInputs);

if (Util.isNFT(color) || Util.isNST(color)) {
return inputUtxos.map(i =>
new Output(i.output.value, to, color, i.output.data)
new Output(i.output.value, to, color, i.output.data)
);
}

const sum = inputUtxos
.reduce((a, u) => add(a, BigInt(u.output.value)), BigInt(0));

if (lessThan(sum, BigInt(amount))) {
throw new Error('Not enough inputs');
}

const outputs = [new Output(amount, to.toLowerCase(), color)];
if (greaterThan(sum, BigInt(amount))) {
outputs.push(
new Output(subtract(sum, BigInt(amount)), from.toLowerCase(), color)
);
}

return outputs;
}

Expand Down
21 changes: 20 additions & 1 deletion lib/transaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ describe('transactions', () => {
}).throw('Too many inputs (>15)');
});

it('should throw with >15 outputs', () => {
const prevTx = '0x7777777777777777777777777777777777777777777777777777777777777777';
const value = BigInt('99000000');
const color = 1337;

const transfer = Tx.transfer(
[new Input(new Outpoint(prevTx, 0))],
Array.from(
{ length: 16 },
() => new Output(value, ADDR, color)
),
);

expect(() => {
transfer.signAll(PRIV);
transfer.toRaw();
}).throw('Too many outputs (>15)');
});

it('should allow to create and parse transfer tx with 2 outputs.', () => {
const prevTx = '0x7777777777777777777777777777777777777777777777777777777777777777';
const value = BigInt('99000000');
Expand Down Expand Up @@ -586,5 +605,5 @@ describe('transactions', () => {

});


});

0 comments on commit 940b73a

Please sign in to comment.