Skip to content

Commit c6cb2f8

Browse files
committed
Merge branch 'doc' of https://github.com/jasonandjay/bitcoinjs-lib into doc
2 parents 3dc81f7 + 5655cd4 commit c6cb2f8

File tree

17 files changed

+325
-231
lines changed

17 files changed

+325
-231
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ We are always accepting of pull requests, but we do adhere to specific standards
1414
GitHub is the preferred method of communication between members.
1515

1616
Otherwise, in order of preference:
17-
* bitcoinjs.slack.com
18-
* #bitcoinjs-dev on Freenode IRC
17+
* #bitcoinjs-dev:matrix.org on Matrix (A part of the #bitcoinjs-space:matrix.org "Space")
18+
* #bitcoinjs on libera.chat IRC
1919

2020

2121
## Workflow

package-lock.json

Lines changed: 257 additions & 124 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
/**
1711
* Embeds data in a Bitcoin payment.
@@ -50,7 +44,7 @@ function p2data(a, opts) {
5044
if (chunks[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid');
5145
if (!chunks.slice(1).every(types_1.typeforce.Buffer))
5246
throw new TypeError('Output is invalid');
53-
if (a.data && !stacksEqual(a.data, o.data))
47+
if (a.data && !(0, types_1.stacksEqual)(a.data, o.data))
5448
throw new TypeError('Data mismatch');
5549
}
5650
}

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
/**
@@ -124,7 +118,7 @@ function p2ms(a, opts) {
124118
throw new TypeError('Output is invalid');
125119
if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch');
126120
if (a.n !== undefined && a.n !== o.n) throw new TypeError('n mismatch');
127-
if (a.pubkeys && !stacksEqual(a.pubkeys, o.pubkeys))
121+
if (a.pubkeys && !(0, types_1.stacksEqual)(a.pubkeys, o.pubkeys))
128122
throw new TypeError('Pubkeys mismatch');
129123
}
130124
if (a.pubkeys) {
@@ -146,7 +140,7 @@ function p2ms(a, opts) {
146140
!o.signatures.every(isAcceptableSignature)
147141
)
148142
throw new TypeError('Input has invalid signature(s)');
149-
if (a.signatures && !stacksEqual(a.signatures, o.signatures))
143+
if (a.signatures && !(0, types_1.stacksEqual)(a.signatures, o.signatures))
150144
throw new TypeError('Signature mismatch');
151145
if (a.m !== undefined && a.m !== a.signatures.length)
152146
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
@@ -196,7 +190,7 @@ function p2sh(a, opts) {
196190
if (
197191
a.redeem &&
198192
a.redeem.witness &&
199-
!stacksEqual(a.redeem.witness, a.witness)
193+
!(0, types_1.stacksEqual)(a.redeem.witness, a.witness)
200194
)
201195
throw new TypeError('Witness and redeem.witness mismatch');
202196
}

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;
@@ -61,14 +62,7 @@ function p2tr(a, opts) {
6162
a,
6263
);
6364
const _address = lazy.value(() => {
64-
const result = bech32_1.bech32m.decode(a.address);
65-
const version = result.words.shift();
66-
const data = bech32_1.bech32m.fromWords(result.words);
67-
return {
68-
version,
69-
prefix: result.prefix,
70-
data: buffer_1.Buffer.from(data),
71-
};
65+
return (0, address_1.fromBech32)(a.address);
7266
});
7367
// remove annex if present, ignored by taproot
7468
const _witness = lazy.value(() => {
@@ -245,7 +239,7 @@ function p2tr(a, opts) {
245239
if (a.redeem.witness) {
246240
if (
247241
o.redeem.witness &&
248-
!stacksEqual(a.redeem.witness, o.redeem.witness)
242+
!(0, types_1.stacksEqual)(a.redeem.witness, o.redeem.witness)
249243
)
250244
throw new TypeError('Redeem.witness and witness mismatch');
251245
}
@@ -297,9 +291,3 @@ function p2tr(a, opts) {
297291
return Object.assign(o, a);
298292
}
299293
exports.p2tr = p2tr;
300-
function stacksEqual(a, b) {
301-
if (a.length !== b.length) return false;
302-
return a.every((x, i) => {
303-
return x.equals(b[i]);
304-
});
305-
}

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) &&
@@ -198,7 +192,7 @@ function p2wsh(a, opts) {
198192
if (
199193
a.witness &&
200194
a.redeem.witness &&
201-
!stacksEqual(a.witness, a.redeem.witness)
195+
!(0, types_1.stacksEqual)(a.witness, a.redeem.witness)
202196
)
203197
throw new TypeError('Witness and redeem.witness mismatch');
204198
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);
@@ -41,9 +42,9 @@ function fromDER(x) {
4142
*/
4243
function decode(buffer) {
4344
const hashType = buffer.readUInt8(buffer.length - 1);
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 decoded = bip66.decode(buffer.slice(0, -1));
4849
const r = fromDER(decoded.r);
4950
const s = fromDER(decoded.s);
@@ -66,9 +67,9 @@ function encode(signature, hashType) {
6667
},
6768
{ signature, hashType },
6869
);
69-
const hashTypeMod = hashType & ~0x80;
70-
if (hashTypeMod <= 0 || hashTypeMod >= 4)
70+
if (!(0, script_1.isDefinedHashType)(hashType)) {
7171
throw new Error('Invalid hashType ' + hashType);
72+
}
7273
const hashTypeBuffer = Buffer.allocUnsafe(1);
7374
hashTypeBuffer.writeUInt8(hashType, 0);
7475
const r = toDER(signature.slice(0, 32));

src/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/// <reference types="node" />
22
export declare const typeforce: any;
3+
/**
4+
* Checks if two arrays of Buffers are equal.
5+
* @param a - The first array of Buffers.
6+
* @param b - The second array of Buffers.
7+
* @returns True if the arrays are equal, false otherwise.
8+
*/
9+
export declare function stacksEqual(a: Buffer[], b: Buffer[]): boolean;
310
/**
411
* Checks if the given value is a valid elliptic curve point.
512
* @param p - The value to check.

src/types.js

Lines changed: 14 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,19 @@ const EC_P = buffer_1.Buffer.from(
3637
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
3738
'hex',
3839
);
40+
/**
41+
* Checks if two arrays of Buffers are equal.
42+
* @param a - The first array of Buffers.
43+
* @param b - The second array of Buffers.
44+
* @returns True if the arrays are equal, false otherwise.
45+
*/
46+
function stacksEqual(a, b) {
47+
if (a.length !== b.length) return false;
48+
return a.every((x, i) => {
49+
return x.equals(b[i]);
50+
});
51+
}
52+
exports.stacksEqual = stacksEqual;
3953
/**
4054
* Checks if the given value is a valid elliptic curve point.
4155
* @param p - The value to check.

0 commit comments

Comments
 (0)