HFClientUserLocalUser
容易理解,一个用户的身份是由它的证书来标识的,同时交易还需要证书对应的私钥,因此LocalUser的核心逻辑就是利用指定的证书和私钥PEM文件满足User接口的要求。
LocalUser
import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
public class LocalUser implements User{ //实现User接口
private String name;
private String mspId;
private Enrollment enrollment;
LocalUser(String name,String mspId){
this.name = name;
this.mspId = mspId;
}
private Enrollment loadFromPemFile(String keyFile,String certFile){ /*见下文说明*/ }
@Override public String getName(){ return name; }
@Override public String getMspId() { return mspId}
@Override public Enrollment getEnrollment() { return enrollment; }
@Override public String getAccount() { return null; }
@Override public String getAffiliation() { return null; }
}
EnrollmentX509Enrollment
private Enrollment loadFromPemFile(String keyFile,String certFile) throws Exception{
byte[] keyPem = Files.readAllBytes(Paths.get(keyFile)); //载入私钥PEM文本
byte[] certPem = Files.readAllBytes(Paths.get(certFile)); //载入证书PEM文本
CryptoPrimitives suite = new CryptoPrimitives(); //载入密码学套件
PrivateKey privateKey = suite.bytesToPrivateKey(keyPem); //将PEM文本转换为私钥对象
return new X509Enrollment(privateKey,new String(certPem)); //创建并返回X509Enrollment对象
}