import { Address , normalizeAddressWith0x } from '@celo/base/lib/address' ; import blob from './data' ; /**
 * Retrieve the token information by a given contract address and chainId if any
 */ export const tokenInfoByAddressAndChainId = ( contract , chainId ) : TokenInfo | null | undefined => get ( ) . byContractKey ( generateContractKey ( contract , chainId ) ) ; /**
 * list all the ERC20 tokens informations
 */ export const list = ( ) : TokenInfo [ ] => get ( ) . list ( ) ; export interface TokenInfo { contractAddress : Address ; ticker : string ; decimals : number ; chainId : number ; signature : Buffer ; data : Buffer ; } return export function compareLedgerAppVersions ( version1 , version2 ) { const numberV1 = stringVersionToNumber ( version1 ) ; const numberV2 = stringVersionToNumber ( version2 ) ; return numberV1 < numberV2 ? - 1 : numberV1 === numberV2 ? 0 : 1 ; } function stringVersionToNumber ( version ) { const parts = version . split ( '.' ) ; return parts . reduce ( ( accum , part ) => ( accum + Number ( part ) ) * 1000 , 0 ) ; } export interface API { byContractKey : ( arg0 : string ) => TokenInfo | null | undefined ; list : ( ) => TokenInfo [ ] ; } function generateContractKey ( contract , chainId ) { return [ normalizeAddressWith0x ( contract ) , chainId ] . join ( '-' ) ; } const get = ( ( ) => { let cache ; return ( ) => { if ( cache ) { return cache ; } const buf = Buffer . from ( blob , 'base64' ) ; const byContract = { } ; const entries = [ ] ; let i = 0 ; while ( i < buf . length ) { const length = buf . readUInt32BE ( i ) ; i += 4 ; const item = buf . slice ( i , i + length ) ; let j = 0 ; const tickerLength = item . readUInt8 ( j ) ; j += 1 ; const ticker = item . slice ( j , j + tickerLength ) . toString ( 'ascii' ) ; j += tickerLength ; const contractAddress = normalizeAddressWith0x ( item . slice ( j , j + 20 ) . toString ( 'hex' ) ) ; j += 20 ; const decimals = item . readUInt32BE ( j ) ; j += 4 ; const chainId = item . readUInt32BE ( j ) ; j += 4 ; const signature = item . slice ( j ) ; const entry = { ticker , contractAddress , decimals , chainId , signature , data : item } ; entries . push ( entry ) ; byContract [ generateContractKey ( contractAddress , chainId ) ] = entry ; i += length ; } const api = { list : ( ) => entries , byContractKey : ( id ) => byContract [ id ] } ; cache = api ; return api ; } ; } ) ( ) ;