Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export toXOnly #2191

Merged
merged 5 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
exports.initEccLib =
exports.Transaction =
exports.opcodes =
exports.toXOnly =
exports.Psbt =
exports.Block =
exports.script =
Expand Down Expand Up @@ -79,6 +80,12 @@ Object.defineProperty(exports, 'Psbt', {
return psbt_js_1.Psbt;
},
});
Object.defineProperty(exports, 'toXOnly', {
enumerable: true,
get: function () {
return psbt_js_1.toXOnly;
},
});
/** @hidden */
var ops_js_1 = require('./ops.cjs');
Object.defineProperty(exports, 'opcodes', {
Expand Down
2 changes: 1 addition & 1 deletion src/cjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { address, crypto, networks, payments, script };
export { Block } from './block.js';
/** @hidden */
export { TaggedHashPrefix } from './crypto.js';
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, } from './psbt.js';
export { Psbt, PsbtTxInput, PsbtTxOutput, Signer, SignerAsync, HDSigner, HDSignerAsync, toXOnly, } from './psbt.js';
/** @hidden */
export { OPS as opcodes } from './ops.js';
export { Transaction } from './transaction.js';
Expand Down
8 changes: 7 additions & 1 deletion src/cjs/psbt.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var __importStar =
return result;
};
Object.defineProperty(exports, '__esModule', { value: true });
exports.Psbt = void 0;
exports.Psbt = exports.toXOnly = void 0;
const bip174_1 = require('bip174');
const varuint = __importStar(require('varuint-bitcoin'));
const bip174_2 = require('bip174');
Expand All @@ -56,6 +56,12 @@ const bip341_js_1 = require('./payments/bip341.cjs');
const bscript = __importStar(require('./script.cjs'));
const transaction_js_1 = require('./transaction.cjs');
const bip371_js_1 = require('./psbt/bip371.cjs');
Object.defineProperty(exports, 'toXOnly', {
enumerable: true,
get: function () {
return bip371_js_1.toXOnly;
},
});
const psbtutils_js_1 = require('./psbt/psbtutils.cjs');
const tools = __importStar(require('uint8array-tools'));
/**
Expand Down
3 changes: 2 additions & 1 deletion src/cjs/psbt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Psbt as PsbtBase } from 'bip174';
import { KeyValue, PsbtGlobalUpdate, PsbtInput, PsbtInputUpdate, PsbtOutput, PsbtOutputUpdate } from 'bip174';
import { Network } from './networks.js';
import { Transaction } from './transaction.js';
import { toXOnly } from './psbt/bip371.js';
export { toXOnly };
export interface TransactionInput {
hash: string | Uint8Array;
index: number;
Expand Down Expand Up @@ -202,4 +204,3 @@ tapLeafHashToFinalize?: Uint8Array) => {
finalScriptWitness: Uint8Array | undefined;
};
type AllScriptType = 'witnesspubkeyhash' | 'pubkeyhash' | 'multisig' | 'pubkey' | 'nonstandard' | 'p2sh-witnesspubkeyhash' | 'p2sh-pubkeyhash' | 'p2sh-multisig' | 'p2sh-pubkey' | 'p2sh-nonstandard' | 'p2wsh-pubkeyhash' | 'p2wsh-multisig' | 'p2wsh-pubkey' | 'p2wsh-nonstandard' | 'p2sh-p2wsh-pubkeyhash' | 'p2sh-p2wsh-multisig' | 'p2sh-p2wsh-pubkey' | 'p2sh-p2wsh-nonstandard';
export {};
2 changes: 1 addition & 1 deletion src/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as payments from './payments/index.js';
import * as script from './script.js';
export { address, crypto, networks, payments, script };
export { Block } from './block.js';
export { Psbt } from './psbt.js';
export { Psbt, toXOnly } from './psbt.js';
/** @hidden */
export { OPS as opcodes } from './ops.js';
export { Transaction } from './transaction.js';
Expand Down
1 change: 1 addition & 0 deletions src/esm/psbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isP2TR,
} from './psbt/psbtutils.js';
import * as tools from 'uint8array-tools';
export { toXOnly };
/**
* These are the default arguments for a Psbt instance.
*/
Expand Down
23 changes: 23 additions & 0 deletions test/bip371.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { toXOnly } from 'bitcoinjs-lib';
import * as assert from 'assert';

describe('toXOnly', () => {
it('should return the input if the pubKey length is 32', () => {
const pubKey = new Uint8Array(32).fill(1); // Example 32-byte public key
const result = toXOnly(pubKey);
assert.strictEqual(result, pubKey); // Expect the same array (reference equality)
});

it('should return the sliced key if the pubKey length is greater than 32', () => {
const pubKey = new Uint8Array(33).fill(1);
pubKey[0] = 0; // Add a leading byte
const result = toXOnly(pubKey);
assert.deepStrictEqual(result, pubKey.slice(1, 33)); // Expect the sliced array
});

it('should return the key if the pubKey length is less than 32', () => {
const pubKey = new Uint8Array(31).fill(1); // Example invalid public key
const result = toXOnly(pubKey);
assert.deepStrictEqual(result, pubKey.slice(1, 33)); // Expect the sliced array
});
});
1 change: 1 addition & 0 deletions ts_src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
SignerAsync,
HDSigner,
HDSignerAsync,
toXOnly,
} from './psbt.js';
/** @hidden */
export { OPS as opcodes } from './ops.js';
Expand Down
2 changes: 2 additions & 0 deletions ts_src/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import {
} from './psbt/psbtutils.js';
import * as tools from 'uint8array-tools';

export { toXOnly };

export interface TransactionInput {
hash: string | Uint8Array;
index: number;
Expand Down
Loading