Skip to content

Commit 26fbc03

Browse files
committedNov 12, 2021
chore: lint & format; fix: discovered bug in findScriptPath() after lint
1 parent 9595927 commit 26fbc03

File tree

5 files changed

+52
-40
lines changed

5 files changed

+52
-40
lines changed
 

‎src/payments/p2tr.js

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ function p2tr(a, opts) {
120120
lazy.prop(o, 'witness', () => {
121121
if (a.witness) return a.witness;
122122
if (a.scriptsTree && a.scriptLeaf && a.internalPubkey) {
123+
// todo: optimize/cache
123124
const hashTree = (0, taproot_1.toHashTree)(a.scriptsTree);
124125
const leafHash = (0, taproot_1.tapLeafHash)(
125126
a.scriptLeaf.output,

‎src/taproot.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,8 @@ function findScriptPath(node, hash) {
116116
if (node.right) {
117117
if (node.right.hash.equals(hash)) return node.left ? [node.left.hash] : [];
118118
const rightPath = findScriptPath(node.right, hash);
119-
if (rightPath.length) {
120-
}
121-
return node.left ? [node.left.hash].concat(rightPath) : rightPath;
119+
if (rightPath.length)
120+
return node.left ? [node.left.hash].concat(rightPath) : rightPath;
122121
}
123122
return [];
124123
}

‎test/fixtures/p2tr.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@
470470
"arguments": {
471471
"internalPubkey": "ee4fe085983462a184015d1f782d6a5f8b9c2b60130aff050ce221ecf3786592",
472472
"scriptLeaf": {
473-
"output": "424950333431 OP_CHECKSIG",
473+
"output": "424950333431",
474474
"version": 152
475475
},
476476
"scriptsTree": [
@@ -492,7 +492,7 @@
492492
"address": "bc1ppa3u5trk8xumkjlqgewvp237u79qwcd6ta0h6mlca2e5puya54ssw9zq0y",
493493
"hash": "f3004d6c183e038105d436db1424f321613366cbb7b05939bf05d763a9ebb962",
494494
"witness": [
495-
"06424950333431ac",
495+
"06424950333431",
496496
"98ee4fe085983462a184015d1f782d6a5f8b9c2b60130aff050ce221ecf37865928ad69ec7cf41c2a4001fd1f738bf1e505ce2277acdcaa63fe4765192497f47a7"
497497
],
498498
"signature": null,

‎ts_src/payments/p2tr.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
4444
pubkey: typef.maybe(typef.BufferN(32)),
4545
signature: typef.maybe(bscript.isCanonicalScriptSignature),
4646
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
47-
4847
// scriptsTree: typef.maybe(typef.TaprootNode), // use merkel.isMast ?
4948
scriptLeaf: typef.maybe({
5049
version: typef.maybe(typef.Number),
@@ -134,14 +133,18 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
134133
if (a.witness) return a.witness;
135134
if (a.scriptsTree && a.scriptLeaf && a.internalPubkey) {
136135
// todo: optimize/cache
137-
const hashTree = toHashTree(a.scriptsTree)
138-
const leafHash = tapLeafHash(a.scriptLeaf.output, a.scriptLeaf.version)
139-
const path = findScriptPath(hashTree, leafHash)
136+
const hashTree = toHashTree(a.scriptsTree);
137+
const leafHash = tapLeafHash(a.scriptLeaf.output, a.scriptLeaf.version);
138+
const path = findScriptPath(hashTree, leafHash);
140139
const outputKey = tweakKey(a.internalPubkey, hashTree.hash);
141-
if (!outputKey) return
142-
const version = a.scriptLeaf.version || 0xc0
143-
const controlBock = NBuffer.concat([NBuffer.from([version | outputKey.parity]), a.internalPubkey].concat(path.reverse()))
144-
return [a.scriptLeaf.output, controlBock]
140+
if (!outputKey) return;
141+
const version = a.scriptLeaf.version || 0xc0;
142+
const controlBock = NBuffer.concat(
143+
[NBuffer.from([version | outputKey.parity]), a.internalPubkey].concat(
144+
path.reverse(),
145+
),
146+
);
147+
return [a.scriptLeaf.output, controlBock];
145148
}
146149
if (a.signature) return [a.signature];
147150
});
@@ -211,7 +214,8 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
211214
const controlBlock = witness[witness.length - 1];
212215
if (controlBlock.length < 33)
213216
throw new TypeError(
214-
`The control-block length is too small. Got ${controlBlock.length
217+
`The control-block length is too small. Got ${
218+
controlBlock.length
215219
}, expected min 33.`,
216220
);
217221

‎ts_src/taproot.ts

+34-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ const BN = require('bn.js');
44
import * as bcrypto from './crypto';
55
// todo: use varuint-bitcoin??
66
import * as varuint from 'bip174/src/lib/converter/varint';
7-
import { TweakedPublicKey, TaprootLeaf, ZERO32, EC_P, GROUP_ORDER } from './types';
7+
import {
8+
TweakedPublicKey,
9+
TaprootLeaf,
10+
ZERO32,
11+
EC_P,
12+
GROUP_ORDER,
13+
} from './types';
814

915
// todo: !!!Temp, to be replaced. Only works because bip32 has it as dependecy. Linting will fail.
1016
const ecc = require('tiny-secp256k1');
@@ -78,7 +84,10 @@ export function tweakKey(
7884
};
7985
}
8086

81-
export function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buffer {
87+
export function rootHashFromPath(
88+
controlBlock: Buffer,
89+
tapLeafMsg: Buffer,
90+
): Buffer {
8291
const k = [tapLeafMsg];
8392
const e = [];
8493

@@ -97,25 +106,24 @@ export function rootHashFromPath(controlBlock: Buffer, tapLeafMsg: Buffer): Buff
97106
}
98107

99108
export interface HashTree {
100-
hash: Buffer
101-
left?: HashTree
102-
right?: HashTree
109+
hash: Buffer;
110+
left?: HashTree;
111+
right?: HashTree;
103112
}
104113

105-
106114
export function toHashTree(scripts: TaprootLeaf[]): HashTree {
107115
if (scripts.length === 1) {
108116
const script = scripts[0];
109117
if (Array.isArray(script)) {
110118
return toHashTree(script);
111119
}
112120
script.version = script.version || LEAF_VERSION_TAPSCRIPT;
113-
if ((script.version & 1) !== 0) throw new TypeError('Invalid script version');
121+
if ((script.version & 1) !== 0)
122+
throw new TypeError('Invalid script version');
114123

115124
return {
116-
hash: tapLeafHash(script.output, script.version)
117-
}
118-
125+
hash: tapLeafHash(script.output, script.version),
126+
};
119127
}
120128
// todo: this is a binary tree, use zero an one index
121129
const half = Math.trunc(scripts.length / 2);
@@ -130,38 +138,38 @@ export function toHashTree(scripts: TaprootLeaf[]): HashTree {
130138
return {
131139
hash: tapBranchHash(leftHash, rightHash),
132140
left,
133-
right
134-
}
141+
right,
142+
};
135143
}
136144

137145
export function findScriptPath(node: HashTree, hash: Buffer): Buffer[] {
138146
if (node.left) {
139-
if (node.left.hash.equals(hash))
140-
return node.right ? [node.right.hash] : []
141-
const leftPath = findScriptPath(node.left, hash)
147+
if (node.left.hash.equals(hash)) return node.right ? [node.right.hash] : [];
148+
const leftPath = findScriptPath(node.left, hash);
142149
if (leftPath.length)
143-
return node.right ? [node.right.hash].concat(leftPath) : leftPath
150+
return node.right ? [node.right.hash].concat(leftPath) : leftPath;
144151
}
145152

146153
if (node.right) {
147-
if (node.right.hash.equals(hash))
148-
return node.left ? [node.left.hash] : []
149-
const rightPath = findScriptPath(node.right, hash)
150-
if (rightPath.length) {}
151-
return node.left ? [node.left.hash].concat(rightPath) : rightPath
154+
if (node.right.hash.equals(hash)) return node.left ? [node.left.hash] : [];
155+
const rightPath = findScriptPath(node.right, hash);
156+
if (rightPath.length)
157+
return node.left ? [node.left.hash].concat(rightPath) : rightPath;
152158
}
153159

154-
return []
155-
160+
return [];
156161
}
157162

158163
export function tapLeafHash(script: Buffer, version?: number): Buffer {
159-
version = version || LEAF_VERSION_TAPSCRIPT
160-
return bcrypto.taggedHash(TAP_LEAF_TAG, NBuffer.concat([NBuffer.from([version]), serializeScript(script)]));
164+
version = version || LEAF_VERSION_TAPSCRIPT;
165+
return bcrypto.taggedHash(
166+
TAP_LEAF_TAG,
167+
NBuffer.concat([NBuffer.from([version]), serializeScript(script)]),
168+
);
161169
}
162170

163171
function tapBranchHash(a: Buffer, b: Buffer): Buffer {
164-
return bcrypto.taggedHash(TAP_BRANCH_TAG, NBuffer.concat([a, b]),);
172+
return bcrypto.taggedHash(TAP_BRANCH_TAG, NBuffer.concat([a, b]));
165173
}
166174

167175
function serializeScript(s: Buffer): Buffer {

0 commit comments

Comments
 (0)