send(m, json) {
    if (typeof m == 'string') m = fromHex(m)

    var msg = bin(JSON.stringify(json))

    if (RPC.requireSig.includes(json.method)) {
      msg = r([
        methodMap('JSON'),
        bin(me.id.publicKey),
        bin(ec(msg, me.id.secretKey)),
        msg
      ])
    } else {
      msg = r([methodMap('JSON'), null, null, msg])
    }

    // regular pubkey
    if (m instanceof Buffer) {
      //if (json.method == 'update') l(`Sending to ${trim(m)} `, toHex(sha3(tx)))

      if (me.sockets[m]) {
        me.sockets[m].send(msg, wscb)
        return true
      } else {
        // try to find by this pubkey among validators/banks
        var validator = K.validators.find((f) => f.pubkey.equals(m))
        var bank = K.banks.find((f) => fromHex(f.pubkey).equals(m))
        if (validator) {
          m = validator
        } else if (bank) {
          m = bank
        } else {
          //l('Not online: ', m)
          return false
        }
      }
    }

    if (me.my_validator == m) {
      // send to self internally
      RPC.external_rpc(false, msg)
      return
    }

    if (trace) l(`Send ${m.id}`, json)

    if (me.sockets[m.pubkey]) {
      return me.sockets[m.pubkey].send(msg, wscb)
    } else {
      me.sockets[m.pubkey] = new WebSocketClient()

      me.sockets[m.pubkey].onmessage = (msg) => {
        RPC.external_rpc(me.sockets[m.pubkey], msg)
      }

      me.sockets[m.pubkey].onerror = function(e) {
        l('Failed to open the socket to ', m)
      }
      me.sockets[m.pubkey].onopen = function(e) {
        if (me.id) {
          me.send(m.pubkey, {method: 'auth', data: ts()})
        }

        // first auth, then send actual message

        me.sockets[m.pubkey].send(msg, wscb)
      }

      me.sockets[m.pubkey].open(m.location)
    }

    return true
  }
// accepts Buffer or valid Service object
accepts Buffer or valid Service object
accepts Buffer or valid Service object
send(m, json) {
    if (typeof m == 'string') m = fromHex(m)
    var msg = bin(JSON.stringify(json))
    if (RPC.requireSig.includes(json.method)) {
      msg = r([
        methodMap('JSON'),
        bin(me.id.publicKey),
        bin(ec(msg, me.id.secretKey)),
        msg
      ])
    } else {
      msg = r([methodMap('JSON'), null, null, msg])
    }
    if (m instanceof Buffer) {
      if (me.sockets[m]) {
        me.sockets[m].send(msg, wscb)
        return true
      } else {
        var validator = K.validators.find((f) => f.pubkey.equals(m))
        var bank = K.banks.find((f) => fromHex(f.pubkey).equals(m))
        if (validator) {
          m = validator
        } else if (bank) {
          m = bank
        } else {
          return false
        }
      }
    }
    if (me.my_validator == m) {
      RPC.external_rpc(false, msg)
      return
    }
    if (trace) l(`Send ${m.id}`, json)
    if (me.sockets[m.pubkey]) {
      return me.sockets[m.pubkey].send(msg, wscb)
    } else {
      me.sockets[m.pubkey] = new WebSocketClient()
      me.sockets[m.pubkey].onmessage = (msg) => {
        RPC.external_rpc(me.sockets[m.pubkey], msg)
      }
      me.sockets[m.pubkey].onerror = function(e) {
        l('Failed to open the socket to ', m)
      }
      me.sockets[m.pubkey].onopen = function(e) {
        if (me.id) {
          me.send(m.pubkey, {method: 'auth', data: ts()})
        }
        me.sockets[m.pubkey].send(msg, wscb)
      }
      me.sockets[m.pubkey].open(m.location)
    }
    return true
  }
send ( m , json ) { if ( typeof m == 'string' ) m = fromHex ( m ) var msg = bin ( JSON . stringify ( json ) ) if ( RPC . requireSig . includes ( json . method ) ) { msg = r ( [ methodMap ( 'JSON' ) , bin ( me . id . publicKey ) , bin ( ec ( msg , me . id . secretKey ) ) , msg ] ) } else { msg = r ( [ methodMap ( 'JSON' ) , null , null , msg ] ) } if ( m instanceof Buffer ) { if ( me . sockets [ m ] ) { me . sockets [ m ] . send ( msg , wscb ) return true } else { var validator = K . validators . find ( ( f ) => f . pubkey . equals ( m ) ) var bank = K . banks . find ( ( f ) => fromHex ( f . pubkey ) . equals ( m ) ) if ( validator ) { m = validator } else if ( bank ) { m = bank } else { return false } } } if ( me . my_validator == m ) { RPC . external_rpc ( false , msg ) return } if ( trace ) l ( ` ${ m . id } ` , json ) if ( me . sockets [ m . pubkey ] ) { return me . sockets [ m . pubkey ] . send ( msg , wscb ) } else { me . sockets [ m . pubkey ] = new WebSocketClient ( ) me . sockets [ m . pubkey ] . onmessage = ( msg ) => { RPC . external_rpc ( me . sockets [ m . pubkey ] , msg ) } me . sockets [ m . pubkey ] . onerror = function ( e ) { l ( 'Failed to open the socket to ' , m ) } me . sockets [ m . pubkey ] . onopen = function ( e ) { if ( me . id ) { me . send ( m . pubkey , { method : 'auth' , data : ts ( ) } ) } me . sockets [ m . pubkey ] . send ( msg , wscb ) } me . sockets [ m . pubkey ] . open ( m . location ) } return true }
accepts Buffer or valid Service object
accepts Buffer or valid Service object
// regular pubkey //if (json.method == 'update') l(`Sending to ${trim(m)} `, toHex(sha3(tx))) // try to find by this pubkey among validators/banks //l('Not online: ', m) // send to self internally // first auth, then send actual message