chore: export `is_on_curve()`

This commit is contained in:
Trent Nelson 2021-04-27 02:33:10 -06:00 committed by mergify[bot]
parent b17d5eeaee
commit aee30e304d
2 changed files with 21 additions and 0 deletions

View File

@ -150,6 +150,13 @@ export class PublicKey {
}
throw new Error(`Unable to find a viable program address nonce`);
}
/**
* Check that a pubkey is on the ed25519 curve.
*/
static isOnCurve(pubkey: Uint8Array): boolean {
return is_on_curve(pubkey) == 1;
}
}
// @ts-ignore

View File

@ -3,6 +3,7 @@ import {Buffer} from 'buffer';
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {Account} from '../src/account';
import {PublicKey, MAX_SEED_LENGTH} from '../src/publickey';
use(chaiAsPromised);
@ -328,4 +329,17 @@ describe('PublicKey', function () {
),
).to.be.true;
});
it('isOnCurve', () => {
let onCurve = new Account().publicKey;
expect(PublicKey.isOnCurve(onCurve.toBuffer())).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
// for curve25519 point decompression :/
let offCurve = new PublicKey(
'12rqwuEgBYiGhBrDJStCiqEtzQpTTiZbh7teNVLuYcFA',
);
expect(PublicKey.isOnCurve(offCurve.toBuffer())).to.be.false;
});
});