// Parsers for cosigner key exports and whole-vault configurations produced by // other wallets (Coldcard / Keystone / Passport / Jade JSON, Sparrow & Core // key-origin strings, Coldcard-style multisig files, output descriptors). import { type BtcNetwork, type Cosigner, type ScriptType, normalizeXpub, normalizePath, normalizeFingerprint, defaultPath, } from './core.ts'; const ORIGIN_RE = /\[([0-9a-fA-F]{8})((?:\/\d+['hH]?)*)\]([xtyzuvYZUV]pub[1-9A-HJ-NP-Za-km-z]{100,112})/g; /** One cosigner from a file / pasted text. */ export function parseCosigner(text: string, scriptType: ScriptType, network: BtcNetwork): Omit { const t = text.trim(); if (t.startsWith('{')) { const j = JSON.parse(t); const xfp = j.xfp ?? j.fingerprint ?? j.master_fingerprint ?? j.MasterFingerprint; // Coldcard-style multisig export: keys per script type const k = { p2wsh: 'p2wsh', 'p2sh-p2wsh': 'p2sh_p2wsh', p2sh: 'p2sh', p2tr: 'p2tr' }[scriptType]; const alt = scriptType === 'p2sh-p2wsh' ? 'p2wsh_p2sh' : k; const xpub = j[k] ?? j[alt] ?? j[`${k}_xpub`] ?? j.bip48_2?.xpub ?? j.xpub ?? j.ExtPubKey; const path = j[`${k}_deriv`] ?? j[`${alt}_deriv`] ?? j.deriv ?? j.path ?? j.derivation ?? j.AccountKeyPath; if (!xfp || !xpub) throw new Error(`No ${scriptType.toUpperCase()} key found in this file.`); return { fingerprint: normalizeFingerprint(String(typeof xfp === 'number' ? xfp.toString(16).padStart(8, '0') : xfp)), xpub: normalizeXpub(xpub, network), path: normalizePath(path ? String(path).replace(/^m?\/?/, 'm/') : defaultPath(scriptType, network)), source: 'file', }; } ORIGIN_RE.lastIndex = 0; const m = ORIGIN_RE.exec(t); if (m) return { fingerprint: normalizeFingerprint(m[1]), path: normalizePath('m' + m[2]), xpub: normalizeXpub(m[3], network), source: 'file' }; throw new Error('Paste a key in the form [fingerprint/48h/0h/0h/2h]xpub… or load a wallet export (.json).'); } export interface ParsedVault { name?: string; scriptType: ScriptType; m: number; cosigners: Omit[]; network: BtcNetwork } /** Whole vault from an output descriptor or a Coldcard-style multisig config file. */ export function parseVaultConfig(text: string): ParsedVault { const t = text.trim(); const keysIn = (s: string) => { const out: { fp: string; path: string; xpub: string }[] = []; ORIGIN_RE.lastIndex = 0; let m; while ((m = ORIGIN_RE.exec(s))) out.push({ fp: m[1], path: 'm' + m[2], xpub: m[3] }); return out; }; const netOf = (xpub: string): BtcNetwork => (/^[tuvUV]pub/.test(xpub) ? 'testnet' : 'mainnet'); if (/^(tr|wsh|sh)\(/.test(t)) { const d = t.replace(/#.*$/, ''); let scriptType: ScriptType; if (d.startsWith('tr(')) scriptType = 'p2tr'; else if (d.startsWith('wsh(')) scriptType = 'p2wsh'; else if (d.startsWith('sh(wsh(')) scriptType = 'p2sh-p2wsh'; else scriptType = 'p2sh'; if (!/sortedmulti(_a)?\(/.test(d)) throw new Error('Only sortedmulti / sortedmulti_a descriptors are supported.'); const mm = /sortedmulti(?:_a)?\((\d+),/.exec(d); const keys = keysIn(d); if (!mm || keys.length < 2) throw new Error('Could not read the descriptor.'); const network = netOf(keys[0].xpub); return { scriptType, m: Number(mm[1]), network, cosigners: keys.map((k) => ({ fingerprint: normalizeFingerprint(k.fp), path: normalizePath(k.path), xpub: normalizeXpub(k.xpub, network), source: 'import' })) }; } // Coldcard-style text file const lines = t.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.startsWith('#')); let name: string | undefined, m = 0, fmt = 'P2WSH', deriv: string | undefined; const cosigners: Omit[] = []; const pending: { fp: string; xpub: string; path: string }[] = []; for (const l of lines) { const [k, ...rest] = l.split(':'); const v = rest.join(':').trim(); const key = k.trim().toLowerCase(); if (key === 'name') name = v; else if (key === 'policy') m = Number(/(\d+)\s*of\s*(\d+)/i.exec(v)?.[1] ?? 0); else if (key === 'format') fmt = v.toUpperCase(); else if (key === 'derivation') deriv = v; else if (/^[0-9a-f]{8}$/i.test(k.trim())) pending.push({ fp: k.trim(), xpub: v, path: deriv ?? '' }); } const scriptType: ScriptType = fmt === 'P2SH' ? 'p2sh' : fmt.includes('P2SH') ? 'p2sh-p2wsh' : 'p2wsh'; if (!pending.length || !m) throw new Error('Not a recognised descriptor or multisig configuration file.'); const network = netOf(pending[0].xpub); for (const p of pending) { cosigners.push({ fingerprint: normalizeFingerprint(p.fp), xpub: normalizeXpub(p.xpub, network), path: normalizePath(p.path || defaultPath(scriptType, network)), source: 'import' }); } return { name, scriptType, m, network, cosigners }; }