import { Injectable } from '@angular/core' ; import * as createHash from 'create-hash' ; import { toHexString } from 'src/app/utils/utils' ; export enum DiceRollType { DEFAULT = 0 , COLDCARD = 1 } @ Injectable ( { providedIn : 'root' } ) export class DiceRollService { private readonly emptyEntropy = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' ; constructor ( ) { } async transformEntropy ( diceEntropy , type ) { if ( type === DiceRollType . COLDCARD ) { return diceEntropy . replace ( /6/g , '0' ) ; } return diceEntropy ; } async validateInput ( diceEntropy ) { if ( typeof diceEntropy !== 'string' ) { throw Error ( 'Input needs to be a string' ) ; } if ( diceEntropy . length < 99 ) { throw Error ( 'Input length needs to be longer than 99' ) ; } if ( ! diceEntropy . split ( '' ) . every ( ( c ) => '123456' . includes ( c ) ) ) { throw Error ( 'Input can only contain "1", "2", "3", "4", "5" and "6"' ) ; } return true ; } async getEntropyFromInput ( diceEntropy , type = DiceRollType . DEFAULT ) { if ( ! ( await this . validateInput ( diceEntropy ) ) ) { throw new Error ( 'Invalid diceEntropy' ) ; } const transformedEntropy = await this . transformEntropy ( diceEntropy , type ) ; const hash = createHash ( 'sha256' ) . update ( transformedEntropy ) . digest ( ) ; const entropy = toHexString ( hash ) ; if ( entropy === this . emptyEntropy ) { throw new Error ( 'Warning! Input is emtpy' ) ; } return entropy ; } }