Skip to content

Commit

Permalink
Merge branch 'opt-decompile' of https://github.com/jasonandjay/bitcoi…
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonandjay committed May 17, 2024
2 parents 846caf6 + 7efa3f9 commit fd57d46
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 230 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ test/*.js
test/integration/*.js
!test/ts-node-register.js
docs
plugin
104 changes: 0 additions & 104 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class Psbt {
if (typeof address === 'string') {
const { network } = this.opts;
const script = (0, address_1.toOutputScript)(address, network);
outputData = Object.assign(outputData, { script });
outputData = Object.assign({}, outputData, { script });
}
(0, bip371_1.checkTaprootOutputFields)(outputData, outputData, 'addOutput');
const c = this.__CACHE;
Expand Down
3 changes: 3 additions & 0 deletions src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ function toASM(chunks) {
if (chunksIsBuffer(chunks)) {
chunks = decompile(chunks);
}
if (!chunks) {
throw new Error('Could not convert invalid chunks to ASM');
}
return chunks
.map(chunk => {
// data?
Expand Down
8 changes: 0 additions & 8 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@ export declare function stacksEqual(a: Buffer[], b: Buffer[]): boolean;
* @returns True if the value is a valid elliptic curve point, false otherwise.
*/
export declare function isPoint(p: Buffer | number | undefined | null): boolean;
export declare function UInt31(value: number): boolean;
export declare function BIP32Path(value: string): boolean;
export declare namespace BIP32Path {
var toJSON: () => string;
}
export declare function Signer(obj: any): boolean;
export declare function Satoshi(value: number): boolean;
export declare const ECPoint: any;
export declare const Network: any;
export interface XOnlyPointAddTweakResult {
parity: 1 | 0;
xOnlyPubkey: Uint8Array;
Expand Down
43 changes: 0 additions & 43 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ exports.oneOf =
exports.isTaptree =
exports.isTapleaf =
exports.TAPLEAF_VERSION_MASK =
exports.Network =
exports.ECPoint =
exports.Satoshi =
exports.Signer =
exports.BIP32Path =
exports.UInt31 =
exports.isPoint =
exports.stacksEqual =
exports.typeforce =
Expand Down Expand Up @@ -72,49 +67,11 @@ function isPoint(p) {
return false;
}
exports.isPoint = isPoint;
const UINT31_MAX = Math.pow(2, 31) - 1;
function UInt31(value) {
return exports.typeforce.UInt32(value) && value <= UINT31_MAX;
}
exports.UInt31 = UInt31;
function BIP32Path(value) {
return (
exports.typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
);
}
exports.BIP32Path = BIP32Path;
BIP32Path.toJSON = () => {
return 'BIP32 derivation path';
};
function Signer(obj) {
return (
(exports.typeforce.Buffer(obj.publicKey) ||
typeof obj.getPublicKey === 'function') &&
typeof obj.sign === 'function'
);
}
exports.Signer = Signer;
const SATOSHI_MAX = 21 * 1e14;
function Satoshi(value) {
return exports.typeforce.UInt53(value) && value <= SATOSHI_MAX;
}
exports.Satoshi = Satoshi;
// external dependent types
exports.ECPoint = exports.typeforce.quacksLike('Point');
// exposed, external API
exports.Network = exports.typeforce.compile({
messagePrefix: exports.typeforce.oneOf(
exports.typeforce.Buffer,
exports.typeforce.String,
),
bip32: {
public: exports.typeforce.UInt32,
private: exports.typeforce.UInt32,
},
pubKeyHash: exports.typeforce.UInt8,
scriptHash: exports.typeforce.UInt8,
wif: exports.typeforce.UInt8,
});
exports.TAPLEAF_VERSION_MASK = 0xfe;
function isTapleaf(o) {
if (!o || !('output' in o)) return false;
Expand Down
37 changes: 0 additions & 37 deletions test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,4 @@ 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"');
});
});
});
2 changes: 1 addition & 1 deletion ts_src/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class Psbt {
if (typeof address === 'string') {
const { network } = this.opts;
const script = toOutputScript(address, network);
outputData = Object.assign(outputData, { script });
outputData = Object.assign({}, outputData, { script });
}
checkTaprootOutputFields(outputData, outputData, 'addOutput');

Expand Down
4 changes: 3 additions & 1 deletion ts_src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export function toASM(chunks: Buffer | Array<number | Buffer>): string {
if (chunksIsBuffer(chunks)) {
chunks = decompile(chunks) as Stack;
}

if (!chunks) {
throw new Error('Could not convert invalid chunks to ASM');
}
return chunks
.map(chunk => {
// data?
Expand Down
35 changes: 0 additions & 35 deletions ts_src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,11 @@ export function isPoint(p: Buffer | number | undefined | null): boolean {
return false;
}

const UINT31_MAX: number = Math.pow(2, 31) - 1;
export function UInt31(value: number): boolean {
return typeforce.UInt32(value) && value <= UINT31_MAX;
}

export function BIP32Path(value: string): boolean {
return typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/);
}
BIP32Path.toJSON = (): string => {
return 'BIP32 derivation path';
};

export function Signer(obj: any): boolean {
return (
(typeforce.Buffer(obj.publicKey) ||
typeof obj.getPublicKey === 'function') &&
typeof obj.sign === 'function'
);
}

const SATOSHI_MAX: number = 21 * 1e14;
export function Satoshi(value: number): boolean {
return typeforce.UInt53(value) && value <= SATOSHI_MAX;
}

// external dependent types
export const ECPoint = typeforce.quacksLike('Point');

// exposed, external API
export const Network = typeforce.compile({
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
bip32: {
public: typeforce.UInt32,
private: typeforce.UInt32,
},
pubKeyHash: typeforce.UInt8,
scriptHash: typeforce.UInt8,
wif: typeforce.UInt8,
});

export interface XOnlyPointAddTweakResult {
parity: 1 | 0;
xOnlyPubkey: Uint8Array;
Expand Down

0 comments on commit fd57d46

Please sign in to comment.