mediachain / aleph

א: The mediachain universe manipulation engine
MIT License
38 stars 16 forks source link

Define separate SignedStatement & UnsignedStatement classes #179

Closed yusefnapora closed 7 years ago

yusefnapora commented 7 years ago

This refactors the Statement class hierarchy a bit to make a clean split between signed and unsigned statements.

There's now a base Statement class that has everything except the signature field, plus common helper methods like .toProtobuf, .expandObjects, etc.

The UnsignedStatement subclass adds the .calculateSignature and .sign methods, which return Buffer and SignedStatement, respectively.

The SignedStatement class has the signature field and the .verifySignature method. Both SignedStatement and UnsignedStatement inherit from Statement directly, so SignedStatement does not have the .sign and .calculateSignature methods. However, there's a helper on the the base class .asUnsignedStatement() that will return an unsigned copy of either a signed or unsigned statement. So, if you need to strip an existing signature and re-sign a message, you can do:

mySignedStmt.asUnsignedStatement().sign(newPublisherId)
  .then(signed => {
    // `signed` has a new signature
  })

Converting from protobuf still happens with Statement.fromProtobuf(), which will return a SignedStatement if the protobuf message has a signature, and an UnsignedStatement otherwise.