@qrkit/core 0.3.2 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/parseXpub.ts","../src/eth/address.ts","../src/eth/deriveAccount.ts","../src/parseConnection.ts","../src/cbor.ts","../src/urEncoding.ts","../src/eth/signRequest.ts","../src/eth/signature.ts"],"sourcesContent":["// Types\nexport type { ScannedUR, Chain, QRKitConfig, Account, EvmAccount } from \"./types.js\";\nexport type { ParsedXpub, XpubType } from \"./parseXpub.js\";\n\n// Connection\nexport { parseConnection } from \"./parseConnection.js\";\n\n// EVM signing\nexport {\n buildEthSignRequestUR,\n buildEthSignRequestURParts,\n EthDataType,\n} from \"./eth/signRequest.js\";\nexport type { EthDataTypeValue, EthSignRequestParams } from \"./eth/signRequest.js\";\nexport { parseEthSignature } from \"./eth/signature.js\";\n","import { HDKey } from \"@scure/bip32\";\nimport { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport type { ScannedUR } from \"./types.js\";\n\nexport type XpubType = \"xpub\";\n\nexport interface ParsedXpub {\n hdKey: HDKey;\n type: XpubType;\n /** BIP-44 purpose index: 44 for EVM */\n purpose: number | undefined;\n /** BIP-44 coin type: 60 = ETH */\n coinType: number | undefined;\n /** source-fingerprint from the origin keypath — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name from crypto-hdkey key 9, set by some wallets (e.g. Keystone) */\n name?: string;\n raw: string;\n}\n\nfunction get(m: unknown, k: number): unknown {\n if (m instanceof Map) return (m as Map<number, unknown>).get(k);\n return undefined;\n}\n\nconst passthrough = (v: unknown) => v;\n\nfunction decodeCbor(cbor: Uint8Array): Map<number, unknown> {\n return cborDecode(cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], {\n 303: passthrough, // crypto-hdkey\n 304: passthrough, // crypto-keypath\n 305: passthrough, // crypto-coin-info\n }),\n }) as Map<number, unknown>;\n}\n\nfunction parseCryptoHdKey(map: unknown, raw: string): ParsedXpub {\n const keyData = get(map, 3) as Uint8Array | undefined;\n const chainCode = get(map, 4) as Uint8Array | undefined;\n\n if (!keyData || !chainCode) {\n throw new Error(\"crypto-hdkey missing key-data or chain-code\");\n }\n\n let purpose: number | undefined;\n let coinType: number | undefined;\n let sourceFingerprint: number | undefined;\n const origin = get(map, 6);\n if (origin) {\n const components = get(origin, 1);\n if (Array.isArray(components)) {\n if (components.length >= 1) purpose = components[0] as number;\n if (components.length >= 3) coinType = components[2] as number;\n }\n sourceFingerprint = get(origin, 2) as number | undefined;\n }\n\n const name = get(map, 9) as string | undefined;\n\n const hdKey = new HDKey({ publicKey: keyData, chainCode });\n return { hdKey, type: \"xpub\", purpose, coinType, sourceFingerprint, name, raw };\n}\n\nfunction parseScannedUR(scanned: ScannedUR): ParsedXpub | ParsedXpub[] {\n const { type, cbor } = scanned;\n const raw = `ur:${type}`;\n\n if (type !== \"crypto-hdkey\" && type !== \"crypto-account\") {\n throw new Error(`Unsupported UR type: ${type}`);\n }\n\n const map = decodeCbor(cbor);\n\n if (type === \"crypto-hdkey\") {\n return parseCryptoHdKey(map, raw);\n }\n\n const accounts = map.get(2) as unknown[] | undefined;\n if (!Array.isArray(accounts) || accounts.length === 0) {\n throw new Error(\"crypto-account contains no keys\");\n }\n return accounts.map((entry) => parseCryptoHdKey(entry, raw));\n}\n\nexport function parseXpub(input: ScannedUR | string): ParsedXpub[] {\n if (typeof input !== \"string\") {\n const result = parseScannedUR(input);\n return Array.isArray(result) ? result : [result];\n }\n // Raw base58 xpub string — @scure/bip32 handles decoding directly\n const hdKey = HDKey.fromExtendedKey(input.trim());\n return [\n {\n hdKey,\n type: \"xpub\",\n purpose: undefined,\n coinType: undefined,\n sourceFingerprint: undefined,\n raw: input.trim(),\n },\n ];\n}\n","import { keccak_256 } from \"@noble/hashes/sha3.js\";\nimport * as secp from \"@noble/secp256k1\";\n\nexport function pubKeyToEthAddress(compressedPubKey: Uint8Array): string {\n const uncompressed = secp.Point.fromBytes(compressedPubKey).toBytes(false);\n const hash = keccak_256(uncompressed.slice(1));\n const hex = [...hash.slice(12)]\n .map((b: number) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n return toChecksumAddress(hex);\n}\n\nfunction toChecksumAddress(hex: string): string {\n const checksumHash = keccak_256(new TextEncoder().encode(hex));\n return (\n \"0x\" +\n [...hex]\n .map((c, i) => {\n if (c >= \"0\" && c <= \"9\") return c;\n const nibble =\n i % 2 === 0\n ? (checksumHash[Math.floor(i / 2)] >> 4) & 0xf\n : checksumHash[Math.floor(i / 2)] & 0xf;\n return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();\n })\n .join(\"\")\n );\n}\n","import type { HDKey } from \"@scure/bip32\";\n\nimport type { ParsedXpub } from \"../parseXpub.js\";\nimport { pubKeyToEthAddress } from \"./address.js\";\n\nexport interface DerivedAccount {\n address: string;\n publicKey: string;\n /** source-fingerprint from the scanned xpub — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name as reported by the hardware wallet, if available */\n device: string | undefined;\n}\n\n// Derive the first external address (index 0) from an account-level xpub.\n// Account-level xpub is at depth 3 (m/purpose'/coin'/account').\n// External chain is child 0, then address index 0.\nfunction firstChild(accountKey: HDKey): HDKey {\n return accountKey.deriveChild(0).deriveChild(0);\n}\n\nfunction toHex(bytes: Uint8Array): string {\n return [...bytes].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport function deriveEvmAccount(parsed: ParsedXpub[]): DerivedAccount | undefined {\n for (const entry of parsed) {\n const { hdKey, purpose, coinType, type, sourceFingerprint, name } = entry;\n const isEvm =\n (purpose === 44 && coinType === 60) || (purpose === undefined && type === \"xpub\");\n\n if (!isEvm) continue;\n\n const child = firstChild(hdKey);\n if (!child.publicKey) continue;\n\n return {\n address: pubKeyToEthAddress(child.publicKey),\n publicKey: toHex(child.publicKey),\n sourceFingerprint,\n device: name,\n };\n }\n return undefined;\n}\n","import { parseXpub } from \"./parseXpub.js\";\nimport { deriveEvmAccount } from \"./eth/deriveAccount.js\";\nimport type { Account, Chain, EvmAccount, QRKitConfig, ScannedUR } from \"./types.js\";\n\nconst ALL_CHAINS: Chain[] = [\"evm\", \"btc\"];\n\n/**\n * Parse a connection QR (crypto-hdkey or crypto-account) and return\n * only the accounts for the chains configured in QRKitConfig.\n *\n * A dApp configured with `chains: [\"evm\"]` will never see BTC accounts,\n * and vice versa. Both chains can be enabled with `chains: [\"evm\", \"btc\"]`.\n * If `chains` is omitted, all supported chains are tried.\n */\nexport function parseConnection(\n scannedUR: ScannedUR,\n config: QRKitConfig = {},\n): Account[] {\n const chains = config.chains ?? ALL_CHAINS;\n const parsed = parseXpub(scannedUR);\n const accounts: Account[] = [];\n\n if (chains.includes(\"evm\")) {\n const account = deriveEvmAccount(parsed);\n if (account) {\n accounts.push({ chain: \"evm\", ...account } satisfies EvmAccount);\n }\n }\n\n // \"btc\" — will be implemented in src/btc/ when Bitcoin support is added.\n\n return accounts;\n}\n","// Minimal CBOR encoder.\n// Supports only the types needed for eth-sign-request: uint, bytes, text, array, map (integer keys), bool, tag.\n\nfunction majorType(major: number, n: number): number[] {\n const base = major << 5;\n if (n <= 23) return [base | n];\n if (n <= 0xff) return [base | 0x18, n];\n if (n <= 0xffff) return [base | 0x19, (n >> 8) & 0xff, n & 0xff];\n return [base | 0x1a, (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff];\n}\n\nexport function encodeItem(value: unknown): number[] {\n if (typeof value === \"boolean\") {\n return [value ? 0xf5 : 0xf4];\n }\n if (typeof value === \"number\") {\n return majorType(0, value);\n }\n if (value instanceof Uint8Array) {\n return [...majorType(2, value.length), ...value];\n }\n if (typeof value === \"string\") {\n const bytes = new TextEncoder().encode(value);\n return [...majorType(3, bytes.length), ...bytes];\n }\n if (Array.isArray(value)) {\n return [...majorType(4, value.length), ...value.flatMap(encodeItem)];\n }\n if (value instanceof CborTag) {\n return [...majorType(6, value.tag), ...encodeItem(value.value)];\n }\n if (value instanceof Map) {\n const entries = [...value.entries()];\n return [\n ...majorType(5, entries.length),\n ...entries.flatMap(([k, v]) => [...encodeItem(k), ...encodeItem(v)]),\n ];\n }\n throw new Error(`Unsupported CBOR value: ${typeof value}`);\n}\n\nexport class CborTag {\n tag: number;\n value: unknown;\n constructor(tag: number, value: unknown) {\n this.tag = tag;\n this.value = value;\n }\n}\n\nexport function encode(value: unknown): Uint8Array {\n return new Uint8Array(encodeItem(value));\n}\n","import { UR, UrFountainEncoder } from \"@qrkit/bc-ur-web\";\n\nexport function encodeURParts(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string[] {\n const ur = UR.fromCbor({ type, payload: cbor });\n const encoder = new UrFountainEncoder(ur, maxFragmentLength);\n return encoder.getAllPartsUr().map((part) => part.toString().toUpperCase());\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\n\n// ERC-4527 eth-sign-request data types\nexport const EthDataType = {\n /** Legacy transaction (RLP-encoded). Wallet applies EIP-155: v = 35 + 2*chainId + recId */\n LegacyTransaction: 1,\n /** EIP-712 typed data bytes. Wallet hashes internally. */\n TypedData: 2,\n /** Personal message (EIP-191). Wallet prepends \"\\x19Ethereum Signed Message:\\n{len}\". */\n PersonalMessage: 3,\n /** EIP-1559 transaction (RLP-encoded). Wallet uses v = recId. */\n TypedTransaction: 4,\n} as const;\n\nexport type EthDataTypeValue = (typeof EthDataType)[keyof typeof EthDataType];\n\n// CBOR tag for crypto-keypath\nconst TAG_KEYPATH = 304;\n\nfunction randomBytes(n: number): Uint8Array {\n const buf = new Uint8Array(n);\n crypto.getRandomValues(buf);\n return buf;\n}\n\nfunction buildKeypath(\n purpose: number,\n coinType: number,\n sourceFingerprint: number | undefined,\n): CborTag {\n // m/purpose'/coinType'/0'/0/0\n const components = [purpose, true, coinType, true, 0, true, 0, false, 0, false];\n const keypathMap = new Map<number, unknown>([[1, components]]);\n if (sourceFingerprint !== undefined) {\n keypathMap.set(2, sourceFingerprint);\n }\n return new CborTag(TAG_KEYPATH, keypathMap);\n}\n\nexport interface EthSignRequestParams {\n /** Raw sign data. For PersonalMessage, a string is UTF-8 encoded automatically. */\n signData: Uint8Array | string;\n /** ERC-4527 data type. Defaults to PersonalMessage (3). Use EthDataType constants. */\n dataType?: number;\n address: string;\n sourceFingerprint: number | undefined;\n /** Chain ID — required by the wallet for v-value encoding on LegacyTransaction (type 1). */\n chainId?: number;\n origin?: string;\n}\n\nfunction buildEthSignRequestCbor(params: EthSignRequestParams): Uint8Array {\n const {\n signData,\n dataType = EthDataType.PersonalMessage,\n address,\n sourceFingerprint,\n chainId,\n origin = \"qrkit\",\n } = params;\n\n const requestId = randomBytes(16);\n const signBytes =\n typeof signData === \"string\" ? new TextEncoder().encode(signData) : signData;\n const keypath = buildKeypath(44, 60, sourceFingerprint);\n\n const addrHex = address.replace(/^0x/i, \"\");\n const addrBytes = new Uint8Array(addrHex.match(/.{2}/g)!.map((b) => parseInt(b, 16)));\n\n // Keys must be in strictly ascending order for zcbor (Shell firmware decoder).\n const map = new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, signBytes], // sign-data\n [3, dataType], // data-type\n ]);\n\n if (chainId !== undefined) {\n map.set(4, chainId); // chain-id — must come before key 5\n }\n\n map.set(5, keypath); // derivation-path\n map.set(6, addrBytes); // address\n map.set(7, origin); // origin\n\n return encode(map);\n}\n\nexport function buildEthSignRequestURParts(params: EthSignRequestParams): string[] {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\");\n}\n\nexport function buildEthSignRequestUR(params: EthSignRequestParams): string {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\")[0];\n}\n","import { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport type { ScannedUR } from \"../types.js\";\n\nexport function parseEthSignature(scanned: ScannedUR): string {\n if (scanned.type !== \"eth-signature\") {\n throw new Error(`Expected eth-signature, got: ${scanned.type}`);\n }\n\n const map = cborDecode(scanned.cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], { 37: (v: unknown) => v }),\n }) as Map<number, unknown>;\n\n const sigBytes = map.get(2) as Uint8Array | undefined;\n if (!sigBytes || sigBytes.length < 64) {\n throw new Error(\"Invalid or missing signature bytes\");\n }\n\n return \"0x\" + [...sigBytes].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsB;AACtB,mBAAsD;AAoBtD,SAAS,IAAI,GAAY,GAAoB;AAC3C,MAAI,aAAa,IAAK,QAAQ,EAA2B,IAAI,CAAC;AAC9D,SAAO;AACT;AAEA,IAAM,cAAc,CAAC,MAAe;AAEpC,SAAS,WAAW,MAAwC;AAC1D,aAAO,aAAAA,QAAW,MAAM;AAAA,IACtB,SAAS;AAAA,IACT,MAAM,OAAO,OAAO,CAAC,GAAmB;AAAA,MACtC,KAAK;AAAA;AAAA,MACL,KAAK;AAAA;AAAA,MACL,KAAK;AAAA;AAAA,IACP,CAAC;AAAA,EACH,CAAC;AACH;AAEA,SAAS,iBAAiB,KAAc,KAAyB;AAC/D,QAAM,UAAU,IAAI,KAAK,CAAC;AAC1B,QAAM,YAAY,IAAI,KAAK,CAAC;AAE5B,MAAI,CAAC,WAAW,CAAC,WAAW;AAC1B,UAAM,IAAI,MAAM,6CAA6C;AAAA,EAC/D;AAEA,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,QAAM,SAAS,IAAI,KAAK,CAAC;AACzB,MAAI,QAAQ;AACV,UAAM,aAAa,IAAI,QAAQ,CAAC;AAChC,QAAI,MAAM,QAAQ,UAAU,GAAG;AAC7B,UAAI,WAAW,UAAU,EAAG,WAAU,WAAW,CAAC;AAClD,UAAI,WAAW,UAAU,EAAG,YAAW,WAAW,CAAC;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AAG,kBAAkB;AAC1E;AAEO,SAAS,sBAAsB,QAAsC;AAC1E,SAAO,cAAc,wBAAwB,MAAM,GAAG,kBAAkB,EAAE,CAAC;AAC7E;;;AC9FA,IAAAC,gBAAsD;AAI/C,SAAS,kBAAkB,SAA4B;AAC5D,MAAI,QAAQ,SAAS,iBAAiB;AACpC,UAAM,IAAI,MAAM,gCAAgC,QAAQ,IAAI,EAAE;AAAA,EAChE;AAEA,QAAM,UAAM,cAAAC,QAAW,QAAQ,MAAM;AAAA,IACnC,SAAS;AAAA,IACT,MAAM,OAAO,OAAO,CAAC,GAAmB,EAAE,IAAI,CAAC,MAAe,EAAE,CAAC;AAAA,EACnE,CAAC;AAED,QAAM,WAAW,IAAI,IAAI,CAAC;AAC1B,MAAI,CAAC,YAAY,SAAS,SAAS,IAAI;AACrC,UAAM,IAAI,MAAM,oCAAoC;AAAA,EACtD;AAEA,SAAO,OAAO,CAAC,GAAG,QAAQ,EAAE,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE;AACjF;","names":["cborDecode","import_cborg","cborDecode"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/btc/address.ts","../src/bytes.ts","../src/btc/deriveAccount.ts","../src/eth/address.ts","../src/eth/deriveAccount.ts","../src/parseXpub.ts","../src/parseConnection.ts","../src/cbor.ts","../src/urEncoding.ts","../src/eth/signRequest.ts","../src/eth/signature.ts","../src/btc/signRequest.ts","../src/btc/signature.ts","../src/btc/psbt.ts"],"sourcesContent":["// Types\nexport type {\n ScannedUR,\n Chain,\n QRKitConfig,\n Account,\n EvmAccount,\n BtcAccount,\n} from \"./types.js\";\nexport type { ParsedXpub, XpubType } from \"./parseXpub.js\";\n\n// Connection\nexport { parseConnection } from \"./parseConnection.js\";\n\n// EVM signing\nexport {\n buildEthSignRequestUR,\n buildEthSignRequestURParts,\n EthDataType,\n} from \"./eth/signRequest.js\";\nexport type { EthDataTypeValue, EthSignRequestParams } from \"./eth/signRequest.js\";\nexport { parseEthSignature } from \"./eth/signature.js\";\n\n// BTC signing\nexport {\n buildBtcSignRequestUR,\n buildBtcSignRequestURParts,\n BtcDataType,\n} from \"./btc/signRequest.js\";\nexport type { BtcDataTypeValue, BtcSignRequestParams } from \"./btc/signRequest.js\";\nexport { parseBtcSignature } from \"./btc/signature.js\";\nexport type { BtcSignature } from \"./btc/signature.js\";\nexport {\n buildCryptoPsbtUR,\n buildCryptoPsbtURParts,\n parseCryptoPsbt,\n} from \"./btc/psbt.js\";\nexport type { CryptoPsbt } from \"./btc/psbt.js\";\n","import { bech32, createBase58check } from \"@scure/base\";\nimport { ripemd160 } from \"@noble/hashes/legacy.js\";\nimport { sha256 } from \"@noble/hashes/sha2.js\";\n\nexport type BtcScriptType = \"p2wpkh\" | \"p2sh-p2wpkh\" | \"p2pkh\";\n\nconst base58check = createBase58check(sha256);\n\nfunction hash160(bytes: Uint8Array): Uint8Array {\n return ripemd160(sha256(bytes));\n}\n\n/** Native SegWit — P2WPKH, `bc1q...` */\nexport function pubKeyToP2wpkh(compressedPubKey: Uint8Array): string {\n const h = hash160(compressedPubKey);\n const words = bech32.toWords(h);\n return bech32.encode(\"bc\", [0, ...words]);\n}\n\n/** Nested SegWit — P2SH-P2WPKH, `3...` */\nexport function pubKeyToP2shP2wpkh(compressedPubKey: Uint8Array): string {\n const h = hash160(compressedPubKey);\n // redeemScript = OP_0 <20-byte-pubkey-hash>\n const redeemScript = new Uint8Array(22);\n redeemScript[0] = 0x00;\n redeemScript[1] = 0x14;\n redeemScript.set(h, 2);\n const scriptHash = hash160(redeemScript);\n const payload = new Uint8Array(21);\n payload[0] = 0x05; // P2SH version byte\n payload.set(scriptHash, 1);\n return base58check.encode(payload);\n}\n\n/** Legacy — P2PKH, `1...` */\nexport function pubKeyToP2pkh(compressedPubKey: Uint8Array): string {\n const h = hash160(compressedPubKey);\n const payload = new Uint8Array(21);\n payload[0] = 0x00; // mainnet P2PKH version byte\n payload.set(h, 1);\n return base58check.encode(payload);\n}\n","export function bytesToBase64(bytes: Uint8Array): string {\n let binary = \"\";\n for (const byte of bytes) {\n binary += String.fromCharCode(byte);\n }\n return btoa(binary);\n}\n\nexport function bytesToHex(bytes: Uint8Array): string {\n return [...bytes].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n const normalized = hex.trim().replace(/^0x/i, \"\");\n if (normalized.length % 2 !== 0 || !/^[0-9a-f]*$/i.test(normalized)) {\n throw new Error(\"Invalid hex string\");\n }\n\n return new Uint8Array(\n normalized.match(/.{2}/g)?.map((byte) => parseInt(byte, 16)) ?? [],\n );\n}\n","import type { HDKey } from \"@scure/bip32\";\n\nimport {\n pubKeyToP2pkh,\n pubKeyToP2shP2wpkh,\n pubKeyToP2wpkh,\n type BtcScriptType,\n} from \"./address.js\";\n\nimport { bytesToHex } from \"../bytes.js\";\nimport type { ParsedXpub } from \"../parseXpub.js\";\n\nexport interface DerivedBtcAccount {\n address: string;\n scriptType: BtcScriptType;\n publicKey: string;\n /** source-fingerprint from the scanned xpub — required for signing */\n sourceFingerprint: number | undefined;\n /** device or key name as reported by the hardware wallet, if available */\n device: string | undefined;\n}\n\n// Derive the first external address (index 0) from an account-level xpub.\n// Account-level xpub is at depth 3 (m/purpose'/coin'/account').\n// External chain is child 0, then address index 0.\nfunction firstChild(accountKey: HDKey): HDKey {\n return accountKey.deriveChild(0).deriveChild(0);\n}\n\nfunction scriptTypeFromPurpose(purpose: number | undefined): BtcScriptType | undefined {\n if (purpose === 84) return \"p2wpkh\";\n if (purpose === 49) return \"p2sh-p2wpkh\";\n if (purpose === 44) return \"p2pkh\";\n return undefined;\n}\n\nfunction deriveAddress(pubKey: Uint8Array, scriptType: BtcScriptType): string {\n if (scriptType === \"p2wpkh\") return pubKeyToP2wpkh(pubKey);\n if (scriptType === \"p2sh-p2wpkh\") return pubKeyToP2shP2wpkh(pubKey);\n return pubKeyToP2pkh(pubKey);\n}\n\nexport function deriveBtcAccount(parsed: ParsedXpub[]): DerivedBtcAccount[] {\n const results: DerivedBtcAccount[] = [];\n\n for (const entry of parsed) {\n const { hdKey, purpose, coinType, sourceFingerprint, name } = entry;\n\n // BTC: coin type 0\n if (coinType !== 0) continue;\n\n const scriptType = scriptTypeFromPurpose(purpose);\n if (!scriptType) continue;\n\n const child = firstChild(hdKey);\n if (!child.publicKey) continue;\n\n results.push({\n address: deriveAddress(child.publicKey, scriptType),\n scriptType,\n publicKey: bytesToHex(child.publicKey),\n sourceFingerprint,\n device: name,\n });\n }\n\n return results;\n}\n","import { secp256k1 } from \"@noble/curves/secp256k1.js\";\nimport { keccak_256 } from \"@noble/hashes/sha3.js\";\n\nexport function pubKeyToEthAddress(compressedPubKey: Uint8Array): string {\n const pubKeyHex = [...compressedPubKey]\n .map((b) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n const uncompressed = secp256k1.Point.fromHex(pubKeyHex).toBytes(false);\n const hash = keccak_256(uncompressed.slice(1));\n const hex = [...hash.slice(12)]\n .map((b: number) => b.toString(16).padStart(2, \"0\"))\n .join(\"\");\n return toChecksumAddress(hex);\n}\n\nfunction toChecksumAddress(hex: string): string {\n const checksumHash = keccak_256(new TextEncoder().encode(hex));\n return (\n \"0x\" +\n [...hex]\n .map((c, i) => {\n if (c >= \"0\" && c <= \"9\") return c;\n const nibble =\n i % 2 === 0\n ? (checksumHash[Math.floor(i / 2)] >> 4) & 0xf\n : checksumHash[Math.floor(i / 2)] & 0xf;\n return nibble >= 8 ? c.toUpperCase() : c.toLowerCase();\n })\n .join(\"\")\n );\n}\n","import type { HDKey } from \"@scure/bip32\";\n\nimport { bytesToHex } from \"../bytes.js\";\nimport { pubKeyToEthAddress } from \"./address.js\";\nimport type { ParsedXpub } from \"../parseXpub.js\";\n\nexport interface DerivedAccount {\n address: string;\n publicKey: string;\n /** source-fingerprint from the scanned xpub — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name as reported by the hardware wallet, if available */\n device: string | undefined;\n}\n\n// Derive the first external address (index 0) from an account-level xpub.\n// Account-level xpub is at depth 3 (m/purpose'/coin'/account').\n// External chain is child 0, then address index 0.\nfunction firstChild(accountKey: HDKey): HDKey {\n return accountKey.deriveChild(0).deriveChild(0);\n}\n\nexport function deriveEvmAccount(parsed: ParsedXpub[]): DerivedAccount[] {\n const results: DerivedAccount[] = [];\n for (const entry of parsed) {\n const { hdKey, purpose, coinType, type, sourceFingerprint, name } = entry;\n const isEvm =\n (purpose === 44 && coinType === 60) || (purpose === undefined && type === \"xpub\");\n\n if (!isEvm) continue;\n\n const child = firstChild(hdKey);\n if (!child.publicKey) continue;\n\n results.push({\n address: pubKeyToEthAddress(child.publicKey),\n publicKey: bytesToHex(child.publicKey),\n sourceFingerprint,\n device: name,\n });\n }\n return results;\n}\n","import { HDKey } from \"@scure/bip32\";\nimport { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport type { ScannedUR } from \"./types.js\";\n\nexport type XpubType = \"xpub\";\n\nexport interface ParsedXpub {\n hdKey: HDKey;\n type: XpubType;\n /** BIP-44 purpose index: 44 for EVM, 44/49/84 for supported BTC scripts */\n purpose: number | undefined;\n /** BIP-44 coin type: 60 = ETH, 0 = BTC */\n coinType: number | undefined;\n /** source-fingerprint from the origin keypath — required by Shell for signing */\n sourceFingerprint: number | undefined;\n /** device or key name from crypto-hdkey key 9, set by some wallets (e.g. Keystone) */\n name?: string;\n raw: string;\n}\n\ntype CborMap = Map<number, unknown>;\n\nfunction get(m: unknown, k: number): unknown {\n if (m instanceof Map) return (m as Map<number, unknown>).get(k);\n return undefined;\n}\n\nfunction bytesFromCbor(value: unknown): Uint8Array | undefined {\n if (value instanceof Uint8Array) return value;\n\n if (value instanceof Map && value.get(\"type\") === \"Buffer\") {\n const data = value.get(\"data\");\n if (\n Array.isArray(data) &&\n data.every((byte) => Number.isInteger(byte) && byte >= 0 && byte <= 0xff)\n ) {\n return new Uint8Array(data);\n }\n }\n\n return undefined;\n}\n\nconst passthrough = (v: unknown) => v;\n\nfunction decodeCbor(cbor: Uint8Array): Map<number, unknown> {\n // Connection payloads often wrap the inner hdkey/output structures in semantic CBOR tags\n // (script expressions, crypto-output, vendor-specific wrappers, etc.). For connection\n // parsing we only care about the inner map/bytes shape, so tags are treated as annotations\n // and stripped during decode. Required structure is validated explicitly later.\n const tags = new Proxy([] as TagDecoder[], {\n get(target, property, receiver) {\n if (typeof property === \"string\" && /^\\d+$/.test(property)) {\n return passthrough;\n }\n return Reflect.get(target, property, receiver);\n },\n });\n\n return cborDecode(cbor, {\n useMaps: true,\n tags,\n }) as Map<number, unknown>;\n}\n\nfunction isCborMap(value: unknown): value is CborMap {\n return value instanceof Map;\n}\n\nfunction assertCryptoHdKeyShape(value: unknown): CborMap {\n if (!isCborMap(value)) {\n throw new Error(\"crypto-hdkey entry must be a CBOR map\");\n }\n\n if (!value.has(3) || !value.has(4)) {\n throw new Error(\"crypto-hdkey missing key-data or chain-code\");\n }\n\n return value;\n}\n\nfunction parseCryptoHdKey(map: CborMap, raw: string, fallbackName?: string): ParsedXpub {\n const keyData = bytesFromCbor(get(map, 3));\n const chainCode = bytesFromCbor(get(map, 4));\n\n if (!keyData || !chainCode) {\n throw new Error(\"crypto-hdkey missing key-data or chain-code\");\n }\n\n let purpose: number | undefined;\n let coinType: number | undefined;\n let sourceFingerprint: number | undefined;\n const origin = get(map, 6);\n if (origin) {\n const components = get(origin, 1);\n if (Array.isArray(components)) {\n if (components.length >= 1) purpose = components[0] as number;\n if (components.length >= 3) coinType = components[2] as number;\n }\n sourceFingerprint = get(origin, 2) as number | undefined;\n }\n\n const name = (get(map, 9) as string | undefined) ?? fallbackName;\n\n const hdKey = new HDKey({ publicKey: keyData, chainCode });\n return { hdKey, type: \"xpub\", purpose, coinType, sourceFingerprint, name, raw };\n}\n\nfunction parseScannedUR(scanned: ScannedUR): ParsedXpub | ParsedXpub[] {\n const { type, cbor } = scanned;\n const raw = `ur:${type}`;\n\n if (\n type !== \"crypto-hdkey\" &&\n type !== \"crypto-account\" &&\n type !== \"crypto-multi-accounts\"\n ) {\n throw new Error(`Unsupported UR type: ${type}`);\n }\n\n const map = decodeCbor(cbor);\n\n if (type === \"crypto-hdkey\") {\n return parseCryptoHdKey(assertCryptoHdKeyShape(map), raw);\n }\n\n const accounts = map.get(2) as unknown[] | undefined;\n if (!Array.isArray(accounts) || accounts.length === 0) {\n throw new Error(`${type} contains no keys`);\n }\n const fallbackName =\n type === \"crypto-multi-accounts\" ? (map.get(3) as string | undefined) : undefined;\n return accounts.map((entry) =>\n parseCryptoHdKey(assertCryptoHdKeyShape(entry), raw, fallbackName),\n );\n}\n\nexport function parseXpub(input: ScannedUR | string): ParsedXpub[] {\n if (typeof input !== \"string\") {\n const result = parseScannedUR(input);\n return Array.isArray(result) ? result : [result];\n }\n // Raw base58 xpub string — @scure/bip32 handles decoding directly\n const hdKey = HDKey.fromExtendedKey(input.trim());\n return [\n {\n hdKey,\n type: \"xpub\",\n purpose: undefined,\n coinType: undefined,\n sourceFingerprint: undefined,\n raw: input.trim(),\n },\n ];\n}\n","import { deriveBtcAccount } from \"./btc/deriveAccount.js\";\nimport { deriveEvmAccount } from \"./eth/deriveAccount.js\";\nimport { parseXpub } from \"./parseXpub.js\";\nimport type {\n Account,\n BtcAccount,\n Chain,\n EvmAccount,\n QRKitConfig,\n ScannedUR,\n} from \"./types.js\";\n\nconst ALL_CHAINS: Chain[] = [\"evm\", \"btc\"];\n\n/**\n * Parse a connection QR (crypto-hdkey or crypto-account) and return\n * only the accounts for the chains configured in QRKitConfig.\n *\n * A dApp configured with `chains: [\"evm\"]` will never see BTC accounts,\n * and vice versa. Both chains can be enabled with `chains: [\"evm\", \"btc\"]`.\n * If `chains` is omitted, all supported chains are tried.\n */\nexport function parseConnection(\n scannedUR: ScannedUR,\n config: QRKitConfig = {},\n): Account[] {\n const chains = config.chains ?? ALL_CHAINS;\n const parsed = parseXpub(scannedUR);\n const accounts: Account[] = [];\n\n if (chains.includes(\"evm\")) {\n for (const account of deriveEvmAccount(parsed)) {\n accounts.push({ chain: \"evm\", ...account } satisfies EvmAccount);\n }\n }\n\n if (chains.includes(\"btc\")) {\n for (const account of deriveBtcAccount(parsed)) {\n accounts.push({ chain: \"btc\", ...account } satisfies BtcAccount);\n }\n }\n\n return accounts;\n}\n","// Minimal CBOR encoder.\n// Supports only the types needed for eth-sign-request: uint, bytes, text, array, map (integer keys), bool, tag.\n\nfunction majorType(major: number, n: number): number[] {\n const base = major << 5;\n if (n <= 23) return [base | n];\n if (n <= 0xff) return [base | 0x18, n];\n if (n <= 0xffff) return [base | 0x19, (n >> 8) & 0xff, n & 0xff];\n return [base | 0x1a, (n >> 24) & 0xff, (n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff];\n}\n\nexport function encodeItem(value: unknown): number[] {\n if (typeof value === \"boolean\") {\n return [value ? 0xf5 : 0xf4];\n }\n if (typeof value === \"number\") {\n return majorType(0, value);\n }\n if (value instanceof Uint8Array) {\n return [...majorType(2, value.length), ...value];\n }\n if (typeof value === \"string\") {\n const bytes = new TextEncoder().encode(value);\n return [...majorType(3, bytes.length), ...bytes];\n }\n if (Array.isArray(value)) {\n return [...majorType(4, value.length), ...value.flatMap(encodeItem)];\n }\n if (value instanceof CborTag) {\n return [...majorType(6, value.tag), ...encodeItem(value.value)];\n }\n if (value instanceof Map) {\n const entries = [...value.entries()];\n return [\n ...majorType(5, entries.length),\n ...entries.flatMap(([k, v]) => [...encodeItem(k), ...encodeItem(v)]),\n ];\n }\n throw new Error(`Unsupported CBOR value: ${typeof value}`);\n}\n\nexport class CborTag {\n tag: number;\n value: unknown;\n constructor(tag: number, value: unknown) {\n this.tag = tag;\n this.value = value;\n }\n}\n\nexport function encode(value: unknown): Uint8Array {\n return new Uint8Array(encodeItem(value));\n}\n","import { UR, UrFountainEncoder } from \"@qrkit/bc-ur-web\";\n\nexport function encodeURParts(\n cbor: Uint8Array,\n type: string,\n maxFragmentLength = 200,\n): string[] {\n const ur = UR.fromCbor({ type, payload: cbor });\n const encoder = new UrFountainEncoder(ur, maxFragmentLength);\n return encoder.getAllPartsUr().map((part) => part.toString().toUpperCase());\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\n\n// ERC-4527 eth-sign-request data types\nexport const EthDataType = {\n /** Legacy transaction (RLP-encoded). Wallet applies EIP-155: v = 35 + 2*chainId + recId */\n LegacyTransaction: 1,\n /** EIP-712 typed data bytes. Wallet hashes internally. */\n TypedData: 2,\n /** Personal message (EIP-191). Wallet prepends \"\\x19Ethereum Signed Message:\\n{len}\". */\n PersonalMessage: 3,\n /** EIP-1559 transaction (RLP-encoded). Wallet uses v = recId. */\n TypedTransaction: 4,\n} as const;\n\nexport type EthDataTypeValue = (typeof EthDataType)[keyof typeof EthDataType];\n\n// CBOR tag for crypto-keypath\nconst TAG_KEYPATH = 304;\n\nfunction randomBytes(n: number): Uint8Array {\n const buf = new Uint8Array(n);\n crypto.getRandomValues(buf);\n return buf;\n}\n\nfunction buildKeypath(\n purpose: number,\n coinType: number,\n sourceFingerprint: number | undefined,\n): CborTag {\n // m/purpose'/coinType'/0'/0/0\n const components = [purpose, true, coinType, true, 0, true, 0, false, 0, false];\n const keypathMap = new Map<number, unknown>([[1, components]]);\n if (sourceFingerprint !== undefined) {\n keypathMap.set(2, sourceFingerprint);\n }\n return new CborTag(TAG_KEYPATH, keypathMap);\n}\n\nexport interface EthSignRequestParams {\n /** Raw sign data. For PersonalMessage, a string is UTF-8 encoded automatically. */\n signData: Uint8Array | string;\n /** ERC-4527 data type. Defaults to PersonalMessage (3). Use EthDataType constants. */\n dataType?: number;\n address: string;\n sourceFingerprint: number | undefined;\n /** Chain ID — required by the wallet for v-value encoding on LegacyTransaction (type 1). */\n chainId?: number;\n origin?: string;\n}\n\nfunction buildEthSignRequestCbor(params: EthSignRequestParams): Uint8Array {\n const {\n signData,\n dataType = EthDataType.PersonalMessage,\n address,\n sourceFingerprint,\n chainId,\n origin = \"qrkit\",\n } = params;\n\n const requestId = randomBytes(16);\n const signBytes =\n typeof signData === \"string\" ? new TextEncoder().encode(signData) : signData;\n const keypath = buildKeypath(44, 60, sourceFingerprint);\n\n const addrHex = address.replace(/^0x/i, \"\");\n const addrBytes = new Uint8Array(addrHex.match(/.{2}/g)!.map((b) => parseInt(b, 16)));\n\n // Keys must be in strictly ascending order for zcbor (Shell firmware decoder).\n const map = new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, signBytes], // sign-data\n [3, dataType], // data-type\n ]);\n\n if (chainId !== undefined) {\n map.set(4, chainId); // chain-id — must come before key 5\n }\n\n map.set(5, keypath); // derivation-path\n map.set(6, addrBytes); // address\n map.set(7, origin); // origin\n\n return encode(map);\n}\n\nexport function buildEthSignRequestURParts(params: EthSignRequestParams): string[] {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\");\n}\n\nexport function buildEthSignRequestUR(params: EthSignRequestParams): string {\n return encodeURParts(buildEthSignRequestCbor(params), \"eth-sign-request\")[0];\n}\n","import { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport { bytesToHex } from \"../bytes.js\";\nimport type { ScannedUR } from \"../types.js\";\n\nexport function parseEthSignature(scanned: ScannedUR): string {\n if (scanned.type !== \"eth-signature\") {\n throw new Error(`Expected eth-signature, got: ${scanned.type}`);\n }\n\n const map = cborDecode(scanned.cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], { 37: (v: unknown) => v }),\n }) as Map<number, unknown>;\n\n const sigBytes = map.get(2) as Uint8Array | undefined;\n if (!sigBytes || sigBytes.length < 64) {\n throw new Error(\"Invalid or missing signature bytes\");\n }\n\n return `0x${bytesToHex(sigBytes)}`;\n}\n","import { encode, CborTag } from \"../cbor.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\nimport type { BtcScriptType } from \"./address.js\";\n\nexport const BtcDataType = {\n Message: 1,\n} as const;\n\nexport type BtcDataTypeValue = (typeof BtcDataType)[keyof typeof BtcDataType];\n\nconst TAG_KEYPATH = 304;\nconst PURPOSE_BY_SCRIPT_TYPE = {\n p2wpkh: 84,\n \"p2sh-p2wpkh\": 49,\n p2pkh: 44,\n} as const satisfies Record<BtcScriptType, number>;\n\nfunction randomBytes(n: number): Uint8Array {\n const buf = new Uint8Array(n);\n crypto.getRandomValues(buf);\n return buf;\n}\n\nfunction purposeFromScriptType(scriptType: BtcScriptType): number {\n return PURPOSE_BY_SCRIPT_TYPE[scriptType];\n}\n\nfunction buildKeypath(\n scriptType: BtcScriptType,\n sourceFingerprint: number | undefined,\n): CborTag {\n const purpose = purposeFromScriptType(scriptType);\n // m/purpose'/0'/0'/0/0\n const components = [purpose, true, 0, true, 0, true, 0, false, 0, false];\n const keypathMap = new Map<number, unknown>([[1, components]]);\n if (sourceFingerprint !== undefined) {\n keypathMap.set(2, sourceFingerprint);\n }\n return new CborTag(TAG_KEYPATH, keypathMap);\n}\n\nexport interface BtcSignRequestParams {\n /** Raw message data. Strings are UTF-8 encoded automatically. */\n signData: Uint8Array | string;\n address: string;\n /** BTC script type for choosing m/44', m/49', or m/84'. */\n scriptType: BtcScriptType;\n sourceFingerprint: number | undefined;\n origin?: string;\n}\n\nfunction buildBtcSignRequestCbor(params: BtcSignRequestParams): Uint8Array {\n const { signData, address, scriptType, sourceFingerprint, origin = \"qrkit\" } = params;\n const requestId = randomBytes(16);\n const signBytes =\n typeof signData === \"string\" ? new TextEncoder().encode(signData) : signData;\n const keypath = buildKeypath(scriptType, sourceFingerprint);\n\n return encode(\n new Map<number, unknown>([\n [1, new CborTag(37, requestId)], // request-id: uuid = #6.37(bstr)\n [2, signBytes], // sign-data\n [3, BtcDataType.Message], // data-type\n [4, [keypath]], // btc-derivation-paths\n [5, [address]], // btc-addresses\n [6, origin], // origin\n ]),\n );\n}\n\nexport function buildBtcSignRequestURParts(params: BtcSignRequestParams): string[] {\n return encodeURParts(buildBtcSignRequestCbor(params), \"btc-sign-request\");\n}\n\nexport function buildBtcSignRequestUR(params: BtcSignRequestParams): string {\n return encodeURParts(buildBtcSignRequestCbor(params), \"btc-sign-request\")[0];\n}\n","import { decode as cborDecode, type TagDecoder } from \"cborg\";\n\nimport { bytesToBase64, bytesToHex } from \"../bytes.js\";\nimport type { ScannedUR } from \"../types.js\";\n\nexport interface BtcSignature {\n signature: string;\n publicKey: string;\n requestId: string | undefined;\n}\n\nexport function parseBtcSignature(scanned: ScannedUR): BtcSignature {\n if (scanned.type !== \"btc-signature\") {\n throw new Error(`Expected btc-signature, got: ${scanned.type}`);\n }\n\n const map = cborDecode(scanned.cbor, {\n useMaps: true,\n tags: Object.assign([] as TagDecoder[], { 37: (v: unknown) => v }),\n }) as Map<number, unknown>;\n\n const requestId = map.get(1) as Uint8Array | undefined;\n const sigBytes = map.get(2) as Uint8Array | undefined;\n const publicKeyBytes = map.get(3) as Uint8Array | undefined;\n\n if (!sigBytes || sigBytes.length !== 65) {\n throw new Error(\"Invalid or missing BTC signature bytes\");\n }\n if (!publicKeyBytes || publicKeyBytes.length !== 33) {\n throw new Error(\"Invalid or missing BTC public key bytes\");\n }\n\n return {\n signature: bytesToBase64(sigBytes),\n publicKey: bytesToHex(publicKeyBytes),\n requestId: requestId ? bytesToHex(requestId) : undefined,\n };\n}\n","import { decode as cborDecode } from \"cborg\";\n\nimport { bytesToHex, hexToBytes } from \"../bytes.js\";\nimport { encode } from \"../cbor.js\";\nimport type { ScannedUR } from \"../types.js\";\nimport { encodeURParts } from \"../urEncoding.js\";\n\nexport interface CryptoPsbt {\n psbt: Uint8Array;\n psbtHex: string;\n}\n\nfunction normalizePsbt(psbt: Uint8Array | string): Uint8Array {\n try {\n return typeof psbt === \"string\" ? hexToBytes(psbt) : psbt;\n } catch (error) {\n throw new Error(\"Invalid PSBT hex\", { cause: error });\n }\n}\n\nexport function parseCryptoPsbt(scanned: ScannedUR): CryptoPsbt {\n if (scanned.type !== \"crypto-psbt\") {\n throw new Error(`Expected crypto-psbt, got: ${scanned.type}`);\n }\n\n const psbt = cborDecode(scanned.cbor) as unknown;\n if (!(psbt instanceof Uint8Array)) {\n throw new Error(\"Invalid crypto-psbt payload\");\n }\n\n return {\n psbt,\n psbtHex: bytesToHex(psbt),\n };\n}\n\nexport function buildCryptoPsbtURParts(psbt: Uint8Array | string): string[] {\n return encodeURParts(encode(normalizePsbt(psbt)), \"crypto-psbt\");\n}\n\nexport function buildCryptoPsbtUR(psbt: Uint8Array | string): string {\n return buildCryptoPsbtURParts(psbt)[0];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA0C;AAC1C,oBAA0B;AAC1B,kBAAuB;AAIvB,IAAM,kBAAc,+BAAkB,kBAAM;AAE5C,SAAS,QAAQ,OAA+B;AAC9C,aAAO,6BAAU,oBAAO,KAAK,CAAC;AAChC;AAGO,SAAS,eAAe,kBAAsC;AACnE,QAAM,IAAI,QAAQ,gBAAgB;AAClC,QAAM,QAAQ,mBAAO,QAAQ,CAAC;AAC9B,SAAO,mBAAO,OAAO,MAAM,CAAC,GAAG,GAAG,KAAK,CAAC;AAC1C;AAGO,SAAS,mBAAmB,kBAAsC;AACvE,QAAM,IAAI,QAAQ,gBAAgB;AAElC,QAAM,eAAe,IAAI,WAAW,EAAE;AACtC,eAAa,CAAC,IAAI;AAClB,eAAa,CAAC,IAAI;AAClB,eAAa,IAAI,GAAG,CAAC;AACrB,QAAM,aAAa,QAAQ,YAAY;AACvC,QAAM,UAAU,IAAI,WAAW,EAAE;AACjC,UAAQ,CAAC,IAAI;AACb,UAAQ,IAAI,YAAY,CAAC;AACzB,SAAO,YAAY,OAAO,OAAO;AACnC;AAGO,SAAS,cAAc,kBAAsC;AAClE,QAAM,IAAI,QAAQ,gBAAgB;AAClC,QAAM,UAAU,IAAI,WAAW,EAAE;AACjC,UAAQ,CAAC,IAAI;AACb,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AACF;;;ACrCA,IAAAC,gBAAqC;AAYrC,SAAS,cAAc,MAAuC;AAC5D,MAAI;AACF,WAAO,OAAO,SAAS,WAAW,WAAW,IAAI,IAAI;AAAA,EACvD,SAAS,OAAO;AACd,UAAM,IAAI,MAAM,oBAAoB,EAAE,OAAO,MAAM,CAAC;AAAA,EACtD;AACF;AAEO,SAAS,gBAAgB,SAAgC;AAC9D,MAAI,QAAQ,SAAS,eAAe;AAClC,UAAM,IAAI,MAAM,8BAA8B,QAAQ,IAAI,EAAE;AAAA,EAC9D;AAEA,QAAM,WAAO,cAAAC,QAAW,QAAQ,IAAI;AACpC,MAAI,EAAE,gBAAgB,aAAa;AACjC,UAAM,IAAI,MAAM,6BAA6B;AAAA,EAC/C;AAEA,SAAO;AAAA,IACL;AAAA,IACA,SAAS,WAAW,IAAI;AAAA,EAC1B;AACF;AAEO,SAAS,uBAAuB,MAAqC;AAC1E,SAAO,cAAc,OAAO,cAAc,IAAI,CAAC,GAAG,aAAa;AACjE;AAEO,SAAS,kBAAkB,MAAmC;AACnE,SAAO,uBAAuB,IAAI,EAAE,CAAC;AACvC;","names":["firstChild","cborDecode","import_cborg","cborDecode","TAG_KEYPATH","randomBytes","buildKeypath","import_cborg","cborDecode","import_cborg","cborDecode"]}
package/dist/index.d.cts CHANGED
@@ -19,15 +19,24 @@ interface EvmAccount {
19
19
  /** device or key name as reported by the hardware wallet, if available */
20
20
  device: string | undefined;
21
21
  }
22
- type Account = EvmAccount;
22
+ interface BtcAccount {
23
+ chain: "btc";
24
+ address: string;
25
+ /** BIP-44 script type implied by the derivation purpose */
26
+ scriptType: "p2wpkh" | "p2sh-p2wpkh" | "p2pkh";
27
+ publicKey: string;
28
+ sourceFingerprint: number | undefined;
29
+ device: string | undefined;
30
+ }
31
+ type Account = EvmAccount | BtcAccount;
23
32
 
24
33
  type XpubType = "xpub";
25
34
  interface ParsedXpub {
26
35
  hdKey: HDKey;
27
36
  type: XpubType;
28
- /** BIP-44 purpose index: 44 for EVM */
37
+ /** BIP-44 purpose index: 44 for EVM, 44/49/84 for supported BTC scripts */
29
38
  purpose: number | undefined;
30
- /** BIP-44 coin type: 60 = ETH */
39
+ /** BIP-44 coin type: 60 = ETH, 0 = BTC */
31
40
  coinType: number | undefined;
32
41
  /** source-fingerprint from the origin keypath — required by Shell for signing */
33
42
  sourceFingerprint: number | undefined;
@@ -73,4 +82,37 @@ declare function buildEthSignRequestUR(params: EthSignRequestParams): string;
73
82
 
74
83
  declare function parseEthSignature(scanned: ScannedUR): string;
75
84
 
76
- export { type Account, type Chain, EthDataType, type EthDataTypeValue, type EthSignRequestParams, type EvmAccount, type ParsedXpub, type QRKitConfig, type ScannedUR, type XpubType, buildEthSignRequestUR, buildEthSignRequestURParts, parseConnection, parseEthSignature };
85
+ type BtcScriptType = "p2wpkh" | "p2sh-p2wpkh" | "p2pkh";
86
+
87
+ declare const BtcDataType: {
88
+ readonly Message: 1;
89
+ };
90
+ type BtcDataTypeValue = (typeof BtcDataType)[keyof typeof BtcDataType];
91
+ interface BtcSignRequestParams {
92
+ /** Raw message data. Strings are UTF-8 encoded automatically. */
93
+ signData: Uint8Array | string;
94
+ address: string;
95
+ /** BTC script type for choosing m/44', m/49', or m/84'. */
96
+ scriptType: BtcScriptType;
97
+ sourceFingerprint: number | undefined;
98
+ origin?: string;
99
+ }
100
+ declare function buildBtcSignRequestURParts(params: BtcSignRequestParams): string[];
101
+ declare function buildBtcSignRequestUR(params: BtcSignRequestParams): string;
102
+
103
+ interface BtcSignature {
104
+ signature: string;
105
+ publicKey: string;
106
+ requestId: string | undefined;
107
+ }
108
+ declare function parseBtcSignature(scanned: ScannedUR): BtcSignature;
109
+
110
+ interface CryptoPsbt {
111
+ psbt: Uint8Array;
112
+ psbtHex: string;
113
+ }
114
+ declare function parseCryptoPsbt(scanned: ScannedUR): CryptoPsbt;
115
+ declare function buildCryptoPsbtURParts(psbt: Uint8Array | string): string[];
116
+ declare function buildCryptoPsbtUR(psbt: Uint8Array | string): string;
117
+
118
+ export { type Account, type BtcAccount, BtcDataType, type BtcDataTypeValue, type BtcSignRequestParams, type BtcSignature, type Chain, type CryptoPsbt, EthDataType, type EthDataTypeValue, type EthSignRequestParams, type EvmAccount, type ParsedXpub, type QRKitConfig, type ScannedUR, type XpubType, buildBtcSignRequestUR, buildBtcSignRequestURParts, buildCryptoPsbtUR, buildCryptoPsbtURParts, buildEthSignRequestUR, buildEthSignRequestURParts, parseBtcSignature, parseConnection, parseCryptoPsbt, parseEthSignature };
package/dist/index.d.ts CHANGED
@@ -19,15 +19,24 @@ interface EvmAccount {
19
19
  /** device or key name as reported by the hardware wallet, if available */
20
20
  device: string | undefined;
21
21
  }
22
- type Account = EvmAccount;
22
+ interface BtcAccount {
23
+ chain: "btc";
24
+ address: string;
25
+ /** BIP-44 script type implied by the derivation purpose */
26
+ scriptType: "p2wpkh" | "p2sh-p2wpkh" | "p2pkh";
27
+ publicKey: string;
28
+ sourceFingerprint: number | undefined;
29
+ device: string | undefined;
30
+ }
31
+ type Account = EvmAccount | BtcAccount;
23
32
 
24
33
  type XpubType = "xpub";
25
34
  interface ParsedXpub {
26
35
  hdKey: HDKey;
27
36
  type: XpubType;
28
- /** BIP-44 purpose index: 44 for EVM */
37
+ /** BIP-44 purpose index: 44 for EVM, 44/49/84 for supported BTC scripts */
29
38
  purpose: number | undefined;
30
- /** BIP-44 coin type: 60 = ETH */
39
+ /** BIP-44 coin type: 60 = ETH, 0 = BTC */
31
40
  coinType: number | undefined;
32
41
  /** source-fingerprint from the origin keypath — required by Shell for signing */
33
42
  sourceFingerprint: number | undefined;
@@ -73,4 +82,37 @@ declare function buildEthSignRequestUR(params: EthSignRequestParams): string;
73
82
 
74
83
  declare function parseEthSignature(scanned: ScannedUR): string;
75
84
 
76
- export { type Account, type Chain, EthDataType, type EthDataTypeValue, type EthSignRequestParams, type EvmAccount, type ParsedXpub, type QRKitConfig, type ScannedUR, type XpubType, buildEthSignRequestUR, buildEthSignRequestURParts, parseConnection, parseEthSignature };
85
+ type BtcScriptType = "p2wpkh" | "p2sh-p2wpkh" | "p2pkh";
86
+
87
+ declare const BtcDataType: {
88
+ readonly Message: 1;
89
+ };
90
+ type BtcDataTypeValue = (typeof BtcDataType)[keyof typeof BtcDataType];
91
+ interface BtcSignRequestParams {
92
+ /** Raw message data. Strings are UTF-8 encoded automatically. */
93
+ signData: Uint8Array | string;
94
+ address: string;
95
+ /** BTC script type for choosing m/44', m/49', or m/84'. */
96
+ scriptType: BtcScriptType;
97
+ sourceFingerprint: number | undefined;
98
+ origin?: string;
99
+ }
100
+ declare function buildBtcSignRequestURParts(params: BtcSignRequestParams): string[];
101
+ declare function buildBtcSignRequestUR(params: BtcSignRequestParams): string;
102
+
103
+ interface BtcSignature {
104
+ signature: string;
105
+ publicKey: string;
106
+ requestId: string | undefined;
107
+ }
108
+ declare function parseBtcSignature(scanned: ScannedUR): BtcSignature;
109
+
110
+ interface CryptoPsbt {
111
+ psbt: Uint8Array;
112
+ psbtHex: string;
113
+ }
114
+ declare function parseCryptoPsbt(scanned: ScannedUR): CryptoPsbt;
115
+ declare function buildCryptoPsbtURParts(psbt: Uint8Array | string): string[];
116
+ declare function buildCryptoPsbtUR(psbt: Uint8Array | string): string;
117
+
118
+ export { type Account, type BtcAccount, BtcDataType, type BtcDataTypeValue, type BtcSignRequestParams, type BtcSignature, type Chain, type CryptoPsbt, EthDataType, type EthDataTypeValue, type EthSignRequestParams, type EvmAccount, type ParsedXpub, type QRKitConfig, type ScannedUR, type XpubType, buildBtcSignRequestUR, buildBtcSignRequestURParts, buildCryptoPsbtUR, buildCryptoPsbtURParts, buildEthSignRequestUR, buildEthSignRequestURParts, parseBtcSignature, parseConnection, parseCryptoPsbt, parseEthSignature };