-
Notifications
You must be signed in to change notification settings - Fork 9
Description
Since beacon objects are immutable, message signing is typically performed with the following steps:
1 construct an object with empty signature
2 sign the object (construct an appropriate signature)
3 re-construct the object with the signature
For example,
beacon-chain-java/consensus/src/test/java/org/ethereum/beacon/consensus/TestUtils.java
Line 42 in aa452ce
| private synchronized static Pair<List<Deposit>, List<KeyPair>> generateRandomDeposits(Random rnd, BeaconChainSpec spec, int count) { |
The re-construction of object introduces code duplication, which can lead to a bug (the second object re-constructed with a different value). And reduces readability.
A better solution is to add an updateSignature (or withSignature) method, which will encapsulate copying re-construction of the source object with the signature field updated.
public DepositData updateSignature(BLSSignature signature) {
return new DepositData(getPubKey(), getWithdrawalCredentials(), getAmount(), signature);
}
Or even encapsulate signing procedure in a helper function, e.g.
public static DepositData signDepositData(
BeaconChainSpec spec, DepositData depositData, BLS381.KeyPair keyPair) {
Hash32 msgHash = spec.signing_root(depositData);
UInt64 domain = spec.compute_domain(DEPOSIT, Bytes4.ZERO);
BLS381.Signature signature = BLS381.sign(MessageParameters.create(msgHash, domain), keyPair);
return depositData.updateSignature(BLSSignature.wrap(signature.getEncoded()));
}