Merge branch 'master' into addPsbtMethods
This commit is contained in:
commit
17c47e9102
22 changed files with 412 additions and 79 deletions
18
CHANGELOG.md
18
CHANGELOG.md
|
@ -1,3 +1,21 @@
|
|||
# 5.1.10
|
||||
__fixed__
|
||||
- Fixed psbt.signInputAsync (and consequentially all Async signing methods) not handling rejection of keypair.sign properly (#1582)
|
||||
|
||||
# 5.1.9
|
||||
__fixed__
|
||||
- Fixed errors for psbt.txOutputs getter (#1578)
|
||||
|
||||
# 5.1.8
|
||||
__fixed__
|
||||
- Throw errors when p2wsh or p2wpkh contain uncompressed pubkeys (#1573)
|
||||
|
||||
__added__
|
||||
- Add txInputs and txOutputs for Psbt (#1561)
|
||||
|
||||
__changed__
|
||||
- (Not exposed) Added BufferWriter to help ease maintenance of certain forks of this library (#1533)
|
||||
|
||||
# 5.1.7
|
||||
__fixed__
|
||||
- Fixed Transaction class Output interface typing for TypeScript (#1506)
|
||||
|
|
15
package-lock.json
generated
15
package-lock.json
generated
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "bitcoinjs-lib",
|
||||
"version": "5.1.7",
|
||||
"version": "5.1.10",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
@ -750,9 +750,9 @@
|
|||
"dev": true
|
||||
},
|
||||
"elliptic": {
|
||||
"version": "6.4.1",
|
||||
"resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.4.1.tgz",
|
||||
"integrity": "sha512-BsXLz5sqX8OHcsh7CqBMztyXARmGQ3LWPtGjJi6DiJHq5C/qvi9P3OqgswKSDftbu8+IoI/QDTAm2fFnQ9SZSQ==",
|
||||
"version": "6.5.3",
|
||||
"resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.3.tgz",
|
||||
"integrity": "sha512-IMqzv5wNQf+E6aHeIqATs0tOLeOTwj1QKbRcS3jBbYkl5oLAserA8yJTT7/VyHUYG91PRmPyeQDObKLPpeS4dw==",
|
||||
"requires": {
|
||||
"bn.js": "^4.4.0",
|
||||
"brorand": "^1.0.1",
|
||||
|
@ -1342,10 +1342,9 @@
|
|||
}
|
||||
},
|
||||
"lodash": {
|
||||
"version": "4.17.15",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
|
||||
"dev": true
|
||||
"version": "4.17.19",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
|
||||
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
|
||||
},
|
||||
"lodash.flattendeep": {
|
||||
"version": "4.4.0",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "bitcoinjs-lib",
|
||||
"version": "5.1.7",
|
||||
"version": "5.1.10",
|
||||
"description": "Client-side Bitcoin JavaScript library",
|
||||
"main": "./src/index.js",
|
||||
"types": "./types/index.d.ts",
|
||||
|
|
|
@ -107,12 +107,14 @@ function p2wpkh(a, opts) {
|
|||
if (hash.length > 0 && !hash.equals(pkh))
|
||||
throw new TypeError('Hash mismatch');
|
||||
else hash = pkh;
|
||||
if (!ecc.isPoint(a.pubkey) || a.pubkey.length !== 33)
|
||||
throw new TypeError('Invalid pubkey for p2wpkh');
|
||||
}
|
||||
if (a.witness) {
|
||||
if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
|
||||
if (!bscript.isCanonicalScriptSignature(a.witness[0]))
|
||||
throw new TypeError('Witness has invalid signature');
|
||||
if (!ecc.isPoint(a.witness[1]))
|
||||
if (!ecc.isPoint(a.witness[1]) || a.witness[1].length !== 33)
|
||||
throw new TypeError('Witness has invalid pubkey');
|
||||
if (a.signature && !a.signature.equals(a.witness[0]))
|
||||
throw new TypeError('Signature mismatch');
|
||||
|
|
|
@ -6,6 +6,7 @@ const bscript = require('../script');
|
|||
const lazy = require('./lazy');
|
||||
const typef = require('typeforce');
|
||||
const OPS = bscript.OPS;
|
||||
const ecc = require('tiny-secp256k1');
|
||||
const bech32 = require('bech32');
|
||||
const EMPTY_BUFFER = Buffer.alloc(0);
|
||||
function stacksEqual(a, b) {
|
||||
|
@ -14,6 +15,18 @@ function stacksEqual(a, b) {
|
|||
return x.equals(b[i]);
|
||||
});
|
||||
}
|
||||
function chunkHasUncompressedPubkey(chunk) {
|
||||
if (
|
||||
Buffer.isBuffer(chunk) &&
|
||||
chunk.length === 65 &&
|
||||
chunk[0] === 0x04 &&
|
||||
ecc.isPoint(chunk)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// input: <>
|
||||
// witness: [redeemScriptSig ...] {redeemScript}
|
||||
// output: OP_0 {sha256(redeemScript)}
|
||||
|
@ -166,14 +179,27 @@ function p2wsh(a, opts) {
|
|||
!stacksEqual(a.witness, a.redeem.witness)
|
||||
)
|
||||
throw new TypeError('Witness and redeem.witness mismatch');
|
||||
}
|
||||
if (a.witness) {
|
||||
if (
|
||||
a.redeem &&
|
||||
a.redeem.output &&
|
||||
!a.redeem.output.equals(a.witness[a.witness.length - 1])
|
||||
)
|
||||
(a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) ||
|
||||
(a.redeem.output &&
|
||||
(bscript.decompile(a.redeem.output) || []).some(
|
||||
chunkHasUncompressedPubkey,
|
||||
))
|
||||
) {
|
||||
throw new TypeError(
|
||||
'redeem.input or redeem.output contains uncompressed pubkey',
|
||||
);
|
||||
}
|
||||
}
|
||||
if (a.witness && a.witness.length > 0) {
|
||||
const wScript = a.witness[a.witness.length - 1];
|
||||
if (a.redeem && a.redeem.output && !a.redeem.output.equals(wScript))
|
||||
throw new TypeError('Witness and redeem.output mismatch');
|
||||
if (
|
||||
a.witness.some(chunkHasUncompressedPubkey) ||
|
||||
(bscript.decompile(wScript) || []).some(chunkHasUncompressedPubkey)
|
||||
)
|
||||
throw new TypeError('Witness contains uncompressed pubkey');
|
||||
}
|
||||
}
|
||||
return Object.assign(o, a);
|
||||
|
|
23
src/psbt.js
23
src/psbt.js
|
@ -125,11 +125,17 @@ class Psbt {
|
|||
}));
|
||||
}
|
||||
get txOutputs() {
|
||||
return this.__CACHE.__TX.outs.map(output => ({
|
||||
script: bufferutils_1.cloneBuffer(output.script),
|
||||
value: output.value,
|
||||
address: address_1.fromOutputScript(output.script, this.opts.network),
|
||||
}));
|
||||
return this.__CACHE.__TX.outs.map(output => {
|
||||
let address;
|
||||
try {
|
||||
address = address_1.fromOutputScript(output.script, this.opts.network);
|
||||
} catch (_) {}
|
||||
return {
|
||||
script: bufferutils_1.cloneBuffer(output.script),
|
||||
value: output.value,
|
||||
address,
|
||||
};
|
||||
});
|
||||
}
|
||||
combine(...those) {
|
||||
this.data.combine(...those.map(o => o.data));
|
||||
|
@ -529,9 +535,9 @@ class Psbt {
|
|||
keyPair,
|
||||
sighashTypes = [transaction_1.Transaction.SIGHASH_ALL],
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return Promise.resolve().then(() => {
|
||||
if (!keyPair || !keyPair.publicKey)
|
||||
return reject(new Error('Need Signer to sign input'));
|
||||
throw new Error('Need Signer to sign input');
|
||||
const { hash, sighashType } = getHashAndSighashType(
|
||||
this.data.inputs,
|
||||
inputIndex,
|
||||
|
@ -539,7 +545,7 @@ class Psbt {
|
|||
this.__CACHE,
|
||||
sighashTypes,
|
||||
);
|
||||
Promise.resolve(keyPair.sign(hash)).then(signature => {
|
||||
return Promise.resolve(keyPair.sign(hash)).then(signature => {
|
||||
const partialSig = [
|
||||
{
|
||||
pubkey: keyPair.publicKey,
|
||||
|
@ -547,7 +553,6 @@ class Psbt {
|
|||
},
|
||||
];
|
||||
this.data.updateInput(inputIndex, { partialSig });
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ describe('bufferutils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
fixtures.invalid.readUInt64LE.forEach(f => {
|
||||
fixtures.invalid.writeUInt64LE.forEach(f => {
|
||||
it('throws on ' + f.description, () => {
|
||||
const buffer = Buffer.alloc(8, 0);
|
||||
|
||||
|
|
|
@ -146,6 +146,13 @@ describe('ECPair', () => {
|
|||
assert.strictEqual(result, f.WIF);
|
||||
});
|
||||
});
|
||||
it('throws if no private key is found', () => {
|
||||
assert.throws(() => {
|
||||
const keyPair = ECPair.makeRandom();
|
||||
delete (keyPair as any).__D;
|
||||
keyPair.toWIF();
|
||||
}, /Missing private key/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('makeRandom', () => {
|
||||
|
|
8
test/fixtures/address.json
vendored
8
test/fixtures/address.json
vendored
|
@ -189,6 +189,14 @@
|
|||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2"
|
||||
},
|
||||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"
|
||||
},
|
||||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "bc1qqqqqqqqqqv9qus"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
26
test/fixtures/bufferutils.json
vendored
26
test/fixtures/bufferutils.json
vendored
|
@ -71,6 +71,32 @@
|
|||
"hex": "0100000000002000",
|
||||
"dec": 9007199254740993
|
||||
}
|
||||
],
|
||||
"writeUInt64LE": [
|
||||
{
|
||||
"description": "n === 2^53",
|
||||
"exception": "RangeError: value out of range",
|
||||
"hex": "0000000000002000",
|
||||
"dec": 9007199254740992
|
||||
},
|
||||
{
|
||||
"description": "n > 2^53",
|
||||
"exception": "RangeError: value out of range",
|
||||
"hex": "0100000000002000",
|
||||
"dec": 9007199254740993
|
||||
},
|
||||
{
|
||||
"description": "n < 0",
|
||||
"exception": "specified a negative value for writing an unsigned value",
|
||||
"hex": "",
|
||||
"dec": -1
|
||||
},
|
||||
{
|
||||
"description": "0 < n < 1",
|
||||
"exception": "value has a fractional component",
|
||||
"hex": "",
|
||||
"dec": 0.1
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
30
test/fixtures/embed.json
vendored
30
test/fixtures/embed.json
vendored
|
@ -44,6 +44,36 @@
|
|||
"arguments": {
|
||||
"output": "OP_1 OP_2 OP_ADD"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Return value and data do not match",
|
||||
"exception": "Data mismatch",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4",
|
||||
"data": [
|
||||
"a3b147dbe4a85579fc4b5a1855555555555555555555555555555555555555555555555555555555"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Script length incorrect",
|
||||
"exception": "Data mismatch",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN a3b1 47db",
|
||||
"data": [
|
||||
"a3b1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Return data is not buffer",
|
||||
"exception": "Output is invalid",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN OP_1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"dynamic": {
|
||||
|
|
20
test/fixtures/p2sh.json
vendored
20
test/fixtures/p2sh.json
vendored
|
@ -323,6 +323,26 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Input and witness provided",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "OP_0",
|
||||
"witness": [
|
||||
"030000000000000000000000000000000000000000000000000000000000000001"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Non push-only scriptSig",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "OP_RETURN",
|
||||
"output": "OP_1"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Redeem.output too short",
|
||||
"arguments": {
|
||||
|
|
27
test/fixtures/p2wpkh.json
vendored
27
test/fixtures/p2wpkh.json
vendored
|
@ -113,6 +113,23 @@
|
|||
"output": "OP_RESERVED ea6d525c0c955d90d3dbd29a81ef8bfb79003727"
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Invalid pubkey for p2wpkh",
|
||||
"description": "Uncompressed pubkey in pubkey",
|
||||
"arguments": {
|
||||
"pubkey": "049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Witness has invalid pubkey",
|
||||
"description": "Uncompressed pubkey in witness",
|
||||
"arguments": {
|
||||
"witness": [
|
||||
"300602010002010001",
|
||||
"049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Pubkey mismatch",
|
||||
"options": {},
|
||||
|
@ -124,6 +141,16 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Signature mismatch",
|
||||
"arguments": {
|
||||
"signature": "300602010002010002",
|
||||
"witness": [
|
||||
"300602010002010001",
|
||||
"030000000000000000000000000000000000000000000000000000000000000001"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Invalid prefix or Network mismatch",
|
||||
"arguments": {
|
||||
|
|
24
test/fixtures/p2wsh.json
vendored
24
test/fixtures/p2wsh.json
vendored
|
@ -344,6 +344,30 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"output": "049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457 OP_CHECKSIG"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Witness contains uncompressed pubkey",
|
||||
"arguments": {
|
||||
"witness": [
|
||||
"049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Invalid prefix or Network mismatch",
|
||||
"arguments": {
|
||||
|
|
4
test/fixtures/transaction_builder.json
vendored
4
test/fixtures/transaction_builder.json
vendored
|
@ -2122,7 +2122,7 @@
|
|||
},
|
||||
{
|
||||
"description": "Transaction w/ P2WSH(P2PK), signing with uncompressed public key",
|
||||
"exception": "BIP143 rejects uncompressed public keys in P2WPKH or P2WSH",
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"inputs": [
|
||||
{
|
||||
"txId": "2fddebc1a7e67e04fc6b77645ae9ae10eeaa35e168606587d79b031ebca33345",
|
||||
|
@ -2148,7 +2148,7 @@
|
|||
},
|
||||
{
|
||||
"description": "Transaction w/ P2SH(P2WSH(P2PK)), signing with uncompressed public key",
|
||||
"exception": "BIP143 rejects uncompressed public keys in P2WPKH or P2WSH",
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"inputs": [
|
||||
{
|
||||
"txId": "2fddebc1a7e67e04fc6b77645ae9ae10eeaa35e168606587d79b031ebca33345",
|
||||
|
|
|
@ -649,7 +649,7 @@ function createPayment(_type: string, myKeys?: any[], network?: any): any {
|
|||
if (type.slice(0, 4) === 'p2ms') {
|
||||
payment = bitcoin.payments.p2ms({
|
||||
m,
|
||||
pubkeys: keys.map(key => key.publicKey).sort(),
|
||||
pubkeys: keys.map(key => key.publicKey).sort((a, b) => a.compare(b)),
|
||||
network,
|
||||
});
|
||||
} else if (['p2sh', 'p2wsh'].indexOf(type) > -1) {
|
||||
|
|
|
@ -2,7 +2,15 @@ import * as assert from 'assert';
|
|||
import * as crypto from 'crypto';
|
||||
import { describe, it } from 'mocha';
|
||||
|
||||
import { bip32, ECPair, networks as NETWORKS, payments, Psbt } from '..';
|
||||
import {
|
||||
bip32,
|
||||
ECPair,
|
||||
networks as NETWORKS,
|
||||
payments,
|
||||
Psbt,
|
||||
Signer,
|
||||
SignerAsync,
|
||||
} from '..';
|
||||
|
||||
import * as preFixtures from './fixtures/psbt.json';
|
||||
|
||||
|
@ -23,6 +31,40 @@ const fixtures = initBuffers(preFixtures);
|
|||
const upperCaseFirstLetter = (str: string): string =>
|
||||
str.replace(/^./, s => s.toUpperCase());
|
||||
|
||||
const toAsyncSigner = (signer: Signer): SignerAsync => {
|
||||
const ret: SignerAsync = {
|
||||
publicKey: signer.publicKey,
|
||||
sign: (hash: Buffer, lowerR: boolean | undefined): Promise<Buffer> => {
|
||||
return new Promise(
|
||||
(resolve, rejects): void => {
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const r = signer.sign(hash, lowerR);
|
||||
resolve(r);
|
||||
} catch (e) {
|
||||
rejects(e);
|
||||
}
|
||||
}, 10);
|
||||
},
|
||||
);
|
||||
},
|
||||
};
|
||||
return ret;
|
||||
};
|
||||
const failedAsyncSigner = (publicKey: Buffer): SignerAsync => {
|
||||
return {
|
||||
publicKey,
|
||||
sign: (__: Buffer): Promise<Buffer> => {
|
||||
return new Promise(
|
||||
(_, reject): void => {
|
||||
setTimeout(() => {
|
||||
reject(new Error('sign failed'));
|
||||
}, 10);
|
||||
},
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
// const b = (hex: string) => Buffer.from(hex, 'hex');
|
||||
|
||||
describe(`Psbt`, () => {
|
||||
|
@ -165,25 +207,39 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signInputAsync(
|
||||
f.shouldSign.inputToCheck,
|
||||
ECPair.fromWIF(f.shouldSign.WIF),
|
||||
f.shouldSign.sighashTypes || undefined,
|
||||
);
|
||||
});
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldsign.signInputAsync(
|
||||
f.shouldSign.inputToCheck,
|
||||
failedAsyncSigner(ECPair.fromWIF(f.shouldSign.WIF).publicKey),
|
||||
f.shouldSign.sighashTypes || undefined,
|
||||
);
|
||||
}, /sign failed/);
|
||||
}
|
||||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signInputAsync(
|
||||
f.shouldThrow.inputToCheck,
|
||||
ECPair.fromWIF(f.shouldThrow.WIF),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp(f.shouldThrow.errorMessage));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signInputAsync(
|
||||
f.shouldThrow.inputToCheck,
|
||||
toAsyncSigner(ECPair.fromWIF(f.shouldThrow.WIF)),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp(f.shouldThrow.errorMessage));
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signInputAsync as any)(
|
||||
f.shouldThrow.inputToCheck,
|
||||
);
|
||||
|
@ -230,7 +286,7 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signAllInputsAsync(
|
||||
ECPair.fromWIF(f.shouldSign.WIF),
|
||||
f.shouldSign.sighashTypes || undefined,
|
||||
|
@ -240,13 +296,13 @@ describe(`Psbt`, () => {
|
|||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signAllInputsAsync(
|
||||
ECPair.fromWIF(f.shouldThrow.WIF),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp('No inputs were signed'));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signAllInputsAsync as any)();
|
||||
}, new RegExp('Need Signer to sign input'));
|
||||
}
|
||||
|
@ -289,7 +345,7 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signInputHDAsync(
|
||||
f.shouldSign.inputToCheck,
|
||||
bip32.fromBase58(f.shouldSign.xprv),
|
||||
|
@ -300,14 +356,14 @@ describe(`Psbt`, () => {
|
|||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signInputHDAsync(
|
||||
f.shouldThrow.inputToCheck,
|
||||
bip32.fromBase58(f.shouldThrow.xprv),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp(f.shouldThrow.errorMessage));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signInputHDAsync as any)(
|
||||
f.shouldThrow.inputToCheck,
|
||||
);
|
||||
|
@ -355,7 +411,7 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signAllInputsHDAsync(
|
||||
bip32.fromBase58(f.shouldSign.xprv),
|
||||
(f.shouldSign as any).sighashTypes || undefined,
|
||||
|
@ -365,13 +421,13 @@ describe(`Psbt`, () => {
|
|||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signAllInputsHDAsync(
|
||||
bip32.fromBase58(f.shouldThrow.xprv),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp('No inputs were signed'));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signAllInputsHDAsync as any)();
|
||||
}, new RegExp('Need HDSigner to sign input'));
|
||||
}
|
||||
|
|
|
@ -41,6 +41,21 @@ describe('script', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('toASM', () => {
|
||||
const OP_RETURN = bscript.OPS.OP_RETURN;
|
||||
it('encodes empty buffer as OP_0', () => {
|
||||
const chunks = [OP_RETURN, Buffer.from([])];
|
||||
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_0');
|
||||
});
|
||||
|
||||
for (let i = 1; i <= 16; i++) {
|
||||
it(`encodes one byte buffer [${i}] as OP_${i}`, () => {
|
||||
const chunks = [OP_RETURN, Buffer.from([i])];
|
||||
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_' + i);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('fromASM/toASM (templates)', () => {
|
||||
fixtures2.valid.forEach(f => {
|
||||
if (f.inputHex) {
|
||||
|
|
|
@ -54,4 +54,41 @@ describe('types', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('UInt31', () => {
|
||||
const UINT31_MAX = Math.pow(2, 31) - 1;
|
||||
it('return true for valid values', () => {
|
||||
assert.strictEqual(types.UInt31(0), true);
|
||||
assert.strictEqual(types.UInt31(1000), true);
|
||||
assert.strictEqual(types.UInt31(UINT31_MAX), true);
|
||||
});
|
||||
|
||||
it('return false for negative values', () => {
|
||||
assert.strictEqual(types.UInt31(-1), false);
|
||||
assert.strictEqual(types.UInt31(-UINT31_MAX), false);
|
||||
});
|
||||
|
||||
it(`return false for value > ${UINT31_MAX}`, () => {
|
||||
assert.strictEqual(types.UInt31(UINT31_MAX + 1), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BIP32Path', () => {
|
||||
it('return true for valid paths', () => {
|
||||
assert.strictEqual(types.BIP32Path("m/0'/0'"), true);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/0"), true);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/1'/2'/3/4'"), true);
|
||||
});
|
||||
|
||||
it('return false for invalid paths', () => {
|
||||
assert.strictEqual(types.BIP32Path('m'), false);
|
||||
assert.strictEqual(types.BIP32Path("n/0'/0'"), false);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/x"), false);
|
||||
});
|
||||
|
||||
it('return "BIP32 derivation path" for JSON.strigify()', () => {
|
||||
const toJsonValue = JSON.stringify(types.BIP32Path);
|
||||
assert.equal(toJsonValue, '"BIP32 derivation path"');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -118,13 +118,15 @@ export function p2wpkh(a: Payment, opts?: PaymentOpts): Payment {
|
|||
if (hash.length > 0 && !hash.equals(pkh))
|
||||
throw new TypeError('Hash mismatch');
|
||||
else hash = pkh;
|
||||
if (!ecc.isPoint(a.pubkey) || a.pubkey.length !== 33)
|
||||
throw new TypeError('Invalid pubkey for p2wpkh');
|
||||
}
|
||||
|
||||
if (a.witness) {
|
||||
if (a.witness.length !== 2) throw new TypeError('Witness is invalid');
|
||||
if (!bscript.isCanonicalScriptSignature(a.witness[0]))
|
||||
throw new TypeError('Witness has invalid signature');
|
||||
if (!ecc.isPoint(a.witness[1]))
|
||||
if (!ecc.isPoint(a.witness[1]) || a.witness[1].length !== 33)
|
||||
throw new TypeError('Witness has invalid pubkey');
|
||||
|
||||
if (a.signature && !a.signature.equals(a.witness[0]))
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import * as bcrypto from '../crypto';
|
||||
import { bitcoin as BITCOIN_NETWORK } from '../networks';
|
||||
import * as bscript from '../script';
|
||||
import { Payment, PaymentOpts, StackFunction } from './index';
|
||||
import { Payment, PaymentOpts, StackElement, StackFunction } from './index';
|
||||
import * as lazy from './lazy';
|
||||
const typef = require('typeforce');
|
||||
const OPS = bscript.OPS;
|
||||
const ecc = require('tiny-secp256k1');
|
||||
|
||||
const bech32 = require('bech32');
|
||||
|
||||
|
@ -18,6 +19,19 @@ function stacksEqual(a: Buffer[], b: Buffer[]): boolean {
|
|||
});
|
||||
}
|
||||
|
||||
function chunkHasUncompressedPubkey(chunk: StackElement): boolean {
|
||||
if (
|
||||
Buffer.isBuffer(chunk) &&
|
||||
chunk.length === 65 &&
|
||||
chunk[0] === 0x04 &&
|
||||
ecc.isPoint(chunk)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// input: <>
|
||||
// witness: [redeemScriptSig ...] {redeemScript}
|
||||
// output: OP_0 {sha256(redeemScript)}
|
||||
|
@ -187,15 +201,28 @@ export function p2wsh(a: Payment, opts?: PaymentOpts): Payment {
|
|||
!stacksEqual(a.witness, a.redeem.witness)
|
||||
)
|
||||
throw new TypeError('Witness and redeem.witness mismatch');
|
||||
if (
|
||||
(a.redeem.input && _rchunks().some(chunkHasUncompressedPubkey)) ||
|
||||
(a.redeem.output &&
|
||||
(bscript.decompile(a.redeem.output) || []).some(
|
||||
chunkHasUncompressedPubkey,
|
||||
))
|
||||
) {
|
||||
throw new TypeError(
|
||||
'redeem.input or redeem.output contains uncompressed pubkey',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (a.witness) {
|
||||
if (
|
||||
a.redeem &&
|
||||
a.redeem.output &&
|
||||
!a.redeem.output.equals(a.witness[a.witness.length - 1])
|
||||
)
|
||||
if (a.witness && a.witness.length > 0) {
|
||||
const wScript = a.witness[a.witness.length - 1];
|
||||
if (a.redeem && a.redeem.output && !a.redeem.output.equals(wScript))
|
||||
throw new TypeError('Witness and redeem.output mismatch');
|
||||
if (
|
||||
a.witness.some(chunkHasUncompressedPubkey) ||
|
||||
(bscript.decompile(wScript) || []).some(chunkHasUncompressedPubkey)
|
||||
)
|
||||
throw new TypeError('Witness contains uncompressed pubkey');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ import {
|
|||
Transaction as ITransaction,
|
||||
TransactionFromBuffer,
|
||||
TransactionInput,
|
||||
TransactionOutput,
|
||||
} from 'bip174/src/lib/interfaces';
|
||||
import { checkForInput, checkForOutput } from 'bip174/src/lib/utils';
|
||||
import { fromOutputScript, toOutputScript } from './address';
|
||||
|
@ -170,12 +171,18 @@ export class Psbt {
|
|||
}));
|
||||
}
|
||||
|
||||
get txOutputs(): PsbtTxOutput[] {
|
||||
return this.__CACHE.__TX.outs.map(output => ({
|
||||
script: cloneBuffer(output.script),
|
||||
value: output.value,
|
||||
address: fromOutputScript(output.script, this.opts.network),
|
||||
}));
|
||||
get txOutputs(): TransactionOutput[] {
|
||||
return this.__CACHE.__TX.outs.map(output => {
|
||||
let address;
|
||||
try {
|
||||
address = fromOutputScript(output.script, this.opts.network);
|
||||
} catch (_) {}
|
||||
return {
|
||||
script: cloneBuffer(output.script),
|
||||
value: output.value,
|
||||
address,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
combine(...those: Psbt[]): this {
|
||||
|
@ -634,31 +641,28 @@ export class Psbt {
|
|||
keyPair: Signer | SignerAsync,
|
||||
sighashTypes: number[] = [Transaction.SIGHASH_ALL],
|
||||
): Promise<void> {
|
||||
return new Promise(
|
||||
(resolve, reject): void => {
|
||||
if (!keyPair || !keyPair.publicKey)
|
||||
return reject(new Error('Need Signer to sign input'));
|
||||
const { hash, sighashType } = getHashAndSighashType(
|
||||
this.data.inputs,
|
||||
inputIndex,
|
||||
keyPair.publicKey,
|
||||
this.__CACHE,
|
||||
sighashTypes,
|
||||
);
|
||||
return Promise.resolve().then(() => {
|
||||
if (!keyPair || !keyPair.publicKey)
|
||||
throw new Error('Need Signer to sign input');
|
||||
const { hash, sighashType } = getHashAndSighashType(
|
||||
this.data.inputs,
|
||||
inputIndex,
|
||||
keyPair.publicKey,
|
||||
this.__CACHE,
|
||||
sighashTypes,
|
||||
);
|
||||
|
||||
Promise.resolve(keyPair.sign(hash)).then(signature => {
|
||||
const partialSig = [
|
||||
{
|
||||
pubkey: keyPair.publicKey,
|
||||
signature: bscript.signature.encode(signature, sighashType),
|
||||
},
|
||||
];
|
||||
return Promise.resolve(keyPair.sign(hash)).then(signature => {
|
||||
const partialSig = [
|
||||
{
|
||||
pubkey: keyPair.publicKey,
|
||||
signature: bscript.signature.encode(signature, sighashType),
|
||||
},
|
||||
];
|
||||
|
||||
this.data.updateInput(inputIndex, { partialSig });
|
||||
resolve();
|
||||
});
|
||||
},
|
||||
);
|
||||
this.data.updateInput(inputIndex, { partialSig });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
toBuffer(): Buffer {
|
||||
|
|
Loading…
Add table
Reference in a new issue