import { FabricWallet } from '../src/FabricWallet' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import * as chai from 'chai' ; import * as chaiAsPromised from 'chai-as-promised' ; import { Wallet } from 'fabric-network' ; import { FabricIdentity } from 'ibm-blockchain-platform-common' ; import * as WalletMigration from 'fabric-wallet-migration' ; chai . use ( chaiAsPromised ) ; chai . use ( sinonChai ) ; const should = chai . should ( ) ; describe ( 'FabricWallet' , ( ) => { let mySandBox ; let putStub ; let removeStub ; let getStub ; let listStub ; let newFileSystemWalletStoreStub ; beforeEach ( async ( ) => { mySandBox = sinon . createSandbox ( ) ; putStub = mySandBox . stub ( Wallet . prototype , 'put' ) ; removeStub = mySandBox . stub ( Wallet . prototype , 'remove' ) ; getStub = mySandBox . stub ( Wallet . prototype , 'get' ) ; listStub = mySandBox . stub ( Wallet . prototype , 'list' ) ; newFileSystemWalletStoreStub = mySandBox . stub ( WalletMigration , 'newFileSystemWalletStore' ) . resolves ( { } ) ; } ) ; afterEach ( ( ) => { mySandBox . restore ( ) ; } ) ; describe ( 'importIdentity' , ( ) => { it ( 'should import identity' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/path' ) ; putStub . resolves ( ) ; await wallet . importIdentity ( '---CERT---' , '---KEY---' , 'identity1' , 'myMSP' ) ; putStub . should . have . been . calledOnceWithExactly ( 'identity1' , { credentials : { certificate : '---CERT---' , privateKey : '---KEY---' , } , mspId : 'myMSP' , type : 'X.509' , } ) ; } ) ; } ) ; describe ( 'removeIdentity' , ( ) => { it ( 'should remove identity' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/path' ) ; removeStub . resolves ( ) ; await wallet . removeIdentity ( 'identity1' ) ; removeStub . should . have . been . calledOnceWithExactly ( 'identity1' ) ; } ) ; } ) ; describe ( 'getWalletPath' , ( ) => { it ( 'should get wallet path' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; const result = wallet . getWalletPath ( ) ; result . should . equal ( 'tmp/myPath' ) ; } ) ; } ) ; describe ( 'getWallet' , ( ) => { it ( 'should get wallet' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; const result = wallet . getWallet ( ) ; should . exist ( result ) ; } ) ; } ) ; describe ( 'getIdentityNames' , ( ) => { it ( 'should get the identity names' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/path' ) ; listStub . resolves ( [ 'label1' , 'label2' , 'label3' ] ) ; const identityNames = await wallet . getIdentityNames ( ) ; identityNames . should . deep . equal ( [ 'label1' , 'label2' , 'label3' ] ) ; } ) ; } ) ; describe ( '#getIDs' , ( ) => { it ( 'should return all requested identities in wallet as an array of Fabric Identities' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; getStub . onFirstCall ( ) . resolves ( { credentials : { certificate : 'myCert' , privateKey : 'myKey' , } , mspId : 'myMSP' , type : 'X.509' , } ) ; getStub . onSecondCall ( ) . resolves ( { credentials : { certificate : 'myCert2' , privateKey : 'myKey2' , } , mspId : 'myMSP2' , type : 'X.509' , } ) ; const allIDs = [ { name : 'identity_a' , cert : Buffer . from ( 'myCert' ) . toString ( 'base64' ) , private_key : Buffer . from ( 'myKey' ) . toString ( 'base64' ) , msp_id : 'myMSP' } , { name : 'identity_b' , cert : Buffer . from ( 'myCert2' ) . toString ( 'base64' ) , private_key : Buffer . from ( 'myKey2' ) . toString ( 'base64' ) , msp_id : 'myMSP2' } ] ; const identities = await wallet . getIDs ( [ 'identity_a' , 'identity_b' ] ) ; identities . should . deep . equal ( allIDs ) ; } ) ; } ) ; describe ( '#getIdentities' , ( ) => { it ( 'should return any identities' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; listStub . resolves ( [ 'label1' , 'label2' , 'label3' ] ) ; getStub . onFirstCall ( ) . resolves ( { credentials : { certificate : 'myCert' , privateKey : 'myKey' , } , mspId : 'myMSP' , type : 'X.509' , } ) ; getStub . onSecondCall ( ) . resolves ( { credentials : { certificate : 'myCert2' , privateKey : 'myKey2' , } , mspId : 'myMSP2' , type : 'X.509' , } ) ; getStub . onThirdCall ( ) . resolves ( { credentials : { certificate : 'myCert3' , privateKey : 'myKey3' , } , mspId : 'myMSP3' , type : 'X.509' , } ) ; const identityOne = { cert : 'myCert' , private_key : 'myKey' , msp_id : 'myMSP' , name : 'label1' } ; const identityTwo = { cert : 'myCert2' , private_key : 'myKey2' , msp_id : 'myMSP2' , name : 'label2' } ; const identityThree = { cert : 'myCert3' , private_key : 'myKey3' , msp_id : 'myMSP3' , name : 'label3' } ; const identities = await wallet . getIdentities ( ) ; identities . should . deep . equal ( [ identityOne , identityTwo , identityThree ] ) ; } ) ; } ) ; describe ( '#getIdentity' , ( ) => { it ( 'should get the wanted identity' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; getStub . resolves ( { credentials : { certificate : 'myCert' , privateKey : 'myKey' , } , mspId : 'myMSP' , type : 'X.509' , } ) ; const identityOne = { cert : 'myCert' , private_key : 'myKey' , msp_id : 'myMSP' , name : 'label1' } ; const identity = await wallet . getIdentity ( 'label1' ) ; identity . should . deep . equal ( identityOne ) ; } ) ; } ) ; describe ( '#exists' , ( ) => { it ( 'should return true if the identity exists' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; getStub . resolves ( { credentials : { certificate : 'myCert' , privateKey : 'myKey' , } , mspId : 'myMSP' , type : 'X.509' , } ) ; const exists = await wallet . exists ( 'label1' ) ; exists . should . equal ( true ) ; } ) ; it ( 'should return false if the identity does not exists' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; getStub . resolves ( undefined ) ; const exists = await wallet . exists ( 'label1' ) ; exists . should . equal ( false ) ; } ) ; } ) ; describe ( 'migrateToV2Wallet' , ( ) => { it ( 'should write v2 identity file if v1 identities found' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; listStub . resolves ( [ 'identity1' ] ) ; getStub . resolves ( { credentials : { certificate : '---CERT---' , privateKey : '---KEY---' , } , mspId : 'myMSP' , type : 'X.509' , } ) ; putStub . resolves ( ) ; await wallet . migrateToV2Wallet ( ) ; newFileSystemWalletStoreStub . should . have . been . calledOnceWithExactly ( 'tmp/myPath' ) ; listStub . should . have . been . calledOnceWithExactly ( ) ; getStub . should . have . been . calledOnceWithExactly ( 'identity1' ) ; putStub . should . have . been . calledOnceWithExactly ( 'identity1' , { credentials : { certificate : '---CERT---' , privateKey : '---KEY---' , } , mspId : 'myMSP' , type : 'X.509' , } ) ; } ) ; it ( 'should not write v2 identity file if no v1 identities found' , async ( ) => { const wallet = await FabricWallet . newFabricWallet ( 'tmp/myPath' ) ; listStub . resolves ( [ 'identity1' ] ) ; getStub . resolves ( ) ; await wallet . migrateToV2Wallet ( ) ; newFileSystemWalletStoreStub . should . have . been . calledOnceWithExactly ( 'tmp/myPath' ) ; listStub . should . have . been . calledOnceWithExactly ( ) ; getStub . should . have . been . calledOnceWithExactly ( 'identity1' ) ; putStub . should . have . not . been . called ; } ) ; } ) ; } ) ;