import * as Promise from 'bluebird' import { Readable } from 'stream' export type Maybe = T | void export type Hash = string export type Tag = string export type Trytes = string export type TransactionTrytes = string export type AttachedTransactionTrytes = string export interface Balance { readonly balance: number } /* Address object */ export interface Address extends Balance { readonly address: Hash readonly keyIndex: number readonly security: number } export const makeAddress = (address: Hash, balance: number, keyIndex: number, security: number): Address => ({ address, keyIndex, security, balance, }) export interface Inputs { readonly inputs: ReadonlyArray
readonly totalBalance: number } /* Transfer object */ export interface Transfer { readonly address: string readonly value: number readonly message?: string readonly tag?: string readonly obsoleteTag?: string } /* Transaction object */ export interface Transaction { readonly hash: string readonly signatureMessageFragment: string readonly address: string readonly value: number readonly obsoleteTag: string readonly timestamp: number readonly currentIndex: number readonly lastIndex: number readonly bundle: string readonly trunkTransaction: string readonly branchTransaction: string readonly tag: string readonly attachmentTimestamp: number readonly attachmentTimestampLowerBound: number readonly attachmentTimestampUpperBound: number readonly nonce: string readonly confirmed?: boolean } export interface TransactionEssence { readonly address: Int8Array readonly value: Int8Array readonly obsoleteTag?: Int8Array readonly issuanceTimestamp?: Int8Array readonly currentIndex: Int8Array } /* Bundle object */ export type Bundle = ReadonlyArray /* Neighbor object */ export interface Neighbor { readonly address: string readonly numberOfAllTransactions: number readonly numberOfInvalidTransactions: number readonly numberOfNewTransactions: number } /* List of Neighbors object, returned by `getNeighbors()` */ export type Neighbors = ReadonlyArray /* List of IRI Commands */ export enum IRICommand { GET_NODE_INFO = 'getNodeInfo', GET_NEIGHBORS = 'getNeighbors', ADD_NEIGHBORS = 'addNeighbors', REMOVE_NEIGHBORS = 'removeNeighbors', GET_TIPS = 'getTips', FIND_TRANSACTIONS = 'findTransactions', GET_TRYTES = 'getTrytes', GET_INCLUSION_STATES = 'getInclusionStates', GET_BALANCES = 'getBalances', GET_TRANSACTIONS_TO_APPROVE = 'getTransactionsToApprove', ATTACH_TO_TANGLE = 'attachToTangle', INTERRUPT_ATTACHING_TO_TANGLE = 'interruptAttachingToTangle', BROADCAST_TRANSACTIONS = 'broadcastTransactions', STORE_TRANSACTIONS = 'storeTransactions', CHECK_CONSISTENCY = 'checkConsistency', WERE_ADDRESSES_SPENT_FROM = 'wereAddressesSpentFrom', } /* IRI Command objects extend from this interface */ export interface BaseCommand { readonly command: string } /** IRI Command/Response interfaces */ export interface AddNeighborsCommand extends BaseCommand { command: IRICommand.ADD_NEIGHBORS readonly uris: ReadonlyArray } export interface AddNeighborsResponse { readonly addedNeighbors: number readonly duration: number } export interface AttachToTangleCommand extends BaseCommand { command: IRICommand.ATTACH_TO_TANGLE readonly trunkTransaction: Hash readonly branchTransaction: Hash readonly minWeightMagnitude: number readonly trytes: ReadonlyArray } export interface AttachToTangleResponse { readonly trytes: ReadonlyArray } export interface BroadcastTransactionsCommand extends BaseCommand { command: IRICommand.BROADCAST_TRANSACTIONS readonly trytes: ReadonlyArray } export type BroadcastTransactionsResponse = void export interface CheckConsistencyCommand extends BaseCommand { command: IRICommand.CHECK_CONSISTENCY readonly tails: ReadonlyArray } export interface CheckConsistencyResponse { readonly state: boolean readonly info: string } export interface FindTransactionsQuery { readonly addresses?: ReadonlyArray readonly approvees?: ReadonlyArray readonly bundles?: ReadonlyArray readonly tags?: ReadonlyArray } export interface FindTransactionsCommand extends BaseCommand, FindTransactionsQuery { readonly command: IRICommand.FIND_TRANSACTIONS } export interface FindTransactionsResponse { readonly hashes: ReadonlyArray } export interface GetBalancesCommand extends BaseCommand { readonly command: string readonly addresses: ReadonlyArray readonly threshold: number readonly tips?: ReadonlyArray } export interface GetBalancesResponse { readonly balances: ReadonlyArray readonly duration: number readonly milestone: string readonly milestoneIndex: number } export interface Balances { readonly balances: ReadonlyArray readonly milestone: string readonly milestoneIndex: number } export interface GetInclusionStatesCommand extends BaseCommand { readonly command: IRICommand.GET_INCLUSION_STATES readonly transactions: ReadonlyArray readonly tips: ReadonlyArray } export interface GetInclusionStatesResponse { readonly states: ReadonlyArray readonly duration: number } export interface GetNeighborsCommand extends BaseCommand { command: IRICommand.GET_NEIGHBORS } export interface GetNeighborsResponse { duration: number neighbors: Neighbors } export interface GetNodeInfoCommand extends BaseCommand { command: IRICommand.GET_NODE_INFO } export interface GetNodeInfoResponse { readonly appName: string readonly appVersion: string readonly duration: number readonly jreAvailableProcessors: number readonly jreFreeMemory: number readonly jreMaxMemory: number readonly jreTotalMemory: number readonly latestMilestone: Hash readonly latestMilestoneIndex: number readonly latestSolidSubtangleMilestone: Hash readonly latestSolidSubtangleMilestoneIndex: number readonly neighbors: number readonly packetsQueueSize: number readonly time: number readonly tips: number readonly transactionsToRequest: number } export interface GetTipsCommand extends BaseCommand { command: IRICommand.GET_TIPS } export interface GetTipsResponse { readonly hashes: ReadonlyArray readonly duration: number } export interface TransactionsToApprove { readonly trunkTransaction: Hash readonly branchTransaction: Hash } export interface GetTransactionsToApproveResponse extends TransactionsToApprove { readonly duration: number } export interface GetTransactionsToApproveCommand extends BaseCommand { command: IRICommand.GET_TRANSACTIONS_TO_APPROVE readonly depth: number readonly reference?: Hash } export interface GetTrytesCommand extends BaseCommand { command: IRICommand.GET_TRYTES readonly hashes: ReadonlyArray } export interface GetTrytesResponse { readonly trytes: ReadonlyArray } export interface InterruptAttachingToTangleCommand extends BaseCommand { command: IRICommand.INTERRUPT_ATTACHING_TO_TANGLE } export type InterruptAttachingToTangleResponse = void export interface RemoveNeighborsCommand extends BaseCommand { command: IRICommand.REMOVE_NEIGHBORS readonly uris: ReadonlyArray } export interface RemoveNeighborsResponse { readonly removedNeighbors: number readonly duration: number } export interface StoreTransactionsCommand extends BaseCommand { command: IRICommand.STORE_TRANSACTIONS readonly trytes: ReadonlyArray } export type StoreTransactionsResponse = void /** Callback */ export type Callback = (err: Readonly | null, res?: Readonly) => void /** Provider interface */ export interface Provider { readonly send: ( command: Readonly, callback?: Callback> ) => Promise> readonly setSettings: (settings?: Readonly>) => void } /** Attach to tangle */ export type AttachToTangle = ( trunkTransaction: Hash, branchTransaction: Hash, minWeightMagnitude: number, trytes: ReadonlyArray, callback?: Callback> ) => Promise> export type NativeGenerateSignatureFunction = ( seed: number[], index: number, numberOfFragments: number, bundle: number[] ) => Promise /* Util methods */ export const asArray = (x: T | ReadonlyArray): ReadonlyArray => (Array.isArray(x) ? x : [x]) export const getOptionsWithDefaults = (defaults: Readonly) => (options: Readonly>): Readonly => Object.assign({}, defaults, options) // tslint:disable-line prefer-object-spread // Persistence export type CreatePersistence = (adapter: PersistenceAdapter) => Persistence export interface Persistence extends Readable { readonly ready: () => Promise readonly increment: () => Promise readonly put: (key: K, value: V) => Promise readonly del: (key: K) => Promise readonly get: (key: K) => Promise readonly batch: (ops: PersistenceBatch) => Promise readonly close: () => Promise readonly open: () => Promise } export type PersistenceBatch = ReadonlyArray | PersistenceDelCommand> export enum PersistenceBatchTypes { put = 'put', del = 'del', } export interface PersistencePutCommand { readonly type: PersistenceBatchTypes.put readonly key: K readonly value: V } export interface PersistenceDelCommand { readonly type: PersistenceBatchTypes.del readonly key: K } // Persistence Adapter export type CreatePersistenceAdapter = (params: PersistenceAdapterParams) => PersistenceAdapter export interface PersistenceAdapterParams { readonly persistenceID: string readonly persistencePath?: string readonly store?: any } export interface PersistenceIteratorOptions { gt?: K gte?: K lt?: K lte?: K reverse?: boolean limit?: number keys?: boolean values?: boolean keyAsBuffer?: boolean valueAsBuffer?: boolean } export interface PersistenceAdapter { readonly get: (key: K) => Promise readonly put: (key: K, value: V) => Promise readonly del: (key: K) => Promise readonly batch: (ops: PersistenceBatch) => Promise readonly createReadStream: (options?: PersistenceIteratorOptions) => NodeJS.ReadableStream readonly close: () => Promise readonly open: () => Promise } export interface PersistenceError extends Error { notFound?: boolean }