import { type PortalError, type MpcErrorData } from '../../../types' export enum MpcErrorCodes { CLIENT_NOT_VERIFIED = 'CLIENT_NOT_VERIFIED', GOOGLE_UNAUTHENTICATED = 'GOOGLE_UNAUTHENTICATED', KEYCHAIN_UNAVAILABLE = 'KEYCHAIN_UNAVAILABLE', MPC_MODULE_NOT_FOUND = 'MPC_MODULE_NOT_FOUND', STORAGE_UNAVAILABLE = 'STORAGE_UNAVAILABLE', UNABLE_TO_READ_SIGNING_STORAGE = 'UNABLE_TO_READ_SIGNING_STORAGE', UNEXPECTED_ERROR = 'UNEXPECTED_ERROR', UNSUPPORTED_MPC_VERSION = 'UNSUPPORTED_MPC_VERSION', UNSUPPORTED_STORAGE_METHOD = 'UNSUPPORTED_STORAGE_METHODS', WALLET_MODIFICATION_ALREADY_IN_PROGRESS = 'WALLET_MODIFICATION_ALREADY_IN_PROGRESS', } const MpcErrorMetadata: Record = { [MpcErrorCodes.CLIENT_NOT_VERIFIED]: { code: MpcErrorCodes.CLIENT_NOT_VERIFIED, message: (_?: string) => `[PortalMpc] Unable to verify client`, }, [MpcErrorCodes.GOOGLE_UNAUTHENTICATED]: { code: MpcErrorCodes.GOOGLE_UNAUTHENTICATED, message: (_?: string) => `[PortalMpc] Unable to authenticate with Google`, }, [MpcErrorCodes.KEYCHAIN_UNAVAILABLE]: { code: MpcErrorCodes.KEYCHAIN_UNAVAILABLE, message: (_?: string) => `[PortalMpc] Keychain is unavailable`, }, [MpcErrorCodes.MPC_MODULE_NOT_FOUND]: { code: MpcErrorCodes.MPC_MODULE_NOT_FOUND, message: (_?: string) => `[PortalMpc] The MPC module could not be found. This is usually an issue with React Native linking. Please verify that the 'PortalReactNative' module is properly linked to this project.`, }, [MpcErrorCodes.STORAGE_UNAVAILABLE]: { code: MpcErrorCodes.STORAGE_UNAVAILABLE, message: (_?: string) => `[PortalMpc] Storage is unavailable`, }, [MpcErrorCodes.UNABLE_TO_READ_SIGNING_STORAGE]: { code: MpcErrorCodes.UNABLE_TO_READ_SIGNING_STORAGE, message: (_?: string) => `[PortalMpc] Unable to read signing share from storage`, }, [MpcErrorCodes.UNEXPECTED_ERROR]: { code: MpcErrorCodes.UNEXPECTED_ERROR, message: (error?: string) => `[PortalMpc] Unexpected error on ${error}`, }, [MpcErrorCodes.UNSUPPORTED_MPC_VERSION]: { code: MpcErrorCodes.UNSUPPORTED_MPC_VERSION, message: (version?: string) => `[PortalMpc] Unsupported MPC version ${version}`, }, [MpcErrorCodes.UNSUPPORTED_STORAGE_METHOD]: { code: MpcErrorCodes.UNSUPPORTED_STORAGE_METHOD, message: (method?: string) => `[PortalMpc] Unsupported storage method ${method}`, }, [MpcErrorCodes.WALLET_MODIFICATION_ALREADY_IN_PROGRESS]: { code: MpcErrorCodes.WALLET_MODIFICATION_ALREADY_IN_PROGRESS, message: (_?: string) => `[PortalMpc] Wallet modification already in progress`, }, } export class MpcError extends Error { public code: string public context?: string constructor(code: MpcErrorCodes, context?: string) { const error = MpcErrorMetadata[code] // Init the actual error super(error.message(context)) // Custom error context this.code = error?.code || '999' if (context) { this.context = context } } } export enum PortalErrorCodes { InvalidRankAndThreshold = 100, FailedToStartDkg = 101, FailedToFinishDkg = 102, FailedToConvertDkgResult = 103, FailedToStartReshare = 104, FailedToFinishReshare = 105, FailedToConvertReshareResult = 106, FailedToStartSign = 107, FailedToFinishSign = 108, FailedToConvertSignResult = 109, DkgProcessError = 110, FailedToParseSocketMessage = 111, FailedToAddMessageToChannel = 112, RefreshShareMismatch = 113, RefreshProcessError = 114, FailedToGetPaillierKeys = 115, SigningShareMismatch = 116, SigningProcessError = 117, // Network Errors FailedToSendSignRequest = 200, BadRequest = 201, InvalidApiKey = 202, NotFound = 203, FailedToReceive101StatusCode = 204, FailedToCreateWebsocket = 205, DkgNetworkError = 206, FailedToReadPartialPublicKeyFromSocket = 207, FailedToWritePartialPublicKeyToSocket = 208, FailedToReadFromSocketInListen = 209, RefreshNetworkError = 210, SigningNetworkError = 211, BlockedAddressByDefault = 212, BlockedAddressByEnvironment = 213, // General Errors FailedToComputeEthAddress = 300, FailedToCreateShareObject = 301, IncorrectInputToSign = 302, FailedToConvertPublicKeyBytes = 303, FailedToComputeSignature = 304, FailedToParseInputShareObject = 305, IncorrectShareObjectType = 306, FailedToConvertStringToBigInt = 307, FailedToPopulateTransaction = 308, FailedToBuildMessage = 309, UnsupportedEthMethod = 310, NodeRpcError = 311, FailedToInitializePublicKey = 312, FailedToCalculateSelfPublicKey = 313, FailedToUnmarshalPartialPublicKeyFromSocket = 314, FailedToMarshalPartialPublicKey = 315, // Encrypt/Decrypt Errors FailedToCreateRandomDekBytes = 400, FailedToCreateCipherBlock = 401, FailedToCreateGcmCipher = 402, FailedToReadNonceIntoBuffer = 403, FailedToDecodeKeyHexString = 404, FailedToDecodeCipherHexString = 405, FailedToGetNonceSize = 406, FailedToDecryptCipher = 407, } export const PortalErrorCodeTypes = { MpcErrors: Array.from({ length: 100 }, (_, i) => i + 100), NetworkErrors: Array.from({ length: 100 }, (_, i) => i + 200), GeneralErrors: Array.from({ length: 100 }, (_, i) => i + 300), EncryptDecryptErrors: Array.from({ length: 100 }, (_, i) => i + 400), } export class PortalMpcError extends Error { code: PortalErrorCodes public constructor(error: PortalError) { super(error.message) this.code = error?.code || 999 } } export default MpcError