Skip to content

Commit

Permalink
US-2042: [Security] Token metadata package includes links to github u…
Browse files Browse the repository at this point in the history
…ser data creates venerability (#108)

* remove broken urls and not used functions

* bring getAllTokens back

* bump version

* Make logo optional but still keep it in type.

---------

Co-authored-by: Jesse Clark <[email protected]>
  • Loading branch information
rodrigoncalves and jessgusclark authored Nov 29, 2023
1 parent 546622b commit 4b169b9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/token/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/token/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rsksmart/rif-wallet-token",
"version": "1.0.1",
"version": "1.0.2",
"description": "RIF Wallet Token",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/token/src/BaseToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export type TokenType = 'erc20' | 'rbtc'
export class BaseToken {
public signer: Signer
public symbol: string
public logo: string
public logo: string | undefined

constructor(signer: Signer, symbol: string, logo: string) {
constructor(signer: Signer, symbol: string, logo?: string) {
this.signer = signer
this.symbol = symbol
this.logo = logo
Expand All @@ -35,7 +35,7 @@ export interface IToken {
amount: BigNumberish,
options?: ITransferOptions,
) => Promise<ContractTransaction>
logo: string
logo: string | undefined
symbol: string
address: string
}
Expand Down
2 changes: 1 addition & 1 deletion packages/token/src/ERC20Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ERC20Token extends BaseToken implements IToken {

public address: string

constructor(address: string, signer: Signer, symbol: string, logo: string) {
constructor(address: string, signer: Signer, symbol: string, logo?: string) {
super(signer, symbol, logo)
this.tokenContract = ERC20__factory.connect(address, signer)
this.address = address
Expand Down
38 changes: 4 additions & 34 deletions packages/token/src/tokenMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Signer } from '@ethersproject/abstract-signer'
import contractMapTestNet from '@rsksmart/rsk-testnet-contract-metadata'
import { IToken } from './BaseToken'
import { ERC20Token } from './ERC20Token'
import { RBTCToken } from './RBTCToken'
import { Signer } from '@ethersproject/abstract-signer'
import { ITokenWithBalance } from './types/ITokenWithBalance'

const contractMapMainnet = require('@rsksmart/rsk-contract-metadata')
const rbtcMainnet = require('./assets/RBTC-mainnet.svg')
const rbtcTestnet = require('./assets/RBTC-testnet.svg')
const tokenMainnet = require('./assets/token-mainnet.svg')
const tokenTestnet = require('./assets/token-testnet.svg')

export interface ITokenMetadata {
[address: string]: {
Expand All @@ -21,37 +19,11 @@ export interface ITokenMetadata {
}
}

export interface IConvertToERC20Options {
chainId: number
signer: Signer
}

export const MAINNET_CHAINID = 30

export const imagesUrlMainnet =
'https://raw.githubusercontent.com/rsksmart/rsk-testnet-contract-metadata/master/images'

export const tokensMetadataMainnet = contractMapMainnet as ITokenMetadata

export const imagesUrlTestnet =
'https://raw.githubusercontent.com/rsksmart/rsk-contract-metadata/master/images'

export const tokensMetadataTestnet = contractMapTestNet as ITokenMetadata

export const getTokenLogo = (address: string, chainId: number) => {
const tokensMetadata =
chainId === MAINNET_CHAINID ? tokensMetadataMainnet : tokensMetadataTestnet

const imageBaseUrl =
chainId === MAINNET_CHAINID ? imagesUrlMainnet : imagesUrlTestnet

if (tokensMetadata[address] && tokensMetadata[address].logo) {
return `${imageBaseUrl}/${tokensMetadata[address].logo}`
}

return chainId === MAINNET_CHAINID ? tokenMainnet : tokenTestnet
}

export const getAllTokens = async (signer: Signer): Promise<IToken[]> => {
const chainId = await signer.getChainId()

Expand All @@ -69,8 +41,7 @@ export const getAllTokens = async (signer: Signer): Promise<IToken[]> => {
for (const address of metadataKeys) {
const addressWithoutChecksum = address.toLowerCase()
const symbol = metadataTokens[address].symbol
const logo = getTokenLogo(addressWithoutChecksum, chainId)
const token = new ERC20Token(addressWithoutChecksum, signer, symbol, logo)
const token = new ERC20Token(addressWithoutChecksum, signer, symbol, '')

tokens.push(token)
}
Expand All @@ -80,11 +51,10 @@ export const getAllTokens = async (signer: Signer): Promise<IToken[]> => {

export const convertToERC20Token = (
token: ITokenWithBalance,
{ chainId, signer }: IConvertToERC20Options,
signer: Signer,
) => {
const addressWithoutChecksum = token.contractAddress.toLowerCase()
const logo = getTokenLogo(addressWithoutChecksum, chainId)
return new ERC20Token(addressWithoutChecksum, signer, token.symbol, logo)
return new ERC20Token(addressWithoutChecksum, signer, token.symbol)
}

export const makeRBTCToken = (signer: Signer, chainId: number): RBTCToken => {
Expand Down

0 comments on commit 4b169b9

Please sign in to comment.