Skip to content

Refactor message signing #198

@ericsson49

Description

@ericsson49

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,

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()));
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions