#
# git clone --recursive https://github.com/hyperledger/fabric-ca.git

## go download all dependeces from internet , better use http proxy
#cd fabric-ca
GOPATH shoule set in each project, such as export GOPATH=$HOME/git/yjbc/opt/go 
export http_proxy=127.0.0.1:8118
export https_proxy=127.0.0.1:8118
go get -u github.com/hyperledger/fabric-ca/cmd/...

#cd fabric-ca
make fabric-ca-server 
make fabric-ca-client
#or just do : make native (both will be generated)

#it will generate fabric-ca-client and server in fabric-ca/bin directory

#now you can start server
fabric-ca-server start -b admin:adminpw

./fabric-ca-client enroll -u http://admin:adminpw@localhost:7054
./fabric-ca-client register -d --id.name org1.admin4 --id.affiliation org1.department1 --id.attrs '"hf.Registrar.Roles=peer,client",hf.Revoker=true'

export FABRIC_CA_CLIENT_HOME=$HOME/fabric-ca/clients/admin
fabric-ca-client register --id.name client1 --id.type client --id.affiliation bu1.department1.Team1

#register peer identity
export FABRIC_CA_CLIENT_HOME=$HOME/fabric-ca/clients/admin
fabric-ca-client register --id.name peer1 --id.type peer --id.affiliation org1.department1 --id.secret peer1pw

export FABRIC_CA_CLIENT_HOME=$HOME/run/fabric-ca/clients/peer1
fabric-ca-client enroll -u http://peer1:peer1pw@localhost:7054 -M $FABRIC_CA_CLIENT_HOME/msp

export FABRIC_CA_CLIENT_HOME=$HOME/run/fabric-ca/clients/peer3
./fabric-ca-client register -d --id.name peer3 --id.secret peerpw --id.affiliation org1.department1 --id.attrs '"hf.Registrar.Roles=peer,client",hf.Revoker=true'
./fabric-ca-client enroll -u http://peer3:peerpw@localhost:7054 -M $FABRIC_CA_CLIENT_HOME/msp

fabric-ca-client revoke -e <enrollment_id> -r <reason>
The following are the supported reasons that can be specified using -r flag:
unspecified
keycompromise
cacompromise
affiliationchanged
superseded
cessationofoperation
certificatehold
removefromcrl
privilegewithdrawn
aacompromise



fabric-ca-client identity list --id user1
fabric-ca-client identity modify user1 --secret newsecret
fabric-ca-client identity modify user1 --secret newsecret

export PATH=$PATH:$HOME/git/yjbc/opt/fabric-ca/bin

export FABRIC_CA_CLIENT_TLS_CERTFILES=/tmp/hyperledger/tls-ca/crypto/tls-ca-cert.pem
export FABRIC_CA_CLIENT_HOME=/tmp/hyperledger/tls-ca/admin
fabric-ca-client enroll -d -u https://tls-ca-admin:tls-ca-adminpw@0.0.0.0:7052
fabric-ca-client register -d --id.name peer1-org1 --id.secret peer1PW --id.type peer -u https://0.0.0.0:7052
fabric-ca-client register -d --id.name peer2-org1 --id.secret peer2PW --id.type peer -u https://0.0.0.0:7052
fabric-ca-client register -d --id.name peer1-org2 --id.secret peer1PW --id.type peer -u https://0.0.0.0:7052
fabric-ca-client register -d --id.name peer2-org2 --id.secret peer2PW --id.type peer -u https://0.0.0.0:7052
fabric-ca-client register -d --id.name orderer1-org0 --id.secret ordererPW --id.type orderer -u https://0.0.0.0:7052


############################################################
# tls ca
export FABRIC_CA_CLIENT_TLS_CERTFILES=/tmp/hyperledger/tls-ca/crypto/tls-ca-cert.pem
export FABRIC_CA_CLIENT_HOME=/tmp/hyperledger/tls-ca/admin

export FABRIC_CA_CLIENT_TLS_CERTFILES=/home/vi/git/yjbc/opt/docker/ca-tls/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=/home/vi/git/yjbc/opt/docker/ca-tls/admin

export FABRIC_CA_CLIENT_TLS_CERTFILES=$PWD/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=$PWD/admin

export FABRIC_CA_CLIENT_MSPDIR=tls-msp
export FABRIC_CA_CLIENT_HOME=/tmp/hyperledger/org1/peer1
export FABRIC_CA_CLIENT_TLS_CERTFILES=/tmp/hyperledger/org1/peer1/assets/ca/org1-ca-cert.pem
export FABRIC_CA_CLIENT_MSPDIR=msp
fabric-ca-client enroll -d -u https://peer1-org1:peer1PW@0.0.0.0:7054

fabric-ca-client enroll -d -u https://peer1-org1:peer1PW@0.0.0.0:7052 --enrollment.profile tls --csr.hosts peer1-org1

fabric-ca-client enroll -d -u https://tls-ca-admin:tls-ca-adminpw@0.0.0.0:17052
fabric-ca-client register -d --id.name peer1-org1 --id.secret peer1PW --id.type peer -u https://0.0.0.0:17052
fabric-ca-client register -d --id.name peer2-org1 --id.secret peer2PW --id.type peer -u https://0.0.0.0:17052
fabric-ca-client register -d --id.name peer1-org2 --id.secret peer1PW --id.type peer -u https://0.0.0.0:17052
fabric-ca-client register -d --id.name peer2-org2 --id.secret peer2PW --id.type peer -u https://0.0.0.0:17052
fabric-ca-client register -d --id.name orderer1-org0 --id.secret ordererPW --id.type orderer -u https://0.0.0.0:17052

####################################
Enroll Orderer Org’s CA Admin

export FABRIC_CA_CLIENT_TLS_CERTFILES=/tmp/hyperledger/org0/ca/crypto/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=/tmp/hyperledger/org0/ca/admin
fabric-ca-client enroll -d -u https://rca-org0-admin:rca-org0-adminpw@0.0.0.0:7053
fabric-ca-client register -d --id.name orderer1-org0 --id.secret ordererPW --id.type orderer -u https://0.0.0.0:7053
fabric-ca-client register -d --id.name admin-org0 --id.secret org0adminpw --id.type admin --id.attrs "hf.Registrar.Roles=client,hf.Registrar.Attributes=*,hf.Revoker=true,hf.GenCRL=true,admin=true:ecert,abac.init=true:ecert" -u https://0.0.0.0:7053


1.msp mkdir admincerts
mkdir /tmp/hyperledger/org0/orderer/msp/admincerts
cp /tmp/hyperledger/org0/admin/msp/signcerts/cert.pem /tmp/hyperledger/org0/orderer/msp/admincerts/orderer-admin-cert.pem
2.tls-msp keystore sk-> key.pem

###################################
Enroll Org1’s CA Admin
export FABRIC_CA_CLIENT_TLS_CERTFILES=/tmp/hyperledger/org1/ca/crypto/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=/tmp/hyperledger/org1/ca/admin

export FABRIC_CA_CLIENT_TLS_CERTFILES=$PWD/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=$PWD/admin
fabric-ca-client enroll -d -u https://rca-org1-admin:rca-org1-adminpw@0.0.0.0:17054
fabric-ca-client register -d --id.name peer1-org1 --id.secret peer1PW --id.type peer -u https://0.0.0.0:17054
fabric-ca-client register -d --id.name peer2-org1 --id.secret peer2PW --id.type peer -u https://0.0.0.0:17054
fabric-ca-client register -d --id.name admin-org1 --id.secret org1AdminPW --id.type admin -u https://0.0.0.0:17054
fabric-ca-client register -d --id.name user-org1 --id.secret org1UserPW --id.type user -u https://0.0.0.0:17054


###################################
Setup Org2’s CA  Enrolling Org2’s CA Admin
export FABRIC_CA_CLIENT_TLS_CERTFILES=$PWD/ca-cert.pem
export FABRIC_CA_CLIENT_HOME=$PWD/admin
fabric-ca-client enroll -d -u https://rca-org2-admin:rca-org2-adminpw@0.0.0.0:17055
fabric-ca-client register -d --id.name peer1-org2 --id.secret peer1PW --id.type peer -u https://0.0.0.0:17055
fabric-ca-client register -d --id.name peer2-org2 --id.secret peer2PW --id.type peer -u https://0.0.0.0:17055
fabric-ca-client register -d --id.name admin-org2 --id.secret org2AdminPW --id.type admin -u https://0.0.0.0:17055
fabric-ca-client register -d --id.name user-org2 --id.secret org2UserPW --id.type user -u https://0.0.0.0:17055



######################################
# setup peer1





peer.chaincodeListenAddress
CORE_PEER_CHAINCODELISTENADDRESS




#setup orderer docker
configtxgen -profile OrgsOrdererGenesis -outputBlock /tmp/hyperledger/org0/orderer/genesis.block -channelID syschannel
configtxgen -profile OrgsChannel -outputCreateChannelTx /tmp/hyperledger/org0/orderer/channel.tx -channelID mychannel

configtxgen -profile OrgsOrdererGenesis -outputBlock ./genesis.block -channelID syschannel
configtxgen -profile OrgsChannel -outputCreateChannelTx ./channel.tx -channelID mychannel








#######################################
# configtxgen
configtxgen -profile OrgsOrdererGenesis -outputBlock /tmp/hyperledger/org0/orderer/genesis.block -channelID syschannel
configtxgen -profile OrgsChannel -outputCreateChannelTx /tmp/hyperledger/org0/orderer/channel.tx -channelID mychannel

为了使用configtxgen，您需要将FABRIC_CFG_PATH环境变量设置为本地包含configtx.yaml文件的目录的路径。
export FABRIC_CFG_PATH=${PWD}/configtx

configtxgen -profile TwoOrgsOrdererGenesis -channelID system-channel -outputBlock ./system-genesis-block/genesis.block

configtxgen -profile ChannelUsingRaft -channelID system-channel -outputBlock ./genesis.block
configtxgen -profile OrgsChannel -outputCreateChannelTx ./channel.tx -channelID mychannel

connection error: desc = "transport: error while dialing: dial tcp: lookup orderer1-org0 on 58.240.5

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id>


/home/vi/git/yjbc/opt/docker/rca-org1/peer1-org1/assets

#peer channel create -c mychannel -f /tmp/hyperledger/org1/peer1/assets/channel.tx -o orderer1-org0:7050 --outputBlock /tmp/hyperledger/org1/peer1/assets/mychannel.block --tls --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-7052.pem

peer channel create -c mychannel -f /tmp/hyperledger/org1/peer1/assets/channel.tx -o orderer1-org0:7050 --outputBlock /tmp/hyperledger/org1/peer1/assets/mychannel.block --tls --cafile /tmp/hyperledger/org1/peer1/tls-msp/tlscacerts/tls-0-0-0-0-17052.pem
peer channel create -c channel1 -f ./channel.tx --orderer orderer1-org0:7050


~/git/yjbc/opt/docker/rca-org1/admin-org1/msp$ cat config.yaml
NodeOUs:
  Enable: true
  ClientOUIdentifier:
    Certificate: cacerts/0-0-0-0-17054.pem
    OrganizationalUnitIdentifier: client
  PeerOUIdentifier:
    Certificate: cacerts/0-0-0-0-17054.pem
    OrganizationalUnitIdentifier: peer
  AdminOUIdentifier:
    Certificate: cacerts/0-0-0-0-17054.pem
    OrganizationalUnitIdentifier: admin
  OrdererOUIdentifier:
    Certificate: cacerts/0-0-0-0-17054.pem
    OrganizationalUnitIdentifier: orderer


- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=fabric_test
