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

Define separate SignedStatement & UnsignedStatement classes #179

Merged
merged 2 commits into from
Feb 8, 2017

Conversation

yusefnapora
Copy link
Contributor

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.

Copy link
Contributor

@parkan parkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feeling this 👍

const {id, publisher, namespace, timestamp, signature, body} = this
return {id, publisher, namespace, timestamp: timestamp, signature, body: body.toProtobuf()}
const {id, publisher, namespace, timestamp, body} = this
const msg: StatementMsg = {id, publisher, namespace, timestamp: timestamp, body: body.toProtobuf()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spent a while staring at these before concurring that this is in fact the best way to do it w/o libs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, yeah. It does look pretty odd :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, I don't know why I did timestamp: timestamp instead of just timestamp - probably some earlier refactor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, that one can get short-handed

@yusefnapora yusefnapora merged commit 090e44e into master Feb 8, 2017
@yusefnapora yusefnapora deleted the yn-unsigned-statement branch February 8, 2017 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants