UNPKG

xud

Version:
204 lines (203 loc) 8.39 kB
import { SwapClientType } from '../constants/enums'; import Logger from '../Logger'; import * as lndrpc from '../proto/lndrpc_pb'; import * as lndwalletunlocker from '../proto/lndwalletunlocker_pb'; import SwapClient, { ChannelBalance, PaymentState, SwapClientInfo, WithdrawArguments } from '../swaps/SwapClient'; import { CloseChannelParams, OpenChannelParams, SwapCapacities, SwapDeal } from '../swaps/types'; import { LndClientConfig, LndInfo } from './types'; interface LndClient { on(event: 'connectionVerified', listener: (swapClientInfo: SwapClientInfo) => void): this; on(event: 'htlcAccepted', listener: (rHash: string, amount: number) => void): this; on(event: 'channelBackup', listener: (channelBackup: Uint8Array) => void): this; on(event: 'channelBackupEnd', listener: () => void): this; on(event: 'locked', listener: () => void): this; once(event: 'initialized', listener: () => void): this; emit(event: 'connectionVerified', swapClientInfo: SwapClientInfo): boolean; emit(event: 'htlcAccepted', rHash: string, amount: number): boolean; emit(event: 'channelBackup', channelBackup: Uint8Array): boolean; emit(event: 'channelBackupEnd'): boolean; emit(event: 'locked'): boolean; emit(event: 'initialized'): boolean; } /** A class representing a client to interact with lnd. */ declare class LndClient extends SwapClient { readonly type = SwapClientType.Lnd; readonly finalLock: number; config: LndClientConfig; currency: string; walletPassword?: string; private lightning?; private walletUnlocker?; /** The maximum time to wait for a client to be ready for making grpc calls, can be used for exponential backoff. */ private maxClientWaitTime; private invoices?; private router?; /** The path to the lnd admin macaroon, will be undefined if `nomacaroons` is enabled */ private macaroonpath?; private meta; private uri; private credentials; /** The identity pub key for this lnd instance. */ private identityPubKey?; /** List of client's public listening uris that are advertised to the network */ private urisList?; /** The identifier for the chain this lnd instance is using in the format [chain]-[network] like "bitcoin-testnet" */ private chainIdentifier?; private channelBackupSubscription?; private invoiceSubscriptions; private initRetryTimeout?; private totalOutboundAmount; private totalInboundAmount; private maxChannelOutboundAmount; private maxChannelInboundAmount; private initWalletResolve?; private watchMacaroonResolve?; private static MINUTES_PER_BLOCK_BY_CURRENCY; /** * Creates an lnd client. */ constructor({ config, logger, currency }: { config: LndClientConfig; logger: Logger; currency: string; }); private waitForClientReady; get minutesPerBlock(): number; get label(): string; /** * Initializes the client for calls to lnd and verifies that we can connect to it. * @param awaitingCreate whether xud is waiting for its node key to be created */ initSpecific: () => Promise<void>; get pubKey(): string | undefined; get uris(): string[] | undefined; get chain(): string | undefined; setReservedInboundAmount: (_reservedInboundAmount: number) => void; /** Lnd specific procedure to mark the client as locked. */ private lock; /** Lnd specific procedure to mark the client as unlocked. */ private setUnlocked; protected updateCapacity: () => Promise<void>; private unaryCall; private loadMacaroon; private unaryInvoiceCall; private unaryWalletUnlockerCall; getLndInfo: () => Promise<LndInfo>; /** * Waits for the lnd wallet to be initialized and for its macaroons to be created then attempts * to verify the connection to lnd. */ private awaitWalletInit; protected verifyConnection: () => Promise<void>; /** * Returns general information concerning the lightning node including it’s identity pubkey, alias, the chains it * is connected to, and information concerning the number of open+pending channels. */ getInfo: () => Promise<lndrpc.GetInfoResponse>; /** * Returns closed channels that this node was a participant in. */ getClosedChannels: () => Promise<lndrpc.ClosedChannelsResponse>; deposit: () => Promise<string>; withdraw: ({ amount, destination, all, fee }: WithdrawArguments) => Promise<string>; sendSmallestAmount: (rHash: string, destination: string) => Promise<string>; sendPayment: (deal: SwapDeal) => Promise<string>; /** * Sends a payment through the Lightning Network. * @returns the preimage in hex format */ private sendPaymentV2; /** * Builds a lndrpc.SendRequest */ private buildSendRequest; /** * Gets a new address for the internal lnd wallet. */ private newAddress; /** * Returns the total of unspent outputs for the internal lnd wallet. */ walletBalance: () => Promise<lndrpc.WalletBalanceResponse.AsObject>; /** * Updates all balances related to channels including active, inactive, and pending balances. * Sets trading limits for this client accordingly. */ private updateChannelBalances; channelBalance: () => Promise<ChannelBalance>; swapCapacities: () => Promise<SwapCapacities>; getHeight: () => Promise<number>; /** * Connects to another lnd node. */ connectPeer: (pubkey: string, address: string) => Promise<lndrpc.ConnectPeerResponse>; /** * Opens a channel given peerPubKey and amount. */ openChannel: ({ remoteIdentifier, units, uris, pushUnits, fee }: OpenChannelParams) => Promise<string>; /** * Tries to connect to a given list of a peer's uris in sequential order. * @returns `true` when successful, otherwise `false`. */ private connectPeerAddresses; /** * Opens a channel with a connected lnd node. */ private openChannelSync; /** * Lists all open channels for this node. */ listChannels: () => Promise<lndrpc.ListChannelsResponse>; /** * Lists all pending channels for this node. */ private pendingChannels; getRoute: (units: number, destination: string, _currency: string, finalLock?: number) => Promise<lndrpc.Route | undefined>; canRouteToNode: (_destination: string) => Promise<boolean>; /** * Lists all routes to destination. */ private queryRoutes; initWallet: (walletPassword: string, seedMnemonic: string[], restore?: boolean, backup?: Uint8Array | undefined) => Promise<lndwalletunlocker.InitWalletResponse.AsObject>; unlockWallet: (walletPassword: string) => Promise<void>; /** * Watches for a change in the admin.macaroon file at the configured path, * then loads the macaroon. */ private watchThenLoadMacaroon; changePassword: (oldPassword: string, newPassword: string) => Promise<void>; addInvoice: ({ rHash, units, expiry }: { rHash: string; units: number; expiry?: number | undefined; }) => Promise<void>; settleInvoice: (rHash: string, rPreimage: string) => Promise<void>; removeInvoice: (rHash: string) => Promise<void>; lookupPayment: (rHash: string) => Promise<{ preimage: string; state: PaymentState; } | { state: PaymentState; preimage?: undefined; }>; private listPayments; restoreChannelBackup: (multiChannelBackup: Uint8Array) => Promise<lndrpc.RestoreBackupResponse>; exportAllChannelBackup: () => Promise<Uint8Array>; private addHoldInvoice; private cancelInvoice; private settleInvoiceLnd; private subscribeSingleInvoice; /** * Subscribes to channel backups */ subscribeChannelBackups: () => void; /** * Closes any payment channels with a specified node. */ closeChannel: ({ remoteIdentifier, force, fee }: CloseChannelParams) => Promise<string[]>; /** A synchronous helper method for the closeChannel call */ closeChannelSync: (fundingTxId: string, outputIndex: number, force: boolean, fee?: number) => Promise<string>; /** Lnd specific procedure to disconnect from the server. */ protected disconnect: () => void; } export default LndClient;