add allowOwnerOffCurve flag in getAssociatedTokenAddress

This commit is contained in:
Yihau Chen 2021-05-29 00:18:06 +08:00 committed by Trent Nelson
parent 9d5ee88707
commit f5fb183b2d
2 changed files with 12 additions and 1 deletions

View File

@ -2239,8 +2239,9 @@ export class Token {
programId: PublicKey,
mint: PublicKey,
owner: PublicKey,
allowOwnerOffCurve: boolean = false,
): Promise<PublicKey> {
if (!PublicKey.isOnCurve(owner.toBuffer())) {
if (!allowOwnerOffCurve && !PublicKey.isOnCurve(owner.toBuffer())) {
throw new Error(`Owner cannot sign: ${owner.toString()}`);
}
return (

View File

@ -52,6 +52,16 @@ describe('Token', () => {
new PublicKey('7o36UsWR1JQLpZ9PE2gn9L4SQ69CNNiWAXd4Jt7rqz9Z'),
associatedPublicKey,
)).to.be.rejectedWith(`Owner cannot sign: ${associatedPublicKey.toString()}`);
const associatedPublicKey2 = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey('7o36UsWR1JQLpZ9PE2gn9L4SQ69CNNiWAXd4Jt7rqz9Z'),
associatedPublicKey,
true,
)
expect(associatedPublicKey2.toString()).to.eql(
new PublicKey('F3DmXZFqkfEWFA7MN2vDPs813GeEWPaT6nLk4PSGuWJd').toString(),
);
});
it('createAssociatedTokenAccount', () => {