Skip to content

Commit 6e6b889

Browse files
authored
Merge pull request bitcoinjs#1991 from jasonandjay/reduant
remove redundant code
2 parents b65513a + c4dbe6f commit 6e6b889

File tree

15 files changed

+47
-105
lines changed

15 files changed

+47
-105
lines changed

src/payments/embed.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ const bscript = require('../script');
66
const types_1 = require('../types');
77
const lazy = require('./lazy');
88
const OPS = bscript.OPS;
9-
function stacksEqual(a, b) {
10-
if (a.length !== b.length) return false;
11-
return a.every((x, i) => {
12-
return x.equals(b[i]);
13-
});
14-
}
159
// output: OP_RETURN ...
1610
function p2data(a, opts) {
1711
if (!a.data && !a.output) throw new TypeError('Not enough data');
@@ -43,7 +37,7 @@ function p2data(a, opts) {
4337
if (chunks[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid');
4438
if (!chunks.slice(1).every(types_1.typeforce.Buffer))
4539
throw new TypeError('Output is invalid');
46-
if (a.data && !stacksEqual(a.data, o.data))
40+
if (a.data && !(0, types_1.stacksEqual)(a.data, o.data))
4741
throw new TypeError('Data mismatch');
4842
}
4943
}

src/payments/p2ms.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ const types_1 = require('../types');
77
const lazy = require('./lazy');
88
const OPS = bscript.OPS;
99
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
10-
function stacksEqual(a, b) {
11-
if (a.length !== b.length) return false;
12-
return a.every((x, i) => {
13-
return x.equals(b[i]);
14-
});
15-
}
1610
// input: OP_0 [signatures ...]
1711
// output: m [pubKeys ...] n OP_CHECKMULTISIG
1812
function p2ms(a, opts) {
@@ -117,7 +111,7 @@ function p2ms(a, opts) {
117111
throw new TypeError('Output is invalid');
118112
if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch');
119113
if (a.n !== undefined && a.n !== o.n) throw new TypeError('n mismatch');
120-
if (a.pubkeys && !stacksEqual(a.pubkeys, o.pubkeys))
114+
if (a.pubkeys && !(0, types_1.stacksEqual)(a.pubkeys, o.pubkeys))
121115
throw new TypeError('Pubkeys mismatch');
122116
}
123117
if (a.pubkeys) {
@@ -139,7 +133,7 @@ function p2ms(a, opts) {
139133
!o.signatures.every(isAcceptableSignature)
140134
)
141135
throw new TypeError('Input has invalid signature(s)');
142-
if (a.signatures && !stacksEqual(a.signatures, o.signatures))
136+
if (a.signatures && !(0, types_1.stacksEqual)(a.signatures, o.signatures))
143137
throw new TypeError('Signature mismatch');
144138
if (a.m !== undefined && a.m !== a.signatures.length)
145139
throw new TypeError('Signature count mismatch');

src/payments/p2sh.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ const types_1 = require('../types');
88
const lazy = require('./lazy');
99
const bs58check = require('bs58check');
1010
const OPS = bscript.OPS;
11-
function stacksEqual(a, b) {
12-
if (a.length !== b.length) return false;
13-
return a.every((x, i) => {
14-
return x.equals(b[i]);
15-
});
16-
}
1711
// input: [redeemScriptSig ...] {redeemScript}
1812
// witness: <?>
1913
// output: OP_HASH160 {hash160(redeemScript)} OP_EQUAL
@@ -188,7 +182,7 @@ function p2sh(a, opts) {
188182
if (
189183
a.redeem &&
190184
a.redeem.witness &&
191-
!stacksEqual(a.redeem.witness, a.witness)
185+
!(0, types_1.stacksEqual)(a.redeem.witness, a.witness)
192186
)
193187
throw new TypeError('Witness and redeem.witness mismatch');
194188
}

src/payments/p2tr.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const ecc_lib_1 = require('../ecc_lib');
99
const bip341_1 = require('./bip341');
1010
const lazy = require('./lazy');
1111
const bech32_1 = require('bech32');
12+
const address_1 = require('../address');
1213
const OPS = bscript.OPS;
1314
const TAPROOT_WITNESS_VERSION = 0x01;
1415
const ANNEX_PREFIX = 0x50;
@@ -53,14 +54,7 @@ function p2tr(a, opts) {
5354
a,
5455
);
5556
const _address = lazy.value(() => {
56-
const result = bech32_1.bech32m.decode(a.address);
57-
const version = result.words.shift();
58-
const data = bech32_1.bech32m.fromWords(result.words);
59-
return {
60-
version,
61-
prefix: result.prefix,
62-
data: buffer_1.Buffer.from(data),
63-
};
57+
return (0, address_1.fromBech32)(a.address);
6458
});
6559
// remove annex if present, ignored by taproot
6660
const _witness = lazy.value(() => {
@@ -237,7 +231,7 @@ function p2tr(a, opts) {
237231
if (a.redeem.witness) {
238232
if (
239233
o.redeem.witness &&
240-
!stacksEqual(a.redeem.witness, o.redeem.witness)
234+
!(0, types_1.stacksEqual)(a.redeem.witness, o.redeem.witness)
241235
)
242236
throw new TypeError('Redeem.witness and witness mismatch');
243237
}
@@ -289,9 +283,3 @@ function p2tr(a, opts) {
289283
return Object.assign(o, a);
290284
}
291285
exports.p2tr = p2tr;
292-
function stacksEqual(a, b) {
293-
if (a.length !== b.length) return false;
294-
return a.every((x, i) => {
295-
return x.equals(b[i]);
296-
});
297-
}

src/payments/p2wsh.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ const lazy = require('./lazy');
99
const bech32_1 = require('bech32');
1010
const OPS = bscript.OPS;
1111
const EMPTY_BUFFER = Buffer.alloc(0);
12-
function stacksEqual(a, b) {
13-
if (a.length !== b.length) return false;
14-
return a.every((x, i) => {
15-
return x.equals(b[i]);
16-
});
17-
}
1812
function chunkHasUncompressedPubkey(chunk) {
1913
if (
2014
Buffer.isBuffer(chunk) &&
@@ -190,7 +184,7 @@ function p2wsh(a, opts) {
190184
if (
191185
a.witness &&
192186
a.redeem.witness &&
193-
!stacksEqual(a.witness, a.redeem.witness)
187+
!(0, types_1.stacksEqual)(a.witness, a.redeem.witness)
194188
)
195189
throw new TypeError('Witness and redeem.witness mismatch');
196190
if (

src/script_signature.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Object.defineProperty(exports, '__esModule', { value: true });
33
exports.encode = exports.decode = void 0;
44
const bip66 = require('./bip66');
5+
const script_1 = require('./script');
56
const types = require('./types');
67
const { typeforce } = types;
78
const ZERO = Buffer.alloc(1, 0);
@@ -23,9 +24,9 @@ function fromDER(x) {
2324
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
2425
function decode(buffer) {
2526
const hashType = buffer.readUInt8(buffer.length - 1);
26-
const hashTypeMod = hashType & ~0x80;
27-
if (hashTypeMod <= 0 || hashTypeMod >= 4)
27+
if (!(0, script_1.isDefinedHashType)(hashType)) {
2828
throw new Error('Invalid hashType ' + hashType);
29+
}
2930
const decoded = bip66.decode(buffer.slice(0, -1));
3031
const r = fromDER(decoded.r);
3132
const s = fromDER(decoded.s);
@@ -41,9 +42,9 @@ function encode(signature, hashType) {
4142
},
4243
{ signature, hashType },
4344
);
44-
const hashTypeMod = hashType & ~0x80;
45-
if (hashTypeMod <= 0 || hashTypeMod >= 4)
45+
if (!(0, script_1.isDefinedHashType)(hashType)) {
4646
throw new Error('Invalid hashType ' + hashType);
47+
}
4748
const hashTypeBuffer = Buffer.allocUnsafe(1);
4849
hashTypeBuffer.writeUInt8(hashType, 0);
4950
const r = toDER(signature.slice(0, 32));

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference types="node" />
22
export declare const typeforce: any;
3+
export declare function stacksEqual(a: Buffer[], b: Buffer[]): boolean;
34
export declare function isPoint(p: Buffer | number | undefined | null): boolean;
45
export declare function UInt31(value: number): boolean;
56
export declare function BIP32Path(value: string): boolean;

src/types.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ exports.oneOf =
2727
exports.BIP32Path =
2828
exports.UInt31 =
2929
exports.isPoint =
30+
exports.stacksEqual =
3031
exports.typeforce =
3132
void 0;
3233
const buffer_1 = require('buffer');
@@ -36,6 +37,13 @@ const EC_P = buffer_1.Buffer.from(
3637
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
3738
'hex',
3839
);
40+
function stacksEqual(a, b) {
41+
if (a.length !== b.length) return false;
42+
return a.every((x, i) => {
43+
return x.equals(b[i]);
44+
});
45+
}
46+
exports.stacksEqual = stacksEqual;
3947
function isPoint(p) {
4048
if (!buffer_1.Buffer.isBuffer(p)) return false;
4149
if (p.length < 33) return false;

ts_src/payments/embed.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
import { bitcoin as BITCOIN_NETWORK } from '../networks';
22
import * as bscript from '../script';
3-
import { typeforce as typef } from '../types';
3+
import { typeforce as typef, stacksEqual } from '../types';
44
import { Payment, PaymentOpts, Stack } from './index';
55
import * as lazy from './lazy';
66

77
const OPS = bscript.OPS;
88

9-
function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
10-
if (a.length !== b.length) return false;
11-
12-
return a.every((x, i) => {
13-
return x.equals(b[i]);
14-
});
15-
}
16-
179
// output: OP_RETURN ...
1810
export function p2data(a: Payment, opts?: PaymentOpts): Payment {
1911
if (!a.data && !a.output) throw new TypeError('Not enough data');

ts_src/payments/p2ms.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
import { bitcoin as BITCOIN_NETWORK } from '../networks';
22
import * as bscript from '../script';
3-
import { isPoint, typeforce as typef } from '../types';
3+
import { isPoint, typeforce as typef, stacksEqual } from '../types';
44
import { Payment, PaymentOpts, Stack } from './index';
55
import * as lazy from './lazy';
66
const OPS = bscript.OPS;
77

88
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
99

10-
function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
11-
if (a.length !== b.length) return false;
12-
13-
return a.every((x, i) => {
14-
return x.equals(b[i]);
15-
});
16-
}
17-
1810
// input: OP_0 [signatures ...]
1911
// output: m [pubKeys ...] n OP_CHECKMULTISIG
2012
export function p2ms(a: Payment, opts?: PaymentOpts): Payment {

0 commit comments

Comments
 (0)