Skip to content

Commit c987b0c

Browse files
committed
chore: code format and lint
1 parent 8fd07fc commit c987b0c

File tree

7 files changed

+179
-137
lines changed

7 files changed

+179
-137
lines changed

src/payments/p2tr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function p2tr(a, opts) {
103103
return witness[witness.length - 1].slice(1, 33);
104104
});
105105
lazy.prop(o, 'signature', () => {
106-
if (a.witness?.length !== 1) return;
106+
if (!a.witness || a.witness.length !== 1) return;
107107
return a.witness[0];
108108
});
109109
lazy.prop(o, 'input', () => {
@@ -150,7 +150,7 @@ function p2tr(a, opts) {
150150
throw new TypeError('Pubkey mismatch');
151151
else pubkey = tweakedKey.x;
152152
}
153-
if (pubkey?.length) {
153+
if (pubkey && pubkey.length) {
154154
if ((0, taproot_1.liftX)(pubkey) === null)
155155
throw new TypeError('Invalid pubkey for p2tr');
156156
}

test/payments.utils.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ export function equate(a: any, b: any, args?: any): void {
8787
if ('pubkey' in b)
8888
t.strictEqual(tryHex(a.pubkey), tryHex(b.pubkey), 'Inequal *.pubkey');
8989
if ('internalPubkey' in b)
90-
t.strictEqual(tryHex(a.internalPubkey), tryHex(b.internalPubkey), 'Inequal *.internalPubkey');
90+
t.strictEqual(
91+
tryHex(a.internalPubkey),
92+
tryHex(b.internalPubkey),
93+
'Inequal *.internalPubkey',
94+
);
9195
if ('signature' in b)
9296
t.strictEqual(
9397
tryHex(a.signature),
@@ -149,8 +153,7 @@ export function preform(x: any): any {
149153
if (x.redeem.network)
150154
x.redeem.network = (BNETWORKS as any)[x.redeem.network];
151155
}
152-
if (x.scriptsTree)
153-
x.scriptsTree = convertScriptsTree(x.scriptsTree)
156+
if (x.scriptsTree) x.scriptsTree = convertScriptsTree(x.scriptsTree);
154157
return x;
155158
}
156159

@@ -176,12 +179,10 @@ export function from(path: string, object: any, result?: any): any {
176179

177180
// todo: solve any type
178181
function convertScriptsTree(scriptsTree: any): any {
179-
if (Array.isArray(scriptsTree))
180-
return scriptsTree.map(convertScriptsTree)
181-
182+
if (Array.isArray(scriptsTree)) return scriptsTree.map(convertScriptsTree);
182183

183184
const script = Object.assign({}, scriptsTree);
184-
if ((typeof script.output === 'string'))
185-
script.output = asmToBuffer(scriptsTree.output)
186-
return script
185+
if (typeof script.output === 'string')
186+
script.output = asmToBuffer(scriptsTree.output);
187+
return script;
187188
}

ts_src/merkle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ export function fastMerkleRoot(
2424
}
2525

2626
return results[0];
27-
}
27+
}

ts_src/payments/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface Payment {
2424
address?: string; // taproot: betch32m
2525
hash?: Buffer; // taproot: MAST root
2626
redeem?: Payment; // taproot: when script path spending is used spending
27-
scriptsTree?: any // todo: solve
27+
scriptsTree?: any; // todo: solve
2828
witness?: Buffer[];
2929
}
3030

ts_src/payments/p2tr.ts

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { bitcoin as BITCOIN_NETWORK } from '../networks';
22
import * as bscript from '../script';
33
import { typeforce as typef } from '../types';
4-
import { computeMastRoot, leafHash, rootHash, tweakKey, liftX } from '../taproot';
4+
import {
5+
computeMastRoot,
6+
leafHash,
7+
rootHash,
8+
tweakKey,
9+
liftX,
10+
} from '../taproot';
511
import { Payment, PaymentOpts } from './index';
612
import * as lazy from './lazy';
713
import { bech32m } from 'bech32';
@@ -14,7 +20,14 @@ const ANNEX_PREFIX = 0x50;
1420
// input: <>
1521
// output: OP_1 {pubKey}
1622
export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
17-
if (!a.address && !a.output && !a.pubkey && !a.output && !a.internalPubkey && !(a.witness && a.witness.length > 1))
23+
if (
24+
!a.address &&
25+
!a.output &&
26+
!a.pubkey &&
27+
!a.output &&
28+
!a.internalPubkey &&
29+
!(a.witness && a.witness.length > 1)
30+
)
1831
throw new TypeError('Not enough data');
1932
opts = Object.assign({ validate: true }, opts || {});
2033

@@ -46,13 +59,16 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
4659
});
4760

4861
const _witness = lazy.value(() => {
49-
if (!a.witness || !a.witness.length) return
50-
if (a.witness.length >= 2 && (a.witness[a.witness.length - 1][0] === ANNEX_PREFIX)) {
62+
if (!a.witness || !a.witness.length) return;
63+
if (
64+
a.witness.length >= 2 &&
65+
a.witness[a.witness.length - 1][0] === ANNEX_PREFIX
66+
) {
5167
// remove annex, ignored by taproot
5268
return a.witness.slice(0, -1);
5369
}
54-
return a.witness.slice()
55-
})
70+
return a.witness.slice();
71+
});
5672

5773
const network = a.network || BITCOIN_NETWORK;
5874
const o: Payment = { name: 'p2tr', network };
@@ -65,48 +81,47 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
6581
return bech32m.encode(network.bech32, words);
6682
});
6783

68-
6984
lazy.prop(o, 'hash', () => {
7085
if (a.hash) return a.hash;
71-
if (a.scriptsTree) return computeMastRoot(a.scriptsTree)
72-
const w = _witness()
86+
if (a.scriptsTree) return computeMastRoot(a.scriptsTree);
87+
const w = _witness();
7388
if (w && w.length > 1) {
7489
const controlBlock = w[w.length - 1];
7590
const leafVersion = controlBlock[0] & 0b11111110;
7691
const script = w[w.length - 2];
77-
const tapLeafHash = leafHash(script, leafVersion)
78-
return rootHash(controlBlock, tapLeafHash)
92+
const tapLeafHash = leafHash(script, leafVersion);
93+
return rootHash(controlBlock, tapLeafHash);
7994
}
80-
return null
95+
return null;
8196
});
8297
lazy.prop(o, 'output', () => {
8398
if (!o.pubkey) return;
8499
return bscript.compile([OPS.OP_1, o.pubkey]);
85100
});
86101
lazy.prop(o, 'pubkey', () => {
87102
if (a.pubkey) return a.pubkey;
88-
if (a.output) return a.output.slice(2)
103+
if (a.output) return a.output.slice(2);
89104
if (a.address) return _address().data;
90105
if (o.internalPubkey) {
91-
const tweakedKey = tweakKey(o.internalPubkey, o.hash)
92-
if (tweakedKey) return tweakedKey.x
106+
const tweakedKey = tweakKey(o.internalPubkey, o.hash);
107+
if (tweakedKey) return tweakedKey.x;
93108
}
94109
});
95110
lazy.prop(o, 'internalPubkey', () => {
96111
if (a.internalPubkey) return a.internalPubkey;
97-
const witness = _witness()
112+
const witness = _witness();
98113
if (witness && witness.length > 1)
99114
return witness[witness.length - 1].slice(1, 33);
100115
});
101116
lazy.prop(o, 'signature', () => {
102-
if (a.witness?.length !== 1) return;
117+
if (!a.witness || a.witness.length !== 1) return;
103118
return a.witness[0];
104119
});
105120
lazy.prop(o, 'input', () => {
106121
// todo: not sure
107122
});
108123
lazy.prop(o, 'witness', () => {
109-
if (a.witness) return a.witness
124+
if (a.witness) return a.witness;
110125
if (!a.signature) return;
111126
return [a.signature];
112127
});
@@ -143,26 +158,26 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
143158
}
144159

145160
if (a.internalPubkey) {
146-
const tweakedKey = tweakKey(a.internalPubkey, o.hash)
147-
if (tweakedKey === null) throw new TypeError('Invalid internalPubkey for p2tr');
161+
const tweakedKey = tweakKey(a.internalPubkey, o.hash);
162+
if (tweakedKey === null)
163+
throw new TypeError('Invalid internalPubkey for p2tr');
148164
if (pubkey.length > 0 && !pubkey.equals(tweakedKey.x))
149165
throw new TypeError('Pubkey mismatch');
150166
else pubkey = tweakedKey.x;
151167
}
152168

153-
if (pubkey?.length) {
169+
if (pubkey && pubkey.length) {
154170
if (liftX(pubkey) === null)
155171
throw new TypeError('Invalid pubkey for p2tr');
156172
}
157173

158174
if (a.hash && a.scriptsTree) {
159-
const hash = computeMastRoot(a.scriptsTree)
160-
if (!a.hash.equals(hash))
161-
throw new TypeError('Hash mismatch');
175+
const hash = computeMastRoot(a.scriptsTree);
176+
if (!a.hash.equals(hash)) throw new TypeError('Hash mismatch');
162177
}
163178

164179
// todo: review cache
165-
const witness = _witness()
180+
const witness = _witness();
166181

167182
if (witness && witness.length) {
168183
if (witness.length === 1) {
@@ -176,14 +191,22 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
176191
// script path spending
177192
const controlBlock = witness[witness.length - 1];
178193
if (controlBlock.length < 33)
179-
throw new TypeError(`The control-block length is too small. Got ${controlBlock.length}, expected min 33.`);
194+
throw new TypeError(
195+
`The control-block length is too small. Got ${
196+
controlBlock.length
197+
}, expected min 33.`,
198+
);
180199

181200
if ((controlBlock.length - 33) % 32 !== 0)
182-
throw new TypeError(`The control-block length of ${controlBlock.length} is incorrect!`);
201+
throw new TypeError(
202+
`The control-block length of ${controlBlock.length} is incorrect!`,
203+
);
183204

184205
const m = (controlBlock.length - 33) / 32;
185206
if (m > 128)
186-
throw new TypeError(`The script path is too long. Got ${m}, expected max 128.`);
207+
throw new TypeError(
208+
`The script path is too long. Got ${m}, expected max 128.`,
209+
);
187210

188211
const internalPubkey = controlBlock.slice(1, 33);
189212
if (a.internalPubkey && !a.internalPubkey.equals(internalPubkey))
@@ -196,23 +219,21 @@ export function p2tr(a: Payment, opts?: PaymentOpts): Payment {
196219
const leafVersion = controlBlock[0] & 0b11111110;
197220
const script = witness[witness.length - 2];
198221

199-
const tapLeafHash = leafHash(script, leafVersion)
200-
const hash = rootHash(controlBlock, tapLeafHash)
222+
const tapLeafHash = leafHash(script, leafVersion);
223+
const hash = rootHash(controlBlock, tapLeafHash);
201224

202-
const outputKey = tweakKey(internalPubkey, hash)
225+
const outputKey = tweakKey(internalPubkey, hash);
203226
if (!outputKey)
204227
// todo: needs test data
205228
throw new TypeError('Invalid outputKey for p2tr witness');
206229

207230
if (pubkey.length && !pubkey.equals(outputKey.x))
208231
throw new TypeError('Pubkey mismatch for p2tr witness');
209232

210-
const controlBlockOddParity = (controlBlock[0] & 1) === 1
233+
const controlBlockOddParity = (controlBlock[0] & 1) === 1;
211234
if (outputKey.isOdd !== controlBlockOddParity)
212-
throw new Error('Incorrect parity')
213-
235+
throw new Error('Incorrect parity');
214236
}
215-
216237
}
217238
}
218239

0 commit comments

Comments
 (0)