@@ -14,6 +14,7 @@ import {
1414 FabricContractInvocationType ,
1515 FabricSigningCredential ,
1616 FileBase64 ,
17+ IPluginLedgerConnectorFabricOptions ,
1718 PluginLedgerConnectorFabric ,
1819} from "@hyperledger/cactus-plugin-ledger-connector-fabric" ;
1920import {
@@ -39,6 +40,11 @@ import fs from "fs-extra";
3940import { getUserFromPseudonim } from "./utils" ;
4041import CryptoMaterial from "../../../crypto-material/crypto-material.json" ;
4142import ExampleOntology from "../../json/ontologies/ontology-satp-erc20-interact-fabric.json" ;
43+ import { PeerCerts } from "@hyperledger/cactus-test-tooling/dist/lib/main/typescript/fabric/fabric-test-ledger-v1" ;
44+ import {
45+ TargetOrganization ,
46+ FabricConfigJSON ,
47+ } from "@hyperledger/cactus-plugin-satp-hermes" ;
4248
4349export class FabricEnvironment {
4450 public static readonly FABRIC_NETWORK_ID : string = "FabricLedgerNetwork" ;
@@ -72,6 +78,10 @@ export class FabricEnvironment {
7278 private fabricKeychainPlugin ?: PluginKeychainMemory ;
7379 private approveAddress ?: string ;
7480
81+ private peer0Org1Certs : PeerCerts | undefined ;
82+ private peer0Org2Certs : PeerCerts | undefined ;
83+ private coreFile : FileBase64 | undefined ;
84+
7585 private readonly logLevel : LogLevelDesc ;
7686
7787 public constructor ( logLevel : LogLevelDesc , network ?: string ) {
@@ -178,21 +188,17 @@ export class FabricEnvironment {
178188 asLocalhost : true ,
179189 } ;
180190
181- const connectorOptions = {
191+ const connectorOptions : IPluginLedgerConnectorFabricOptions = {
182192 instanceId : uuidv4 ( ) ,
183- dockerBinary : "/usr/local/bin/docker" ,
184- peerBinary : "/fabric-samples/bin/peer" ,
185- goBinary : "/usr/local/go/bin/go" ,
186193 pluginRegistry,
187- cliContainerEnv : FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 ,
188- sshConfig : this . sshConfig ,
189194 logLevel : this . logLevel ,
190195 connectionProfile : this . connectionProfileOrg1 ,
191196 discoveryOptions : this . discoveryOptions ,
192197 eventHandlerOptions : {
193198 strategy : DefaultEventHandlerStrategy . NetworkScopeAllfortx ,
194199 commitTimeout : 300 ,
195200 } ,
201+ dockerNetworkName : this . dockerNetwork ,
196202 } ;
197203
198204 this . connector = new PluginLedgerConnectorFabric ( connectorOptions ) ;
@@ -201,6 +207,22 @@ export class FabricEnvironment {
201207 keychainId : keychainId ,
202208 keychainRef : keychainEntryKey4 ,
203209 } ;
210+
211+ this . peer0Org1Certs = await this . ledger . getPeerOrgCertsAndConfig (
212+ "org1" ,
213+ "peer0" ,
214+ ) ;
215+ this . peer0Org2Certs = await this . ledger . getPeerOrgCertsAndConfig (
216+ "org2" ,
217+ "peer0" ,
218+ ) ;
219+
220+ const filePath = path . join ( __dirname , "../../yml/resources/core.yml" ) ;
221+ const buffer = await fs . readFile ( filePath ) ;
222+ this . coreFile = {
223+ body : buffer . toString ( "base64" ) ,
224+ filename : "core.yaml" ,
225+ } ;
204226 }
205227
206228 // Deploys smart contracts and sets up configurations for testing
@@ -298,23 +320,43 @@ export class FabricEnvironment {
298320 } ) ;
299321 }
300322
323+ if ( ! this . peer0Org1Certs || ! this . peer0Org2Certs || ! this . coreFile ) {
324+ throw new Error ( "Peer certificates are not defined" ) ;
325+ }
326+
301327 const res = await this . connector ?. deployContract ( {
302328 channelId : this . fabricChannelName ,
303329 ccVersion : "1.0.0" ,
304330 sourceFiles : satpSourceFiles ,
305331 ccName : FabricEnvironment . SATP_CONTRACT_NAME ,
306332 targetOrganizations : [
307- FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 ,
308- FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_2 ,
333+ {
334+ CORE_PEER_LOCALMSPID :
335+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 . CORE_PEER_LOCALMSPID ,
336+ CORE_PEER_ADDRESS :
337+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 . CORE_PEER_ADDRESS ,
338+ CORE_PEER_MSPCONFIG : this . peer0Org1Certs . mspConfig ,
339+ CORE_PEER_TLS_ROOTCERT : this . peer0Org1Certs . peerTlsCert ,
340+ ORDERER_TLS_ROOTCERT : this . peer0Org1Certs . ordererTlsRootCert ,
341+ } ,
342+ {
343+ CORE_PEER_LOCALMSPID :
344+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_2 . CORE_PEER_LOCALMSPID ,
345+ CORE_PEER_ADDRESS :
346+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_2 . CORE_PEER_ADDRESS ,
347+ CORE_PEER_MSPCONFIG : this . peer0Org2Certs . mspConfig ,
348+ CORE_PEER_TLS_ROOTCERT : this . peer0Org2Certs . peerTlsCert ,
349+ ORDERER_TLS_ROOTCERT : this . peer0Org2Certs . ordererTlsRootCert ,
350+ } ,
309351 ] ,
310- caFile :
311- FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 . ORDERER_TLS_ROOTCERT_FILE ,
352+ caFile : this . peer0Org1Certs . ordererTlsRootCert ,
312353 ccLabel : "satp-contract" ,
313354 ccLang : ChainCodeProgrammingLanguage . Typescript ,
314355 ccSequence : 1 ,
315356 orderer : "orderer.example.com:7050" ,
316357 ordererTLSHostnameOverride : "orderer.example.com" ,
317358 connTimeout : 60 ,
359+ coreYamlFile : this . coreFile ,
318360 } ) ;
319361
320362 if ( ! res ) {
@@ -556,28 +598,133 @@ export class FabricEnvironment {
556598
557599 return parseInt ( response . functionOutput ) ;
558600 }
559- public async createFabricDockerConfig ( ) : Promise < INetworkOptions > {
601+
602+ public async createFabricDockerConfig (
603+ testFilesDirectory : string ,
604+ ) : Promise < FabricConfigJSON & INetworkOptions > {
605+ if ( ! this . peer0Org1Certs || ! this . peer0Org2Certs || ! this . coreFile ) {
606+ throw new Error ( "Peer certificates are not defined" ) ;
607+ }
608+
609+ const testFileCerts = path . join ( testFilesDirectory , "certs" ) ;
610+
611+ if ( ! fs . existsSync ( testFileCerts ) ) {
612+ fs . mkdirSync ( testFileCerts , { recursive : true } ) ;
613+ }
614+
615+ for ( const org of [ "org1" , "org2" ] ) {
616+ if ( ! fs . existsSync ( path . join ( testFileCerts , "certs" , org ) ) ) {
617+ fs . mkdirSync ( path . join ( testFileCerts , org ) , {
618+ recursive : true ,
619+ } ) ;
620+ } else {
621+ // Clear the directory if it already exists
622+ fs . emptyDirSync ( path . join ( testFileCerts , org ) ) ;
623+ }
624+ }
625+
626+ // Write peer certs and core.yaml to files in the testFilesDirectory
627+ const org1Dir = path . join ( testFileCerts , "org1" ) ;
628+ const org2Dir = path . join ( testFileCerts , "org2" ) ;
629+ const coreYamlPath = path . join ( testFileCerts , "core.yaml" ) ;
630+
631+ // Org1 certs
632+ const org1MspConfigPath = path . join ( org1Dir , "msp" ) ;
633+ const org1PeerTlsCertPath = path . join ( org1Dir , "peerTlsCert.pem" ) ;
634+ const org1OrdererTlsRootCertPath = path . join (
635+ org1Dir ,
636+ "ordererTlsRootCert.pem" ,
637+ ) ;
638+
639+ // Org2 certs
640+ const org2MspConfigPath = path . join ( org2Dir , "msp" ) ;
641+ const org2PeerTlsCertPath = path . join ( org2Dir , "peerTlsCert.pem" ) ;
642+ const org2OrdererTlsRootCertPath = path . join (
643+ org2Dir ,
644+ "ordererTlsRootCert.pem" ,
645+ ) ;
646+
647+ // mspConfig is a folder of files with relative paths, so we need to write each file
648+ for ( const peerCerts of [
649+ { certs : this . peer0Org1Certs , mspPath : org1MspConfigPath } ,
650+ { certs : this . peer0Org2Certs , mspPath : org2MspConfigPath } ,
651+ ] ) {
652+ for ( const file of peerCerts . certs . mspConfig ) {
653+ const destPath = path . join (
654+ peerCerts . mspPath ,
655+ file . filepath || "" ,
656+ file . filename ,
657+ ) ;
658+ const destDir = path . dirname ( destPath ) ;
659+ if ( ! fs . existsSync ( destDir ) ) {
660+ fs . mkdirSync ( destDir , { recursive : true } ) ;
661+ }
662+ // Ensure fileContent is a string or Buffer
663+ await fs . writeFile ( destPath , Buffer . from ( file . body , "base64" ) ) ;
664+ }
665+ }
666+
667+ await fs . writeFile ( org1PeerTlsCertPath , this . peer0Org1Certs . peerTlsCert ) ;
668+ await fs . writeFile (
669+ org1OrdererTlsRootCertPath ,
670+ this . peer0Org1Certs . ordererTlsRootCert ,
671+ ) ;
672+
673+ await fs . writeFile ( org2PeerTlsCertPath , this . peer0Org2Certs . peerTlsCert ) ;
674+ await fs . writeFile (
675+ org2OrdererTlsRootCertPath ,
676+ this . peer0Org2Certs . ordererTlsRootCert ,
677+ ) ;
678+
679+ // core.yaml
680+ await fs . writeFile ( coreYamlPath , Buffer . from ( this . coreFile . body , "base64" ) ) ;
681+
682+ // targetOrganizations array
683+ const basePath = path . join ( "/opt/cacti/satp-hermes/config" , "certs" ) ;
684+ const targetOrganizations : TargetOrganization [ ] = [
685+ {
686+ CORE_PEER_LOCALMSPID :
687+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 . CORE_PEER_LOCALMSPID ,
688+ CORE_PEER_ADDRESS :
689+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 . CORE_PEER_ADDRESS ,
690+ CORE_PEER_MSPCONFIG_PATH : path . join ( basePath , "org1/msp" ) ,
691+ CORE_PEER_TLS_ROOTCERT_PATH : path . join (
692+ basePath ,
693+ "/org1/peerTlsCert.pem" ,
694+ ) ,
695+ ORDERER_TLS_ROOTCERT_PATH : path . join (
696+ basePath ,
697+ "/org1/ordererTlsRootCert.pem" ,
698+ ) ,
699+ } ,
700+ {
701+ CORE_PEER_LOCALMSPID :
702+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_2 . CORE_PEER_LOCALMSPID ,
703+ CORE_PEER_ADDRESS :
704+ FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_2 . CORE_PEER_ADDRESS ,
705+ CORE_PEER_MSPCONFIG_PATH : path . join ( basePath , "org2/msp" ) ,
706+ CORE_PEER_TLS_ROOTCERT_PATH : path . join (
707+ basePath ,
708+ "/org2/peerTlsCert.pem" ,
709+ ) ,
710+ ORDERER_TLS_ROOTCERT_PATH : path . join (
711+ basePath ,
712+ "/org2/ordererTlsRootCert.pem" ,
713+ ) ,
714+ } ,
715+ ] ;
716+ this . peer0Org2Certs ;
560717 return {
561718 networkIdentification : this . network ,
562719 userIdentity : this . bridgeIdentity ,
563720 channelName : this . fabricChannelName ,
564- targetOrganizations : [
565- FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 ,
566- FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_2 ,
567- ] ,
568- caFile :
569- FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_2 . ORDERER_TLS_ROOTCERT_FILE ,
570- ccSequence : 1 ,
721+ targetOrganizations : targetOrganizations ,
722+ caFilePath : path . join ( basePath , "/org2/ordererTlsRootCert.pem" ) ,
571723 orderer : "orderer.example.com:7050" ,
572724 ordererTLSHostnameOverride : "orderer.example.com" ,
573725 connTimeout : 60 ,
574726 mspId : this . bridgeMSPID ,
575727 connectorOptions : {
576- dockerBinary : "/usr/local/bin/docker" ,
577- peerBinary : "/fabric-samples/bin/peer" ,
578- goBinary : "/usr/local/go/bin/go" ,
579- cliContainerEnv : FABRIC_25_LTS_FABRIC_SAMPLES_ENV_INFO_ORG_1 ,
580- sshConfig : await this . ledger . getSshConfig ( false ) ,
581728 connectionProfile : await this . ledger . getConnectionProfileOrgX (
582729 "org2" ,
583730 false ,
@@ -592,7 +739,8 @@ export class FabricEnvironment {
592739 } ,
593740 } ,
594741 claimFormats : [ ClaimFormat . DEFAULT ] ,
595- } as INetworkOptions ;
742+ coreYamlFilePath : path . join ( basePath , "core.yaml" ) ,
743+ } ;
596744 }
597745
598746 public async giveRoleToBridge ( mspID : string ) : Promise < void > {
0 commit comments