Skip to content

Commit

Permalink
solana address validation fix. (#655)
Browse files Browse the repository at this point in the history
* solana address validation fix.
  • Loading branch information
b4rtaz authored Sep 6, 2022
1 parent ded9b92 commit a0db370
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-lizards-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/sol-utils': patch
---

The validator of a Solana address supports program addresses now.
2 changes: 1 addition & 1 deletion packages/solUtils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
},
"dependencies": {
"@moralisweb3/core": "^2.3.0",
"@solana/web3.js": "^1.55.0"
"@solana/web3.js": "^1.56.2"
}
}
37 changes: 24 additions & 13 deletions packages/solUtils/src/dataTypes/SolAddress/SolAddress.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { SolAddress } from './SolAddress';

describe('SolAddress', () => {
const ADDRESS = '5xoBq7f7CDgZwqHrDBdRWM84ExRetg4gZq93dyJtoSwp';

it('creates an instance', () => {
const address = SolAddress.create(ADDRESS);

expect(address.address).toEqual(ADDRESS);
expect(address.toJSON()).toEqual(ADDRESS);
expect(address.toString()).toEqual(ADDRESS);
expect(address.format()).toEqual(ADDRESS);
});
const ADDRESS = '5xoBq7f7CDgZwqHrDBdRWM84ExRetg4gZq93dyJtoSwp'; // account

const addresses = [
ADDRESS,
'JdbMBJNENmKowddNkM7gKKkRXGbjTCVU1hYtUZquLbE', // token account (not on the curve)
'DS2tt4BX7YwCw7yrDNwbAdnYrxjeCPeGJbHmZEYC8RTb', // account
'TLPv2tuSVvn3fSk8RgW3yPddkp5oFivzZV3rA9hQxtX', // program
'5JQ8Mhdp2wv3HWcfjq9Ts8kwzCAeBADFBDAgBznzRsE4', // program
'AMM55ShdkoGRB5jVYPjWziwk8m5MpwyDgsMWHaMSQWH6', // program
'9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM', // account
'9QgXqrgdbVU8KcpfskqJpAXKzbaYQJecgMAruSWoXDkM', // stake account (not on the curve)
];

for (const address of addresses) {
it(`creates an instance for ${address}`, () => {
const sol = SolAddress.create(address);

expect(sol.address).toEqual(address);
expect(sol.toJSON()).toEqual(address);
expect(sol.toString()).toEqual(address);
expect(sol.format()).toEqual(address);
});
}

it('create() throws an error when a passed address is invalid', () => {
expect(() => SolAddress.create('5x5x5x5x5x5x5x5x5x5x5x5x5x5x5x5x5x5x5x5x5xwp')).toThrowError(
'Invalid Solana address provided',
);
expect(() => SolAddress.create('5x')).toThrowError('Invalid Solana address provided');
});

it('create() does not create a new instance when SolAddress passed', () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/solUtils/src/dataTypes/SolAddress/SolAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ export class SolAddress implements MoralisData {
}

private static parse(address: string): string {
if (!PublicKey.isOnCurve(address)) {
try {
const publicKey = new PublicKey(address);
return publicKey.toBase58();
} catch (e) {
throw new MoralisCoreError({
code: CoreErrorCode.INVALID_ARGUMENT,
message: 'Invalid Solana address provided',
message: `Invalid Solana address provided: ${address}`,
cause: e,
});
}
return address;
}

public constructor(public readonly address: string) {}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2306,10 +2306,10 @@
dependencies:
buffer "~6.0.3"

"@solana/web3.js@^1.55.0":
version "1.55.0"
resolved "https://registry.npmjs.org/@solana/web3.js/-/web3.js-1.55.0.tgz"
integrity sha512-hXEc5CtA/5tZMvwEy0Cv6iynBpWoSLk6ud2PmoiGfWrZXZqkrfgTPkgd4yBw+VNZJRBRMKVbs/P7kT1sBwZUrw==
"@solana/web3.js@^1.56.2":
version "1.56.2"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.56.2.tgz#5212e8b147ebc216ea5a7aa99d5b555ebe41f9bd"
integrity sha512-ByWfNA8H/1EB4g0749uhkQ0zZZAQealzRmmT3UMIv3xe0DeHwnrzQUavBtAlHNMrKqLHu8kd+XtPci6zreMjjA==
dependencies:
"@babel/runtime" "^7.12.5"
"@noble/ed25519" "^1.7.0"
Expand Down

0 comments on commit a0db370

Please sign in to comment.