def instantiate_chaincode(fabric_version, bin_path, operation, yaml_path, peer_address, order_address, peer_id, org_id, domain_name, channel_name, ccname, ccversion, init_param, policy, crypto_type, connect_param): global param tls_root_file = yaml_path + "crypto-config/peerOrganizations/org%s.%s/peers/peer%s.org%s.%s/tls/ca.crt" % ( org_id, domain_name, peer_id, org_id, domain_name) msp_path = yaml_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) order_tls_path = yaml_path + "crypto-config/ordererOrganizations/ord1.%s/orderers/orderer0.ord1.%s/msp/tlscacerts/tlsca.ord1.%s-cert.pem" % ( domain_name, domain_name, domain_name) env = ' FABRIC_CFG_PATH=%s ' % yaml_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_TLS_ROOTCERT_FILE=%s' % tls_root_file env = env + ' CORE_PEER_MSPCONFIGPATH=%s' % msp_path env = env + ' CORE_PEER_TLS_ENABLED=true' env = env + ' CORE_PEER_ADDRESS=%s ' % peer_address bin = utils.get_bin_path(bin_path, "peer", crypto_type) if fabric_version == "1.4": param = ' chaincode %s -o %s -C %s -n %s -v %s -c %s -P %s ' % ( operation, order_address, channel_name, ccname, ccversion, init_param, policy) else: param = ' lifecycle chaincode %s -o %s --channelID %s --name %s %s --version %s --sequence %s --init-required ' % ( "commit", order_address, channel_name, ccname, connect_param, ccversion, ccversion) tls = ' --tls --cafile %s' % order_tls_path command = env + bin + param + tls local(command)
def approve_chaincode(bin_path, yaml_path, peer_address, order_address, peer_id, org_id, domain_name, channel_name, ccname, ccversion, crypto_type): tls_root_file = yaml_path + "crypto-config/peerOrganizations/org%s.%s/peers/peer%s.org%s.%s/tls/ca.crt" % ( org_id, domain_name, peer_id, org_id, domain_name) msp_path = yaml_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) order_tls_path = yaml_path + "crypto-config/ordererOrganizations/ord1.%s/orderers/orderer0.ord1.%s/msp/tlscacerts/tlsca.ord1.%s-cert.pem" % ( domain_name, domain_name, domain_name) env = ' FABRIC_CFG_PATH=%s ' % yaml_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_TLS_ROOTCERT_FILE=%s' % tls_root_file env = env + ' CORE_PEER_MSPCONFIGPATH=%s' % msp_path env = env + ' CORE_PEER_TLS_ENABLED=true' env = env + ' CORE_PEER_ADDRESS=%s ' % peer_address bin = utils.get_bin_path(bin_path, "peer", crypto_type) param = ' lifecycle chaincode queryinstalled | grep %s_%s > %s/log.txt' % ( ccname, ccversion, yaml_path) command = env + bin + param local(command) curcmd = "sed -n '/Package/{s/^Package ID: //; s/, Label:.*$//; p;}' %s/log.txt" % yaml_path pkgId = local(curcmd, capture=True) print "---Package ID----" print pkgId param = ' lifecycle chaincode %s -o %s --channelID %s --name %s --version %s --sequence %s --package-id %s --init-required --waitForEvent' % ( "approveformyorg", order_address, channel_name, ccname, ccversion, ccversion, pkgId) tls = ' --tls --cafile %s' % order_tls_path command = env + bin + param + tls local(command)
def install_chaincode(fabric_version, bin_path, config_path, peer_address, peer_id, org_id, domain_name, ccname, ccversion, ccpath, ccinstalltype, crypto_type): global param tls_root_file = config_path + "crypto-config/peerOrganizations/org%s.%s/peers/peer%s.org%s.%s/tls/ca.crt" % ( org_id, domain_name, peer_id, org_id, domain_name) msp_path = config_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) env = ' FABRIC_CFG_PATH=%s ' % config_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_TLS_ROOTCERT_FILE=%s' % tls_root_file env = env + ' CORE_PEER_MSPCONFIGPATH=%s' % msp_path env = env + ' CORE_PEER_TLS_ENABLED=true' env = env + ' CORE_PEER_ADDRESS=%s ' % peer_address bin = utils.get_bin_path(bin_path, "peer", crypto_type) if fabric_version == "1.4": if ccinstalltype == "path": #param = ' chaincode install -n %s -v %s -p %s'%(ccname,ccversion,ccpath) param = ' chaincode install %s/%s_%s.pkg' % (config_path, ccname, ccversion) else: param = ' chaincode install %s' % ccpath else: param = ' lifecycle chaincode install %s/%s.tar.gz' % (config_path, ccname) command = env + bin + param local(command)
def create_channel(bin_path, yaml_path, out_path, channel_name, orderer_address, domain_name, crypto_type): if not os.path.exists(yaml_path + "core.yaml"): local("cp %s/core.yaml %s" % (bin_path, yaml_path)) ret = create_channeltx(bin_path, yaml_path, out_path, channel_name, crypto_type) print ret channeltx_name = channel_name + '.tx' msp_path = yaml_path + "crypto-config/peerOrganizations/org1.%s/users/Admin@org1.%s/msp" % ( domain_name, domain_name) channel_dir = out_path + channel_name order_tls_path = yaml_path + "crypto-config/ordererOrganizations/ord1.%s/orderers/orderer0.ord1.%s/msp/tlscacerts/tlsca.ord1.%s-cert.pem" % ( domain_name, domain_name, domain_name) env = 'FABRIC_CFG_PATH=%s ' % yaml_path env = env + 'CORE_PEER_LOCALMSPID=Org1MSP ' env = env + ' CORE_PEER_MSPCONFIGPATH=%s ' % msp_path bin = utils.get_bin_path(bin_path, "peer", crypto_type) param = ' channel create -o %s -t 3000s -c %s -f %s/%s' % ( orderer_address, channel_name, channel_dir, channeltx_name) tls = ' --tls --cafile %s' % order_tls_path command = env + bin + param + tls local(command) channel_block = channel_name + '.block' local('mv %s %s' % (channel_block, channel_dir)) local("chmod -R 777 %s" % out_path)
def test_chaincode(fabric_version, func, bin_path, yaml_path, peer_address, order_address, peer_id, org_id, domain_name, channel_name, ccname, args, crypto_type, connect_param): global param tls_root_file = yaml_path + "crypto-config/peerOrganizations/org%s.%s/peers/peer%s.org%s.%s/tls/ca.crt" % ( org_id, domain_name, peer_id, org_id, domain_name) msp_path = yaml_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) order_tls_path = yaml_path + "crypto-config/ordererOrganizations/ord1.%s/orderers/orderer0.ord1.%s/msp/tlscacerts/tlsca.ord1.%s-cert.pem" % ( domain_name, domain_name, domain_name) env = ' FABRIC_CFG_PATH=%s ' % yaml_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_TLS_ROOTCERT_FILE=%s' % tls_root_file env = env + ' CORE_PEER_MSPCONFIGPATH=%s' % msp_path env = env + ' CORE_PEER_TLS_ENABLED=true' env = env + ' CORE_PEER_ADDRESS=%s ' % peer_address bin = utils.get_bin_path(bin_path, "peer", crypto_type) if fabric_version == "1.4": param = ' chaincode %s -o %s -C %s -n %s -c %s ' % ( func, order_address, channel_name, ccname, args) else: param = ' chaincode %s -o %s -C %s -n %s %s -c %s ' % ( func, order_address, channel_name, ccname, connect_param, args) tls = ' --tls --cafile %s' % order_tls_path command = env + bin + param + tls local(command)
def generate_certs(bin_path, cfg_path, out_path, crypto_type): cryptotool = utils.get_bin_path(bin_path, "cryptogen", crypto_type) yamlfile = cfg_path + "crypto-config.yaml" mm_path = out_path + "crypto-config" local("%s generate --config=%s --output='%s'" % (cryptotool, yamlfile, mm_path)) local("chmod -R 777 %s" % mm_path)
def create_anchor_tx(bin_path, yaml_path, out_path, channel_name, org_id, crypto_type): channel_dir = out_path + channel_name env = ' FABRIC_CFG_PATH=%s ' % yaml_path param = ' -profile OrgsChannel -outputAnchorPeersUpdate %s/Org%sMSPanchors.tx -channelID %s -asOrg Org%sMSP' % ( channel_dir, org_id, channel_name, org_id) bin = utils.get_bin_path(bin_path, "configtxgen", crypto_type) command = env + bin + param local(command) local("chmod -R 777 %s" % out_path)
def create_channeltx(bin_path, yaml_path, out_path, channel_name, crypto_type): bin = utils.get_bin_path(bin_path, "configtxgen", crypto_type) channel_dir = out_path + channel_name if not os.path.exists(channel_dir): local("mkdir -p %s" % channel_dir) channeltx_name = channel_name + '.tx' env = 'FABRIC_CFG_PATH=%s ' % yaml_path param = ' -profile OrgsChannel -outputCreateChannelTx %s/%s -channelID %s' % ( channel_dir, channeltx_name, channel_name) command = env + bin + param local(command) local("chmod -R 777 %s" % out_path)
def test_query_tx(bin_path, yaml_path, peer_address, peer_id, org_id, domain_name, channel_name, ccname, tx_args, crypto_type): tls_root_file = yaml_path + "crypto-config/peerOrganizations/org%s.%s/peers/peer%s.org%s.%s/tls/ca.crt" % ( org_id, domain_name, peer_id, org_id, domain_name) msp_path = yaml_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) env = ' FABRIC_CFG_PATH=%s ' % yaml_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_TLS_ROOTCERT_FILE=%s' % tls_root_file env = env + ' CORE_PEER_MSPCONFIGPATH=%s' % msp_path env = env + ' CORE_PEER_TLS_ENABLED=true' env = env + ' CORE_PEER_ADDRESS=%s ' % peer_address bin = utils.get_bin_path(bin_path, "peer", crypto_type) param = ' chaincode query -C %s -n %s -c %s ' % (channel_name, ccname, tx_args) command = env + bin + param local(command)
def pkg_chaincode(fabric_version, bin_path, config_path, org_id, domain_name, ccname, ccversion, ccpath, ccinstalltype, crypto_type): global param msp_path = config_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) env = ' FABRIC_CFG_PATH=%s ' % config_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_MSPCONFIGPATH=%s' % msp_path bin = utils.get_bin_path(bin_path, "peer", crypto_type) if fabric_version == "1.4": if ccinstalltype == "path": param = ' chaincode package -n %s -p %s -v %s %s/%s_%s.pkg' % ( ccname, ccpath, ccversion, config_path, ccname, ccversion) else: param = ' lifecycle chaincode package %s/%s.tar.gz --path %s --lang golang --label %s_%s' % ( config_path, ccname, ccpath, ccname, ccversion) command = env + bin + param local(command)
def generate_genesis_block(model, bin_path, cfg_path, out_path, crypto_type): if not os.path.exists(out_path + "crypto-config"): with lcd(out_path): local("tar -zxvf crypto-config.tar.gz") if not os.path.exists(cfg_path + "core.yaml"): local("cp %s/core.yaml %s" % (bin_path, cfg_path)) tool = utils.get_bin_path(bin_path, "configtxgen", crypto_type) channel_path = out_path + "channel-artifacts" local("rm -rf %s" % channel_path) local("mkdir -p %s" % channel_path) env = "FABRIC_CFG_PATH=%s" % cfg_path local( "%s %s -profile %s -channelID byfn-sys-channel -outputBlock %s/genesis.block" % (env, tool, model, channel_path)) with lcd(out_path): local("tar -zcvf channel-artifacts.tar.gz channel-artifacts") local("chmod -R 777 channel-artifacts")
def join_channel(bin_path, yaml_path, out_path, channel_name, peer_address, peer_id, org_id, domain_name, crypto_type): channel_block = channel_name + '.block' tls_root_file = yaml_path + "crypto-config/peerOrganizations/org%s.%s/peers/peer%s.org%s.%s/tls/ca.crt" % ( org_id, domain_name, peer_id, org_id, domain_name) msp_path = yaml_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) channel_dir = out_path + channel_name env = ' FABRIC_CFG_PATH=%s ' % yaml_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_TLS_ROOTCERT_FILE=%s' % tls_root_file env = env + ' CORE_PEER_MSPCONFIGPATH=%s' % msp_path env = env + ' CORE_PEER_TLS_ENABLED=true' env = env + ' CORE_PEER_ADDRESS=%s ' % peer_address bin = utils.get_bin_path(bin_path, "peer", crypto_type) param = ' channel join -b %s/%s' % (channel_dir, channel_block) command = env + bin + param local(command)
def update_anchor(bin_path, yaml_path, out_path, channel_name, org_id, orderer_address, domain_name, crypto_type): create_anchor_tx(bin_path, yaml_path, out_path, channel_name, org_id, crypto_type) channel_dir = out_path + channel_name order_tls_path = yaml_path + "crypto-config/ordererOrganizations/ord1.%s/orderers/orderer0.ord1.%s/msp/tlscacerts/tlsca.ord1.%s-cert.pem" % ( domain_name, domain_name, domain_name) msp_path = yaml_path + "crypto-config/peerOrganizations/org%s.%s/users/Admin@org%s.%s/msp" % ( org_id, domain_name, org_id, domain_name) env = ' FABRIC_CFG_PATH=%s ' % yaml_path env = env + ' CORE_PEER_LOCALMSPID=Org%sMSP' % org_id env = env + ' CORE_PEER_MSPCONFIGPATH=%s ' % msp_path bin = utils.get_bin_path(bin_path, "peer", crypto_type) param = ' channel update -o %s -c %s -f %s/Org%sMSPanchors.tx' % ( orderer_address, channel_name, channel_dir, org_id) tls = ' --tls --cafile %s' % order_tls_path command = env + bin + param + tls local(command) local("chmod -R 777 %s" % out_path)
def generate_certs_to_ca(bin_path, out_path, crypto_type, node_type, full_name, org_name, ca_url, tlsca_url, admin_name, admin_pw): ca_tool = utils.get_bin_path(bin_path, "fabric-ca-client", crypto_type) cert_path = out_path + "crypto-config" node_password = "******" ca_admin = "%s/cadata/%s/%s" % (out_path, org_name, admin_name) user = "******" % (out_path, org_name, full_name) tls_ca_admin = "%s/tlscadata/%s/%s" % (out_path, org_name, admin_name) tls_user = "******" % (out_path, org_name, full_name) print "----------------------------------------" print "------generate %s cert start-----------" % full_name print "----------------------------------------" print "-------------%s admin login-------------" % full_name if not os.path.exists(ca_admin): print "---------%s do not exist, need admin enroll---------------" % ca_admin local("%s enroll -u http://%s:%s@%s -H %s" % (ca_tool, admin_name, admin_pw, ca_url, ca_admin)) local("%s enroll -u http://%s:%s@%s -H %s" % (ca_tool, admin_name, admin_pw, tlsca_url, tls_ca_admin)) else: print "---------%s already exist---------------" % ca_admin if node_type == "orderer": org_path = "%s/ordererOrganizations/%s" % (cert_path, org_name) org_user = "******" % (org_path, full_name) else: org_path = "%s/peerOrganizations/%s" % (cert_path, org_name) org_user = "******" % (org_path, full_name) print "----------------------------------------------" print "------generate %s tls cert start----------------" % full_name print "----------------------------------------------" # 注册登记 local("%s register --id.name %s_tls --id.type %s --id.secret %s -H %s" % (ca_tool, full_name, node_type, node_password, tls_ca_admin)) local("%s enroll -u http://%s_tls:%s@%s --csr.hosts %s,%s -H %s" % (ca_tool, full_name, node_password, tlsca_url, full_name, node_type, tls_user)) # 生成组织 ca tls证书 local("mkdir -p %s/msp/tlscacerts/" % org_path) local("cp %s/msp/cacerts/*.pem %s/msp/tlscacerts/tlsca.%s-cert.pem" % (tls_ca_admin, org_path, org_name)) # 生成 amdmin tls 证书 local("mkdir -p %s/users/%s/tls" % (org_path, admin_name)) local("cp %s/msp/cacerts/*.pem %s/users/%s/tls/ca.crt" % (tls_ca_admin, org_path, admin_name)) local("cp %s/msp/signcerts/*.pem %s/users/%s/tls/client.crt" % (tls_ca_admin, org_path, admin_name)) local("cp %s/msp/keystore/*_sk %s/users/%s/tls/client.key" % (tls_ca_admin, org_path, admin_name)) # 生成 节点 tls 证书 local("mkdir -p %s/tls" % org_user) local("cp %s/msp/cacerts/*.pem %s/tls/ca.crt" % (tls_user, org_user)) local("cp %s/msp/signcerts/*.pem %s/tls/server.crt" % (tls_user, org_user)) local("cp %s/msp/keystore/*_sk %s/tls/server.key" % (tls_user, org_user)) print "---------------------------------------------------------" print "-----------------generate %s cert----------------------" % full_name print "---------------------------------------------------------" # 注册登记 local("%s register --id.name %s --id.type %s --id.secret %s -H %s" % (ca_tool, full_name, node_type, node_password, ca_admin)) local("%s enroll -u http://%s:%s@%s --csr.hosts %s,%s -H %s" % (ca_tool, full_name, node_password, ca_url, full_name, node_type, user)) # 生成组织ca证书 local("mkdir -p %s/msp/cacerts/" % org_path) local("cp %s/msp/cacerts/*.pem %s/msp/cacerts/ca.%s-cert.pem" % (ca_admin, org_path, org_name)) # 生成组织admin证书 local("mkdir -p %s/msp/admincerts/" % org_path) local("cp %s/msp/signcerts/cert.pem %s/msp/admincerts/%s-cert.pem" % (ca_admin, org_path, admin_name)) local("mkdir -p %s/users/%s" % (org_path, admin_name)) local("cp -r %s/msp %s/users/%s/" % (org_path, org_path, admin_name)) local("cp -r %s/msp/keystore %s/users/%s/msp/" % (ca_admin, org_path, admin_name)) local("mkdir -p %s/users/%s/msp/signcerts" % (org_path, admin_name)) local("cp -r %s/users/%s/msp/admincerts/* %s/users/%s/msp/signcerts/" % (org_path, admin_name, org_path, admin_name)) # 生成组织节点证书 local("mkdir -p %s/msp/signcerts" % org_user) local("cp -r %s/msp/ %s/" % (org_path, org_user)) local("cp %s/msp/signcerts/cert.pem %s/msp/signcerts/%s-cert.pem" % (user, org_user, full_name)) local("cp -r %s/msp/keystore/ %s/msp/" % (user, org_user)) config_tpl_path = utils.get_bin_path(bin_path, "config.yamlt", "") # 生成config.yaml配置文件 if node_type == "orderer": print "orderer do not generate config.yaml" else: local('sed "s/ORG_NAME/%s/g" %s > %s/msp/config.yaml' % (org_name, config_tpl_path, org_path)) local('sed "s/ORG_NAME/%s/g" %s > %s/msp/config.yaml' % (org_name, config_tpl_path, org_user)) local("chmod -R 777 %s" % org_path)