feat: allow PublicKey.isOnCurve to accept PublicKeyInitData (#24602)

This commit is contained in:
Kartik Soneji 2022-04-25 18:12:41 +05:30 committed by GitHub
parent 70d57245b4
commit 758fcd383d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -222,8 +222,9 @@ export class PublicKey extends Struct {
/**
* Check that a pubkey is on the ed25519 curve.
*/
static isOnCurve(pubkey: Uint8Array): boolean {
return is_on_curve(pubkey) == 1;
static isOnCurve(pubkeyData: PublicKeyInitData): boolean {
const pubkey = new PublicKey(pubkeyData);
return is_on_curve(pubkey.toBytes()) == 1;
}
}

View File

@ -232,6 +232,8 @@ describe('PublicKey', function () {
it('isOnCurve', () => {
let onCurve = Keypair.generate().publicKey;
expect(PublicKey.isOnCurve(onCurve.toBuffer())).to.be.true;
expect(PublicKey.isOnCurve(onCurve.toBase58())).to.be.true;
expect(PublicKey.isOnCurve(onCurve)).to.be.true;
// A program address, yanked from one of the above tests. This is a pretty
// poor test vector since it was created by the same code it is testing.
// Unfortunately, I've been unable to find a golden negative example input
@ -240,6 +242,8 @@ describe('PublicKey', function () {
'12rqwuEgBYiGhBrDJStCiqEtzQpTTiZbh7teNVLuYcFA',
);
expect(PublicKey.isOnCurve(offCurve.toBuffer())).to.be.false;
expect(PublicKey.isOnCurve(offCurve.toBase58())).to.be.false;
expect(PublicKey.isOnCurve(offCurve)).to.be.false;
});
it('canBeSerializedWithBorsh', () => {