Refactor message signing
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, https://github.com/harmony-dev/beacon-chain-java/blob/aa452ce5acfc73f359b4a5228733fceb8dd29692/consensus/src/test/java/org/ethereum/beacon/consensus/TestUtils.java#L42
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()));
}