import BigNumber from "bignumber.js"; import type { BitcoinResourcesRaw, BitcoinResources, BitcoinInputRaw, BitcoinInput, BitcoinOutputRaw, BitcoinOutput, BitcoinAccountRaw, BitcoinAccount, } from "./types"; import wallet from "@ledgerhq/wallet-btc/index"; import { Account, AccountRaw } from "@ledgerhq/types-live"; import { walletBtcCurrencyById, type ExplorerConfig } from "./walletBtcCurrency"; import type { Logger } from "@ledgerhq/coin-module-framework/config"; export function toBitcoinInputRaw({ address, value, previousTxHash, previousOutputIndex, }: BitcoinInput): BitcoinInputRaw { return [address, value ? value.toString() : undefined, previousTxHash, previousOutputIndex]; } export function fromBitcoinInputRaw([ address, value, previousTxHash, previousOutputIndex, ]: BitcoinInputRaw): BitcoinInput { return { address: address || undefined, value: value ? new BigNumber(value) : undefined, previousTxHash: previousTxHash || undefined, previousOutputIndex, }; } export function toBitcoinOutputRaw({ hash, outputIndex, blockHeight, address, value, rbf, isChange, }: BitcoinOutput): BitcoinOutputRaw { return [hash, outputIndex, blockHeight, address, value.toString(), rbf ? 1 : 0, isChange ? 1 : 0]; } export function fromBitcoinOutputRaw([ hash, outputIndex, blockHeight, address, value, rbf, isChange, ]: BitcoinOutputRaw): BitcoinOutput { return { hash, outputIndex, blockHeight: blockHeight || undefined, address: address || undefined, value: new BigNumber(value), rbf: !!rbf, isChange: !!isChange, }; } export function toBitcoinResourcesRaw(r: BitcoinResources): BitcoinResourcesRaw { return { utxos: r.utxos.map(toBitcoinOutputRaw), walletAccount: r.walletAccount && wallet.exportToSerializedAccountSync(r.walletAccount), }; } // No explorer endpoint at deserialization: the bridge binds it from the coin config before first // use (see explorer.ts), so a remote endpoint change is not frozen here. const UNBOUND_EXPLORER: ExplorerConfig = { explorer: { url: "" } }; export function fromBitcoinResourcesRaw(logger: Logger, r: BitcoinResourcesRaw): BitcoinResources { return { utxos: r.utxos.map(fromBitcoinOutputRaw), walletAccount: r.walletAccount && wallet.importFromSerializedAccountSync( logger, r.walletAccount, walletBtcCurrencyById(r.walletAccount.params.currency, UNBOUND_EXPLORER), ), }; } export function assignToAccountRaw(account: Account, accountRaw: AccountRaw) { const bitcoinAccount = account as BitcoinAccount; if (bitcoinAccount.bitcoinResources) { (accountRaw as BitcoinAccountRaw).bitcoinResources = toBitcoinResourcesRaw( bitcoinAccount.bitcoinResources, ); } } export const makeAssignFromAccountRaw = (logger: Logger) => (accountRaw: AccountRaw, account: Account): void => { const bitcoinResourcesRaw = (accountRaw as BitcoinAccountRaw).bitcoinResources; if (bitcoinResourcesRaw) (account as BitcoinAccount).bitcoinResources = fromBitcoinResourcesRaw( logger, bitcoinResourcesRaw, ); };