import Keyring from "@polkadot/keyring" ; import * as bs58 from "bs58" ; import * as hash from "hash.js" ; import { Config } from "./config" ; export const sleep = ( ms ) : Promise < void > => new Promise ( ( resolve ) => { setTimeout ( ( ) => resolve ( ) , ms ) ; } ) ; export const getNow = ( ) : number => new Date ( ) . getTime ( ) ; export const getRawPeerId = ( peerId ) : string => { const buf = bs58 . decode ( peerId ) ; const prefix = buf . slice ( 0 , 2 ) . toString ( "hex" ) ; if ( prefix == "0024" ) { return hash . sha256 ( ) . update ( buf . slice ( 2 ) ) . digest ( "hex" ) ; } if ( prefix == "1220" ) { return buf . slice ( 2 ) . toString ( "hex" ) ; } return "" ; } ; export function mapToObj ( inputMap ) { const obj = { } ; inputMap . forEach ( function ( value , key ) { obj [ key ] = value ; } ) ; return obj ; } export const toDecimals = ( raw , networkDecimals ) : number => { return raw / Math . pow ( 10 , networkDecimals ) ; } ; export const formatAddress = ( address , config ) : string => { const keyring = new Keyring ( ) ; const ss58Prefix = config . global . networkPrefix == 2 ? 2 : 0 ; return keyring . encodeAddress ( address , ss58Prefix ) ; } ; export const hex2a = ( hex ) => { return decodeURIComponent ( "%" + hex . match ( /.{1,2}/g ) . join ( "%" ) ) ; } ; export const subscanUrl = ( config ) => { return config . global . networkPrefix == 2 ? "kusama.subscan.io" : "polkadot.subscan.io" ; } ; export const addressUrl = ( address , config ) => { return `<a href="https://${ subscanUrl ( config ) }/account/${ address }">${ address }</a>` ; } ;