Skip to content

Commit 4347d89

Browse files
Merge pull request #119 from BitGo/BTC-2980.add-haspsbtmagic
feat(wasm-utxo): add hasPsbtMagic utility function
2 parents fbc7c4a + de806b9 commit 4347d89

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

packages/wasm-utxo/js/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@ export { WrapDescriptor as Descriptor } from "./wasm/wasm_utxo.js";
6868
export { WrapMiniscript as Miniscript } from "./wasm/wasm_utxo.js";
6969
export { WrapPsbt as Psbt } from "./wasm/wasm_utxo.js";
7070
export { DashTransaction, Transaction, ZcashTransaction } from "./transaction.js";
71+
export { hasPsbtMagic } from "./psbt.js";

packages/wasm-utxo/js/psbt.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/** PSBT magic bytes: "psbt" (0x70 0x73 0x62 0x74) followed by separator 0xff */
2+
const PSBT_MAGIC = new Uint8Array([0x70, 0x73, 0x62, 0x74, 0xff]);
3+
4+
/**
5+
* Check if a byte array has the PSBT magic bytes
6+
*
7+
* PSBTs start with the magic bytes "psbt" (0x70 0x73 0x62 0x74) followed by a separator 0xff.
8+
* This method checks if the given bytes start with these 5 magic bytes.
9+
*
10+
* @param bytes - The byte array to check
11+
* @returns true if the bytes start with PSBT magic, false otherwise
12+
*
13+
* @example
14+
* ```typescript
15+
* import { hasPsbtMagic } from "@bitgo/wasm-utxo";
16+
*
17+
* if (hasPsbtMagic(data)) {
18+
* const psbt = BitGoPsbt.fromBytes(data, network);
19+
* }
20+
* ```
21+
*/
22+
export function hasPsbtMagic(bytes: Uint8Array): boolean {
23+
if (bytes.length < PSBT_MAGIC.length) {
24+
return false;
25+
}
26+
for (let i = 0; i < PSBT_MAGIC.length; i++) {
27+
if (bytes[i] !== PSBT_MAGIC[i]) {
28+
return false;
29+
}
30+
}
31+
return true;
32+
}

0 commit comments

Comments
 (0)