import Fingerprint from 'fingerprintjs2' ; import { Adapter } from '../adapters' ; const TcbFingerprintIdName = 'TcbFingerprintId' ; const excludesIdle = [ 'screenResolution' , 'availableScreenResolution' , 'plugins' ] ; const excludesAni = [ 'screenResolution' , 'availableScreenResolution' , 'plugins' , 'webgl' , 'canvas' , 'webglVendorAndRenderer' ] ; const promise = new Promise ( ( resolve ) => { function genFingerprint ( excludes ) { Fingerprint . get ( function ( components ) { components = components . filter ( item => { return excludes . indexOf ( item . key ) === - 1 ; } ) ; const values = components . map ( function ( component ) { return component . value ; } ) ; const tcbFingerprintId = Fingerprint . x64hash128 ( values . join ( '' ) , 31 ) ; resolve ( tcbFingerprintId ) ; } ) ; } if ( window && ( window as any ) . requestIdleCallback ) { ( window as any ) . requestIdleCallback ( function ( ) { genFingerprint ( excludesIdle ) ; } ) ; } else if ( window && ( window as any ) . requestAnimationFrame ) { ( window as any ) . requestAnimationFrame ( function ( ) { genFingerprint ( excludesAni ) ; } ) ; } setTimeout ( ( ) => { resolve ( null ) ; } , 500 ) ; } ) ; promise . then ( ) . catch ( ) ; export async function getTcbFingerprintId ( ) { let storageClass = ( Adapter . adapter && Adapter . adapter . localStorage ) || null ; let id ; if ( storageClass ) { id = storageClass . getItem ( TcbFingerprintIdName ) ; } if ( id ) { return id ; } try { id = await promise ; if ( id && id . length > 32 ) { id = id . slice ( 0 , 32 ) ; } } catch ( e ) { } let isLocal ; if ( ! id ) { isLocal = true ; id = Math . random ( ) . toString ( 16 ) . slice ( 2 ) + '_' + parseInt ( String ( Date . now ( ) / 1000 ) , 10 ) ; } if ( storageClass ) { storageClass . setItem ( TcbFingerprintIdName , id ) ; } if ( ! storageClass && isLocal ) { return null ; } return id ; }