Skip to content

Commit 1f44f72

Browse files
authored
Merge pull request #1745 from brandonblack/feature/taproot
Taproot
2 parents f484edd + 31e512e commit 1f44f72

21 files changed

+688
-55
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# 6.0.0
2+
__removed__
3+
- bip32: Removed the re-export. Please add as dependency to your app instead.
4+
- ECPair: Please use bip32 moving forward. ecpair package was created for those who need it.
5+
- TransactionBuilder: Any internal files used only in TB (classify, templates, etc.) were also removed.
6+
7+
__added__
8+
- taproot segwit v1 address support (bech32m) via address module (#1676)
9+
- hashForWitnessV1 method on Transaction class (#1745)
10+
11+
__fixed__
12+
- Transaction version read/write differed. (#1717)
13+
114
# 5.2.0
215
__changed__
316
- Updated PSBT to allow for witnessUtxo and nonWitnessUtxo simultaneously (Re: segwit psbt bug) (#1563)

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# BitcoinJS (bitcoinjs-lib)
2-
[![Build Status](https://travis-ci.org/bitcoinjs/bitcoinjs-lib.png?branch=master)](https://travis-ci.org/bitcoinjs/bitcoinjs-lib)
3-
[![NPM](https://img.shields.io/npm/v/bitcoinjs-lib.svg)](https://www.npmjs.org/package/bitcoinjs-lib)
4-
5-
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
2+
[![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)
63

74
A javascript Bitcoin library for node.js and browsers. Written in TypeScript, but committing the JS files to verify.
85

@@ -94,6 +91,8 @@ The below examples are implemented as integration tests, they should be very eas
9491
Otherwise, pull requests are appreciated.
9592
Some examples interact (via HTTPS) with a 3rd Party Blockchain Provider (3PBP).
9693

94+
- [Taproot Key Spend](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.md)
95+
9796
- [Generate a random address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
9897
- [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
9998
- [Generate a 2-of-3 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitcoinjs-lib",
3-
"version": "5.2.0",
3+
"version": "6.0.0",
44
"description": "Client-side Bitcoin JavaScript library",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",
@@ -62,7 +62,7 @@
6262
"@types/bs58check": "^2.1.0",
6363
"@types/create-hash": "^1.2.2",
6464
"@types/mocha": "^5.2.7",
65-
"@types/node": "^16.11.1",
65+
"@types/node": "^16.11.7",
6666
"@types/proxyquire": "^1.3.28",
6767
"@types/randombytes": "^2.0.0",
6868
"@types/wif": "^2.0.2",

src/bufferutils.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export declare function cloneBuffer(buffer: Buffer): Buffer;
1111
export declare class BufferWriter {
1212
buffer: Buffer;
1313
offset: number;
14+
static withCapacity(size: number): BufferWriter;
1415
constructor(buffer: Buffer, offset?: number);
1516
writeUInt8(i: number): void;
1617
writeInt32(i: number): void;
@@ -20,6 +21,7 @@ export declare class BufferWriter {
2021
writeSlice(slice: Buffer): void;
2122
writeVarSlice(slice: Buffer): void;
2223
writeVector(vector: Buffer[]): void;
24+
end(): Buffer;
2325
}
2426
/**
2527
* Helper class for reading of bitcoin data types from a buffer.

src/bufferutils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class BufferWriter {
5858
this.offset = offset;
5959
typeforce(types.tuple(types.Buffer, types.UInt32), [buffer, offset]);
6060
}
61+
static withCapacity(size) {
62+
return new BufferWriter(Buffer.alloc(size));
63+
}
6164
writeUInt8(i) {
6265
this.offset = this.buffer.writeUInt8(i, this.offset);
6366
}
@@ -88,6 +91,12 @@ class BufferWriter {
8891
this.writeVarInt(vector.length);
8992
vector.forEach(buf => this.writeVarSlice(buf));
9093
}
94+
end() {
95+
if (this.buffer.length === this.offset) {
96+
return this.buffer;
97+
}
98+
throw new Error(`buffer size ${this.buffer.length}, offset ${this.offset}`);
99+
}
91100
}
92101
exports.BufferWriter = BufferWriter;
93102
/**

src/crypto.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ export declare function sha1(buffer: Buffer): Buffer;
44
export declare function sha256(buffer: Buffer): Buffer;
55
export declare function hash160(buffer: Buffer): Buffer;
66
export declare function hash256(buffer: Buffer): Buffer;
7+
declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
8+
export declare type TaggedHashPrefix = typeof TAGS[number];
9+
export declare function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer;
10+
export {};

src/crypto.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.hash256 = exports.hash160 = exports.sha256 = exports.sha1 = exports.ripemd160 = void 0;
3+
exports.taggedHash = exports.hash256 = exports.hash160 = exports.sha256 = exports.sha1 = exports.ripemd160 = void 0;
44
const createHash = require('create-hash');
55
function ripemd160(buffer) {
66
try {
@@ -34,3 +34,25 @@ function hash256(buffer) {
3434
return sha256(sha256(buffer));
3535
}
3636
exports.hash256 = hash256;
37+
const TAGS = [
38+
'BIP0340/challenge',
39+
'BIP0340/aux',
40+
'BIP0340/nonce',
41+
'TapLeaf',
42+
'TapBranch',
43+
'TapSighash',
44+
'TapTweak',
45+
'KeyAgg list',
46+
'KeyAgg coefficient',
47+
];
48+
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
49+
const TAGGED_HASH_PREFIXES = Object.fromEntries(
50+
TAGS.map(tag => {
51+
const tagHash = sha256(Buffer.from(tag));
52+
return [tag, Buffer.concat([tagHash, tagHash])];
53+
}),
54+
);
55+
function taggedHash(prefix, data) {
56+
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));
57+
}
58+
exports.taggedHash = taggedHash;

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as payments from './payments';
55
import * as script from './script';
66
export { address, crypto, networks, payments, script };
77
export { Block } from './block';
8+
export { TaggedHashPrefix } from './crypto';
89
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt';
910
export { OPS as opcodes } from './ops';
1011
export { Transaction } from './transaction';

src/transaction.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ export interface Input {
1212
}
1313
export declare class Transaction {
1414
static readonly DEFAULT_SEQUENCE = 4294967295;
15+
static readonly SIGHASH_DEFAULT = 0;
1516
static readonly SIGHASH_ALL = 1;
1617
static readonly SIGHASH_NONE = 2;
1718
static readonly SIGHASH_SINGLE = 3;
1819
static readonly SIGHASH_ANYONECANPAY = 128;
20+
static readonly SIGHASH_OUTPUT_MASK = 3;
21+
static readonly SIGHASH_INPUT_MASK = 128;
1922
static readonly ADVANCED_TRANSACTION_MARKER = 0;
2023
static readonly ADVANCED_TRANSACTION_FLAG = 1;
2124
static fromBuffer(buffer: Buffer, _NO_STRICT?: boolean): Transaction;
@@ -42,6 +45,7 @@ export declare class Transaction {
4245
* This hash can then be used to sign the provided transaction input.
4346
*/
4447
hashForSignature(inIndex: number, prevOutScript: Buffer, hashType: number): Buffer;
48+
hashForWitnessV1(inIndex: number, prevOutScripts: Buffer[], values: number[], hashType: number, leafHash?: Buffer, annex?: Buffer): Buffer;
4549
hashForWitnessV0(inIndex: number, prevOutScript: Buffer, value: number, hashType: number): Buffer;
4650
getHash(forWitness?: boolean): Buffer;
4751
getId(): string;

0 commit comments

Comments
 (0)