交易ID(txid) 的生成规则
交易ID的生成是在客户端完成(因为多方背书必须要一致的交易ID)
通过fabric-sdk-java
项目
交易ID生成的代码在TransactionContext
的构造方法中
public TransactionContext(Channel channel, User user, CryptoSuite cryptoPrimitives) {
this.user = user;
this.channel = channel;
//TODO clean up when public classes are interfaces.
this.verify = !"".equals(channel.getName()); //if name is not blank not system channel and need verify.
// this.txID = transactionID;
this.cryptoPrimitives = cryptoPrimitives;
// Get the signing identity from the user
this.signingIdentity = IdentityFactory.getSigningIdentity(cryptoPrimitives, user);
// Serialize signingIdentity
this.identity = signingIdentity.createSerializedIdentity();
// 这里获取一个随机的byte种子,长度24
ByteString no = getNonce();
// 将随机byte种子与证书中的身份信息连接
ByteString comp = no.concat(identity.toByteString());
//
byte[] txh = cryptoPrimitives.hash(comp.toByteArray());
// txID = Hex.encodeHexString(txh);
txID = new String(Utils.toHexString(txh));
toString = "TransactionContext{ txID: " + txID + ", mspid: " + user.getMspId() + ", user: " + user.getName() + "}";
}
getNonce()
方法实际是调用了Utils.generateNonce(),实现如下
public static byte[] generateNonce() {
byte[] values = new byte[24];
RANDOM.nextBytes(values);
return values;
}
由此可以看出,TXID
的是在客户端由一串长度为24的随机byte
种子+证书中的身份信息部分组成的hash
字符串