Skip to content

Commit 5f81cf4

Browse files
committed
Remove unnecessary arrays of values
The spec uses this notation because in a spec there's no such thing as reassigning a value. In real code it is appropriate to us accumulators or such.
1 parent 299308a commit 5f81cf4

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/payments/taprootutils.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ const bufferutils_1 = require('../bufferutils');
77
const types_1 = require('../types');
88
exports.LEAF_VERSION_TAPSCRIPT = 0xc0;
99
function rootHashFromPath(controlBlock, tapleafMsg) {
10-
const k = [tapleafMsg];
11-
const e = [];
1210
const m = (controlBlock.length - 33) / 32;
11+
let kj = tapleafMsg;
1312
for (let j = 0; j < m; j++) {
14-
e[j] = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
15-
if (k[j].compare(e[j]) < 0) {
16-
k[j + 1] = tapBranchHash(k[j], e[j]);
13+
const ej = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
14+
if (kj.compare(ej) < 0) {
15+
kj = tapBranchHash(kj, ej);
1716
} else {
18-
k[j + 1] = tapBranchHash(e[j], k[j]);
17+
kj = tapBranchHash(ej, kj);
1918
}
2019
}
21-
return k[m];
20+
return kj;
2221
}
2322
exports.rootHashFromPath = rootHashFromPath;
2423
const isHashBranch = ht => 'left' in ht && 'right' in ht;

ts_src/payments/taprootutils.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,19 @@ export function rootHashFromPath(
1010
controlBlock: Buffer,
1111
tapleafMsg: Buffer,
1212
): Buffer {
13-
const k = [tapleafMsg];
14-
const e = [];
15-
1613
const m = (controlBlock.length - 33) / 32;
1714

15+
let kj = tapleafMsg;
1816
for (let j = 0; j < m; j++) {
19-
e[j] = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
20-
if (k[j].compare(e[j]) < 0) {
21-
k[j + 1] = tapBranchHash(k[j], e[j]);
17+
const ej = controlBlock.slice(33 + 32 * j, 65 + 32 * j);
18+
if (kj.compare(ej) < 0) {
19+
kj = tapBranchHash(kj, ej);
2220
} else {
23-
k[j + 1] = tapBranchHash(e[j], k[j]);
21+
kj = tapBranchHash(ej, kj);
2422
}
2523
}
2624

27-
return k[m];
25+
return kj;
2826
}
2927

3028
interface HashLeaf {

0 commit comments

Comments
 (0)