Export isWitnessProgram function and add PSBT_GLOBAL

This commit is contained in:
natsoni 2026-07-08 11:18:17 +02:00
parent c31b0e816d
commit a9c296c44b
No known key found for this signature in database
GPG key ID: 5554443C889C720E

View file

@ -713,7 +713,7 @@ function isNonStandardLegacySigops(tx: Transaction, height?: number, network?: s
// A witness program is any valid scriptpubkey that consists of a 1-byte push opcode
// followed by a data push between 2 and 40 bytes.
// https://github.com/bitcoin/bitcoin/blob/2c79abc7ad4850e9e3ba32a04c530155cda7f980/src/script/script.cpp#L224-L240
function isWitnessProgram(scriptpubkey: string): false | { version: number, program: string } {
export function isWitnessProgram(scriptpubkey: string): false | { version: number, program: string } {
if (scriptpubkey.length < 8 || scriptpubkey.length > 84) {
return false;
}
@ -1456,6 +1456,11 @@ function fromBuffer(buffer: Uint8Array, network: string, inputs?: PsbtKeyValueMa
export type PsbtKeyValue = { keyData: Uint8Array; value: Uint8Array; };
export type PsbtKeyValueMap = Map<number, PsbtKeyValue[]>;
export const PSBT_GLOBAL = {
UNSIGNED_TX: 0x00,
GENERIC_SIGNED_MESSAGE: 0x09,
};
export const PSBT_IN = {
NON_WITNESS_UTXO: 0x00,
WITNESS_UTXO: 0x01,
@ -1601,9 +1606,10 @@ function decodePsbt(psbtBuffer: Uint8Array): { rawTx: Uint8Array; inputs: PsbtKe
* @param rawTx - The unsigned transaction as Uint8Array
* @param inputs - Array of input maps containing key-value pairs for each input
* @param outputs - Array of output maps containing key-value pairs for each output
* @param globals - Global key-value pairs for the PSBT
* @returns PSBT buffer as Uint8Array
*/
export function encodePsbt(rawTx: Uint8Array, inputs: PsbtKeyValueMap[], outputs: PsbtKeyValueMap[]): Uint8Array {
export function encodePsbt(rawTx: Uint8Array, inputs: PsbtKeyValueMap[], outputs: PsbtKeyValueMap[], globals: PsbtKeyValueMap = new Map()): Uint8Array {
const result: number[] = [];
// Magic bytes: "psbt" in ASCII
@ -1633,7 +1639,12 @@ export function encodePsbt(rawTx: Uint8Array, inputs: PsbtKeyValueMap[], outputs
// GLOBAL MAP
// Add unsigned transaction (key type 0x00)
writeKeyValue(0x00, new Uint8Array(), rawTx);
writeKeyValue(PSBT_GLOBAL.UNSIGNED_TX, new Uint8Array(), rawTx);
for (const [keyType, items] of globals) {
for (const record of items) {
writeKeyValue(keyType, record.keyData, record.value);
}
}
// End global map
result.push(0x00);