Skip to content

Commit 17c47e9

Browse files
committed
Merge branch 'master' into addPsbtMethods
2 parents f87a20c + cd45774 commit 17c47e9

22 files changed

+410
-77
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
# 5.1.10
2+
__fixed__
3+
- Fixed psbt.signInputAsync (and consequentially all Async signing methods) not handling rejection of keypair.sign properly (#1582)
4+
5+
# 5.1.9
6+
__fixed__
7+
- Fixed errors for psbt.txOutputs getter (#1578)
8+
9+
# 5.1.8
10+
__fixed__
11+
- Throw errors when p2wsh or p2wpkh contain uncompressed pubkeys (#1573)
12+
13+
__added__
14+
- Add txInputs and txOutputs for Psbt (#1561)
15+
16+
__changed__
17+
- (Not exposed) Added BufferWriter to help ease maintenance of certain forks of this library (#1533)
18+
119
# 5.1.7
220
__fixed__
321
- Fixed Transaction class Output interface typing for TypeScript (#1506)

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitcoinjs-lib",
3-
"version": "5.1.7",
3+
"version": "5.1.10",
44
"description": "Client-side Bitcoin JavaScript library",
55
"main": "./src/index.js",
66
"types": "./types/index.d.ts",

src/payments/p2wpkh.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,14 @@ function p2wpkh(a, opts) {
107107
if (hash.length > 0 && !hash.equals(pkh))
108108
throw new TypeError('Hash mismatch');
109109
else hash = pkh;
110+
if (!ecc.isPoint(a.pubkey) || a.pubkey.length !== 33)
111+
throw new TypeError('Invalid pubkey for p2wpkh');
110112
}
111113
if (a.witness) {
112114
if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
113115
if (!bscript.isCanonicalScriptSignature(a.witness[0]))
114116
throw new TypeError('Witness has invalid signature');
115-
if (!ecc.isPoint(a.witness[1]))
117+
if (!ecc.isPoint(a.witness[1]) || a.witness[1].length !== 33)
116118
throw new TypeError('Witness has invalid pubkey');
117119
if (a.signature && !a.signature.equals(a.witness[0]))
118120
throw new TypeError('Signature mismatch');

src/payments/p2wsh.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const bscript = require('../script');
66
const lazy = require('./lazy');
77
const typef = require('typeforce');
88
const OPS = bscript.OPS;
9+
const ecc = require('tiny-secp256k1');
910
const bech32 = require('bech32');
1011
const EMPTY_BUFFER = Buffer.alloc(0);
1112
function stacksEqual(a, b) {
@@ -14,6 +15,18 @@ function stacksEqual(a, b) {
1415
return x.equals(b[i]);
1516
});
1617
}
18+
function chunkHasUncompressedPubkey(chunk) {
19+
if (
20+
Buffer.isBuffer(chunk) &&
21+
chunk.length === 65 &&
22+
chunk[0] === 0x04 &&
23+
ecc.isPoint(chunk)
24+
) {
25+
return true;
26+
} else {
27+
return false;
28+
}
29+
}
1730
// input: <>
1831
// witness: [redeemScriptSig ...] {redeemScript}
1932
// output: OP_0 {sha256(redeemScript)}
@@ -166,14 +179,27 @@ function p2wsh(a, opts) {
166179
!stacksEqual(a.witness, a.redeem.witness)
167180
)
168181
throw new TypeError('Witness and redeem.witness mismatch');
182+
if (
183+
(a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) ||
184+
(a.redeem.output &&
185+
(bscript.decompile(a.redeem.output) || []).some(
186+
chunkHasUncompressedPubkey,
187+
))
188+
) {
189+
throw new TypeError(
190+
'redeem.input or redeem.output contains uncompressed pubkey',
191+
);
192+
}
169193
}
170-
if (a.witness) {
194+
if (a.witness && a.witness.length > 0) {
195+
const wScript = a.witness[a.witness.length - 1];
196+
if (a.redeem && a.redeem.output && !a.redeem.output.equals(wScript))
197+
throw new TypeError('Witness and redeem.output mismatch');
171198
if (
172-
a.redeem &&
173-
a.redeem.output &&
174-
!a.redeem.output.equals(a.witness[a.witness.length - 1])
199+
a.witness.some(chunkHasUncompressedPubkey) ||
200+
(bscript.decompile(wScript) || []).some(chunkHasUncompressedPubkey)
175201
)
176-
throw new TypeError('Witness and redeem.output mismatch');
202+
throw new TypeError('Witness contains uncompressed pubkey');
177203
}
178204
}
179205
return Object.assign(o, a);

src/psbt.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,17 @@ class Psbt {
125125
}));
126126
}
127127
get txOutputs() {
128-
return this.__CACHE.__TX.outs.map(output => ({
129-
script: bufferutils_1.cloneBuffer(output.script),
130-
value: output.value,
131-
address: address_1.fromOutputScript(output.script, this.opts.network),
132-
}));
128+
return this.__CACHE.__TX.outs.map(output => {
129+
let address;
130+
try {
131+
address = address_1.fromOutputScript(output.script, this.opts.network);
132+
} catch (_) {}
133+
return {
134+
script: bufferutils_1.cloneBuffer(output.script),
135+
value: output.value,
136+
address,
137+
};
138+
});
133139
}
134140
combine(...those) {
135141
this.data.combine(...those.map(o => o.data));
@@ -529,25 +535,24 @@ class Psbt {
529535
keyPair,
530536
sighashTypes = [transaction_1.Transaction.SIGHASH_ALL],
531537
) {
532-
return new Promise((resolve, reject) => {
538+
return Promise.resolve().then(() => {
533539
if (!keyPair || !keyPair.publicKey)
534-
return reject(new Error('Need Signer to sign input'));
540+
throw new Error('Need Signer to sign input');
535541
const { hash, sighashType } = getHashAndSighashType(
536542
this.data.inputs,
537543
inputIndex,
538544
keyPair.publicKey,
539545
this.__CACHE,
540546
sighashTypes,
541547
);
542-
Promise.resolve(keyPair.sign(hash)).then(signature => {
548+
return Promise.resolve(keyPair.sign(hash)).then(signature => {
543549
const partialSig = [
544550
{
545551
pubkey: keyPair.publicKey,
546552
signature: bscript.signature.encode(signature, sighashType),
547553
},
548554
];
549555
this.data.updateInput(inputIndex, { partialSig });
550-
resolve();
551556
});
552557
});
553558
}

test/bufferutils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('bufferutils', () => {
4242
});
4343
});
4444

45-
fixtures.invalid.readUInt64LE.forEach(f => {
45+
fixtures.invalid.writeUInt64LE.forEach(f => {
4646
it('throws on ' + f.description, () => {
4747
const buffer = Buffer.alloc(8, 0);
4848

test/ecpair.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ describe('ECPair', () => {
146146
assert.strictEqual(result, f.WIF);
147147
});
148148
});
149+
it('throws if no private key is found', () => {
150+
assert.throws(() => {
151+
const keyPair = ECPair.makeRandom();
152+
delete (keyPair as any).__D;
153+
keyPair.toWIF();
154+
}, /Missing private key/);
155+
});
149156
});
150157

151158
describe('makeRandom', () => {

test/fixtures/address.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@
189189
{
190190
"exception": "has no matching Script",
191191
"address": "BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2"
192+
},
193+
{
194+
"exception": "has no matching Script",
195+
"address": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"
196+
},
197+
{
198+
"exception": "has no matching Script",
199+
"address": "bc1qqqqqqqqqqv9qus"
192200
}
193201
]
194202
}

test/fixtures/bufferutils.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@
7171
"hex": "0100000000002000",
7272
"dec": 9007199254740993
7373
}
74+
],
75+
"writeUInt64LE": [
76+
{
77+
"description": "n === 2^53",
78+
"exception": "RangeError: value out of range",
79+
"hex": "0000000000002000",
80+
"dec": 9007199254740992
81+
},
82+
{
83+
"description": "n > 2^53",
84+
"exception": "RangeError: value out of range",
85+
"hex": "0100000000002000",
86+
"dec": 9007199254740993
87+
},
88+
{
89+
"description": "n < 0",
90+
"exception": "specified a negative value for writing an unsigned value",
91+
"hex": "",
92+
"dec": -1
93+
},
94+
{
95+
"description": "0 < n < 1",
96+
"exception": "value has a fractional component",
97+
"hex": "",
98+
"dec": 0.1
99+
}
74100
]
75101
}
76102
}

0 commit comments

Comments
 (0)