UNPKG

@ledgerhq/types-live

Version:
220 lines 9.33 kB
/// <reference types="node" /> /// <reference types="node" /> import { BigNumber } from "bignumber.js"; import type { Observable } from "rxjs"; import type { CryptoCurrency, Unit } from "@ledgerhq/types-cryptoassets"; import type { DeviceModelId } from "@ledgerhq/types-devices"; import type { AccountLike, Account, AccountRaw, TokenAccount, TokenAccountRaw } from "./account"; import type { SignOperationEvent, SignedOperation, TransactionCommon, TransactionStatusCommon } from "./transaction"; import type { Operation, OperationExtra, OperationExtraRaw } from "./operation"; import type { DerivationMode } from "./derivation"; import type { SyncConfig } from "./pagination"; import { CryptoCurrencyIds, NFTCollectionMetadataResponse, NFTMetadataResponse } from "./nft"; export type ScanAccountEvent = { type: "discovered"; account: Account; }; /** * More events will come in the future */ export type ScanAccountEventRaw = { type: "discovered"; account: AccountRaw; }; /** * Unique identifier of a device. It will depend on the underlying implementation. */ export type DeviceId = string; /** * */ export type PreloadStrategy = Partial<{ preloadMaxAge: number; }>; export type BroadcastConfig = { mevProtected: boolean; }; /** * */ export type BroadcastArg<A extends Account> = { account: A; signedOperation: SignedOperation; broadcastConfig?: BroadcastConfig; }; /** * */ export type SignOperationArg0<T extends TransactionCommon, A extends Account> = { account: A; transaction: T; deviceId: DeviceId; deviceModelId?: DeviceModelId; certificateSignatureKind?: "prod" | "test"; }; /** * */ export type SignOperationFnSignature<T extends TransactionCommon, A extends Account> = (arg0: SignOperationArg0<T, A>) => Observable<SignOperationEvent>; export type BroadcastFnSignature<A extends Account = Account> = (arg0: BroadcastArg<A>) => Promise<Operation>; export type Bridge<T extends TransactionCommon, A extends Account = Account, U extends TransactionStatusCommon = TransactionStatusCommon, O extends Operation = Operation, R extends AccountRaw = AccountRaw> = { currencyBridge: CurrencyBridge; accountBridge: AccountBridge<T, A, U, O, R>; }; export type ScanInfo = { currency: CryptoCurrency; deviceId: DeviceId; scheme?: DerivationMode | null | undefined; syncConfig: SyncConfig; preferredNewAccountScheme?: DerivationMode; }; /** * Abstraction related to a currency */ export interface CurrencyBridge { preload(currency: CryptoCurrency): Promise<Record<string, any> | Array<unknown> | void>; hydrate(data: unknown, currency: CryptoCurrency): void; scanAccounts(info: ScanInfo): Observable<ScanAccountEvent>; getPreloadStrategy?: (currency: CryptoCurrency) => PreloadStrategy; nftResolvers?: { nftMetadata: (arg: { contract: string; tokenId: string; currencyId: string; }) => Promise<NFTMetadataResponse>; collectionMetadata: (arg: { contract: string; currencyId: string; }) => Promise<NFTCollectionMetadataResponse>; }; } /** * Abstraction related to an account */ interface SendReceiveAccountBridge<T extends TransactionCommon, A extends Account = Account, U extends TransactionStatusCommon = TransactionStatusCommon> { sync(initialAccount: A, syncConfig: SyncConfig): Observable<(arg0: A) => A>; receive(account: A, arg1: { verify?: boolean; deviceId: string; subAccountId?: string; freshAddressIndex?: number; path?: string; }): Observable<{ address: string; path: string; publicKey: string; chainCode?: string; }>; createTransaction(account: AccountLike<A>): T; updateTransaction(t: T, patch: Partial<T>): T; prepareTransaction(account: A, transaction: T): Promise<T>; getTransactionStatus(account: A, transaction: T): Promise<U>; estimateMaxSpendable(arg0: { account: AccountLike<A>; parentAccount?: A | null | undefined; transaction?: T | null | undefined; }): Promise<BigNumber>; /** * This function mutates the 'account' object to extend it with any extra fields of the coin. * For instance bitcoinResources needs to be created. * * @param {Account} account - The original account object to mutates in-place. */ initAccount?: (account: A) => void; signOperation: SignOperationFnSignature<T, A>; broadcast: BroadcastFnSignature<A>; } interface SerializationAccountBridge<A extends Account, O extends Operation = Operation, R extends AccountRaw = AccountRaw> { /** * This function mutates the 'accountRaw' object in-place to add any extra fields that the coin may need to set. * It is called during the serialization mechanism, for instance bitcoinResources need to be serialized. * * @param {Account} account - The original account object. * @param {AccountRaw} accountRaw - The account in its serialized form. */ assignToAccountRaw: (account: A, accountRaw: R) => void; /** * This function mutates the 'account' object in-place to add any extra fields that the coin may need to set. * It is called during the deserialization mechanism, for instance bitcoinResources need to be deserialized. * * @param {AccountRaw} accountRaw - The account in its serialized form. * @param {Account} account - The original account object. */ assignFromAccountRaw: (accountRaw: R, account: A) => void; /** * This function mutates the 'tokenAccountRaw' object in-place to add any extra fields that the coin may need to set. * It is called during the serialization mechanism * * @param {TokenAccount} tokenAccount - The original token account object. * @param {TokenAccountRaw} tokenAccountRaw - The token account in its serialized form. */ assignToTokenAccountRaw: (tokenAccount: TokenAccount, tokenAccountRaw: TokenAccountRaw) => void; /** * This function mutates the 'tokenAccount' object in-place to add any extra fields that the coin may need to set. * It is called during the deserialization mechanism * * @param {TokenAccountRaw} tokenAccountRaw - The token account in its serialized form. * @param {TokenAccount} tokenAccount - The original token account object. */ assignFromTokenAccountRaw?: (tokenAccountRaw: TokenAccountRaw, tokenAccount: TokenAccount) => void; fromOperationExtraRaw: (extraRaw: OperationExtraRaw) => OperationExtra; toOperationExtraRaw: (extra: OperationExtra) => OperationExtraRaw; formatAccountSpecifics: (account: A) => string; formatOperationSpecifics: (operation: O, unit: Unit | null | undefined) => string; } type AccountBridgeWithExchange<A extends Account = Account> = { getSerializedAddressParameters: (account: A, addressFormat?: string) => Buffer; }; export type AccountBridge<T extends TransactionCommon, A extends Account = Account, U extends TransactionStatusCommon = TransactionStatusCommon, O extends Operation = Operation, R extends AccountRaw = AccountRaw> = SendReceiveAccountBridge<T, A, U> & AccountBridgeWithExchange<A> & Partial<SerializationAccountBridge<A, O, R>>; type ExpectFn = (...args: Array<any>) => any; type CurrencyTransaction<T extends TransactionCommon> = { name: string; transaction: T | ((transaction: T, account: Account, accountBridge: AccountBridge<T>) => T); expectedStatus?: Partial<TransactionStatusCommon> | ((account: Account, transaction: T, status: TransactionStatusCommon) => Partial<TransactionStatusCommon>); test?: (arg0: ExpectFn, arg1: T, arg2: TransactionStatusCommon, arg3: AccountBridge<T>) => any; apdus?: string; testSignedOperation?: (arg0: ExpectFn, arg1: SignedOperation, arg2: Account, arg3: T, arg4: TransactionStatusCommon, arg5: AccountBridge<T>) => any; }; export type AccountTestData<T extends TransactionCommon> = { raw: AccountRaw; implementations?: string[]; FIXME_tests?: Array<string | RegExp>; transactions?: Array<CurrencyTransaction<T>>; test?: (arg0: ExpectFn, arg1: Account, arg2: AccountBridge<T>) => any; }; /** * */ export type CurrenciesData<T extends TransactionCommon> = { FIXME_ignoreAccountFields?: string[]; FIXME_ignoreOperationFields?: string[]; FIXME_ignorePreloadFields?: string[] | true; IgnorePrepareTransactionFields?: string[]; mockDeviceOptions?: any; scanAccounts?: Array<{ name: string; apdus: string; unstableAccounts?: boolean; test?: (expect: ExpectFn, scanned: Account[], bridge: CurrencyBridge) => any; }>; accounts?: Array<AccountTestData<T>>; test?: (arg0: ExpectFn, arg1: CurrencyBridge) => any; }; /** * */ export type DatasetTest<T extends TransactionCommon> = { implementations: string[]; currencies: Record<CryptoCurrencyIds, CurrenciesData<T>> | Record<string, never>; }; /** * */ export type BridgeCacheSystem = { hydrateCurrency: (currency: CryptoCurrency) => Promise<unknown | null | undefined>; prepareCurrency: (currency: CryptoCurrency, { forceUpdate }?: { forceUpdate: boolean; }) => Promise<unknown | null | undefined>; }; export {}; //# sourceMappingURL=bridge.d.ts.map