-
Notifications
You must be signed in to change notification settings - Fork 52
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 secp256k1 verify and pubKey recovery function #583
base: main
Are you sure you want to change the base?
Conversation
src/api/account.ts
Outdated
*/ | ||
async verifySecp256k1Account(args: { | ||
message: HexInput; | ||
signature: HexInput | Secp256k1Signature | AnySignature; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we accept AnySignature
if we only verify for Secp256k1Account
? why is Secp256k1Signature
is not part of AnySignature
? Also, why do we accept HexInput
as a signature? dont we want to make sure the function accepts a valid signature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, public key recovery should be in the signature to return. I don't think it belongs at the account level given you can't recover the private key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because Secp256k1Account does not exist. SingleKeyAccount exists which returns AnySignature when it signs something. You can get the inner Secp256k1Signature via signature.signature, but you would still have to type check it into Secp256k1Signature. Having the function handle it seems better DevX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gregnazario not sure what you are suggesting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh and HexInput is again for convenience. The function will check signature validity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also would expect this function to be in a different place than Account
.
I think Secp256k1PublicKey.fromMessageAndSignature
makes total sense (fromSignedMessage
could be even more concise).
Ideally that's all the devs would need, and they could do the following:
const innerPublicKey = new Secp256k1PublicKey.fromSignedMessage({ message, signature });
const publicKey = new AnyPublicKey({ publicKey: innerPublicKey });
const derivedAddress = publicKey.authKey().derivedAddress.toString();
// Simple case
const isValid = derivedAddress === accountAddress;
// Handle rotated auth keys
const { authentication_key } = await aptos.getAccountInfo({ accountAddress });
const isValid = derivedAddress === authentication_key;
now.. how can we make this even easier / more concise?
- Going from
Secp256k1PublicKey
toAnyPublicKey
is a bit annoying, maybe we can have the constructor there instead:AnyPublicKey.fromSecp256k1SignedMessage({ message, signature })
- The action of verifying the publicKey is associated to an account from its address is agnostic to the signature scheme, so we could have
verifyAuthenticationKey({ authKey, accountAddress })
which could live in theapi
to avoid dependency cycles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really hate the AnyPublicKey
name ugh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the problem is you need to look up the auth key with the address, and only via the auth key can you figure out which public key is correct. If a recovery is provided 2 public keys would be returned.
I'm down for AnyPublicKey.fromSecp256k1SignedMessage({ message, signature }) though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I re-evaluated it and did similar here aptos-labs/aptos-go-sdk#108
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the problem is you need to look up the auth key with the address, and only via the auth key can you figure out which public key is correct. If a recovery is provided 2 public keys would be returned.
Oh I see, gotcha makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's compartmentalize this properly to the Secp256k1 signatures since it applies to that, and then we won't need to do other parsing
src/api/account.ts
Outdated
*/ | ||
async verifySecp256k1Account(args: { | ||
message: HexInput; | ||
signature: HexInput | Secp256k1Signature | AnySignature; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also would expect this function to be in a different place than Account
.
I think Secp256k1PublicKey.fromMessageAndSignature
makes total sense (fromSignedMessage
could be even more concise).
Ideally that's all the devs would need, and they could do the following:
const innerPublicKey = new Secp256k1PublicKey.fromSignedMessage({ message, signature });
const publicKey = new AnyPublicKey({ publicKey: innerPublicKey });
const derivedAddress = publicKey.authKey().derivedAddress.toString();
// Simple case
const isValid = derivedAddress === accountAddress;
// Handle rotated auth keys
const { authentication_key } = await aptos.getAccountInfo({ accountAddress });
const isValid = derivedAddress === authentication_key;
now.. how can we make this even easier / more concise?
- Going from
Secp256k1PublicKey
toAnyPublicKey
is a bit annoying, maybe we can have the constructor there instead:AnyPublicKey.fromSecp256k1SignedMessage({ message, signature })
- The action of verifying the publicKey is associated to an account from its address is agnostic to the signature scheme, so we could have
verifyAuthenticationKey({ authKey, accountAddress })
which could live in theapi
to avoid dependency cycles
Description
With secp256k1 you can recover the publicKey from the signature and message. This is common in EVM world. This will ease migrations as it allows users to use familiar APIs, which may be missing the publicKey.
Test Plan
e2e test added
Related Links
Checklist
pnpm fmt
?CHANGELOG.md
?