{
  "version": 3,
  "sources": ["../src/utils/signature.ts", "../src/utils/keys.ts"],
  "sourcesContent": [
    "import { sha256 } from \"@noble/hashes/sha2.js\";\nimport { Signature as NobleSignature, verify } from \"@noble/secp256k1\";\nimport { c32address } from \"./address.ts\";\nimport { AddressVersion } from \"./constants.ts\";\nimport { concatBytes, hexToBytes, utf8ToBytes } from \"./encoding.ts\";\nimport { hashP2PKH } from \"./hash.ts\";\n\nexport interface RecoverableSignature {\n\t/** Recovery id (0–3) */\n\trecovery: number;\n\t/** 32-byte r value as hex */\n\tr: string;\n\t/** 32-byte s value as hex */\n\ts: string;\n}\n\n// --- Format conversion ---\n\n/**\n * Parse a 65-byte VRS hex signature into components.\n * VRS format: recovery (1 byte) + r (32 bytes) + s (32 bytes)\n */\nexport function parseSignature(hex: string): RecoverableSignature {\n\tconst clean = hex.startsWith(\"0x\") ? hex.slice(2) : hex;\n\tif (clean.length !== 130) {\n\t\tthrow new Error(\n\t\t\t`Invalid signature length: expected 130 hex chars, got ${clean.length}`,\n\t\t);\n\t}\n\treturn {\n\t\trecovery: Number.parseInt(clean.slice(0, 2), 16),\n\t\tr: clean.slice(2, 66),\n\t\ts: clean.slice(66, 130),\n\t};\n}\n\n/**\n * Serialize a recoverable signature to 65-byte VRS hex (130 chars).\n */\nexport function serializeSignature(sig: RecoverableSignature): string {\n\tconst v = sig.recovery.toString(16).padStart(2, \"0\");\n\treturn v + sig.r + sig.s;\n}\n\n/** Convert VRS hex to RSV hex: r+s (128 chars) + recovery (2 chars). */\nexport function signatureVrsToRsv(vrs: string): string {\n\tconst clean = vrs.startsWith(\"0x\") ? vrs.slice(2) : vrs;\n\treturn clean.slice(2) + clean.slice(0, 2);\n}\n\n/** Convert RSV hex to VRS hex: recovery (2 chars) + r+s (128 chars). */\nexport function signatureRsvToVrs(rsv: string): string {\n\tconst clean = rsv.startsWith(\"0x\") ? rsv.slice(2) : rsv;\n\treturn clean.slice(-2) + clean.slice(0, -2);\n}\n\n// --- Recovery ---\n\n/**\n * Recover a public key from a message hash and VRS signature.\n * @returns hex-encoded public key (33 bytes compressed, or 65 bytes uncompressed)\n */\nexport function recoverPublicKey(\n\thash: Uint8Array | string,\n\tsignature: string,\n\tcompressed = true,\n): string {\n\tconst parsed = parseSignature(signature);\n\tconst rs = parsed.r + parsed.s;\n\tconst sig = NobleSignature.fromCompact(rs).addRecoveryBit(parsed.recovery);\n\tconst msgHash = typeof hash === \"string\" ? hexToBytes(hash) : hash;\n\tconst point = sig.recoverPublicKey(msgHash);\n\treturn point.toHex(compressed);\n}\n\n/**\n * Recover a Stacks address from a message hash and VRS signature.\n * @param addressVersion defaults to MainnetSingleSig (22)\n */\nexport function recoverAddress(\n\thash: Uint8Array | string,\n\tsignature: string,\n\taddressVersion: number = AddressVersion.MainnetSingleSig,\n): string {\n\tconst pubkey = recoverPublicKey(hash, signature, true);\n\tconst pubkeyHash = hashP2PKH(hexToBytes(pubkey));\n\treturn c32address(addressVersion, pubkeyHash);\n}\n\n// --- Verification ---\n\n/**\n * Verify an ECDSA signature against a message hash and public key.\n * Accepts compact (64-byte) signature as hex or Uint8Array.\n */\nexport function verifySignature(\n\thash: Uint8Array | string,\n\tsignature: string | Uint8Array,\n\tpublicKey: string | Uint8Array,\n): boolean {\n\treturn verify(signature, hash, publicKey, { lowS: false });\n}\n\n/** Bitcoin-style varint encoding for message length prefix. */\nfunction encodeVarint(n: number): Uint8Array {\n\tif (n < 0xfd) return new Uint8Array([n]);\n\tif (n <= 0xffff) {\n\t\tconst buf = new Uint8Array(3);\n\t\tbuf[0] = 0xfd;\n\t\tbuf[1] = n & 0xff;\n\t\tbuf[2] = (n >> 8) & 0xff;\n\t\treturn buf;\n\t}\n\tconst buf = new Uint8Array(5);\n\tbuf[0] = 0xfe;\n\tbuf[1] = n & 0xff;\n\tbuf[2] = (n >> 8) & 0xff;\n\tbuf[3] = (n >> 16) & 0xff;\n\tbuf[4] = (n >> 24) & 0xff;\n\treturn buf;\n}\n\nconst STACKS_MESSAGE_PREFIX = \"\\x17Stacks Signed Message:\\n\";\nconst LEGACY_MESSAGE_PREFIX = \"\\x18Stacks Message Signing:\\n\";\n\n/** Hash a message with the Stacks structured message prefix. */\nfunction hashMessage(message: string | Uint8Array, prefix: string): Uint8Array {\n\tconst messageBytes =\n\t\ttypeof message === \"string\" ? utf8ToBytes(message) : message;\n\tconst prefixBytes = utf8ToBytes(prefix);\n\tconst lengthBytes = encodeVarint(messageBytes.length);\n\treturn sha256(concatBytes(prefixBytes, lengthBytes, messageBytes));\n}\n\n/**\n * Verify a signed Stacks message.\n * Parses a VRS signature, extracts r+s for verification.\n * Falls back to legacy prefix if standard prefix fails.\n */\nexport function verifyMessageSignature(\n\tmessage: string | Uint8Array,\n\tsignature: string,\n\tpublicKey: string,\n): boolean {\n\tconst parsed = parseSignature(signature);\n\tconst compactSig = parsed.r + parsed.s;\n\n\tconst msgHash = hashMessage(message, STACKS_MESSAGE_PREFIX);\n\tif (verify(compactSig, msgHash, publicKey, { lowS: false })) return true;\n\n\t// Fallback to legacy prefix\n\tif (typeof message === \"string\") {\n\t\tconst legacyHash = hashMessage(message, LEGACY_MESSAGE_PREFIX);\n\t\tif (verify(compactSig, legacyHash, publicKey, { lowS: false })) return true;\n\t}\n\n\treturn false;\n}\n",
    "import { Point, etc } from \"@noble/secp256k1\";\nimport { bytesToHex, hexToBytes } from \"./encoding.ts\";\n\n/** Compress a public key to 33 bytes (hex). */\nexport function compressPublicKey(publicKey: string | Uint8Array): string {\n\tconst hex = typeof publicKey === \"string\" ? publicKey : bytesToHex(publicKey);\n\treturn Point.fromHex(hex).toHex(true);\n}\n\n/** Uncompress a public key to 65 bytes (hex). */\nexport function uncompressPublicKey(publicKey: string | Uint8Array): string {\n\tconst hex = typeof publicKey === \"string\" ? publicKey : bytesToHex(publicKey);\n\treturn Point.fromHex(hex).toHex(false);\n}\n\n/** Check if a public key is in compressed format (starts with 0x02 or 0x03). */\nexport function isCompressedPublicKey(publicKey: string | Uint8Array): boolean {\n\tconst bytes =\n\t\ttypeof publicKey === \"string\" ? hexToBytes(publicKey) : publicKey;\n\treturn bytes.length === 33 && (bytes[0] === 0x02 || bytes[0] === 0x03);\n}\n\n/** Generate cryptographically secure random bytes. Defaults to 32 bytes. */\nexport function randomBytes(length?: number): Uint8Array {\n\treturn etc.randomBytes(length ?? 32);\n}\n"
  ],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAuB,IAAvB;AACoD,IAApD;AAqBO,SAAS,cAAc,CAAC,KAAmC;AAAA,EACjE,MAAM,QAAQ,IAAI,WAAW,IAAI,IAAI,IAAI,MAAM,CAAC,IAAI;AAAA,EACpD,IAAI,MAAM,WAAW,KAAK;AAAA,IACzB,MAAM,IAAI,MACT,yDAAyD,MAAM,QAChE;AAAA,EACD;AAAA,EACA,OAAO;AAAA,IACN,UAAU,OAAO,SAAS,MAAM,MAAM,GAAG,CAAC,GAAG,EAAE;AAAA,IAC/C,GAAG,MAAM,MAAM,GAAG,EAAE;AAAA,IACpB,GAAG,MAAM,MAAM,IAAI,GAAG;AAAA,EACvB;AAAA;AAMM,SAAS,kBAAkB,CAAC,KAAmC;AAAA,EACrE,MAAM,IAAI,IAAI,SAAS,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAAA,EACnD,OAAO,IAAI,IAAI,IAAI,IAAI;AAAA;AAIjB,SAAS,iBAAiB,CAAC,KAAqB;AAAA,EACtD,MAAM,QAAQ,IAAI,WAAW,IAAI,IAAI,IAAI,MAAM,CAAC,IAAI;AAAA,EACpD,OAAO,MAAM,MAAM,CAAC,IAAI,MAAM,MAAM,GAAG,CAAC;AAAA;AAIlC,SAAS,iBAAiB,CAAC,KAAqB;AAAA,EACtD,MAAM,QAAQ,IAAI,WAAW,IAAI,IAAI,IAAI,MAAM,CAAC,IAAI;AAAA,EACpD,OAAO,MAAM,MAAM,EAAE,IAAI,MAAM,MAAM,GAAG,EAAE;A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