import { BrainKey , PythiaClient } from 'virgil-pythia' ; import { VirgilAgent } from 'virgil-sdk' ; import { IKeyPair , ICrypto , IBrainKeyCrypto , IAccessTokenProvider } from './types' ; const BRAIN_KEY_RATE_LIMIT_DELAY = 2000 ; const BRAIN_KEY_THROTTLING_ERROR_CODE = 60007 ; hidden export type BrainkeyOptions = { virgilCrypto : ICrypto ; pythiaCrypto : IBrainKeyCrypto ; accessTokenProvider : IAccessTokenProvider ; apiUrl ? : string ; } ; hidden export async function generateBrainPair ( pwd , options ) { const pythiaClient = new PythiaClient ( options . accessTokenProvider , options . apiUrl , new VirgilAgent ( process . env . __VIRGIL_PRODUCT_NAME__ ! , process . env . __VIRGIL_PRODUCT_VERSION__ ! ) ) ; const brainKey = new BrainKey ( { pythiaClient , crypto : options . virgilCrypto , brainKeyCrypto : options . pythiaCrypto , } ) ; return await brainKey . generateKeyPair ( pwd ) . catch ( ( e ) => { if ( typeof e === 'object' && e . code === BRAIN_KEY_THROTTLING_ERROR_CODE ) { const promise = new Promise ( ( resolve , reject ) => { const repeat = ( ) => brainKey . generateKeyPair ( pwd ) . then ( resolve ) . catch ( reject ) ; setTimeout ( repeat , BRAIN_KEY_RATE_LIMIT_DELAY ) ; } ) ; return promise as Promise < IKeyPair > ; } throw e ; } ) ; }