import BigNumber from "bignumber.js"; import { CryptoCurrency } from "@ledgerhq/ledger-wallet-framework/types"; import { getCryptoCurrencyById } from "@ledgerhq/ledger-wallet-framework/currencies"; import { toWalletBtcCurrency } from "./walletBtcCurrency"; import type { BitcoinContext } from "./config"; import { bindExplorer } from "./explorer"; import { DEFAULT_REPLACED_OPERATION_EXPIRY_MS } from "./constants"; import type { AccountShapeInfo, GetAccountShapeStream, } from "@ledgerhq/ledger-wallet-framework/bridge/jsHelpers"; import { mergeOps } from "@ledgerhq/ledger-wallet-framework/bridge/jsHelpers"; import { encodeAccountId, decodeAccountId } from "@ledgerhq/ledger-wallet-framework/account/index"; import { isSegwitDerivationMode, isNativeSegwitDerivationMode, isTaprootDerivationMode, } from "@ledgerhq/ledger-wallet-framework/derivation"; import { DerivationMode, SYNC_TYPE_TRANSPARENT, SyncConfig } from "@ledgerhq/types-live"; import type { Currency, Output as WalletOutput } from "@ledgerhq/wallet-btc/index"; import wallet, { DerivationModes as WalletDerivationModes } from "@ledgerhq/wallet-btc/index"; import { removeReplaced, deduplicateOperations } from "@ledgerhq/wallet-btc/operations"; // re-exported for backward compatibility (consumers/tests import it from here) export { removeReplaced } from "@ledgerhq/wallet-btc/operations"; import { BitcoinAccount, BitcoinOutput, BtcOperation } from "./types"; import { perCoinLogic, mapTxToOperations } from "./logic"; import { BitcoinXPub, SignerContext } from "./signer"; import { merge, Observable } from "rxjs"; import { getNetworkParameters } from "./networks"; // Map LL's DerivationMode to wallet-btc's const toWalletDerivationMode = (mode: DerivationMode): WalletDerivationModes => { if (isTaprootDerivationMode(mode)) { return WalletDerivationModes.TAPROOT; } if (isNativeSegwitDerivationMode(mode)) { return WalletDerivationModes.NATIVE_SEGWIT; } if (isSegwitDerivationMode(mode)) { return WalletDerivationModes.SEGWIT; } return WalletDerivationModes.LEGACY; }; // Map LL's currency ID to wallet-btc's Account.params.network const toWalletNetwork = (currencyId: string): "testnet" | "mainnet" => { return getCryptoCurrencyById(currencyId).isTestnetFor ? "testnet" : "mainnet"; }; // Map wallet-btc's Output to LL's BitcoinOutput export const fromWalletUtxo = (utxo: WalletOutput, changeAddresses: Set): BitcoinOutput => { return { hash: utxo.output_hash, outputIndex: utxo.output_index, blockHeight: utxo.block_height, address: utxo.address, value: new BigNumber(utxo.value), rbf: utxo.rbf, isChange: changeAddresses.has(utxo.address), }; }; export async function performTransparentSync( info: AccountShapeInfo, signerContext: SignerContext, context: BitcoinContext, ): Promise> { const { currency, index, derivationPath, derivationMode, initialAccount, deviceId } = info; const config = await context.config(currency.id); // In case we get a full derivation path, extract the seed identification part // 44'/0'/0'/0/0 --> 44'/0' // FIXME Only the CLI provides a full derivationPath: why? const rootPath = derivationPath.split("/", 2).join("/"); const accountPath = `${rootPath}/${index}'`; const paramXpub = initialAccount ? decodeAccountId(initialAccount.id).xpubOrAddress : undefined; const xpub = await generateXpubIfNeeded(paramXpub, { deviceId, currency, signerContext, accountPath, }); const accountId = encodeAccountId({ type: "js", version: "2", currencyId: currency.id, xpubOrAddress: xpub, derivationMode, }); const walletNetwork = toWalletNetwork(currency.id); const walletDerivationMode = toWalletDerivationMode(derivationMode); // Bound in both cases: wallet-btc caches one explorer per currency id, first set wins, and // deserialization seeds that cache unbound, so a freshly generated account can come back // with a stale explorer too. const walletAccount = bindExplorer( initialAccount?.bitcoinResources?.walletAccount || (await wallet.generateAccount( { xpub, path: rootPath, index, currency: currency.id, network: walletNetwork, derivationMode: walletDerivationMode, }, toWalletBtcCurrency(currency, config), )), currency, config, ); const oldOperations = (initialAccount?.operations || []) as BtcOperation[]; const currentBlock = await walletAccount.xpub.explorer.getCurrentBlock(); const blockHeight = currentBlock?.height || 0; await wallet.syncAccount(context.logger, walletAccount, blockHeight); const { txs: transactions } = await wallet.getAccountTransactions(walletAccount); const accountAddresses: Set = new Set(); const accountAddressesWithInfo = await walletAccount.xpub.getXpubAddresses(); accountAddressesWithInfo.forEach(a => accountAddresses.add(a.address)); const changeAddresses: Set = new Set(); const changeAddressesWithInfo = walletAccount.xpub.storage.getUniquesAddresses({ account: 1, }); changeAddressesWithInfo.forEach(a => changeAddresses.add(a.address)); const newOperations = transactions ?.map(tx => mapTxToOperations(tx, currency.id, accountId, accountAddresses, changeAddresses)) .flat(); const newUniqueOperations = deduplicateOperations(newOperations); const _operations = mergeOps(oldOperations, newUniqueOperations); const now = Date.now(); const expiryMs = config.sync?.replacedOperationExpiryMs ?? DEFAULT_REPLACED_OPERATION_EXPIRY_MS; const operations = removeReplaced(_operations as BtcOperation[], now, false, expiryMs); const balanceOperations = removeReplaced(_operations as BtcOperation[], now, true, expiryMs); const keptOperationHashes = new Set(balanceOperations.map(op => op.hash)); const removedOperationHashes = new Set( (_operations as BtcOperation[]) .filter(op => !keptOperationHashes.has(op.hash)) .map(op => op.hash), ); const rawUtxos = await wallet.getAccountUnspentUtxos(walletAccount); const filteredRawUtxos = rawUtxos.filter(utxo => { if (utxo.block_height !== null) return true; if (!utxo.rbf) return true; return !removedOperationHashes.has(utxo.output_hash); }); const utxos = filteredRawUtxos.map(utxo => fromWalletUtxo(utxo, changeAddresses)); // 1) Keep operations that have the same hash as those utxos const utxoHashes = new Set(utxos.map(u => u.hash)); const operationsOfUtxos = balanceOperations.filter(op => utxoHashes.has(op.hash)); // 2) Keep only one operation when multiple operations share the same inputs const getInputsKey = (op: BtcOperation): string | null => { const inputs = op.extra?.inputs; if (!Array.isArray(inputs) || inputs.length === 0) return null; return [...inputs].sort().join("|"); }; const isBetterCandidate = (candidate: BtcOperation, existing: BtcOperation): boolean => { const candidateConfirmed = typeof candidate.blockHeight === "number"; const existingConfirmed = typeof existing.blockHeight === "number"; if (candidateConfirmed !== existingConfirmed) return candidateConfirmed; const candidateHeight = candidate.blockHeight ?? -1; const existingHeight = existing.blockHeight ?? -1; if (candidateHeight !== existingHeight) return candidateHeight > existingHeight; return new Date(candidate.date).getTime() > new Date(existing.date).getTime(); }; const bestOpByInputsKey = new Map(); for (const op of operationsOfUtxos) { const key = getInputsKey(op); if (!key) continue; const existing = bestOpByInputsKey.get(key); if (!existing || isBetterCandidate(op, existing)) { bestOpByInputsKey.set(key, op); } } const finalOperationsOfUtxos = operationsOfUtxos.filter(op => { const key = getInputsKey(op); if (!key) return true; return bestOpByInputsKey.get(key)?.hash === op.hash; }); // 3) Filter the original utxos to keep the ones that share a hash with the final operations const finalOperationHashes = new Set(finalOperationsOfUtxos.map(op => op.hash)); const finalUtxos = finalOperationHashes.size > 0 ? utxos.filter(utxo => finalOperationHashes.has(utxo.hash)) : utxos; const balance = finalUtxos.reduce((total, utxo) => total.plus(utxo.value), new BigNumber(0)); return { id: accountId, xpub, balance, spendableBalance: balance, operations, operationsCount: operations.length, freshAddress: walletAccount.xpub.freshAddress, freshAddressPath: `${accountPath}/0/${walletAccount.xpub.freshAddressIndex}`, blockHeight, bitcoinResources: { utxos: finalUtxos, walletAccount, }, }; } export function createTransparentSyncObservable( info: AccountShapeInfo, signerContext: SignerContext, context: BitcoinContext, ): Observable> { const currencyId = info.currency.id; context.logger( "bitcoin/createTransparentSyncObservable", `Initiating transparent sync for ${currencyId}`, ); return new Observable>(subscriber => { performTransparentSync(info, signerContext, context) .then(result => { context.logger( "bitcoin/createTransparentSyncObservable", `Transparent sync completed for ${currencyId}`, { operationsCount: result.operationsCount, blockHeight: result.blockHeight, balance: result.balance?.toString(), spendableBalance: result.spendableBalance?.toString(), }, ); subscriber.next(result); subscriber.complete(); }) .catch(error => { context.logger( "bitcoin/createTransparentSyncObservable", `Transparent sync error for ${currencyId}`, { error: error.message, }, ); subscriber.error(error); }); }); } export function buildSyncObservables( info: AccountShapeInfo, syncConfig: SyncConfig, signerContext: SignerContext, context: BitcoinContext, ): { syncs: Observable>[]; syncType: number } { const syncType = syncConfig.syncType ?? SYNC_TYPE_TRANSPARENT; const syncs: Observable>[] = []; if (syncType & SYNC_TYPE_TRANSPARENT) { syncs.push(createTransparentSyncObservable(info, signerContext, context)); } return { syncs, syncType }; } export function makeGetAccountShape( signerContext: SignerContext, context: BitcoinContext, ): GetAccountShapeStream { return (info: AccountShapeInfo, syncConfig: SyncConfig) => new Observable(o => { const { currency } = info; const { syncs, syncType } = buildSyncObservables(info, syncConfig, signerContext, context); if (syncs.length === 0) { context.logger("bitcoin/makeGetAccountShape", `No syncs to perform for ${currency.id}`); o.complete(); return; } context.logger( "bitcoin/makeGetAccountShape", `Merging ${syncs.length} sync(s) for ${currency.id}`, { hasTransparent: !!(syncType & SYNC_TYPE_TRANSPARENT), syncCount: syncs.length, }, ); const sub = merge(...syncs).subscribe({ next: result => { context.logger("bitcoin/makeGetAccountShape", `Sync update received for ${currency.id}`, { blockHeight: result.blockHeight, operationsCount: result.operationsCount, hasBitcoinResources: !!result.bitcoinResources, }); o.next(result); }, complete: () => { context.logger("bitcoin/makeGetAccountShape", `All syncs completed for ${currency.id}`); o.complete(); }, error: error => { context.logger("bitcoin/makeGetAccountShape", `Sync error for ${currency.id}`, { error: error.message, }); o.error(error); }, }); // Propagate unsubscription from the outer Observable to the inner merge. // Without this, calling unsubscribe() on the outer subscription has no effect // on the running sync streams. return () => sub.unsubscribe(); }); } type XpubGenerateParameter = { deviceId: string | undefined; currency: CryptoCurrency; signerContext: SignerContext; accountPath: string; }; async function generateXpubIfNeeded( providedXpub: string | undefined, params: XpubGenerateParameter, ): Promise { if (providedXpub) { return Promise.resolve(providedXpub); } // Xpub not provided, generate it using the hwapp const { deviceId, currency, signerContext, accountPath } = params; if (deviceId === undefined || deviceId === null) { throw new Error("deviceId required to generate the xpub"); } const xpubVersion = getNetworkParameters(currency.id).xpubVersion.readUInt32BE(0); return signerContext(deviceId, currency, signer => signer.getWalletXpub({ path: accountPath, xpubVersion, }), ); } /** * Reconcile optimistic operations with their now-confirmed counterparts. * * `shouldRetainPendingOperation` matches on operation id, and an id embeds the * operation type: the optimistic operation created at signing time is an `OUT`, * while the confirmed one a sync produces may carry another type. The two never * match, so the pending entry * survives next to its own confirmed counterpart until the optimistic retention * window expires. The transaction hash is what actually identifies them. * * Before dropping the optimistic operation, take from it the address the user * actually entered: it is the most faithful answer available. */ function reconcileConfirmedPendingOperations(account: BitcoinAccount): BitcoinAccount { if (account.pendingOperations.length === 0) return account; const pendingByHash = new Map(account.pendingOperations.map(op => [op.hash, op])); const operations = account.operations.map(op => { const optimistic = pendingByHash.get(op.hash); // An incoming operation shares its hash with the send that produced it, but // it is not the operation that paid the entered address. if (!optimistic?.recipients.length || op.type.endsWith("IN")) return op; return { ...op, recipients: optimistic.recipients }; }); // Only the optimistic operations that now have a confirmed counterpart are // resolved; the rest are still in flight. The explorer also returns mempool // operations: dropping the optimistic one for those would make the // transaction lose its speed-up and its pending inputs while it is unconfirmed. const confirmedHashes = new Set( account.operations.filter(op => typeof op.blockHeight === "number").map(op => op.hash), ); const pendingOperations = account.pendingOperations.filter(op => !confirmedHashes.has(op.hash)); return { ...account, operations, pendingOperations }; } export const makePostSync = (context: BitcoinContext) => (_initial: BitcoinAccount, synced: BitcoinAccount): BitcoinAccount => { context.logger("bitcoin/postSync", "bitcoinResources"); const perCoin = perCoinLogic[synced.currency.id]; let syncedBtc = synced; if (perCoin) { const { postBuildBitcoinResources, syncReplaceAddress } = perCoin; // FIXME: unused, can remove? if (postBuildBitcoinResources) { syncedBtc.bitcoinResources = postBuildBitcoinResources( syncedBtc, syncedBtc.bitcoinResources, ); } if (syncReplaceAddress) { syncedBtc.freshAddress = syncReplaceAddress(syncedBtc.freshAddress); if (syncedBtc.bitcoinResources) { syncedBtc.bitcoinResources.utxos = syncedBtc.bitcoinResources?.utxos.map(u => ({ ...u, address: u.address && syncReplaceAddress(u.address), })); } } } syncedBtc = reconcileConfirmedPendingOperations(syncedBtc); context.logger("bitcoin/postSync", "bitcoinResources DONE"); return syncedBtc; };