diff --git a/src/core/operations/FromBech32.mjs b/src/core/operations/FromBech32.mjs index 51d8dae5..8a01d4db 100644 --- a/src/core/operations/FromBech32.mjs +++ b/src/core/operations/FromBech32.mjs @@ -34,7 +34,7 @@ class FromBech32 extends Operation { { "name": "Output Format", "type": "option", - "value": ["Hex", "Bitcoin scriptPubKey", "Raw", "HRP: Hex", "JSON"] + "value": ["Raw", "Hex", "Bitcoin scriptPubKey", "HRP: Hex", "JSON"] } ]; this.checks = [ diff --git a/src/core/operations/ToBech32.mjs b/src/core/operations/ToBech32.mjs index c8afa8c8..a59b4028 100644 --- a/src/core/operations/ToBech32.mjs +++ b/src/core/operations/ToBech32.mjs @@ -6,6 +6,7 @@ import Operation from "../Operation.mjs"; import { encode } from "../lib/Bech32.mjs"; +import { fromHex } from "../lib/Hex.mjs"; /** * To Bech32 operation @@ -38,7 +39,7 @@ class ToBech32 extends Operation { { "name": "Input Format", "type": "option", - "value": ["Raw bytes", "Bitcoin SegWit (version + program)"] + "value": ["Raw bytes", "Hex", "Bitcoin SegWit (version + program)"] } ]; } @@ -53,7 +54,15 @@ class ToBech32 extends Operation { const encoding = args[1]; const inputFormat = args[2]; - const inputArray = new Uint8Array(input); + let inputArray; + if (inputFormat === "Hex") { + // Convert hex string to bytes + const hexStr = new TextDecoder().decode(new Uint8Array(input)).replace(/\s/g, ""); + inputArray = fromHex(hexStr); + } else { + inputArray = new Uint8Array(input); + } + const segwit = inputFormat === "Bitcoin SegWit (version + program)"; return encode(hrp, inputArray, encoding, segwit);