diff --git a/hyperledger_fabric/test/fetch-config-block.sh b/hyperledger_fabric/test/fetch-config-block.sh new file mode 100644 index 00000000..2c3952f4 --- /dev/null +++ b/hyperledger_fabric/test/fetch-config-block.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# Fetch a config block and decode it. It will generate 3 local files: +# ${channel}_config.block: the config block; +# ${channel}_config.block.json: decoded config block; +# ${channel}_config.block.cfg.json: core config section of the config blcok + +# Usage: ./script mspId channel ordererURL mspPath=${PWD}/msp-mspId + +# use configtxlator to decode pb to json +# Usage: configtxlatorEncode msgType input output +configtxlatorDecode() { + local msgType=$1 + local input=$2 + local output=$3 + + if [ ! -f "$input" ]; then + echo "configDecode: input file not found" + exit 1 + fi + + if ! command -v configtxlator &> /dev/null; then + echo "configtxlator could not be found, please install it first" + exit 1 + fi + + echo "Config Decode $input --> $output using type $msgType" + configtxlator proto_decode \ + --type="${msgType}" \ + --input="${input}" \ + --output="${output}" +} + +# fetchConfigBlock fetch the config block +# Usage: fetchConfigBlock channel ordererURL tlscaFile +fetchConfigBlock() { + local channel=$1 + local ordererURL=$2 + local tlscaFile=$3 + local config_block=${channel}_config.block + PAYLOAD_CFG_PATH=".data.data[0].payload.data.config" + + peer channel fetch config "${config_block}" \ + -c "${channel}" \ + -o "${ordererURL}" \ + --tls \ + --cafile "${tlscaFile}" + + echo "[${channel}] Decode config block into JSON with configtxlator" + configtxlatorDecode "common.Block" "${channel}_config.block" "${channel}_config.block.json" + + echo "[${channel}] Export the config section ${PAYLOAD_CFG_PATH} from config block" + jq "${PAYLOAD_CFG_PATH}" "${channel}_config.block.json" >"${channel}_config.block.cfg.json" +} + +if [ $# -eq 0 ]; then + echo "Please use the as the argument" + exit +fi + +# Entry function +main() { + if [ $# -lt 3 ]; then + echo "Not enough argument supplied" + echo "$(basename $0) mspId channel ordererURL mspPath=${PWD}/msp-mspId" + fi + local mspId=$1 + local channel=$2 + local ordererURL=$3 + local mspPath=${4:-${PWD}/msp-${mspId}} # Suppose the local msp path named as msp-${msp_id} + + export FABRIC_LOGGING_SPEC="debug" + export CORE_PEER_LOCALMSPID=${mspId} + export CORE_PEER_MSPCONFIGPATH=${mspPath} + export CORE_PEER_TLS_ROOTCERT_FILE=${mspPath}/tlscacerts/tlsca.cert + #export CORE_PEER_TLS_ENABLED=true # Let client use TLS connection when connecting to peer + + echo "[${channel}] Fetch config block and decode it into JSON" + fetchConfigBlock "${channel}" "${ordererURL}" "${CORE_PEER_TLS_ROOTCERT_FILE}" +} + +main "$@" diff --git a/hyperledger_fabric/test/genMSP.sh b/hyperledger_fabric/test/genMSP.sh new file mode 100644 index 00000000..2f8d82f9 --- /dev/null +++ b/hyperledger_fabric/test/genMSP.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# Generate a valid msp dir based on given certificates.json and admin-credential +# It will overwrite any local msp path with the same name as msp-${mspId} + +# Usage: ./script mspId + +if [ $# -lt 1 ]; then + echo "Please use the as the argument" && exit 1 +fi + +mspId=$1 +echo "msp id = ${mspId}" + +cert_file=${mspId}-certificates.json +admin_dir=${mspId}-admin-credential + +mkdir -p "msp-${mspId}" +pushd "msp-${mspId}" && mkdir tlscacerts signcerts keystore cacerts admincerts && popd || exit 1 + +echo "Unzip ${mspId}-admin-credential.zip file to create the ${admin_dir}" +unzip -d "${mspId}-admin-credential" "${mspId}-admin-credential.zip" + +echo "Get tlscacert from ${cert_file}" +jq -r .certs.tlscacert "${cert_file}" > "msp-${mspId}/tlscacerts/tlsca.cert" + +echo "Get signcerts from ${admin_dir}" +cp "${admin_dir}/${mspId}-cert.pem" "msp-${mspId}/signcerts/" + +echo "Get keystore from ${admin_dir}" +cp "${admin_dir}/${mspId}-key" "msp-${mspId}/keystore/" + +echo "Get cacerts from ${cert_file}" +jq -r .certs.cacert "${cert_file}" > "msp-${mspId}/cacerts/ca.cert" + +echo "Get admincerts from ${admin_dir}" +cp "${admin_dir}/${mspId}-cert.pem" "msp-${mspId}/admincerts/"