Skip to content

Commit fd57d46

Browse files
committed
Merge branch 'opt-decompile' of https://github.com/jasonandjay/bitcoinjs-lib
2 parents 846caf6 + 7efa3f9 commit fd57d46

File tree

10 files changed

+9
-230
lines changed

10 files changed

+9
-230
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ test/*.js
66
test/integration/*.js
77
!test/ts-node-register.js
88
docs
9+
plugin

package-lock.json

-104
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/psbt.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class Psbt {
243243
if (typeof address === 'string') {
244244
const { network } = this.opts;
245245
const script = (0, address_1.toOutputScript)(address, network);
246-
outputData = Object.assign(outputData, { script });
246+
outputData = Object.assign({}, outputData, { script });
247247
}
248248
(0, bip371_1.checkTaprootOutputFields)(outputData, outputData, 'addOutput');
249249
const c = this.__CACHE;

src/script.js

+3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ function toASM(chunks) {
158158
if (chunksIsBuffer(chunks)) {
159159
chunks = decompile(chunks);
160160
}
161+
if (!chunks) {
162+
throw new Error('Could not convert invalid chunks to ASM');
163+
}
161164
return chunks
162165
.map(chunk => {
163166
// data?

src/types.d.ts

-8
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,7 @@ export declare function stacksEqual(a: Buffer[], b: Buffer[]): boolean;
1313
* @returns True if the value is a valid elliptic curve point, false otherwise.
1414
*/
1515
export declare function isPoint(p: Buffer | number | undefined | null): boolean;
16-
export declare function UInt31(value: number): boolean;
17-
export declare function BIP32Path(value: string): boolean;
18-
export declare namespace BIP32Path {
19-
var toJSON: () => string;
20-
}
21-
export declare function Signer(obj: any): boolean;
2216
export declare function Satoshi(value: number): boolean;
23-
export declare const ECPoint: any;
24-
export declare const Network: any;
2517
export interface XOnlyPointAddTweakResult {
2618
parity: 1 | 0;
2719
xOnlyPubkey: Uint8Array;

src/types.js

-43
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ exports.oneOf =
2020
exports.isTaptree =
2121
exports.isTapleaf =
2222
exports.TAPLEAF_VERSION_MASK =
23-
exports.Network =
24-
exports.ECPoint =
2523
exports.Satoshi =
26-
exports.Signer =
27-
exports.BIP32Path =
28-
exports.UInt31 =
2924
exports.isPoint =
3025
exports.stacksEqual =
3126
exports.typeforce =
@@ -72,49 +67,11 @@ function isPoint(p) {
7267
return false;
7368
}
7469
exports.isPoint = isPoint;
75-
const UINT31_MAX = Math.pow(2, 31) - 1;
76-
function UInt31(value) {
77-
return exports.typeforce.UInt32(value) && value <= UINT31_MAX;
78-
}
79-
exports.UInt31 = UInt31;
80-
function BIP32Path(value) {
81-
return (
82-
exports.typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
83-
);
84-
}
85-
exports.BIP32Path = BIP32Path;
86-
BIP32Path.toJSON = () => {
87-
return 'BIP32 derivation path';
88-
};
89-
function Signer(obj) {
90-
return (
91-
(exports.typeforce.Buffer(obj.publicKey) ||
92-
typeof obj.getPublicKey === 'function') &&
93-
typeof obj.sign === 'function'
94-
);
95-
}
96-
exports.Signer = Signer;
9770
const SATOSHI_MAX = 21 * 1e14;
9871
function Satoshi(value) {
9972
return exports.typeforce.UInt53(value) && value <= SATOSHI_MAX;
10073
}
10174
exports.Satoshi = Satoshi;
102-
// external dependent types
103-
exports.ECPoint = exports.typeforce.quacksLike('Point');
104-
// exposed, external API
105-
exports.Network = exports.typeforce.compile({
106-
messagePrefix: exports.typeforce.oneOf(
107-
exports.typeforce.Buffer,
108-
exports.typeforce.String,
109-
),
110-
bip32: {
111-
public: exports.typeforce.UInt32,
112-
private: exports.typeforce.UInt32,
113-
},
114-
pubKeyHash: exports.typeforce.UInt8,
115-
scriptHash: exports.typeforce.UInt8,
116-
wif: exports.typeforce.UInt8,
117-
});
11875
exports.TAPLEAF_VERSION_MASK = 0xfe;
11976
function isTapleaf(o) {
12077
if (!o || !('output' in o)) return false;

test/types.spec.ts

-37
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,4 @@ describe('types', () => {
5454
});
5555
});
5656
});
57-
58-
describe('UInt31', () => {
59-
const UINT31_MAX = Math.pow(2, 31) - 1;
60-
it('return true for valid values', () => {
61-
assert.strictEqual(types.UInt31(0), true);
62-
assert.strictEqual(types.UInt31(1000), true);
63-
assert.strictEqual(types.UInt31(UINT31_MAX), true);
64-
});
65-
66-
it('return false for negative values', () => {
67-
assert.strictEqual(types.UInt31(-1), false);
68-
assert.strictEqual(types.UInt31(-UINT31_MAX), false);
69-
});
70-
71-
it(`return false for value > ${UINT31_MAX}`, () => {
72-
assert.strictEqual(types.UInt31(UINT31_MAX + 1), false);
73-
});
74-
});
75-
76-
describe('BIP32Path', () => {
77-
it('return true for valid paths', () => {
78-
assert.strictEqual(types.BIP32Path("m/0'/0'"), true);
79-
assert.strictEqual(types.BIP32Path("m/0'/0"), true);
80-
assert.strictEqual(types.BIP32Path("m/0'/1'/2'/3/4'"), true);
81-
});
82-
83-
it('return false for invalid paths', () => {
84-
assert.strictEqual(types.BIP32Path('m'), false);
85-
assert.strictEqual(types.BIP32Path("n/0'/0'"), false);
86-
assert.strictEqual(types.BIP32Path("m/0'/x"), false);
87-
});
88-
89-
it('return "BIP32 derivation path" for JSON.strigify()', () => {
90-
const toJsonValue = JSON.stringify(types.BIP32Path);
91-
assert.equal(toJsonValue, '"BIP32 derivation path"');
92-
});
93-
});
9457
});

ts_src/psbt.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class Psbt {
331331
if (typeof address === 'string') {
332332
const { network } = this.opts;
333333
const script = toOutputScript(address, network);
334-
outputData = Object.assign(outputData, { script });
334+
outputData = Object.assign({}, outputData, { script });
335335
}
336336
checkTaprootOutputFields(outputData, outputData, 'addOutput');
337337

ts_src/script.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ export function toASM(chunks: Buffer | Array<number | Buffer>): string {
168168
if (chunksIsBuffer(chunks)) {
169169
chunks = decompile(chunks) as Stack;
170170
}
171-
171+
if (!chunks) {
172+
throw new Error('Could not convert invalid chunks to ASM');
173+
}
172174
return chunks
173175
.map(chunk => {
174176
// data?

ts_src/types.ts

-35
Original file line numberDiff line numberDiff line change
@@ -46,46 +46,11 @@ export function isPoint(p: Buffer | number | undefined | null): boolean {
4646
return false;
4747
}
4848

49-
const UINT31_MAX: number = Math.pow(2, 31) - 1;
50-
export function UInt31(value: number): boolean {
51-
return typeforce.UInt32(value) && value <= UINT31_MAX;
52-
}
53-
54-
export function BIP32Path(value: string): boolean {
55-
return typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/);
56-
}
57-
BIP32Path.toJSON = (): string => {
58-
return 'BIP32 derivation path';
59-
};
60-
61-
export function Signer(obj: any): boolean {
62-
return (
63-
(typeforce.Buffer(obj.publicKey) ||
64-
typeof obj.getPublicKey === 'function') &&
65-
typeof obj.sign === 'function'
66-
);
67-
}
68-
6949
const SATOSHI_MAX: number = 21 * 1e14;
7050
export function Satoshi(value: number): boolean {
7151
return typeforce.UInt53(value) && value <= SATOSHI_MAX;
7252
}
7353

74-
// external dependent types
75-
export const ECPoint = typeforce.quacksLike('Point');
76-
77-
// exposed, external API
78-
export const Network = typeforce.compile({
79-
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
80-
bip32: {
81-
public: typeforce.UInt32,
82-
private: typeforce.UInt32,
83-
},
84-
pubKeyHash: typeforce.UInt8,
85-
scriptHash: typeforce.UInt8,
86-
wif: typeforce.UInt8,
87-
});
88-
8954
export interface XOnlyPointAddTweakResult {
9055
parity: 1 | 0;
9156
xOnlyPubkey: Uint8Array;

0 commit comments

Comments
 (0)