import { JsonKey , KeyParameters } from './Key' ; import { KeyType } from './KeyType' ; import { PrivateKey } from './PrivateKey' ; /**
 * Interface for Key deserializers
 */ export interface KeyDeserializer { getType ( ) : string ; deserialize ( json ) : PrivateKey ; } /**
 * Default private key deserializer.
 */ export class DefaultKeyDeserializer implements KeyDeserializer { getType ( ) { return '' ; } deserialize ( json ) { if ( json . key != null ) { return new PrivateKey ( json . key , KeyType . fromLabel ( json . algorithm ) , KeyParameters . deserializeJson ( json . parameters ) ) ; } else { throw new Error ( 'Unsupported Key type.' ) ; } } } /**
 * Registered key deserializers
 */ const keyDeserializers = [ ] ; const defaultKeyDeserializer = new DefaultKeyDeserializer ( ) ; param deserializer export function registerKeyDeserializer ( deserializer ) { keyDeserializers . push ( deserializer ) ; } param json export function deserializeFromJson ( json ) { if ( json . external == null ) { return defaultKeyDeserializer . deserialize ( json ) ; } else { for ( const deserializer of keyDeserializers ) { if ( deserializer . getType ( ) === json . external . type ) { return deserializer . deserialize ( json ) ; } } throw new Error ( 'Unsupported Key type.' ) ; } }