Fabric链码中是如何利用GetCreator() ([]byte, error)读取用户的身份的
首先我先贴上链码中关于读取用户证书的代码:
//获取用户的证书信息 -c '{"Args":["getUserCert"]}'
func (t *SimpleChaincode) getUserCert(stub shim.ChaincodeStubInterface) pb.Response{
creatorByte, _ := stub.GetCreator()
certStart := bytes.IndexAny(creatorByte, "-----BEGIN")
if certStart == -1 {
fmt.Errorf("No certificate found")
}
certText := creatorByte[certStart:]
bl, _ := pem.Decode(certText)
if bl == nil {
fmt.Errorf("Could not decode the PEM structure")
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
fmt.Errorf("ParseCertificate failed")
}
uname := cert.Subject.CommonName
fmt.Println("Name:" + uname)
//logger.Infof("===========Name======Name=========%s", uname)
return shim.Success([]byte(uname))
}
下面进入docker环境进行操作验证:
安装一个新的链码文件,过程见我之前的操作过程:关于fabric重新部署环境和测试链码的心得。_lakersssss24的博客-CSDN博客
进行查询当前操作用户的身份信息:

然后我们进入dokcer-compose配置文件中的cli部分查看:

可以看到我们在cli容器中进行操作的环境变量是user/Admin文件,也就意味着我们当前是用org的Admin管理员身份进行操作的!
说明我们链码读取的信息是正确的!