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

Add support for signed payload signer keys to StrKey #511

Merged
merged 9 commits into from
Apr 4, 2022

Conversation

Shaptic
Copy link
Contributor

@Shaptic Shaptic commented Mar 31, 2022

This adds support for encode signed payload signers (CAP-40) to their StrKey representation (SEP-23):

+--------------------+
|   signer address   |
|                    | <------------> "PA7QYNF7SOWQ3GL..."
|      payload       |
+--------------------+

Closes #507.

@Shaptic Shaptic self-assigned this Mar 31, 2022
@Shaptic Shaptic marked this pull request as ready for review April 1, 2022 16:27
@Shaptic Shaptic merged commit 17a9133 into protocol-19 Apr 4, 2022
@Shaptic Shaptic deleted the signed-payloads branch April 4, 2022 23:10
Copy link
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

Looks like there's one issue in this PR (❗). I also left some less critical notes about the terminology we use for strkey encoded values.

@@ -236,4 +238,30 @@ export class Keypair {

return new xdr.DecoratedSignature({ hint, signature });
}

/**
* Returns the signature hint for a signed payload signer.
Copy link
Member

Choose a reason for hiding this comment

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

💬 Minor discrepancy here, the comment says this function returns the signature hint, but it returns a decorated signature.

Comment on lines +244 to +259
* This is defined as the last 4 bytes of the signer key XORed with last 4
* bytes of the payload (zero-left-padded if necessary).
*
* @param {Buffer} data data to both sign and treat as the payload
* @return {xdr.DecoratedSignature}
*
* @see https://github.com/stellar/stellar-protocol/blob/master/core/cap-0040.md#signature-hint
*/
signPayloadDecorated(data) {
const signature = this.sign(data);
const keyHint = this.signatureHint();

let hint = Buffer.from(data.slice(-4));
if (hint.length < 4) {
// front-pad with zeroes if necessary
hint = Buffer.concat([Buffer.alloc(4 - data.length, 0), hint]);
Copy link
Member

Choose a reason for hiding this comment

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

❗ The function comment states that the last 4 bytes are zero-left-padded if less than 4 bytes, but it is zero-right-padded in CAP-40:

If the payload has a length less than 4 bytes, then 1 to 4 zero bytes are appended to the payload such that it has a length of 4 bytes, for calculating the hint.

Ref: https://github.com/stellar/stellar-protocol/blob/f13f159/core/cap-0040.md#signature-hint

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, caught this later in #523. Wish GH had a better notification system! Def worth a ping in my case if you add comments post-merge 👍

Comment on lines +68 to +72
* @param {string} address data to decode
* @returns {Buffer}
*/
static decodeEd25519SecretSeed(data) {
return decodeCheck('ed25519SecretSeed', data);
static decodeEd25519SecretSeed(address) {
return decodeCheck('ed25519SecretSeed', address);
Copy link
Member

Choose a reason for hiding this comment

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

💡 Fwiw the secret seed strkey is not an address, it's a seed encoded in a strkey. Using the term encoded instead of data may be more accurate if you're wanting to get away from the generic data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's a pretty good point... I wanted to get away from the generic data to indicate the type. Calling it encoded doesn't help, since that's implied by decodeEd25.... I find myself checking the docstring every single time I use this set of helpers, because "am I encoding to strkey or am I decoding from strkey or am I decoding from binary or am I ..." etc.

Copy link
Member

Choose a reason for hiding this comment

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

Naming it str may help then.

Comment on lines +122 to +126
* @param {string} address data to decode
* @returns {Buffer}
*/
static decodePreAuthTx(data) {
return decodeCheck('preAuthTx', data);
static decodePreAuthTx(address) {
return decodeCheck('preAuthTx', address);
Copy link
Member

Choose a reason for hiding this comment

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

💡 Same here, this is also not an address, it's a tx hash encoded in a strkey.

Comment on lines +140 to +144
* @param {string} address data to decode
* @returns {Buffer}
*/
static decodeSha256Hash(data) {
return decodeCheck('sha256Hash', data);
static decodeSha256Hash(address) {
return decodeCheck('sha256Hash', address);
Copy link
Member

Choose a reason for hiding this comment

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

💡 Same here.

Comment on lines +158 to +162
* @param {string} address address to decode
* @returns {Buffer}
*/
static decodeSignedPayload(address) {
return decodeCheck('signedPayload', address);
Copy link
Member

Choose a reason for hiding this comment

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

💡 Same here.

Shaptic added a commit that referenced this pull request Apr 11, 2022
* Update XDR definitions and relevant code for Protocol 19 (#509)
* Add support for signed payload signer keys to `StrKey` (#511)
* Support new preconditions when using building transactions (#513)
* Add conversion layer between `xdr.SignerKey`s and `StrKey`s (#520, #523)

Co-authored-by: Paul Bellamy <[email protected]>
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.

3 participants