import { InvalidWitnessScopeError , InvalidWitnessScopeJSONError } from '../errors' ; import { WitnessScopeJSON } from './types' ; export enum WitnessScopeModel { None = 0x00 , CalledByEntry = 0x01 , CustomContracts = 0x10 , CustomGroups = 0x20 , Global = 0x80 , CalledByEntryAndCustomContracts = 0x11 , CalledByEntryAndCustomGroups = 0x21 , CalledByEntryAndCustomGroupsAndCustomContracts = 0x31 , CustomContractsAndCustomGroups = 0x30 } export const isWitnessScope = ( value ) : value is WitnessScopeModel => WitnessScopeModel [ value ] !== undefined ; export const assertWitnessScope = ( value ) : WitnessScopeModel => { if ( isWitnessScope ( value ) ) { return value ; } throw new InvalidWitnessScopeError ( value ) ; } ; export const toJSONWitnessScope = ( type ) : WitnessScopeJSON => assertWitnessScopeJSON ( WitnessScopeModel [ type ] ) ; const isWitnessScopeJSON = ( value ) : value is WitnessScopeJSON => WitnessScopeModel [ value as any ] !== undefined ; export const assertWitnessScopeJSON = ( value ) : WitnessScopeJSON => { if ( isWitnessScopeJSON ( value ) ) { return value ; } throw new InvalidWitnessScopeJSONError ( value ) ; } ; export const toWitnessScope = ( value ) : WitnessScopeModel => assertWitnessScope ( WitnessScopeModel [ value ] ) ; export const witnessScopeHasFlag = ( value , flag ) => { if ( ( value === WitnessScopeModel . Global && flag !== WitnessScopeModel . Global ) || ( value !== WitnessScopeModel . Global && flag === WitnessScopeModel . Global ) ) { return false ; } if ( ( value & flag ) === flag ) { return true ; } return false ; } ;