@metamask/eth-ledger-bridge-keyring
Version:
A MetaMask compatible keyring, for ledger hardware wallets
1 lines • 4.08 kB
Source Map (JSON)
{"version":3,"file":"internal-utils.cjs","sourceRoot":"","sources":["../../src/dmk/internal-utils.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAa;IAC1C,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AACzD,CAAC;AAFD,wCAEC;AAED;;;;;;;GAOG;AACH,SAAgB,eAAe,CAAC,IAAY;IAC1C,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AACnC,CAAC;AAFD,0CAEC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,KAA+B;IACzD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC5B,CAAC;AALD,kCAKC;AAED,MAAM,cAAc,GAAG,mBAAmB,CAAC;AAE3C;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CAAC,KAAa;IACtC,MAAM,UAAU,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IACzC,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC7B,CAAC;IACD,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CACb,yDAAyD;YACvD,GAAG,UAAU,CAAC,MAAM,OAAO,UAAU,GAAG,CAC3C,CAAC;IACJ,CAAC;IAED,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAC3C,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,iEAAiE;QACjE,OAAO,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC7B,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CACb,4CAA4C,IAAI,SAAS,UAAU,GAAG,CACvE,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC;AA3BD,gCA2BC","sourcesContent":["/**\n * Pure helpers shared by the DMK bridge and its tests. Extracted from\n * `LedgerDmkBridge` so they can be unit-tested in isolation and reused\n * without an instance.\n *\n * @module dmk/internal-utils\n */\n\n/**\n * Strip a leading `0x` from a hex string, if present.\n *\n * @param value - The hex string (with or without `0x` prefix).\n * @returns The hex string with no `0x` prefix.\n */\nexport function stripHexPrefix(value: string): string {\n return value.startsWith('0x') ? value.slice(2) : value;\n}\n\n/**\n * Strip the leading `m/` from a BIP-32 derivation path.\n *\n * DMK / signer-kit accepts paths without the leading `m/`.\n *\n * @param path - The BIP-32 path (e.g. `m/44'/60'/0'/0/0`).\n * @returns The path with no leading `m/`.\n */\nexport function stripPathPrefix(path: string): string {\n return path.replace(/^m\\//u, '');\n}\n\n/**\n * Normalize a signature `v` (or any numeric/string hex-ish value) to a hex\n * string without `0x` prefix.\n *\n * @param value - The raw value (`bigint`, `number`, or hex string).\n * @returns The hex string with no `0x` prefix.\n */\nexport function toHexString(value: bigint | number | string): string {\n if (typeof value === 'string') {\n return stripHexPrefix(value);\n }\n return value.toString(16);\n}\n\nconst HEX_PAIR_REGEX = /^[0-9a-fA-F]{2}$/u;\n\n/**\n * Convert a hex string to a `Uint8Array`. Strips a leading `0x` if present.\n *\n * Throws on inputs that would silently produce wrong bytes:\n * - odd-length strings (e.g. `'abc'`)\n * - non-hex characters (e.g. `'zz'`)\n *\n * @param value - The hex string (with or without `0x` prefix).\n * @returns The decoded bytes.\n * @throws {Error} If the input has odd length or contains non-hex characters.\n */\nexport function hexToBytes(value: string): Uint8Array {\n const normalized = stripHexPrefix(value);\n if (normalized.length === 0) {\n return Uint8Array.from([]);\n }\n if (normalized.length % 2 !== 0) {\n throw new Error(\n 'Hex string must have an even number of characters (got ' +\n `${normalized.length}): \"${normalized}\"`,\n );\n }\n\n const pairs = normalized.match(/.{1,2}/gu);\n if (!pairs) {\n // Should be unreachable given the length check above; defensive.\n return Uint8Array.from([]);\n }\n\n for (const pair of pairs) {\n if (!HEX_PAIR_REGEX.test(pair)) {\n throw new Error(\n `Hex string contains non-hex characters: \"${pair}\" in \"${normalized}\"`,\n );\n }\n }\n\n return Uint8Array.from(pairs.map((pair) => parseInt(pair, 16)));\n}\n"]}