Skip to content

Commit

Permalink
Merge pull request #819 from EdgeApp/matthew/solana-token-sync
Browse files Browse the repository at this point in the history
Fix getTokenId to validate token address
  • Loading branch information
peachbits authored Aug 31, 2024
2 parents 4f7e9eb + 00107c3 commit 9842b01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- fixed: (Solana) Properly validate custom token addresses

## 4.22.0 (2024-08-30)

- added: Public FIO fetch fns to return non-cached addresses and domains
Expand Down
17 changes: 14 additions & 3 deletions src/solana/SolanaTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ import {
SolanaNetworkInfo
} from './solanaTypes'

export const isValidAddress = (address: string): boolean => {
try {
PublicKey.isOnCurve(new PublicKey(address).toBytes())
return true
} catch (e) {}
return false
}

export class SolanaTools implements EdgeCurrencyTools {
builtinTokens: EdgeTokenMap
currencyInfo: EdgeCurrencyInfo
Expand Down Expand Up @@ -146,7 +154,7 @@ export class SolanaTools implements EdgeCurrencyTools {

if (
edgeParsedUri.publicAddress != null &&
!PublicKey.isOnCurve(new PublicKey(edgeParsedUri.publicAddress).toBytes())
!isValidAddress(edgeParsedUri.publicAddress)
) {
throw new Error('InvalidPublicAddressError')
}
Expand All @@ -162,7 +170,7 @@ export class SolanaTools implements EdgeCurrencyTools {
const { pluginId } = this.currencyInfo
const { nativeAmount, currencyCode, publicAddress } = obj

if (!PublicKey.isOnCurve(new PublicKey(publicAddress).toBytes()))
if (!isValidAddress(publicAddress))
throw new Error('InvalidPublicAddressError')

let amount
Expand Down Expand Up @@ -232,7 +240,10 @@ export class SolanaTools implements EdgeCurrencyTools {
async getTokenId(token: EdgeToken): Promise<string> {
validateToken(token)
const cleanLocation = asMaybeContractLocation(token.networkLocation)
if (cleanLocation == null) {
if (
cleanLocation == null ||
!isValidAddress(cleanLocation.contractAddress)
) {
throw new Error('ErrorInvalidContractAddress')
}
return cleanLocation.contractAddress
Expand Down

0 comments on commit 9842b01

Please sign in to comment.