Skip to content

Commit 10fcf3d

Browse files
committed
Remove private __byteLength from Transaction
1 parent 48bf08c commit 10fcf3d

File tree

5 files changed

+50
-60
lines changed

5 files changed

+50
-60
lines changed

src/block.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ class Block {
137137
return (
138138
80 +
139139
varuint.encodingLength(this.transactions.length) +
140-
// @ts-ignore using the __byteLength private method on Transaction
141-
this.transactions.reduce((a, x) => a + x.__byteLength(allowWitness), 0)
140+
this.transactions.reduce((a, x) => a + x.byteLength(allowWitness), 0)
142141
);
143142
}
144143
getHash() {

src/transaction.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,31 @@ class Transaction {
179179
});
180180
}
181181
weight() {
182-
const base = this.__byteLength(false);
183-
const total = this.__byteLength(true);
182+
const base = this.byteLength(false);
183+
const total = this.byteLength(true);
184184
return base * 3 + total;
185185
}
186186
virtualSize() {
187187
return Math.ceil(this.weight() / 4);
188188
}
189-
byteLength() {
190-
return this.__byteLength(true);
189+
byteLength(_ALLOW_WITNESS = true) {
190+
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
191+
return (
192+
(hasWitnesses ? 10 : 8) +
193+
varuint.encodingLength(this.ins.length) +
194+
varuint.encodingLength(this.outs.length) +
195+
this.ins.reduce((sum, input) => {
196+
return sum + 40 + varSliceSize(input.script);
197+
}, 0) +
198+
this.outs.reduce((sum, output) => {
199+
return sum + 8 + varSliceSize(output.script);
200+
}, 0) +
201+
(hasWitnesses
202+
? this.ins.reduce((sum, input) => {
203+
return sum + vectorSize(input.witness);
204+
}, 0)
205+
: 0)
206+
);
191207
}
192208
clone() {
193209
const newTx = new Transaction();
@@ -269,7 +285,7 @@ class Transaction {
269285
txTmp.ins[inIndex].script = ourScript;
270286
}
271287
// serialize and hash
272-
const buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4);
288+
const buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
273289
buffer.writeInt32LE(hashType, buffer.length - 4);
274290
txTmp.__toBuffer(buffer, 0, false);
275291
return bcrypto.hash256(buffer);
@@ -386,27 +402,8 @@ class Transaction {
386402
typeforce(types.tuple(types.Number, [types.Buffer]), arguments);
387403
this.ins[index].witness = witness;
388404
}
389-
__byteLength(_ALLOW_WITNESS) {
390-
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
391-
return (
392-
(hasWitnesses ? 10 : 8) +
393-
varuint.encodingLength(this.ins.length) +
394-
varuint.encodingLength(this.outs.length) +
395-
this.ins.reduce((sum, input) => {
396-
return sum + 40 + varSliceSize(input.script);
397-
}, 0) +
398-
this.outs.reduce((sum, output) => {
399-
return sum + 8 + varSliceSize(output.script);
400-
}, 0) +
401-
(hasWitnesses
402-
? this.ins.reduce((sum, input) => {
403-
return sum + vectorSize(input.witness);
404-
}, 0)
405-
: 0)
406-
);
407-
}
408-
__toBuffer(buffer, initialOffset, _ALLOW_WITNESS) {
409-
if (!buffer) buffer = Buffer.allocUnsafe(this.__byteLength(_ALLOW_WITNESS));
405+
__toBuffer(buffer, initialOffset, _ALLOW_WITNESS = false) {
406+
if (!buffer) buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS));
410407
let offset = initialOffset || 0;
411408
function writeSlice(slice) {
412409
offset += slice.copy(buffer, offset);

ts_src/block.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ export class Block {
160160
return (
161161
80 +
162162
varuint.encodingLength(this.transactions.length) +
163-
// @ts-ignore using the __byteLength private method on Transaction
164-
this.transactions.reduce((a, x) => a + x.__byteLength(allowWitness), 0)
163+
this.transactions.reduce((a, x) => a + x.byteLength(allowWitness), 0)
165164
);
166165
}
167166

ts_src/transaction.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,34 @@ export class Transaction {
237237
}
238238

239239
weight(): number {
240-
const base = this.__byteLength(false);
241-
const total = this.__byteLength(true);
240+
const base = this.byteLength(false);
241+
const total = this.byteLength(true);
242242
return base * 3 + total;
243243
}
244244

245245
virtualSize(): number {
246246
return Math.ceil(this.weight() / 4);
247247
}
248248

249-
byteLength(): number {
250-
return this.__byteLength(true);
249+
byteLength(_ALLOW_WITNESS: boolean = true): number {
250+
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
251+
252+
return (
253+
(hasWitnesses ? 10 : 8) +
254+
varuint.encodingLength(this.ins.length) +
255+
varuint.encodingLength(this.outs.length) +
256+
this.ins.reduce((sum, input) => {
257+
return sum + 40 + varSliceSize(input.script);
258+
}, 0) +
259+
this.outs.reduce((sum, output) => {
260+
return sum + 8 + varSliceSize(output.script);
261+
}, 0) +
262+
(hasWitnesses
263+
? this.ins.reduce((sum, input) => {
264+
return sum + vectorSize(input.witness);
265+
}, 0)
266+
: 0)
267+
);
251268
}
252269

253270
clone(): Transaction {
@@ -352,7 +369,7 @@ export class Transaction {
352369
}
353370

354371
// serialize and hash
355-
const buffer: Buffer = Buffer.allocUnsafe(txTmp.__byteLength(false) + 4);
372+
const buffer: Buffer = Buffer.allocUnsafe(txTmp.byteLength(false) + 4);
356373
buffer.writeInt32LE(hashType, buffer.length - 4);
357374
txTmp.__toBuffer(buffer, 0, false);
358375

@@ -506,34 +523,13 @@ export class Transaction {
506523
this.ins[index].witness = witness;
507524
}
508525

509-
private __byteLength(_ALLOW_WITNESS: boolean): number {
510-
const hasWitnesses = _ALLOW_WITNESS && this.hasWitnesses();
511-
512-
return (
513-
(hasWitnesses ? 10 : 8) +
514-
varuint.encodingLength(this.ins.length) +
515-
varuint.encodingLength(this.outs.length) +
516-
this.ins.reduce((sum, input) => {
517-
return sum + 40 + varSliceSize(input.script);
518-
}, 0) +
519-
this.outs.reduce((sum, output) => {
520-
return sum + 8 + varSliceSize(output.script);
521-
}, 0) +
522-
(hasWitnesses
523-
? this.ins.reduce((sum, input) => {
524-
return sum + vectorSize(input.witness);
525-
}, 0)
526-
: 0)
527-
);
528-
}
529-
530526
private __toBuffer(
531527
buffer?: Buffer,
532528
initialOffset?: number,
533-
_ALLOW_WITNESS?: boolean,
529+
_ALLOW_WITNESS: boolean = false,
534530
): Buffer {
535531
if (!buffer)
536-
buffer = Buffer.allocUnsafe(this.__byteLength(_ALLOW_WITNESS!)) as Buffer;
532+
buffer = Buffer.allocUnsafe(this.byteLength(_ALLOW_WITNESS)) as Buffer;
537533

538534
let offset = initialOffset || 0;
539535

types/transaction.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export declare class Transaction {
3030
hasWitnesses(): boolean;
3131
weight(): number;
3232
virtualSize(): number;
33-
byteLength(): number;
33+
byteLength(_ALLOW_WITNESS?: boolean): number;
3434
clone(): Transaction;
3535
/**
3636
* Hash transaction for signing a specific input.
@@ -48,6 +48,5 @@ export declare class Transaction {
4848
toHex(): string;
4949
setInputScript(index: number, scriptSig: Buffer): void;
5050
setWitness(index: number, witness: Buffer[]): void;
51-
private __byteLength;
5251
private __toBuffer;
5352
}

0 commit comments

Comments
 (0)