diff --git a/src/background.js b/src/background.js index 73bacc8..765fac2 100644 --- a/src/background.js +++ b/src/background.js @@ -1,4 +1,4 @@ -import { Address, Signer, Tx } from "@cmdcode/tapscript"; +import { Address, Script, Signer, Tx } from "@cmdcode/tapscript"; import { schnorr, secp256k1 } from "@noble/curves/secp256k1"; import { bytesToHex } from "@noble/curves/abstract/utils"; @@ -22,14 +22,22 @@ function hydrate(hex, prevouts) { if (!Array.isArray(prevouts) || prevouts.length !== tx.vin.length) throw new Error("Prevout set does not match transaction inputs."); tx.vin.forEach((input, i) => { const prevout = prevouts[i]; - if (!Number.isSafeInteger(prevout?.value) || prevout.value < 0 || !Array.isArray(prevout?.scriptPubKey)) throw new Error("Invalid signing prevout."); - input.prevout = { value: prevout.value, scriptPubKey: prevout.scriptPubKey }; + const script = prevout?.scriptPubKey; + const scriptOk = Array.isArray(script) || (typeof script === "string" && /^(?:[0-9a-f]{2})+$/i.test(script)); + if (!Number.isSafeInteger(prevout?.value) || prevout.value < 0 || !scriptOk) throw new Error("Invalid signing prevout."); + input.prevout = { value: prevout.value, scriptPubKey: script }; }); return tx; } +function scriptHex(script) { + return typeof script === "string" ? script.toLowerCase() : Script.encode(script).hex.toLowerCase(); +} + function assertOwnKeyPath(me, tx, inputIndex) { - if (JSON.stringify(tx.vin[inputIndex]?.prevout?.scriptPubKey) !== JSON.stringify(["OP_1", me.pubkey])) { + const actual = tx.vin[inputIndex]?.prevout?.scriptPubKey; + const expected = Script.encode(["OP_1", me.pubkey]).hex.toLowerCase(); + if (!actual || scriptHex(actual) !== expected) { throw new Error("Refusing key-path signature for an input not owned by Inscriboor."); } }