import { DidJson , WalletConfig , WalletCredentials , WalletRecord } from "./WalletInterface" ; import { NativeModules } from "react-native" ; import { RecordType } from "../utils/Helpers" ; import DatabaseServices from '../storage' ; import WalletStorageService from "./WalletStorageService" ; const { ArnimaSdk } = NativeModules ; class WalletService { async createWallet ( config , credentials , label ) { try { await ArnimaSdk . createWallet ( JSON . stringify ( config ) , JSON . stringify ( credentials ) ) ; return await this . createWalletDidStore ( config , credentials , { } , true , label ) ; } catch ( error ) { console . log ( 'WalletService - Create wallet error = ' , error ) ; throw ( error ) ; } } async createWalletDidStore ( config , credentials , didJson , createMasterSecret , label ) { const response = await ArnimaSdk . createAndStoreMyDid ( JSON . stringify ( config ) , JSON . stringify ( credentials ) , JSON . stringify ( didJson ) , createMasterSecret ) ; if ( response . length > 0 ) { const [ did , verkey , masterSecretId ] = response ; DatabaseServices . storeWallet ( { walletConfig : JSON . stringify ( config ) , walletCredentials : JSON . stringify ( credentials ) , label : label , serviceEndpoint : "" , routingKey : "" , publicDid : did , verKey : verkey , masterSecretId : masterSecretId , } ) ; const walletRecord = { walletConfig : config , walletCredentials : credentials , label : label , serviceEndpoint : "" , routingKey : "" , publicDid : did , verKey : verkey , masterSecretId : masterSecretId , } ; const walletRecordTags = { walletName : label , publicDid : did , verKey : verkey , } ; await WalletStorageService . addWalletRecord ( config , credentials , RecordType . MediatorAgent , '1' , JSON . stringify ( walletRecord ) , JSON . stringify ( walletRecordTags ) ) ; } return response ; } async getWallet ( ) { try { const response = await DatabaseServices . getWallet ( ) ; return response ; } catch ( error ) { console . log ( 'WalletService - Get wallet error = ' , error ) ; throw error ; } } async openWallet ( ) { try { const sdkDB = DatabaseServices . getWallet ( ) ; const response = await ArnimaSdk . openInitWallet ( sdkDB . walletConfig , sdkDB . walletCredentials ) ; return response ; } catch ( error ) { console . log ( 'WalletService - Open wallet error = ' , error ) ; throw error ; } } ; } export default new WalletService ( ) ;