Skip to content
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
1 change: 1 addition & 0 deletions packages/wasm-utxo/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ export { WrapDescriptor as Descriptor } from "./wasm/wasm_utxo.js";
export { WrapMiniscript as Miniscript } from "./wasm/wasm_utxo.js";
export { WrapPsbt as Psbt } from "./wasm/wasm_utxo.js";
export { DashTransaction, Transaction, ZcashTransaction } from "./transaction.js";
export { hasPsbtMagic } from "./psbt.js";
32 changes: 32 additions & 0 deletions packages/wasm-utxo/js/psbt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/** PSBT magic bytes: "psbt" (0x70 0x73 0x62 0x74) followed by separator 0xff */
const PSBT_MAGIC = new Uint8Array([0x70, 0x73, 0x62, 0x74, 0xff]);

/**
* Check if a byte array has the PSBT magic bytes
*
* PSBTs start with the magic bytes "psbt" (0x70 0x73 0x62 0x74) followed by a separator 0xff.
* This method checks if the given bytes start with these 5 magic bytes.
*
* @param bytes - The byte array to check
* @returns true if the bytes start with PSBT magic, false otherwise
*
* @example
* ```typescript
* import { hasPsbtMagic } from "@bitgo/wasm-utxo";
*
* if (hasPsbtMagic(data)) {
* const psbt = BitGoPsbt.fromBytes(data, network);
* }
* ```
*/
export function hasPsbtMagic(bytes: Uint8Array): boolean {
if (bytes.length < PSBT_MAGIC.length) {
return false;
}
for (let i = 0; i < PSBT_MAGIC.length; i++) {
if (bytes[i] !== PSBT_MAGIC[i]) {
return false;
}
}
return true;
}