'use strict' ; import { Wallet , X509Identity , Wallets , WalletStore , Identity } from 'fabric-network' ; import { FabricIdentity , IFabricWallet } from 'ibm-blockchain-platform-common' ; import * as WalletMigration from 'fabric-wallet-migration' ; export class FabricWallet implements IFabricWallet { public static async newFabricWallet ( walletPath ) { const wallet = await Wallets . newFileSystemWallet ( walletPath ) ; return new FabricWallet ( walletPath , wallet ) ; } private wallet : Wallet ; private walletPath : string ; private constructor ( walletPath , wallet ) { this . walletPath = walletPath ; this . wallet = wallet ; } public getWallet ( ) { return this . wallet ; } public async importIdentity ( certificate , privateKey , identityName , mspid ) { const identity = { credentials : { certificate : certificate , privateKey : privateKey , } , mspId : mspid , type : 'X.509' , } ; await this . wallet . put ( identityName , identity ) ; } public async getIdentityNames ( ) { return this . wallet . list ( ) ; } public async getIDs ( identities ) { const allIDs = [ ] ; for ( const identity of identities ) { const ID = await this . wallet . get ( identity ) as X509Identity ; const cert = Buffer . from ( ID . credentials . certificate ) . toString ( 'base64' ) ; const privKey = Buffer . from ( ID . credentials . privateKey ) . toString ( 'base64' ) ; const fabricIdentity = new FabricIdentity ( identity , cert , privKey , ID . mspId ) ; allIDs . push ( fabricIdentity ) ; } return allIDs ; } public async getIdentities ( ) { const identityNames = await this . getIdentityNames ( ) ; const identities = [ ] ; for ( const identityName of identityNames ) { const identity = await this . wallet . get ( identityName ) as X509Identity ; const fabricIdentity = new FabricIdentity ( identityName , identity . credentials . certificate , identity . credentials . privateKey , identity . mspId ) ; identities . push ( fabricIdentity ) ; } return identities ; } public async getIdentity ( identityName ) { const identity = await this . wallet . get ( identityName ) as X509Identity ; return new FabricIdentity ( identityName , identity . credentials . certificate , identity . credentials . privateKey , identity . mspId ) ; } public getWalletPath ( ) { return this . walletPath ; } public async exists ( identityName ) { const identity = await this . wallet . get ( identityName ) ; return ! ! identity ; } public removeIdentity ( identityName ) { return this . wallet . remove ( identityName ) ; } public async migrateToV2Wallet ( ) { const walletStore = await WalletMigration . newFileSystemWalletStore ( this . walletPath ) ; const oldWallet = new Wallet ( walletStore ) ; const identityLabels = await oldWallet . list ( ) ; for ( const label of identityLabels ) { const identity = await oldWallet . get ( label ) ; if ( identity ) { await this . wallet . put ( label , identity ) ; } } } }