import RadixECIES from '../crypto/RadixECIES' ; import RadixIdentity from './RadixIdentity' ; import RadixAccount from '../account/RadixAccount' ; import { RadixAtom , RadixAddress , RadixSerializer } from '../atommodel' ; import { logger } from '../common/RadixLogger' ; import { Client } from 'rpc-websockets' ; export default class RadixRemoteIdentity extends RadixIdentity { private token : string ; private remoteUrl : string ; private socket : Client ; private constructor ( readonly address , token , remoteUrl ) { super ( address ) ; this . token = token ; this . remoteUrl = remoteUrl ; } returns private getSocketConnection ( ) { this . socket = new Client ( this . remoteUrl ) ; this . socket . on ( 'error' , ( error ) => logger . error ( error ) ) ; this . socket . on ( 'close' , ( ) => logger . info ( 'Socket closed' ) ) ; return this . socket ; } param atom returns public signAtom ( atom ) { return new Promise < RadixAtom > ( ( resolve , reject ) => { const socket = this . getSocketConnection ( ) ; socket . on ( 'open' , ( ) => { socket . call ( 'sign_atom' , { token : this . token , atom : RadixSerializer . toJSON ( atom ) , } ) . then ( ( response ) => { atom . signatures = RadixSerializer . fromJSON ( response ) ; resolve ( atom ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) . finally ( ( ) => { socket . close ( ) ; } ) ; } ) ; } ) ; } param payload returns public async decryptECIESPayload ( payload ) { return new Promise < Buffer > ( ( resolve , reject ) => { const socket = this . getSocketConnection ( ) ; socket . on ( 'open' , ( ) => { socket . call ( 'decrypt_ecies_payload' , { token : this . token , payload , } ) . then ( ( response ) => { resolve ( Buffer . from ( response . data ) ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) . finally ( ( ) => { socket . close ( ) ; } ) ; } ) ; } ) ; } public async decryptECIESPayloadWithProtectors ( protectors , payload ) { return new Promise < Buffer > ( ( resolve , reject ) => { const socket = this . getSocketConnection ( ) ; socket . on ( 'open' , ( ) => { socket . call ( 'decrypt_ecies_payload_with_protectors' , { token : this . token , protectors , payload , } ) . then ( ( response ) => { resolve ( Buffer . from ( response . data ) ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) . finally ( ( ) => { socket . close ( ) ; } ) ; } ) ; } ) ; } returns public getPublicKey ( ) { return this . address . getPublic ( ) ; } param name param description param host param port returns public static async createNew ( name , description , permissions = [ 'sign_atom' , 'decrypt_ecies_payload' , 'get_public_key' ] , host = 'localhost' , port = '54345' ) { try { const token = await RadixRemoteIdentity . register ( name , description , permissions , host , port ) ; const publicKey = await RadixRemoteIdentity . getRemotePublicKey ( token , host , port ) ; return new RadixRemoteIdentity ( RadixAddress . fromPublic ( publicKey ) , token , `ws://${ host }:${ port }` ) ; } catch ( error ) { throw error ; } } param name param description param host param port returns public static register ( name , description , permissions , host = 'localhost' , port = '54345' ) { return new Promise < string > ( ( resolve , reject ) => { const socket = new Client ( `ws://${ host }:${ port }` ) ; socket . on ( 'open' , ( ) => { socket . call ( 'register' , { name , description , permissions , } ) . then ( ( response ) => { resolve ( response . token ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) ; } ) ; } ) ; } param host param port returns public static getRemotePublicKey ( token , host = 'localhost' , port = '54345' ) { return new Promise < Buffer > ( ( resolve , reject ) => { const socket = new Client ( `ws://${ host }:${ port }` ) ; socket . on ( 'open' , ( ) => { socket . call ( 'get_public_key' , { token , } ) . then ( ( response ) => { resolve ( response . data ) ; } ) . catch ( ( error ) => { reject ( error ) ; } ) . finally ( ( ) => { socket . close ( ) ; } ) ; } ) ; } ) ; } param host param port returns public static isServerUp ( host = 'localhost' , port = '54345' ) { return new Promise < boolean > ( ( resolve , reject ) => { const socket = new Client ( `ws://${ host }:${ port }` ) ; socket . on ( 'open' , ( ) => resolve ( true ) ) ; setTimeout ( ( ) => { if ( socket && socket . ready ) { socket . close ( ) ; resolve ( true ) ; } else { socket . close ( ) ; resolve ( false ) ; } } , 2000 ) ; } ) ; } }