import BdkYaml from '../bdkYaml' ; interface ConfigtxInterface { Organizations : ( PeerOrganizationInterface | OrdererOrganizationInterface ) [ ] ; Profiles : { [ profileName ] : SystemChannelProfileInterface | ApplicationChannelProfileInterface ; } ; } interface OrganizationInterface { Name : string ; ID : string ; MSPDir : string ; } export interface OrdererOrganizationInterface extends OrganizationInterface { Policies : { Readers : PolicyInterface ; Writers : PolicyInterface ; Admins : PolicyInterface ; } ; OrdererEndpoints : string [ ] ; } export interface PeerOrganizationInterface extends OrganizationInterface { Policies : { Readers : PolicyInterface ; Writers : PolicyInterface ; Admins : PolicyInterface ; Endorsement : PolicyInterface ; } ; AnchorPeers : { Host : string ; Port : number ; } [ ] ; } interface ChannelCapabilityInterface { V2_0 : boolean ; } interface OrdererCapabilityInterface { V2_0 : boolean ; } interface ApplicationCapabilityInterface { V2_0 : boolean ; } interface ApplicationInterface { Organizations : PeerOrganizationInterface [ ] ; Policies : { Readers : PolicyInterface ; Writers : PolicyInterface ; Admins : PolicyInterface ; LifecycleEndorsement : PolicyInterface ; Endorsement : PolicyInterface ; } ; Capabilities : ApplicationCapabilityInterface ; } const applicationDefaults = { Organizations : [ ] , Policies : { Readers : { Type : 'ImplicitMeta' , Rule : 'ANY Readers' , } , Writers : { Type : 'ImplicitMeta' , Rule : 'ANY Writers' , } , Admins : { Type : 'ImplicitMeta' , Rule : 'MAJORITY Admins' , } , LifecycleEndorsement : { Type : 'ImplicitMeta' , Rule : 'MAJORITY Endorsement' , } , Endorsement : { Type : 'ImplicitMeta' , Rule : 'MAJORITY Endorsement' , } , } , Capabilities : { V2_0 : true , } , } ; interface OrdererInterface { OrdererType : string ; Addresses : string [ ] ; EtcdRaft : { Consenters : EtcdRaftConsentersInterface [ ] ; } ; BatchTimeout : string ; BatchSize : BatchSizeInterface ; Organizations : OrdererOrganizationInterface [ ] ; Policies : { Readers : PolicyInterface ; Writers : PolicyInterface ; Admins : PolicyInterface ; BlockValidation : PolicyInterface ; } ; Capabilities : OrdererCapabilityInterface ; } interface EtcdRaftConsentersInterface { Host : string ; Port : number ; ClientTLSCert : string ; ServerTLSCert : string ; } interface BatchSizeInterface { MaxMessageCount : number ; AbsoluteMaxBytes : string ; PreferredMaxBytes : string ; } const ordererDefaults = { OrdererType : 'etcdraft' , Addresses : [ ] , EtcdRaft : { Consenters : [ ] , } , BatchTimeout : '2s' , BatchSize : { MaxMessageCount : 10 , AbsoluteMaxBytes : '99 MB' , PreferredMaxBytes : '512 KB' , } , Organizations : [ ] , Capabilities : { V2_0 : true , } , Policies : { Readers : { Type : 'ImplicitMeta' , Rule : 'ANY Readers' , } , Writers : { Type : 'ImplicitMeta' , Rule : 'ANY Writers' , } , Admins : { Type : 'ImplicitMeta' , Rule : 'MAJORITY Admins' , } , BlockValidation : { Type : 'ImplicitMeta' , Rule : 'ANY Writers' , } , } , } ; interface ChannelInterface { Policies : { Readers : PolicyInterface ; Writers : PolicyInterface ; Admins : PolicyInterface ; } ; Capabilities : ChannelCapabilityInterface ; } const channelDefaults = { Policies : { Readers : { Type : 'ImplicitMeta' , Rule : 'ANY Readers' , } , Writers : { Type : 'ImplicitMeta' , Rule : 'ANY Writers' , } , Admins : { Type : 'ImplicitMeta' , Rule : 'MAJORITY Admins' , } , } , Capabilities : { V2_0 : true , } , } ; interface SystemChannelProfileInterface extends ChannelInterface { Orderer : OrdererInterface ; Consortiums : ConsortiumsInterface ; } interface ConsortiumsInterface { [ consortiumName ] : { Organizations : PeerOrganizationInterface [ ] ; } ; } interface ApplicationChannelProfileInterface extends ChannelInterface { Consortium : string ; Application : ApplicationInterface ; } interface PolicyInterface { Type : 'Signature' | 'ImplicitMeta' ; Rule : string ; } export interface ConfigtxOrgs { ordererOrgs : { [ orgName ] : OrdererOrganizationInterface ; } ; peerOrgs : { [ orgName ] : PeerOrganizationInterface ; } ; } class ConfigtxYaml extends BdkYaml < ConfigtxInterface > { public ordererOrgs : { [ orgName ] : OrdererOrganizationInterface ; } ; public peerOrgs : { [ orgName ] : PeerOrganizationInterface ; } ; constructor ( value ? ) { const val = value || { Organizations : [ ] , Profiles : { } } ; super ( val ) ; this . ordererOrgs = { } ; this . peerOrgs = { } ; } public addOrdererOrg ( payload ) { const newOrdererOrg = { Name : payload . name , ID : `${ payload . name }` , MSPDir : payload . mspDir , Policies : { Readers : { Type : 'Signature' , Rule : `OR('${ payload . name }.member')` , } , Writers : { Type : 'Signature' , Rule : `OR('${ payload . name }.member')` , } , Admins : { Type : 'Signature' , Rule : `OR('${ payload . name }.admin')` , } , } , OrdererEndpoints : payload . hostname . map ( ( x , i ) => ( `${ x }.${ payload . domain }:${ payload . ports ?. [ i ] || 7050 }` ) ) , } ; this . ordererOrgs [ payload . name ] = newOrdererOrg ; this . value . Organizations . push ( newOrdererOrg ) ; return newOrdererOrg ; } public addPeerOrg ( payload ) { const newPeerOrg = { Name : payload . name , ID : payload . name , MSPDir : payload . mspDir , Policies : { Readers : { Type : 'Signature' , Rule : `OR('${ payload . name }.admin', '${ payload . name }.peer', '${ payload . name }.client')` , } , Writers : { Type : 'Signature' , Rule : `OR('${ payload . name }.admin', '${ payload . name }.client')` , } , Admins : { Type : 'Signature' , Rule : `OR('${ payload . name }.admin')` , } , Endorsement : { Type : 'Signature' , Rule : `OR('${ payload . name }.peer')` , } , } , AnchorPeers : payload . anchorPeers . map ( anchorPeer => ( { Host : anchorPeer . hostname , Port : anchorPeer . port || 7051 } ) ) , } ; this . peerOrgs [ payload . name ] = newPeerOrg ; this . value . Organizations . push ( newPeerOrg ) ; return newPeerOrg ; } public addSystemChannelProfile ( payload ) { const ordererAddresses = [ ] ; payload . ordererOrgs . forEach ( ordererOrg => ordererAddresses . concat ( this . ordererOrgs [ ordererOrg ] . OrdererEndpoints ) ) ; const consortiums = { } ; Object . entries ( payload . consortiums ) . forEach ( ( [ consortiumName , orgNames ] ) => { consortiums [ consortiumName ] = { Organizations : orgNames . map ( x => ( this . peerOrgs [ x ] ) ) } ; } ) ; const newProfile = { ... channelDefaults , Orderer : { ... ordererDefaults , Addresses : ordererAddresses , EtcdRaft : { Consenters : payload . etcdRaftConsenters , } , BatchTimeout : payload . batchTimeout || ordererDefaults . BatchTimeout , BatchSize : payload . BatchSize || ordererDefaults . BatchSize , Organizations : payload . ordererOrgs . map ( x => this . ordererOrgs [ x ] ) , } , Consortiums : consortiums , } ; this . value . Profiles [ payload . name ] = newProfile ; } public addApplicationChannelProfile ( payload ) { const newProfile = { ... channelDefaults , Consortium : payload . consortium , Application : { ... applicationDefaults , Organizations : payload . organizations . map ( x => ( this . peerOrgs [ x ] ) ) , } , } ; this . value . Profiles [ payload . name ] = newProfile ; } public setApplicationChannelPolicy ( payload ) { const profile = this . value . Profiles [ payload . profileName ] ; if ( 'Application' in profile ) { profile . Application . Policies [ payload . policyKey ] = payload . policy ; } } public importOrgs ( data ) { this . ordererOrgs = data . ordererOrgs ; this . peerOrgs = data . peerOrgs ; } } export default ConfigtxYaml ;