Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor message signing #198

Open
ericsson49 opened this issue Sep 18, 2019 · 0 comments
Open

Refactor message signing #198

ericsson49 opened this issue Sep 18, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@ericsson49
Copy link
Contributor

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()));
  }
@ericsson49 ericsson49 added the enhancement New feature or request label Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant