import type { CustomAuthArgs } from "@oraichain/customauth"; import BN from "bn.js"; import { Point, Polynomial, PublicPolynomial, PublicPolynomialMap, PublicShare, PublicSharePolyIDShareIndexMap, Share, ShareMap, ShareStore, ShareStoreMap, ShareStorePolyIDShareIndexMap, } from "../base"; import { BNString, EncryptedMessage, IBlsdkgSignable, ISerializable, IServiceProvider, IStorageLayer, PolyIDAndShares, PolynomialID, ShareDescriptionMap, } from "./commonTypes"; export interface IModule { moduleName: string; // eslint-disable-next-line no-use-before-define setModuleReferences(api: ITKeyApi): void; // called to initialize a module on the main TBSDK. // currenty called immediately after the base metadata has been set on the SDK initialize(): Promise; } // @flow export type ModuleMap = { [moduleName: string]: IModule; }; export type RefreshMiddlewareMap = { [moduleName: string]: (generalStore: unknown, oldShareStores: ShareStoreMap, newShareStores: ShareStoreMap) => unknown; }; export type ReconstructKeyMiddlewareMap = { [moduleName: string]: () => Promise; }; export type ShareSerializationMiddleware = { serialize: (share: BN, type: string) => Promise; deserialize: (serializedShare: unknown, type: string) => Promise; }; export interface IMetadata extends ISerializable { pubKey: Point; publicPolynomials: PublicPolynomialMap; publicShares: PublicSharePolyIDShareIndexMap; polyIDList: PolyIDAndShares[]; generalStore: { [moduleName: string]: unknown; }; tkeyStore: { [moduleName: string]: unknown; }; scopedStore: { [moduleName: string]: unknown; }; nonce: number; getShareIndexesForPolynomial(polyID: PolynomialID): string[]; getLatestPublicPolynomial(): PublicPolynomial; addPublicShare(polynomialID: PolynomialID, publicShare: PublicShare): void; setGeneralStoreDomain(key: string, obj: unknown): void; getGeneralStoreDomain(key: string): unknown; deleteGeneralStoreDomain(key: string): unknown; setTkeyStoreDomain(key: string, arr: unknown): void; getTkeyStoreDomain(key: string): unknown; addFromPolynomialAndShares(polynomial: Polynomial, shares: Array | ShareMap): void; setScopedStore(domain: string, data: unknown): void; getEncryptedShare(shareStore: ShareStore): Promise; getShareDescription(): ShareDescriptionMap; shareToShareStore(share: BN): ShareStore; addShareDescription(shareIndex: string, description: string): void; deleteShareDescription(shareIndex: string, description: string): void; updateShareDescription(shareIndex: string, oldDescription: string, newDescription: string): void; clone(): IMetadata; } export type InitializeNewKeyResult = { privKey: BN; deviceShare?: ShareStore; userShare?: ShareStore; }; export type ReconstructedKeyResult = { privKey: BN; seedPhrase?: BN[]; allKeys?: BN[]; }; export type CatchupToLatestShareResult = { latestShare: ShareStore; shareMetadata: IMetadata; }; export type GenerateNewShareResult = { newShareStores: ShareStoreMap; newShareIndex: BN; }; export type DeleteShareResult = { newShareStores: ShareStoreMap; }; export type RefreshSharesResult = { shareStores: ShareStoreMap; }; export type KeyDetails = { pubKey: Point; requiredShares: number; threshold: number; totalShares: number; shareDescriptions: ShareDescriptionMap; }; export type TKeyArgs = { enableLogging?: boolean; modules?: ModuleMap; serviceProvider?: IServiceProvider; blsDkgPackage?: IBlsdkgSignable; storageLayer?: IStorageLayer; customAuthArgs?: CustomAuthArgs; manualSync?: boolean; serverTimeOffset?: number; }; export interface SecurityQuestionStoreArgs { nonce: BNString; shareIndex: BNString; sqPublicShare: PublicShare; polynomialID: PolynomialID; questions: string; } export interface TkeyStoreDataArgs { [key: string]: unknown; } export interface TkeyStoreArgs { data: TkeyStoreDataArgs; } export interface ShareTransferStorePointerArgs { pointer: BNString; } export type BufferObj = { type: string; data: number[]; }; export interface ShareRequestArgs { encPubKey: unknown; encShareInTransit: EncryptedMessage; availableShareIndexes: string[]; userAgent: string; userIp: string; timestamp: number; } export type TkeyStoreItemType = { id: string; }; export type ISeedPhraseStore = TkeyStoreItemType & { seedPhrase: string; type: string; }; export type ISQAnswerStore = TkeyStoreItemType & { answer: string; }; export type ISeedPhraseStoreWithKeys = ISeedPhraseStore & { keys: BN[]; }; export type MetamaskSeedPhraseStore = ISeedPhraseStore & { numberOfWallets: number; }; export type IPrivateKeyStore = TkeyStoreItemType & { privateKey: BN; type: string; }; export interface ISeedPhraseFormat { type: string; validateSeedPhrase(seedPhrase: string): boolean; deriveKeysFromSeedPhrase(seedPhraseStore: ISeedPhraseStore): Promise; createSeedPhraseStore(seedPhrase?: string): Promise; } export interface IPrivateKeyFormat { privateKey: BN; type: string; validatePrivateKey(privateKey: BN): boolean; createPrivateKeyStore(privateKey: BN): IPrivateKeyStore; } export interface IAuthMetadata { metadata: IMetadata; privKey?: BN; } export interface IMessageMetadata { message: string; dateAdded?: number; } export type IAuthMetadatas = IAuthMetadata[]; export type ShareStores = ShareStore[]; export type IMessageMetadatas = IMessageMetadata[]; export type LocalTransitionShares = BN[]; export type LocalTransitionData = [...IAuthMetadatas, ...ShareStores, ...IMessageMetadatas]; export type LocalMetadataTransitions = [LocalTransitionShares, LocalTransitionData]; export interface ITKeyApi { getMetadata(): IMetadata; getStorageLayer(): IStorageLayer; initialize(params: { input?: ShareStore; importKey?: BN; neverInitializeNewKey?: boolean }): Promise; catchupToLatestShare(params: { shareStore: ShareStore; polyID?: string; includeLocalMetadataTransitions?: boolean; }): Promise; _syncShareMetadata(adjustScopedStore?: (ss: unknown) => unknown): Promise; inputShareStoreSafe(shareStore: ShareStore, autoUpdateMetadata?: boolean): Promise; _setDeviceStorage(storeDeviceStorage: (deviceShareStore: ShareStore) => Promise): void; addShareDescription(shareIndex: string, description: string, updateMetadata?: boolean): Promise; _addRefreshMiddleware( moduleName: string, middleware: (generalStore: unknown, oldShareStores: ShareStoreMap, newShareStores: ShareStoreMap) => unknown ): void; _addReconstructKeyMiddleware(moduleName: string, middleware: () => Promise>): void; _addShareSerializationMiddleware( serialize: (share: BN, type: string) => Promise, deserialize: (serializedShare: unknown, type: string) => Promise ): void; generateNewShare(): Promise; outputShareStore(shareIndex: BNString, polyID?: string): ShareStore; inputShare(share: unknown, type?: string): Promise; outputShare(shareIndex: BNString, type?: string): Promise; inputShareStore(shareStore: ShareStore): void; deleteShare(shareIndex: BNString): Promise; encrypt(data: Buffer): Promise; decrypt(encryptedMesage: EncryptedMessage): Promise; getTKeyStoreItem(moduleName: string, id: string): Promise; getTKeyStore(moduleName: string): Promise; _deleteTKeyStoreItem(moduleName: string, id: string): Promise; _setTKeyStoreItem(moduleName: string, data: TkeyStoreItemType, updateMetadata?: boolean): Promise; } export interface ITKey extends ITKeyApi, ISerializable { modules: ModuleMap; enableLogging: boolean; serviceProvider: IServiceProvider; shares: ShareStorePolyIDShareIndexMap; privKey: BN; _localMetadataTransitions: LocalMetadataTransitions; manualSync: boolean; _refreshMiddleware: RefreshMiddlewareMap; _reconstructKeyMiddleware: ReconstructKeyMiddlewareMap; _shareSerializationMiddleware: ShareSerializationMiddleware; initialize(params: { input?: ShareStore; importKey?: BN; neverInitializeNewKey?: boolean }): Promise; reconstructKey(): Promise; reconstructLatestPoly(): Polynomial; _refreshShares(threshold: number, newShareIndexes: Array, previousPolyID: PolynomialID): Promise; _initializeNewKey(params: { userInput?: BN; initializeModules?: boolean }): Promise; _setKey(privKey: BN): void; getKeyDetails(): KeyDetails; }