export type BitcoinNetwork = | 'MAINNET' | 'TESTNET' | 'TESTNET4' | 'REGTEST' | 'SIGNET'; export const BitcoinNetwork = { MAINNET: 'MAINNET' as const, TESTNET: 'TESTNET' as const, TESTNET4: 'TESTNET4' as const, REGTEST: 'REGTEST' as const, SIGNET: 'SIGNET' as const, } as const; export type Keys = { mnemonic?: string | null; xpub: string; accountXpubVanilla: string; accountXpubColored: string; masterFingerprint: string; }; export type Balance = { settled: number; future: number; spendable: number; }; export type BtcBalance = { vanilla: Balance; colored: Balance; }; export type AssetSchema = 'NIA' | 'UDA' | 'CFA' | 'IFA'; export const AssetSchema = { NIA: 'NIA' as const, UDA: 'UDA' as const, CFA: 'CFA' as const, IFA: 'IFA' as const, } as const; export type AssignmentType = | 'FUNGIBLE' | 'NON_FUNGIBLE' | 'INFLATION_RIGHT' | 'REPLACE_RIGHT' | 'ANY'; export type Assignment = { type: AssignmentType; amount?: number; }; export type Outpoint = { txid: string; vout: number; }; export type Utxo = { outpoint: Outpoint; btcAmount: number; colorable: boolean; exists: boolean; }; export type RgbAllocation = { assetId?: string; assignment: Assignment; settled: boolean; }; export type RefreshTransferStatus = | 'WAITING_COUNTERPARTY' | 'WAITING_CONFIRMATIONS'; export type RefreshFilter = { status: RefreshTransferStatus; incoming: boolean; }; export type WitnessData = { amountSat: number; blinding?: number; }; export type Recipient = { recipientId: string; witnessData?: WitnessData; assignment: Assignment; transportEndpoints: string[]; }; export type Media = { filePath: string; mime: string; digest: string; }; export type AssetCfa = { assetId: string; name: string; details?: string; precision: number; issuedSupply: number; timestamp: number; addedAt: number; balance: Balance; media?: Media; }; export type AssetIfa = { assetId: string; ticker: string; name: string; details?: string; precision: number; initialSupply: number; maxSupply: number; knownCirculatingSupply: number; timestamp: number; addedAt: number; balance: Balance; media?: Media; rejectListUrl?: string; }; export type AssetNia = { assetId: string; ticker: string; name: string; details?: string; precision: number; issuedSupply: number; timestamp: number; addedAt: number; balance: Balance; media?: Media; }; export type AssetUda = { assetId: string; ticker: string; name: string; details?: string; precision: number; timestamp: number; addedAt: number; balance: Balance; token?: { index: number; ticker?: string; name?: string; details?: string; embeddedMedia: boolean; media?: Media; attachments: Array<{ key: number; filePath: string; mime: string; digest: string; }>; reserves: boolean; }; }; export type Assets = { nia: AssetNia[]; uda: AssetUda[]; cfa: AssetCfa[]; ifa: AssetIfa[]; }; export type OperationResult = { txid: string; batchTransferIdx: number; }; export type ReceiveData = { invoice: string; recipientId: string; expirationTimestamp: number | null; batchTransferIdx: number; }; export type InvoiceData = { invoice: string; recipientId: string; assetSchema?: AssetSchema; assetId?: string; assignment: Assignment; assignmentName?: string; network: BitcoinNetwork; expirationTimestamp: number | null; transportEndpoints: string[]; }; /** * Raw invoice data format returned from Node.js rgb-lib bindings. * This differs from the React Native format and needs normalization. */ type RawInvoiceData = { recipientId: string; assetSchema?: string | null; assetId?: string | null; assignment: Record | string; assignmentName?: string; network: string; expirationTimestamp: number | null; transportEndpoints: string[]; }; /** * Normalizes raw invoice data from Node.js bindings to SDK format. * Converts Rust enum format to SDK-friendly format to match React Native SDK. * @internal */ export function normalizeInvoiceData(invoice: string, raw: RawInvoiceData): InvoiceData { // Parse assignment from Rust enum format let assignment: Assignment; if (typeof raw.assignment === 'string') { // Handle string variants like "Any", "NonFungible" const typeMap: Record = { 'Any': 'ANY', 'NonFungible': 'NON_FUNGIBLE', 'InflationRight': 'INFLATION_RIGHT', 'ReplaceRight': 'REPLACE_RIGHT', }; assignment = { type: typeMap[raw.assignment] || 'ANY', }; } else if (typeof raw.assignment === 'object' && raw.assignment !== null) { // Handle object variants like {"Fungible": 1000} const entries = Object.entries(raw.assignment); if (entries.length > 0) { const [key, value] = entries[0]; const typeMap: Record = { 'Fungible': 'FUNGIBLE', 'NonFungible': 'NON_FUNGIBLE', 'InflationRight': 'INFLATION_RIGHT', 'ReplaceRight': 'REPLACE_RIGHT', }; assignment = { type: typeMap[key] || 'FUNGIBLE', amount: typeof value === 'number' ? value : undefined, }; } else { assignment = { type: 'ANY' }; } } else { assignment = { type: 'ANY' }; } // Normalize network format (e.g., "Testnet4" -> "TESTNET4") const networkMap: Record = { 'Mainnet': 'MAINNET', 'Testnet': 'TESTNET', 'Testnet4': 'TESTNET4', 'Regtest': 'REGTEST', 'Signet': 'SIGNET', }; const network = networkMap[raw.network] || raw.network as BitcoinNetwork; return { invoice, recipientId: raw.recipientId, assetSchema: raw.assetSchema as AssetSchema | undefined, assetId: raw.assetId || undefined, assignment, assignmentName: raw.assignmentName, network, expirationTimestamp: raw.expirationTimestamp, transportEndpoints: raw.transportEndpoints, }; } export type Transaction = { transactionType: 'RGB_SEND' | 'DRAIN' | 'CREATE_UTXOS' | 'USER'; txid: string; received: number; sent: number; fee: number; confirmationTime?: number; }; export type TransferTransportEndpoint = { endpoint: string; used: boolean; transportType: string; }; export type Transfer = { idx: number; batchTransferIdx: number; createdAt: number; updatedAt: number; kind: 'ISSUANCE' | 'RECEIVE_BLIND' | 'RECEIVE_WITNESS' | 'SEND' | 'INFLATION'; status: | 'WAITING_COUNTERPARTY' | 'WAITING_CONFIRMATIONS' | 'SETTLED' | 'FAILED'; txid?: string; recipientId?: string; requestedAssignment?: Assignment; assignments: Assignment[]; receiveUtxo?: Outpoint; changeUtxo?: Outpoint; expiration?: number; transportEndpoints: TransferTransportEndpoint[]; invoiceString?: string; consignmentPath?: string; }; export type Unspent = { utxo: Utxo; rgbAllocations: RgbAllocation[]; pendingBlinded: number; }; export type RefreshedTransfer = { updatedStatus?: | 'WAITING_COUNTERPARTY' | 'WAITING_CONFIRMATIONS' | 'SETTLED' | 'FAILED'; failure?: string; }; export type WalletData = { dataDir: string; bitcoinNetwork: string; databaseType: string; maxAllocationsPerUtxo: number; accountXpubVanilla: string; accountXpubColored: string; mnemonic?: string; masterFingerprint: string; vanillaKeychain?: number; supportedSchemas: string[]; }; export type AssetMetadata = { assetId: string; name: string; ticker?: string; details?: string; precision?: number; issuedSupply?: number; timestamp?: number; amounts?: number[]; assetNia?: AssetNia; assetUda?: AssetUda; assetCfa?: AssetCfa; assetIfa?: AssetIfa; }; /** * Error types that can be returned by RGB library methods. * These correspond to the RgbLibError * Errors with associated values are represented as objects with the error name and associated data. */ export type RgbLibErrors = | 'AllocationsAlreadyAvailable' | { type: 'AssetNotFound'; assetId: string } | { type: 'BatchTransferNotFound'; idx: number } | 'BitcoinNetworkMismatch' | 'CannotChangeOnline' | 'CannotDeleteBatchTransfer' | 'CannotEstimateFees' | 'CannotFailBatchTransfer' | 'CannotFinalizePsbt' | 'CannotUseIfaOnMainnet' | { type: 'EmptyFile'; filePath: string } | { type: 'FailedBdkSync'; details: string } | { type: 'FailedBroadcast'; details: string } | { type: 'FailedIssuance'; details: string } | { type: 'FileAlreadyExists'; path: string } | 'FingerprintMismatch' | { type: 'Io'; details: string } | { type: 'Inconsistency'; details: string } | { type: 'Indexer'; details: string } | 'InexistentDataDir' | 'InsufficientAllocationSlots' | { type: 'InsufficientAssignments'; assetId: string; available: unknown } | { type: 'InsufficientBitcoins'; needed: number; available: number } | { type: 'Internal'; details: string } | { type: 'InvalidAddress'; details: string } | 'InvalidAmountZero' | { type: 'InvalidAssetId'; assetId: string } | 'InvalidAssignment' | { type: 'InvalidAttachments'; details: string } | 'InvalidBitcoinKeys' | { type: 'InvalidBitcoinNetwork'; network: string } | { type: 'InvalidColoringInfo'; details: string } | 'InvalidConsignment' | { type: 'InvalidDetails'; details: string } | { type: 'InvalidElectrum'; details: string } | 'InvalidEstimationBlocks' | { type: 'InvalidFeeRate'; details: string } | { type: 'InvalidFilePath'; filePath: string } | 'InvalidFingerprint' | { type: 'InvalidIndexer'; details: string } | { type: 'InvalidInvoice'; details: string } | { type: 'InvalidMnemonic'; details: string } | { type: 'InvalidName'; details: string } | { type: 'InvalidPrecision'; details: string } | { type: 'InvalidProxyProtocol'; version: string } | { type: 'InvalidPsbt'; details: string } | { type: 'InvalidPubkey'; details: string } | { type: 'InvalidRecipientData'; details: string } | 'InvalidRecipientId' | 'InvalidRecipientNetwork' | { type: 'InvalidRejectListUrl'; details: string } | { type: 'InvalidTicker'; details: string } | { type: 'InvalidTransportEndpoint'; details: string } | { type: 'InvalidTransportEndpoints'; details: string } | 'InvalidTxid' | 'InvalidVanillaKeychain' | { type: 'MaxFeeExceeded'; txid: string } | { type: 'MinFeeNotMet'; txid: string } | { type: 'Network'; details: string } | 'NoConsignment' | 'NoInflationAmounts' | 'NoIssuanceAmounts' | 'NoSupportedSchemas' | 'NoValidTransportEndpoint' | 'Offline' | 'OnlineNeeded' | 'OutputBelowDustLimit' | { type: 'Proxy'; details: string } | 'RecipientIdAlreadyUsed' | 'RecipientIdDuplicated' | { type: 'RejectListService'; details: string } | { type: 'RestClientBuild'; details: string } | 'TooHighInflationAmounts' | 'TooHighIssuanceAmounts' | { type: 'UnknownRgbSchema'; schemaId: string } | { type: 'UnknownTransfer'; txid: string } | { type: 'UnsupportedBackupVersion'; version: string } | { type: 'UnsupportedInflation'; assetSchema: string } | { type: 'UnsupportedLayer1'; layer1: string } | { type: 'UnsupportedSchema'; assetSchema: string } | 'UnsupportedTransportType' | { type: 'WalletDirAlreadyExists'; path: string } | 'WatchOnly' | 'WrongPassword'; /** * Constants for all RGB library errors. * Use these for error code comparison and type safety. */ export const RgbLibErrors = { AllocationsAlreadyAvailable: 'AllocationsAlreadyAvailable' as const, BitcoinNetworkMismatch: 'BitcoinNetworkMismatch' as const, CannotChangeOnline: 'CannotChangeOnline' as const, CannotDeleteBatchTransfer: 'CannotDeleteBatchTransfer' as const, CannotEstimateFees: 'CannotEstimateFees' as const, CannotFailBatchTransfer: 'CannotFailBatchTransfer' as const, CannotFinalizePsbt: 'CannotFinalizePsbt' as const, CannotUseIfaOnMainnet: 'CannotUseIfaOnMainnet' as const, FingerprintMismatch: 'FingerprintMismatch' as const, InexistentDataDir: 'InexistentDataDir' as const, InsufficientAllocationSlots: 'InsufficientAllocationSlots' as const, InvalidAmountZero: 'InvalidAmountZero' as const, InvalidAssignment: 'InvalidAssignment' as const, InvalidBitcoinKeys: 'InvalidBitcoinKeys' as const, InvalidConsignment: 'InvalidConsignment' as const, InvalidEstimationBlocks: 'InvalidEstimationBlocks' as const, InvalidFingerprint: 'InvalidFingerprint' as const, InvalidRecipientId: 'InvalidRecipientId' as const, InvalidRecipientNetwork: 'InvalidRecipientNetwork' as const, InvalidTxid: 'InvalidTxid' as const, InvalidVanillaKeychain: 'InvalidVanillaKeychain' as const, NoConsignment: 'NoConsignment' as const, NoInflationAmounts: 'NoInflationAmounts' as const, NoIssuanceAmounts: 'NoIssuanceAmounts' as const, NoSupportedSchemas: 'NoSupportedSchemas' as const, NoValidTransportEndpoint: 'NoValidTransportEndpoint' as const, Offline: 'Offline' as const, OnlineNeeded: 'OnlineNeeded' as const, OutputBelowDustLimit: 'OutputBelowDustLimit' as const, RecipientIdAlreadyUsed: 'RecipientIdAlreadyUsed' as const, RecipientIdDuplicated: 'RecipientIdDuplicated' as const, TooHighInflationAmounts: 'TooHighInflationAmounts' as const, TooHighIssuanceAmounts: 'TooHighIssuanceAmounts' as const, UnsupportedTransportType: 'UnsupportedTransportType' as const, WatchOnly: 'WatchOnly' as const, WrongPassword: 'WrongPassword' as const, } as const;