import ThresholdKey from '@tkey/core' import WebStorageModule from '@tkey/web-storage' import TorusServiceProvider from '@tkey/service-provider-torus' import TorusStorageLayer from '@tkey/storage-layer-torus' import SecurityQuestionsModule from '@tkey/security-questions' import ShareTransferModule from '@tkey/share-transfer' import SeedPhraseModule, { MetamaskSeedPhraseFormat } from '@tkey/seed-phrase' import type { TorusServiceProviderArgs } from '@tkey/common-types' import type { TorusLoginResponse } from '@toruslabs/customauth' import type AuthcClient from './AuthcClient' export interface TKeyOptions { verifier: string } export default class TKey { directParams: TorusServiceProviderArgs['customAuthArgs'] thresholdKey: ThresholdKey serviceProvider: TorusServiceProvider constructor(public authc: AuthcClient, private options: TKeyOptions) { this.directParams = { baseUrl: `${window.location.origin}`, enableLogging: true, networkUrl: 'https://rpc.ankr.com/eth_ropsten', network: 'testnet' as any, redirectPathName: '', } this.init() } get keyDetails() { return this.thresholdKey.getKeyDetails() } async init() { const serviceProvider = new TorusServiceProvider({ customAuthArgs: this.directParams }) // 2. Initializing tKey const webStorageModule = new WebStorageModule() const securityQuestionsModule = new SecurityQuestionsModule() const shareTransferModule = new ShareTransferModule() const storageLayer = new TorusStorageLayer({ hostUrl: 'https://metadata.tor.us' }) const seedPhraseModule = new SeedPhraseModule([new MetamaskSeedPhraseFormat(this.directParams.networkUrl)]) this.thresholdKey = new ThresholdKey({ serviceProvider, storageLayer, modules: { seedPhrase: seedPhraseModule, webStorage: webStorageModule, securityQuestions: securityQuestionsModule, shareTransfer: shareTransferModule }, }) this.serviceProvider = this.thresholdKey.serviceProvider as TorusServiceProvider await serviceProvider.init({ skipInit: true, }) } async triggerLogin(): Promise { const tokens = await this.authc.getTokenSilently({ detailedResponse: true, ignoreCache: true }) return (this.serviceProvider as TorusServiceProvider).triggerLogin({ typeOfLogin: 'jwt', verifier: this.options.verifier, clientId: 'DO_NOT_NEED', jwtParams: { domain: this.authc.options.domain, id_token: tokens.id_token, }, }) .then(async (res) => { await this.thresholdKey.initialize({ // neverInitializeNewKey: true }) return res }) } }