Socket
Socket
Sign inDemoInstall

bitcoinjs-lib

Package Overview
Dependencies
19
Maintainers
3
Versions
86
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.2.0 to 6.0.0

src/address.d.ts

13

CHANGELOG.md

@@ -0,1 +1,14 @@

# 6.0.0
__removed__
- bip32: Removed the re-export. Please add as dependency to your app instead.
- ECPair: Please use bip32 moving forward. ecpair package was created for those who need it.
- TransactionBuilder: Any internal files used only in TB (classify, templates, etc.) were also removed.
__added__
- taproot segwit v1 address support (bech32m) via address module (#1676)
- hashForWitnessV1 method on Transaction class (#1745)
__fixed__
- Transaction version read/write differed. (#1717)
# 5.2.0

@@ -2,0 +15,0 @@ __changed__

40

package.json
{
"name": "bitcoinjs-lib",
"version": "5.2.0",
"version": "6.0.0",
"description": "Client-side Bitcoin JavaScript library",
"main": "./src/index.js",
"types": "./types/index.d.ts",
"types": "./src/index.d.ts",
"engines": {

@@ -18,5 +18,6 @@ "node": ">=8.0.0"

"scripts": {
"audit": "NPM_AUDIT_IGNORE_DEV=1 NPM_AUDIT_IGNORE_LEVEL=low npm-audit-whitelister .npm-audit-whitelister.json",
"build": "npm run clean && tsc -p ./tsconfig.json && npm run formatjs",
"build:tests": "npm run clean:jstests && tsc -p ./test/tsconfig.json",
"clean": "rimraf src types",
"clean": "rimraf src",
"clean:jstests": "rimraf 'test/**/!(ts-node-register)*.js'",

@@ -49,20 +50,11 @@ "coverage-report": "npm run build && npm run nobuild:coverage-report",

"files": [
"src",
"types"
"src"
],
"dependencies": {
"bech32": "^1.1.2",
"bech32": "^2.0.0",
"bip174": "^2.0.1",
"bip32": "^2.0.4",
"bip66": "^1.1.0",
"bitcoin-ops": "^1.4.0",
"bs58check": "^2.0.0",
"bs58check": "^2.1.2",
"create-hash": "^1.1.0",
"create-hmac": "^1.1.3",
"merkle-lib": "^2.0.10",
"pushdata-bitcoin": "^1.0.1",
"randombytes": "^2.0.1",
"tiny-secp256k1": "^1.1.1",
"typeforce": "^1.11.3",
"varuint-bitcoin": "^1.0.4",
"varuint-bitcoin": "^1.1.2",
"wif": "^2.0.1"

@@ -72,5 +64,10 @@ },

"@types/bs58": "^4.0.0",
"@types/bs58check": "^2.1.0",
"@types/create-hash": "^1.2.2",
"@types/mocha": "^5.2.7",
"@types/node": "12.7.5",
"@types/node": "^16.11.7",
"@types/proxyquire": "^1.3.28",
"@types/randombytes": "^2.0.0",
"@types/wif": "^2.0.2",
"bip32": "^2.0.6",
"bip39": "^3.0.2",

@@ -82,15 +79,18 @@ "bip65": "^1.0.1",

"dhttp": "^3.0.0",
"ecpair": "^1.0.0",
"hoodwink": "^2.0.0",
"minimaldata": "^1.0.2",
"mocha": "^7.1.1",
"nyc": "^15.0.0",
"npm-audit-whitelister": "^1.0.2",
"nyc": "^15.1.0",
"prettier": "1.16.4",
"proxyquire": "^2.0.1",
"randombytes": "^2.1.0",
"regtest-client": "0.2.0",
"rimraf": "^2.6.3",
"ts-node": "^8.3.0",
"tslint": "^5.20.1",
"typescript": "3.2.2"
"tslint": "^6.1.3",
"typescript": "^4.4.4"
},
"license": "MIT"
}
# BitcoinJS (bitcoinjs-lib)
[![Build Status](https://travis-ci.org/bitcoinjs/bitcoinjs-lib.png?branch=master)](https://travis-ci.org/bitcoinjs/bitcoinjs-lib)
[![NPM](https://img.shields.io/npm/v/bitcoinjs-lib.svg)](https://www.npmjs.org/package/bitcoinjs-lib)
[![Github CI](https://github.com/bitcoinjs/bitcoinjs-lib/actions/workflows/main_ci.yml/badge.svg)](https://github.com/bitcoinjs/bitcoinjs-lib/actions/workflows/main_ci.yml) [![NPM](https://img.shields.io/npm/v/bitcoinjs-lib.svg)](https://www.npmjs.org/package/bitcoinjs-lib) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
A javascript Bitcoin library for node.js and browsers. Written in TypeScript, but committing the JS files to verify.

@@ -33,2 +30,3 @@

You can find a [Web UI](https://bitcoincore.tech/apps/bitcoinjs-ui/index.html) that covers most of the `psbt.ts`, `transaction.ts` and `p2*.ts` APIs [here](https://bitcoincore.tech/apps/bitcoinjs-ui/index.html).

@@ -38,7 +36,15 @@ ## Installation

npm install bitcoinjs-lib
# optionally, install a key derivation library as well
npm install ecpair bip32
# ecpair is the ECPair class for single keys
# bip32 is for generating HD keys
```
Typically we support the [Node Maintenance LTS version](https://github.com/nodejs/Release).
If in doubt, see the [.travis.yml](.travis.yml) for what versions are used by our continuous integration tests.
Previous versions of the library included classes for key management (ECPair, HDNode(->"bip32")) but now these have been separated into different libraries. This lowers the bundle size significantly if you don't need to perform any crypto functions (converting private to public keys and deriving HD keys).
Typically we support the [Node Maintenance LTS version](https://github.com/nodejs/Release). TypeScript target will be set
to the ECMAScript version in which all features are fully supported by current Active Node LTS.
However, depending on adoption among other environments (browsers etc.) we may keep the target back a year or two.
If in doubt, see the [main_ci.yml](.github/workflows/main_ci.yml) for what versions are used by our continuous integration tests.
**WARNING**: We presently don't provide any tooling to verify that the release on `npm` matches GitHub. As such, you should verify anything downloaded by `npm` against your own verified copy.

@@ -80,3 +86,3 @@

**WARNING**: iOS devices have [problems](https://github.com/feross/buffer/issues/136), use atleast [buffer@5.0.5](https://github.com/feross/buffer/pull/155) or greater, and enforce the test suites (for `Buffer`, and any other dependency) pass before use.
**WARNING**: iOS devices have [problems](https://github.com/feross/buffer/issues/136), use at least [buffer@5.0.5](https://github.com/feross/buffer/pull/155) or greater, and enforce the test suites (for `Buffer`, and any other dependency) pass before use.

@@ -91,2 +97,4 @@ ### Typescript or VSCode users

- [Taproot Key Spend](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.md)
- [Generate a random address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)

@@ -152,3 +160,3 @@ - [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)

- [Base58 Check](https://github.com/bitcoinjs/bs58check) - Base58 check encoding/decoding
- [Bech32](https://github.com/bitcoinjs/bech32) - A BIP173 compliant Bech32 encoding library
- [Bech32](https://github.com/bitcoinjs/bech32) - A BIP173/BIP350 compliant Bech32/Bech32m encoding library
- [coinselect](https://github.com/bitcoinjs/coinselect) - A fee-optimizing, transaction input selection module for bitcoinjs-lib.

@@ -155,0 +163,0 @@ - [merkle-lib](https://github.com/bitcoinjs/merkle-lib) - A performance conscious library for merkle root and tree calculations.

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.toOutputScript = exports.fromOutputScript = exports.toBech32 = exports.toBase58Check = exports.fromBech32 = exports.fromBase58Check = void 0;
const networks = require('./networks');

@@ -7,5 +8,27 @@ const payments = require('./payments');

const types = require('./types');
const bech32 = require('bech32');
const bech32_1 = require('bech32');
const bs58check = require('bs58check');
const typeforce = require('typeforce');
const { typeforce } = types;
const FUTURE_SEGWIT_MAX_SIZE = 40;
const FUTURE_SEGWIT_MIN_SIZE = 2;
const FUTURE_SEGWIT_MAX_VERSION = 16;
const FUTURE_SEGWIT_MIN_VERSION = 1;
const FUTURE_SEGWIT_VERSION_DIFF = 0x50;
function _toFutureSegwitAddress(output, network) {
const data = output.slice(2);
if (
data.length < FUTURE_SEGWIT_MIN_SIZE ||
data.length > FUTURE_SEGWIT_MAX_SIZE
)
throw new TypeError('Invalid program length for segwit address');
const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
if (
version < FUTURE_SEGWIT_MIN_VERSION ||
version > FUTURE_SEGWIT_MAX_VERSION
)
throw new TypeError('Invalid version for segwit address');
if (output[1] !== data.length)
throw new TypeError('Invalid script for segwit address');
return toBech32(data, version, network.bech32);
}
function fromBase58Check(address) {

@@ -22,6 +45,18 @@ const payload = bs58check.decode(address);

function fromBech32(address) {
const result = bech32.decode(address);
const data = bech32.fromWords(result.words.slice(1));
let result;
let version;
try {
result = bech32_1.bech32.decode(address);
} catch (e) {}
if (result) {
version = result.words[0];
if (version !== 0) throw new TypeError(address + ' uses wrong encoding');
} else {
result = bech32_1.bech32m.decode(address);
version = result.words[0];
if (version === 0) throw new TypeError(address + ' uses wrong encoding');
}
const data = bech32_1.bech32.fromWords(result.words.slice(1));
return {
version: result.words[0],
version,
prefix: result.prefix,

@@ -41,5 +76,7 @@ data: Buffer.from(data),

function toBech32(data, version, prefix) {
const words = bech32.toWords(data);
const words = bech32_1.bech32.toWords(data);
words.unshift(version);
return bech32.encode(prefix, words);
return version === 0
? bech32_1.bech32.encode(prefix, words)
: bech32_1.bech32m.encode(prefix, words);
}

@@ -62,2 +99,5 @@ exports.toBech32 = toBech32;

} catch (e) {}
try {
return _toFutureSegwitAddress(output, network);
} catch (e) {}
throw new Error(bscript.toASM(output) + ' has no matching Address');

@@ -90,3 +130,12 @@ }

return payments.p2wsh({ hash: decodeBech32.data }).output;
}
} else if (
decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
)
return bscript.compile([
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
decodeBech32.data,
]);
}

@@ -93,0 +142,0 @@ }

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.Block = void 0;
const bufferutils_1 = require('./bufferutils');
const bcrypto = require('./crypto');
const merkle_1 = require('./merkle');
const transaction_1 = require('./transaction');
const types = require('./types');
const fastMerkleRoot = require('merkle-lib/fastRoot');
const typeforce = require('typeforce');
const varuint = require('varuint-bitcoin');
const { typeforce } = types;
const errorMerkleNoTxes = new TypeError(

@@ -75,3 +75,3 @@ 'Cannot compute merkle root for zero transactions',

);
const rootHash = fastMerkleRoot(hashes, bcrypto.hash256);
const rootHash = (0, merkle_1.fastMerkleRoot)(hashes, bcrypto.hash256);
return forWitness

@@ -121,3 +121,3 @@ ? bcrypto.hash256(

80 +
varuint.encodingLength(this.transactions.length) +
bufferutils_1.varuint.encodingLength(this.transactions.length) +
this.transactions.reduce((a, x) => a + x.byteLength(allowWitness), 0)

@@ -130,3 +130,3 @@ );

getId() {
return bufferutils_1.reverseBuffer(this.getHash()).toString('hex');
return (0, bufferutils_1.reverseBuffer)(this.getHash()).toString('hex');
}

@@ -149,4 +149,8 @@ getUTCDate() {

if (headersOnly || !this.transactions) return buffer;
varuint.encode(this.transactions.length, buffer, bufferWriter.offset);
bufferWriter.offset += varuint.encode.bytes;
bufferutils_1.varuint.encode(
this.transactions.length,
buffer,
bufferWriter.offset,
);
bufferWriter.offset += bufferutils_1.varuint.encode.bytes;
this.transactions.forEach(tx => {

@@ -173,3 +177,3 @@ const txSize = tx.byteLength(); // TODO: extract from toBuffer?

checkProofOfWork() {
const hash = bufferutils_1.reverseBuffer(this.getHash());
const hash = (0, bufferutils_1.reverseBuffer)(this.getHash());
const target = Block.calculateTarget(this.bits);

@@ -176,0 +180,0 @@ return hash.compare(target) <= 0;

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.BufferReader = exports.BufferWriter = exports.cloneBuffer = exports.reverseBuffer = exports.writeUInt64LE = exports.readUInt64LE = exports.varuint = void 0;
const types = require('./types');
const typeforce = require('typeforce');
const { typeforce } = types;
const varuint = require('varuint-bitcoin');
exports.varuint = varuint;
// https://github.com/feross/buffer/blob/master/index.js#L1127

@@ -59,2 +61,5 @@ function verifuint(value, max) {

}
static withCapacity(size) {
return new BufferWriter(Buffer.alloc(size));
}
writeUInt8(i) {

@@ -90,2 +95,8 @@ this.offset = this.buffer.writeUInt8(i, this.offset);

}
end() {
if (this.buffer.length === this.offset) {
return this.buffer;
}
throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`);
}
}

@@ -92,0 +103,0 @@ exports.BufferWriter = BufferWriter;

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.taggedHash = exports.hash256 = exports.hash160 = exports.sha256 = exports.sha1 = exports.ripemd160 = void 0;
const createHash = require('create-hash');

@@ -36,1 +37,23 @@ function ripemd160(buffer) {

exports.hash256 = hash256;
const TAGS = [
'BIP0340/challenge',
'BIP0340/aux',
'BIP0340/nonce',
'TapLeaf',
'TapBranch',
'TapSighash',
'TapTweak',
'KeyAgg list',
'KeyAgg coefficient',
];
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
const TAGGED_HASH_PREFIXES = Object.fromEntries(
TAGS.map(tag => {
const tagHash = sha256(Buffer.from(tag));
return [tag, Buffer.concat([tagHash, tagHash])];
}),
);
function taggedHash(prefix, data) {
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));
}
exports.taggedHash = taggedHash;
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const bip32 = require('bip32');
exports.bip32 = bip32;
exports.Transaction = exports.opcodes = exports.Psbt = exports.Block = exports.script = exports.payments = exports.networks = exports.crypto = exports.address = void 0;
const address = require('./address');

@@ -9,4 +8,2 @@ exports.address = address;

exports.crypto = crypto;
const ECPair = require('./ecpair');
exports.ECPair = ECPair;
const networks = require('./networks');

@@ -19,10 +16,28 @@ exports.networks = networks;

var block_1 = require('./block');
exports.Block = block_1.Block;
Object.defineProperty(exports, 'Block', {
enumerable: true,
get: function() {
return block_1.Block;
},
});
var psbt_1 = require('./psbt');
exports.Psbt = psbt_1.Psbt;
var script_1 = require('./script');
exports.opcodes = script_1.OPS;
Object.defineProperty(exports, 'Psbt', {
enumerable: true,
get: function() {
return psbt_1.Psbt;
},
});
var ops_1 = require('./ops');
Object.defineProperty(exports, 'opcodes', {
enumerable: true,
get: function() {
return ops_1.OPS;
},
});
var transaction_1 = require('./transaction');
exports.Transaction = transaction_1.Transaction;
var transaction_builder_1 = require('./transaction_builder');
exports.TransactionBuilder = transaction_builder_1.TransactionBuilder;
Object.defineProperty(exports, 'Transaction', {
enumerable: true,
get: function() {
return transaction_1.Transaction;
},
});
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.testnet = exports.regtest = exports.bitcoin = void 0;
exports.bitcoin = {

@@ -4,0 +5,0 @@ messagePrefix: '\x18Bitcoin Signed Message:\n',

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2data = void 0;
const networks_1 = require('../networks');
const bscript = require('../script');
const types_1 = require('../types');
const lazy = require('./lazy');
const typef = require('typeforce');
const OPS = bscript.OPS;

@@ -18,7 +19,9 @@ function stacksEqual(a, b) {

opts = Object.assign({ validate: true }, opts || {});
typef(
(0, types_1.typeforce)(
{
network: typef.maybe(typef.Object),
output: typef.maybe(typef.Buffer),
data: typef.maybe(typef.arrayOf(typef.Buffer)),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
data: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
},

@@ -42,3 +45,3 @@ a,

if (chunks[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid');
if (!chunks.slice(1).every(typef.Buffer))
if (!chunks.slice(1).every(types_1.typeforce.Buffer))
throw new TypeError('Output is invalid');

@@ -45,0 +48,0 @@ if (a.data && !stacksEqual(a.data, o.data))

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2wsh = exports.p2wpkh = exports.p2sh = exports.p2pkh = exports.p2pk = exports.p2ms = exports.embed = void 0;
const embed_1 = require('./embed');
exports.embed = embed_1.p2data;
Object.defineProperty(exports, 'embed', {
enumerable: true,
get: function() {
return embed_1.p2data;
},
});
const p2ms_1 = require('./p2ms');
exports.p2ms = p2ms_1.p2ms;
Object.defineProperty(exports, 'p2ms', {
enumerable: true,
get: function() {
return p2ms_1.p2ms;
},
});
const p2pk_1 = require('./p2pk');
exports.p2pk = p2pk_1.p2pk;
Object.defineProperty(exports, 'p2pk', {
enumerable: true,
get: function() {
return p2pk_1.p2pk;
},
});
const p2pkh_1 = require('./p2pkh');
exports.p2pkh = p2pkh_1.p2pkh;
Object.defineProperty(exports, 'p2pkh', {
enumerable: true,
get: function() {
return p2pkh_1.p2pkh;
},
});
const p2sh_1 = require('./p2sh');
exports.p2sh = p2sh_1.p2sh;
Object.defineProperty(exports, 'p2sh', {
enumerable: true,
get: function() {
return p2sh_1.p2sh;
},
});
const p2wpkh_1 = require('./p2wpkh');
exports.p2wpkh = p2wpkh_1.p2wpkh;
Object.defineProperty(exports, 'p2wpkh', {
enumerable: true,
get: function() {
return p2wpkh_1.p2wpkh;
},
});
const p2wsh_1 = require('./p2wsh');
exports.p2wsh = p2wsh_1.p2wsh;
Object.defineProperty(exports, 'p2wsh', {
enumerable: true,
get: function() {
return p2wsh_1.p2wsh;
},
});
// TODO
// witness commitment
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.value = exports.prop = void 0;
function prop(object, name, f) {

@@ -4,0 +5,0 @@ Object.defineProperty(object, name, {

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2ms = void 0;
const networks_1 = require('../networks');
const bscript = require('../script');
const types_1 = require('../types');
const lazy = require('./lazy');
const OPS = bscript.OPS;
const typef = require('typeforce');
const ecc = require('tiny-secp256k1');
const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1

@@ -33,11 +33,15 @@ function stacksEqual(a, b) {

}
typef(
(0, types_1.typeforce)(
{
network: typef.maybe(typef.Object),
m: typef.maybe(typef.Number),
n: typef.maybe(typef.Number),
output: typef.maybe(typef.Buffer),
pubkeys: typef.maybe(typef.arrayOf(ecc.isPoint)),
signatures: typef.maybe(typef.arrayOf(isAcceptableSignature)),
input: typef.maybe(typef.Buffer),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
m: types_1.typeforce.maybe(types_1.typeforce.Number),
n: types_1.typeforce.maybe(types_1.typeforce.Number),
output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
pubkeys: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.isPoint),
),
signatures: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(isAcceptableSignature),
),
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
},

@@ -105,5 +109,6 @@ a,

decode(a.output);
if (!typef.Number(chunks[0])) throw new TypeError('Output is invalid');
if (!typef.Number(chunks[chunks.length - 2]))
if (!types_1.typeforce.Number(chunks[0]))
throw new TypeError('Output is invalid');
if (!types_1.typeforce.Number(chunks[chunks.length - 2]))
throw new TypeError('Output is invalid');
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG)

@@ -113,3 +118,3 @@ throw new TypeError('Output is invalid');

throw new TypeError('Output is invalid');
if (!o.pubkeys.every(x => ecc.isPoint(x)))
if (!o.pubkeys.every(x => (0, types_1.isPoint)(x)))
throw new TypeError('Output is invalid');

@@ -116,0 +121,0 @@ if (a.m !== undefined && a.m !== o.m) throw new TypeError('m mismatch');

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2pk = void 0;
const networks_1 = require('../networks');
const bscript = require('../script');
const types_1 = require('../types');
const lazy = require('./lazy');
const typef = require('typeforce');
const OPS = bscript.OPS;
const ecc = require('tiny-secp256k1');
// input: {signature}

@@ -15,9 +15,9 @@ // output: {pubKey} OP_CHECKSIG

opts = Object.assign({ validate: true }, opts || {});
typef(
(0, types_1.typeforce)(
{
network: typef.maybe(typef.Object),
output: typef.maybe(typef.Buffer),
pubkey: typef.maybe(ecc.isPoint),
signature: typef.maybe(bscript.isCanonicalScriptSignature),
input: typef.maybe(typef.Buffer),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
pubkey: types_1.typeforce.maybe(types_1.isPoint),
signature: types_1.typeforce.maybe(bscript.isCanonicalScriptSignature),
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
},

@@ -56,3 +56,3 @@ a,

throw new TypeError('Output is invalid');
if (!ecc.isPoint(o.pubkey))
if (!(0, types_1.isPoint)(o.pubkey))
throw new TypeError('Output pubkey is invalid');

@@ -59,0 +59,0 @@ if (a.pubkey && !a.pubkey.equals(o.pubkey))

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2pkh = void 0;
const bcrypto = require('../crypto');
const networks_1 = require('../networks');
const bscript = require('../script');
const types_1 = require('../types');
const lazy = require('./lazy');
const typef = require('typeforce');
const bs58check = require('bs58check');
const OPS = bscript.OPS;
const ecc = require('tiny-secp256k1');
const bs58check = require('bs58check');
// input: {signature} {pubkey}

@@ -17,11 +17,11 @@ // output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG

opts = Object.assign({ validate: true }, opts || {});
typef(
(0, types_1.typeforce)(
{
network: typef.maybe(typef.Object),
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
output: typef.maybe(typef.BufferN(25)),
pubkey: typef.maybe(ecc.isPoint),
signature: typef.maybe(bscript.isCanonicalScriptSignature),
input: typef.maybe(typef.Buffer),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
address: types_1.typeforce.maybe(types_1.typeforce.String),
hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(20)),
output: types_1.typeforce.maybe(types_1.typeforce.BufferN(25)),
pubkey: types_1.typeforce.maybe(types_1.isPoint),
signature: types_1.typeforce.maybe(bscript.isCanonicalScriptSignature),
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
},

@@ -120,3 +120,3 @@ a,

throw new TypeError('Input has invalid signature');
if (!ecc.isPoint(chunks[1]))
if (!(0, types_1.isPoint)(chunks[1]))
throw new TypeError('Input has invalid pubkey');

@@ -123,0 +123,0 @@ if (a.signature && !a.signature.equals(chunks[0]))

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2sh = void 0;
const bcrypto = require('../crypto');
const networks_1 = require('../networks');
const bscript = require('../script');
const types_1 = require('../types');
const lazy = require('./lazy');
const typef = require('typeforce');
const bs58check = require('bs58check');
const OPS = bscript.OPS;
const bs58check = require('bs58check');
function stacksEqual(a, b) {

@@ -23,16 +24,20 @@ if (a.length !== b.length) return false;

opts = Object.assign({ validate: true }, opts || {});
typef(
(0, types_1.typeforce)(
{
network: typef.maybe(typef.Object),
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
output: typef.maybe(typef.BufferN(23)),
redeem: typef.maybe({
network: typef.maybe(typef.Object),
output: typef.maybe(typef.Buffer),
input: typef.maybe(typef.Buffer),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
address: types_1.typeforce.maybe(types_1.typeforce.String),
hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(20)),
output: types_1.typeforce.maybe(types_1.typeforce.BufferN(23)),
redeem: types_1.typeforce.maybe({
network: types_1.typeforce.maybe(types_1.typeforce.Object),
output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
witness: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
}),
input: typef.maybe(typef.Buffer),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
witness: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
},

@@ -99,3 +104,4 @@ a,

const nameParts = ['p2sh'];
if (o.redeem !== undefined) nameParts.push(o.redeem.name);
if (o.redeem !== undefined && o.redeem.name !== undefined)
nameParts.push(o.redeem.name);
return nameParts.join('-');

@@ -102,0 +108,0 @@ });

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2wpkh = void 0;
const bcrypto = require('../crypto');
const networks_1 = require('../networks');
const bscript = require('../script');
const types_1 = require('../types');
const lazy = require('./lazy');
const typef = require('typeforce');
const bech32_1 = require('bech32');
const OPS = bscript.OPS;
const ecc = require('tiny-secp256k1');
const bech32 = require('bech32');
const EMPTY_BUFFER = Buffer.alloc(0);

@@ -19,12 +19,14 @@ // witness: {signature} {pubKey}

opts = Object.assign({ validate: true }, opts || {});
typef(
(0, types_1.typeforce)(
{
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(20)),
input: typef.maybe(typef.BufferN(0)),
network: typef.maybe(typef.Object),
output: typef.maybe(typef.BufferN(22)),
pubkey: typef.maybe(ecc.isPoint),
signature: typef.maybe(bscript.isCanonicalScriptSignature),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
address: types_1.typeforce.maybe(types_1.typeforce.String),
hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(20)),
input: types_1.typeforce.maybe(types_1.typeforce.BufferN(0)),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
output: types_1.typeforce.maybe(types_1.typeforce.BufferN(22)),
pubkey: types_1.typeforce.maybe(types_1.isPoint),
signature: types_1.typeforce.maybe(bscript.isCanonicalScriptSignature),
witness: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
},

@@ -34,5 +36,5 @@ a,

const _address = lazy.value(() => {
const result = bech32.decode(a.address);
const result = bech32_1.bech32.decode(a.address);
const version = result.words.shift();
const data = bech32.fromWords(result.words);
const data = bech32_1.bech32.fromWords(result.words);
return {

@@ -48,5 +50,5 @@ version,

if (!o.hash) return;
const words = bech32.toWords(o.hash);
const words = bech32_1.bech32.toWords(o.hash);
words.unshift(0x00);
return bech32.encode(network.bech32, words);
return bech32_1.bech32.encode(network.bech32, words);
});

@@ -113,3 +115,3 @@ lazy.prop(o, 'hash', () => {

else hash = pkh;
if (!ecc.isPoint(a.pubkey) || a.pubkey.length !== 33)
if (!(0, types_1.isPoint)(a.pubkey) || a.pubkey.length !== 33)
throw new TypeError('Invalid pubkey for p2wpkh');

@@ -121,3 +123,3 @@ }

throw new TypeError('Witness has invalid signature');
if (!ecc.isPoint(a.witness[1]) || a.witness[1].length !== 33)
if (!(0, types_1.isPoint)(a.witness[1]) || a.witness[1].length !== 33)
throw new TypeError('Witness has invalid pubkey');

@@ -124,0 +126,0 @@ if (a.signature && !a.signature.equals(a.witness[0]))

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.p2wsh = void 0;
const bcrypto = require('../crypto');
const networks_1 = require('../networks');
const bscript = require('../script');
const types_1 = require('../types');
const lazy = require('./lazy');
const typef = require('typeforce');
const bech32_1 = require('bech32');
const OPS = bscript.OPS;
const ecc = require('tiny-secp256k1');
const bech32 = require('bech32');
const EMPTY_BUFFER = Buffer.alloc(0);

@@ -23,3 +23,3 @@ function stacksEqual(a, b) {

chunk[0] === 0x04 &&
ecc.isPoint(chunk)
(0, types_1.isPoint)(chunk)
) {

@@ -38,16 +38,20 @@ return true;

opts = Object.assign({ validate: true }, opts || {});
typef(
(0, types_1.typeforce)(
{
network: typef.maybe(typef.Object),
address: typef.maybe(typef.String),
hash: typef.maybe(typef.BufferN(32)),
output: typef.maybe(typef.BufferN(34)),
redeem: typef.maybe({
input: typef.maybe(typef.Buffer),
network: typef.maybe(typef.Object),
output: typef.maybe(typef.Buffer),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
address: types_1.typeforce.maybe(types_1.typeforce.String),
hash: types_1.typeforce.maybe(types_1.typeforce.BufferN(32)),
output: types_1.typeforce.maybe(types_1.typeforce.BufferN(34)),
redeem: types_1.typeforce.maybe({
input: types_1.typeforce.maybe(types_1.typeforce.Buffer),
network: types_1.typeforce.maybe(types_1.typeforce.Object),
output: types_1.typeforce.maybe(types_1.typeforce.Buffer),
witness: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
}),
input: typef.maybe(typef.BufferN(0)),
witness: typef.maybe(typef.arrayOf(typef.Buffer)),
input: types_1.typeforce.maybe(types_1.typeforce.BufferN(0)),
witness: types_1.typeforce.maybe(
types_1.typeforce.arrayOf(types_1.typeforce.Buffer),
),
},

@@ -57,5 +61,5 @@ a,

const _address = lazy.value(() => {
const result = bech32.decode(a.address);
const result = bech32_1.bech32.decode(a.address);
const version = result.words.shift();
const data = bech32.fromWords(result.words);
const data = bech32_1.bech32.fromWords(result.words);
return {

@@ -77,5 +81,5 @@ version,

if (!o.hash) return;
const words = bech32.toWords(o.hash);
const words = bech32_1.bech32.toWords(o.hash);
words.unshift(0x00);
return bech32.encode(network.bech32, words);
return bech32_1.bech32.encode(network.bech32, words);
});

@@ -125,3 +129,4 @@ lazy.prop(o, 'hash', () => {

const nameParts = ['p2wsh'];
if (o.redeem !== undefined) nameParts.push(o.redeem.name);
if (o.redeem !== undefined && o.redeem.name !== undefined)
nameParts.push(o.redeem.name);
return nameParts.join('-');

@@ -128,0 +133,0 @@ });

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.Psbt = void 0;
const bip174_1 = require('bip174');

@@ -9,3 +10,2 @@ const varuint = require('bip174/src/lib/converter/varint');

const crypto_1 = require('./crypto');
const ecpair_1 = require('./ecpair');
const networks_1 = require('./networks');

@@ -29,3 +29,3 @@ const payments = require('./payments');

*/
maximumFeeRate: 5000,
maximumFeeRate: 5000, // satoshi per byte
};

@@ -74,4 +74,5 @@ /**

__TX: this.data.globalMap.unsignedTx.tx,
// Old TransactionBuilder behavior was to not confirm input values
// before signing. Even though we highly encourage people to get
// Psbt's predecesor (TransactionBuilder - now removed) behavior
// was to not confirm input values before signing.
// Even though we highly encourage people to get
// the full parent transaction to verify values, the ability to

@@ -125,3 +126,3 @@ // sign non-segwit inputs without the full transaction was often

return this.__CACHE.__TX.ins.map(input => ({
hash: bufferutils_1.cloneBuffer(input.hash),
hash: (0, bufferutils_1.cloneBuffer)(input.hash),
index: input.index,

@@ -135,6 +136,9 @@ sequence: input.sequence,

try {
address = address_1.fromOutputScript(output.script, this.opts.network);
address = (0, address_1.fromOutputScript)(
output.script,
this.opts.network,
);
} catch (_) {}
return {
script: bufferutils_1.cloneBuffer(output.script),
script: (0, bufferutils_1.cloneBuffer)(output.script),
value: output.value,

@@ -238,3 +242,3 @@ address,

const { network } = this.opts;
const script = address_1.toOutputScript(address, network);
const script = (0, address_1.toOutputScript)(address, network);
outputData = Object.assign(outputData, { script });

@@ -272,3 +276,3 @@ }

finalizeAllInputs() {
utils_1.checkForInput(this.data.inputs, 0); // making sure we have at least one
(0, utils_1.checkForInput)(this.data.inputs, 0); // making sure we have at least one
range(this.data.inputs.length).forEach(idx => this.finalizeInput(idx));

@@ -278,3 +282,3 @@ return this;

finalizeInput(inputIndex, finalScriptsFunc = getFinalScripts) {
const input = utils_1.checkForInput(this.data.inputs, inputIndex);
const input = (0, utils_1.checkForInput)(this.data.inputs, inputIndex);
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput(

@@ -304,3 +308,3 @@ inputIndex,

getInputType(inputIndex) {
const input = utils_1.checkForInput(this.data.inputs, inputIndex);
const input = (0, utils_1.checkForInput)(this.data.inputs, inputIndex);
const script = getScriptFromUtxo(inputIndex, input, this.__CACHE);

@@ -320,7 +324,7 @@ const result = getMeaningfulScript(

inputHasPubkey(inputIndex, pubkey) {
const input = utils_1.checkForInput(this.data.inputs, inputIndex);
const input = (0, utils_1.checkForInput)(this.data.inputs, inputIndex);
return pubkeyInInput(pubkey, input, inputIndex, this.__CACHE);
}
inputHasHDKey(inputIndex, root) {
const input = utils_1.checkForInput(this.data.inputs, inputIndex);
const input = (0, utils_1.checkForInput)(this.data.inputs, inputIndex);
const derivationIsMine = bip32DerivationIsMine(root);

@@ -332,7 +336,7 @@ return (

outputHasPubkey(outputIndex, pubkey) {
const output = utils_1.checkForOutput(this.data.outputs, outputIndex);
const output = (0, utils_1.checkForOutput)(this.data.outputs, outputIndex);
return pubkeyInOutput(pubkey, output, outputIndex, this.__CACHE);
}
outputHasHDKey(outputIndex, root) {
const output = utils_1.checkForOutput(this.data.outputs, outputIndex);
const output = (0, utils_1.checkForOutput)(this.data.outputs, outputIndex);
const derivationIsMine = bip32DerivationIsMine(root);

@@ -343,10 +347,10 @@ return (

}
validateSignaturesOfAllInputs() {
utils_1.checkForInput(this.data.inputs, 0); // making sure we have at least one
validateSignaturesOfAllInputs(validator) {
(0, utils_1.checkForInput)(this.data.inputs, 0); // making sure we have at least one
const results = range(this.data.inputs.length).map(idx =>
this.validateSignaturesOfInput(idx),
this.validateSignaturesOfInput(idx, validator),
);
return results.reduce((final, res) => res === true && final, true);
}
validateSignaturesOfInput(inputIndex, pubkey) {
validateSignaturesOfInput(inputIndex, validator, pubkey) {
const input = this.data.inputs[inputIndex];

@@ -356,2 +360,4 @@ const partialSig = (input || {}).partialSig;

throw new Error('No signatures to validate');
if (typeof validator !== 'function')
throw new Error('Need validator function to validate signatures');
const mySigs = pubkey

@@ -380,4 +386,3 @@ ? partialSig.filter(sig => sig.pubkey.equals(pubkey))

checkScriptForPubkey(pSig.pubkey, script, 'verify');
const keypair = ecpair_1.fromPublicKey(pSig.pubkey);
results.push(keypair.verify(hash, sig.signature));
results.push(validator(pSig.pubkey, hash, sig.signature));
}

@@ -659,3 +664,3 @@ return results.every(res => res === true);

typeof input.hash === 'string'
? bufferutils_1.reverseBuffer(Buffer.from(input.hash, 'hex'))
? (0, bufferutils_1.reverseBuffer)(Buffer.from(input.hash, 'hex'))
: input.hash;

@@ -703,4 +708,3 @@ this.tx.addInput(hash, input.index, input.sequence);

.map(pkey => {
const pubkey = ecpair_1.fromPublicKey(pkey, { compressed: true })
.publicKey;
const pubkey = compressPubkey(pkey);
return partialSig.find(pSig => pSig.pubkey.equals(pubkey));

@@ -836,3 +840,3 @@ })

const key =
bufferutils_1.reverseBuffer(Buffer.from(input.hash)).toString('hex') +
(0, bufferutils_1.reverseBuffer)(Buffer.from(input.hash)).toString('hex') +
':' +

@@ -932,3 +936,3 @@ input.index;

) {
const input = utils_1.checkForInput(inputs, inputIndex);
const input = (0, utils_1.checkForInput)(inputs, inputIndex);
const { hash, sighashType, script } = getHashForSig(

@@ -1019,4 +1023,4 @@ inputIndex,

'means there is a chance that a miner could feed you incorrect information ' +
'to trick you into paying large fees. This behavior is the same as the old ' +
'TransactionBuilder class when signing non-segwit scripts. You are not ' +
"to trick you into paying large fees. This behavior is the same as Psbt's predecesor " +
'(TransactionBuilder - now removed) when signing non-segwit scripts. You are not ' +
'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +

@@ -1118,3 +1122,3 @@ 'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +

function getSignersFromHD(inputIndex, inputs, hdKeyPair) {
const input = utils_1.checkForInput(inputs, inputIndex);
const input = (0, utils_1.checkForInput)(inputs, inputIndex);
if (!input.bip32Derivation || input.bip32Derivation.length === 0) {

@@ -1346,2 +1350,11 @@ throw new Error('Need bip32Derivation to sign with HD');

}
function compressPubkey(pubkey) {
if (pubkey.length === 65) {
const parity = pubkey[64] & 1;
const newKey = pubkey.slice(0, 33);
newKey[0] = 2 | parity;
return newKey;
}
return pubkey.slice();
}
function isPubkeyLike(buf) {

@@ -1402,3 +1415,3 @@ return buf.length === 33 && bscript.isCanonicalPubKey(buf);

function pubkeyInScript(pubkey, script) {
const pubkeyHash = crypto_1.hash160(pubkey);
const pubkeyHash = (0, crypto_1.hash160)(pubkey);
const decompiled = bscript.decompile(script);

@@ -1405,0 +1418,0 @@ if (decompiled === null) throw new Error('Unknown script error');

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.encode = exports.decode = void 0;
function decode(buffer, maxLength, minimal) {

@@ -4,0 +5,0 @@ maxLength = maxLength || 4;

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.encode = exports.decode = void 0;
const bip66 = require('./bip66');
const types = require('./types');
const bip66 = require('bip66');
const typeforce = require('typeforce');
const { typeforce } = types;
const ZERO = Buffer.alloc(1, 0);

@@ -7,0 +8,0 @@ function toDER(x) {

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.signature = exports.number = exports.isCanonicalScriptSignature = exports.isDefinedHashType = exports.isCanonicalPubKey = exports.toStack = exports.fromASM = exports.toASM = exports.decompile = exports.compile = exports.isPushOnly = exports.OPS = void 0;
const bip66 = require('./bip66');
const ops_1 = require('./ops');
Object.defineProperty(exports, 'OPS', {
enumerable: true,
get: function() {
return ops_1.OPS;
},
});
const pushdata = require('./push_data');
const scriptNumber = require('./script_number');
const scriptSignature = require('./script_signature');
const types = require('./types');
const bip66 = require('bip66');
const ecc = require('tiny-secp256k1');
const pushdata = require('pushdata-bitcoin');
const typeforce = require('typeforce');
exports.OPS = require('bitcoin-ops');
const REVERSE_OPS = require('bitcoin-ops/map');
const OP_INT_BASE = exports.OPS.OP_RESERVED; // OP_1 - 1
const { typeforce } = types;
const OP_INT_BASE = ops_1.OPS.OP_RESERVED; // OP_1 - 1
function isOPInt(value) {
return (
types.Number(value) &&
(value === exports.OPS.OP_0 ||
(value >= exports.OPS.OP_1 && value <= exports.OPS.OP_16) ||
value === exports.OPS.OP_1NEGATE)
(value === ops_1.OPS.OP_0 ||
(value >= ops_1.OPS.OP_1 && value <= ops_1.OPS.OP_16) ||
value === ops_1.OPS.OP_1NEGATE)
);

@@ -29,6 +34,6 @@ }

function asMinimalOP(buffer) {
if (buffer.length === 0) return exports.OPS.OP_0;
if (buffer.length === 0) return ops_1.OPS.OP_0;
if (buffer.length !== 1) return;
if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0];
if (buffer[0] === 0x81) return exports.OPS.OP_1NEGATE;
if (buffer[0] === 0x81) return ops_1.OPS.OP_1NEGATE;
}

@@ -94,3 +99,3 @@ function chunksIsBuffer(buf) {

// data chunk
if (opcode > exports.OPS.OP_0 && opcode <= exports.OPS.OP_PUSHDATA4) {
if (opcode > ops_1.OPS.OP_0 && opcode <= ops_1.OPS.OP_PUSHDATA4) {
const d = pushdata.decode(buffer, i);

@@ -133,3 +138,3 @@ // did reading a pushDataInt fail?

// opcode!
return REVERSE_OPS[chunk];
return ops_1.REVERSE_OPS[chunk];
})

@@ -144,3 +149,3 @@ .join(' ');

// opcode?
if (exports.OPS[chunkStr] !== undefined) return exports.OPS[chunkStr];
if (ops_1.OPS[chunkStr] !== undefined) return ops_1.OPS[chunkStr];
typeforce(types.Hex, chunkStr);

@@ -158,3 +163,3 @@ // data!

if (singleChunkIsBuffer(op)) return op;
if (op === exports.OPS.OP_0) return Buffer.allocUnsafe(0);
if (op === ops_1.OPS.OP_0) return Buffer.allocUnsafe(0);
return scriptNumber.encode(op - OP_INT_BASE);

@@ -165,3 +170,3 @@ });

function isCanonicalPubKey(buffer) {
return ecc.isPoint(buffer);
return types.isPoint(buffer);
}

@@ -168,0 +173,0 @@ exports.isCanonicalPubKey = isCanonicalPubKey;

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.Transaction = void 0;
const bufferutils_1 = require('./bufferutils');

@@ -8,7 +9,6 @@ const bcrypto = require('./crypto');

const types = require('./types');
const typeforce = require('typeforce');
const varuint = require('varuint-bitcoin');
const { typeforce } = types;
function varSliceSize(someScript) {
const length = someScript.length;
return varuint.encodingLength(length) + length;
return bufferutils_1.varuint.encodingLength(length) + length;
}

@@ -18,3 +18,3 @@ function vectorSize(someVector) {

return (
varuint.encodingLength(length) +
bufferutils_1.varuint.encodingLength(length) +
someVector.reduce((sum, witness) => {

@@ -25,3 +25,3 @@ return sum + varSliceSize(witness);

}
const EMPTY_SCRIPT = Buffer.allocUnsafe(0);
const EMPTY_BUFFER = Buffer.allocUnsafe(0);
const EMPTY_WITNESS = [];

@@ -38,3 +38,3 @@ const ZERO = Buffer.from(

const BLANK_OUTPUT = {
script: EMPTY_SCRIPT,
script: EMPTY_BUFFER,
valueBuffer: VALUE_UINT64_MAX,

@@ -131,3 +131,3 @@ };

index,
script: scriptSig || EMPTY_SCRIPT,
script: scriptSig || EMPTY_BUFFER,
sequence: sequence,

@@ -165,4 +165,4 @@ witness: EMPTY_WITNESS,

(hasWitnesses ? 10 : 8) +
varuint.encodingLength(this.ins.length) +
varuint.encodingLength(this.outs.length) +
bufferutils_1.varuint.encodingLength(this.ins.length) +
bufferutils_1.varuint.encodingLength(this.outs.length) +
this.ins.reduce((sum, input) => {

@@ -256,3 +256,3 @@ return sum + 40 + varSliceSize(input.script);

txTmp.ins.forEach(input => {
input.script = EMPTY_SCRIPT;
input.script = EMPTY_BUFFER;
});

@@ -267,2 +267,137 @@ txTmp.ins[inIndex].script = ourScript;

}
hashForWitnessV1(inIndex, prevOutScripts, values, hashType, leafHash, annex) {
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#common-signature-message
typeforce(
types.tuple(
types.UInt32,
typeforce.arrayOf(types.Buffer),
typeforce.arrayOf(types.Satoshi),
types.UInt32,
),
arguments,
);
if (
values.length !== this.ins.length ||
prevOutScripts.length !== this.ins.length
) {
throw new Error('Must supply prevout script and value for all inputs');
}
const outputType =
hashType === Transaction.SIGHASH_DEFAULT
? Transaction.SIGHASH_ALL
: hashType & Transaction.SIGHASH_OUTPUT_MASK;
const inputType = hashType & Transaction.SIGHASH_INPUT_MASK;
const isAnyoneCanPay = inputType === Transaction.SIGHASH_ANYONECANPAY;
const isNone = outputType === Transaction.SIGHASH_NONE;
const isSingle = outputType === Transaction.SIGHASH_SINGLE;
let hashPrevouts = EMPTY_BUFFER;
let hashAmounts = EMPTY_BUFFER;
let hashScriptPubKeys = EMPTY_BUFFER;
let hashSequences = EMPTY_BUFFER;
let hashOutputs = EMPTY_BUFFER;
if (!isAnyoneCanPay) {
let bufferWriter = bufferutils_1.BufferWriter.withCapacity(
36 * this.ins.length,
);
this.ins.forEach(txIn => {
bufferWriter.writeSlice(txIn.hash);
bufferWriter.writeUInt32(txIn.index);
});
hashPrevouts = bcrypto.sha256(bufferWriter.end());
bufferWriter = bufferutils_1.BufferWriter.withCapacity(
8 * this.ins.length,
);
values.forEach(value => bufferWriter.writeUInt64(value));
hashAmounts = bcrypto.sha256(bufferWriter.end());
bufferWriter = bufferutils_1.BufferWriter.withCapacity(
prevOutScripts.map(varSliceSize).reduce((a, b) => a + b),
);
prevOutScripts.forEach(prevOutScript =>
bufferWriter.writeVarSlice(prevOutScript),
);
hashScriptPubKeys = bcrypto.sha256(bufferWriter.end());
bufferWriter = bufferutils_1.BufferWriter.withCapacity(
4 * this.ins.length,
);
this.ins.forEach(txIn => bufferWriter.writeUInt32(txIn.sequence));
hashSequences = bcrypto.sha256(bufferWriter.end());
}
if (!(isNone || isSingle)) {
const txOutsSize = this.outs
.map(output => 8 + varSliceSize(output.script))
.reduce((a, b) => a + b);
const bufferWriter = bufferutils_1.BufferWriter.withCapacity(txOutsSize);
this.outs.forEach(out => {
bufferWriter.writeUInt64(out.value);
bufferWriter.writeVarSlice(out.script);
});
hashOutputs = bcrypto.sha256(bufferWriter.end());
} else if (isSingle && inIndex < this.outs.length) {
const output = this.outs[inIndex];
const bufferWriter = bufferutils_1.BufferWriter.withCapacity(
8 + varSliceSize(output.script),
);
bufferWriter.writeUInt64(output.value);
bufferWriter.writeVarSlice(output.script);
hashOutputs = bcrypto.sha256(bufferWriter.end());
}
const spendType = (leafHash ? 2 : 0) + (annex ? 1 : 0);
// Length calculation from:
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-14
// With extension from:
// https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#signature-validation
const sigMsgSize =
174 -
(isAnyoneCanPay ? 49 : 0) -
(isNone ? 32 : 0) +
(annex ? 32 : 0) +
(leafHash ? 37 : 0);
const sigMsgWriter = bufferutils_1.BufferWriter.withCapacity(sigMsgSize);
sigMsgWriter.writeUInt8(hashType);
// Transaction
sigMsgWriter.writeInt32(this.version);
sigMsgWriter.writeUInt32(this.locktime);
sigMsgWriter.writeSlice(hashPrevouts);
sigMsgWriter.writeSlice(hashAmounts);
sigMsgWriter.writeSlice(hashScriptPubKeys);
sigMsgWriter.writeSlice(hashSequences);
if (!(isNone || isSingle)) {
sigMsgWriter.writeSlice(hashOutputs);
}
// Input
sigMsgWriter.writeUInt8(spendType);
if (isAnyoneCanPay) {
const input = this.ins[inIndex];
sigMsgWriter.writeSlice(input.hash);
sigMsgWriter.writeUInt32(input.index);
sigMsgWriter.writeUInt64(values[inIndex]);
sigMsgWriter.writeVarSlice(prevOutScripts[inIndex]);
sigMsgWriter.writeUInt32(input.sequence);
} else {
sigMsgWriter.writeUInt32(inIndex);
}
if (annex) {
const bufferWriter = bufferutils_1.BufferWriter.withCapacity(
varSliceSize(annex),
);
bufferWriter.writeVarSlice(annex);
sigMsgWriter.writeSlice(bcrypto.sha256(bufferWriter.end()));
}
// Output
if (isSingle) {
sigMsgWriter.writeSlice(hashOutputs);
}
// BIP342 extension
if (leafHash) {
sigMsgWriter.writeSlice(leafHash);
sigMsgWriter.writeUInt8(0);
sigMsgWriter.writeUInt32(0xffffffff);
}
// Extra zero byte because:
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-19
return bcrypto.taggedHash(
'TapSighash',
Buffer.concat([Buffer.of(0x00), sigMsgWriter.end()]),
);
}
hashForWitnessV0(inIndex, prevOutScript, value, hashType) {

@@ -327,3 +462,3 @@ typeforce(

const input = this.ins[inIndex];
bufferWriter.writeUInt32(this.version);
bufferWriter.writeInt32(this.version);
bufferWriter.writeSlice(hashPrevouts);

@@ -348,3 +483,5 @@ bufferWriter.writeSlice(hashSequence);

// transaction hash's are displayed in reverse order
return bufferutils_1.reverseBuffer(this.getHash(false)).toString('hex');
return (0, bufferutils_1.reverseBuffer)(this.getHash(false)).toString(
'hex',
);
}

@@ -405,3 +542,5 @@ toBuffer(buffer, initialOffset) {

}
exports.Transaction = Transaction;
Transaction.DEFAULT_SEQUENCE = 0xffffffff;
Transaction.SIGHASH_DEFAULT = 0x00;
Transaction.SIGHASH_ALL = 0x01;

@@ -411,4 +550,5 @@ Transaction.SIGHASH_NONE = 0x02;

Transaction.SIGHASH_ANYONECANPAY = 0x80;
Transaction.SIGHASH_OUTPUT_MASK = 0x03;
Transaction.SIGHASH_INPUT_MASK = 0x80;
Transaction.ADVANCED_TRANSACTION_MARKER = 0x00;
Transaction.ADVANCED_TRANSACTION_FLAG = 0x01;
exports.Transaction = Transaction;
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const typeforce = require('typeforce');
exports.oneOf = exports.Null = exports.BufferN = exports.Function = exports.UInt32 = exports.UInt8 = exports.tuple = exports.maybe = exports.Hex = exports.Buffer = exports.String = exports.Boolean = exports.Array = exports.Number = exports.Hash256bit = exports.Hash160bit = exports.Buffer256bit = exports.Network = exports.ECPoint = exports.Satoshi = exports.Signer = exports.BIP32Path = exports.UInt31 = exports.isPoint = exports.typeforce = void 0;
const buffer_1 = require('buffer');
exports.typeforce = require('typeforce');
const ZERO32 = buffer_1.Buffer.alloc(32, 0);
const EC_P = buffer_1.Buffer.from(
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
'hex',
);
function isPoint(p) {
if (!buffer_1.Buffer.isBuffer(p)) return false;
if (p.length < 33) return false;
const t = p[0];
const x = p.slice(1, 33);
if (x.compare(ZERO32) === 0) return false;
if (x.compare(EC_P) >= 0) return false;
if ((t === 0x02 || t === 0x03) && p.length === 33) {
return true;
}
const y = p.slice(33);
if (y.compare(ZERO32) === 0) return false;
if (y.compare(EC_P) >= 0) return false;
if (t === 0x04 && p.length === 65) return true;
return false;
}
exports.isPoint = isPoint;
const UINT31_MAX = Math.pow(2, 31) - 1;
function UInt31(value) {
return typeforce.UInt32(value) && value <= UINT31_MAX;
return exports.typeforce.UInt32(value) && value <= UINT31_MAX;
}
exports.UInt31 = UInt31;
function BIP32Path(value) {
return typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/);
return (
exports.typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
);
}

@@ -18,3 +44,3 @@ exports.BIP32Path = BIP32Path;

return (
(typeforce.Buffer(obj.publicKey) ||
(exports.typeforce.Buffer(obj.publicKey) ||
typeof obj.getPublicKey === 'function') &&

@@ -27,34 +53,37 @@ typeof obj.sign === 'function'

function Satoshi(value) {
return typeforce.UInt53(value) && value <= SATOSHI_MAX;
return exports.typeforce.UInt53(value) && value <= SATOSHI_MAX;
}
exports.Satoshi = Satoshi;
// external dependent types
exports.ECPoint = typeforce.quacksLike('Point');
exports.ECPoint = exports.typeforce.quacksLike('Point');
// exposed, external API
exports.Network = typeforce.compile({
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
exports.Network = exports.typeforce.compile({
messagePrefix: exports.typeforce.oneOf(
exports.typeforce.Buffer,
exports.typeforce.String,
),
bip32: {
public: typeforce.UInt32,
private: typeforce.UInt32,
public: exports.typeforce.UInt32,
private: exports.typeforce.UInt32,
},
pubKeyHash: typeforce.UInt8,
scriptHash: typeforce.UInt8,
wif: typeforce.UInt8,
pubKeyHash: exports.typeforce.UInt8,
scriptHash: exports.typeforce.UInt8,
wif: exports.typeforce.UInt8,
});
exports.Buffer256bit = typeforce.BufferN(32);
exports.Hash160bit = typeforce.BufferN(20);
exports.Hash256bit = typeforce.BufferN(32);
exports.Number = typeforce.Number; // tslint:disable-line variable-name
exports.Array = typeforce.Array;
exports.Boolean = typeforce.Boolean; // tslint:disable-line variable-name
exports.String = typeforce.String; // tslint:disable-line variable-name
exports.Buffer = typeforce.Buffer;
exports.Hex = typeforce.Hex;
exports.maybe = typeforce.maybe;
exports.tuple = typeforce.tuple;
exports.UInt8 = typeforce.UInt8;
exports.UInt32 = typeforce.UInt32;
exports.Function = typeforce.Function;
exports.BufferN = typeforce.BufferN;
exports.Null = typeforce.Null;
exports.oneOf = typeforce.oneOf;
exports.Buffer256bit = exports.typeforce.BufferN(32);
exports.Hash160bit = exports.typeforce.BufferN(20);
exports.Hash256bit = exports.typeforce.BufferN(32);
exports.Number = exports.typeforce.Number; // tslint:disable-line variable-name
exports.Array = exports.typeforce.Array;
exports.Boolean = exports.typeforce.Boolean; // tslint:disable-line variable-name
exports.String = exports.typeforce.String; // tslint:disable-line variable-name
exports.Buffer = exports.typeforce.Buffer;
exports.Hex = exports.typeforce.Hex;
exports.maybe = exports.typeforce.maybe;
exports.tuple = exports.typeforce.tuple;
exports.UInt8 = exports.typeforce.UInt8;
exports.UInt32 = exports.typeforce.UInt32;
exports.Function = exports.typeforce.Function;
exports.BufferN = exports.typeforce.BufferN;
exports.Null = exports.typeforce.Null;
exports.oneOf = exports.typeforce.oneOf;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc