2018-06-13 22:39:39 +03:00
|
|
|
|
|
|
|
import net from 'net';
|
|
|
|
|
|
|
|
import { JsonRpcProvider } from './json-rpc-provider';
|
2018-06-18 12:42:41 +03:00
|
|
|
|
|
|
|
import { defineReadOnly } from '../utils/properties';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
// Imported Types
|
|
|
|
import { Networkish } from '../utils/networks';
|
|
|
|
|
2018-06-13 22:39:39 +03:00
|
|
|
import * as errors from '../utils/errors';
|
|
|
|
|
|
|
|
export class IpcProvider extends JsonRpcProvider {
|
|
|
|
readonly path: string;
|
2018-06-18 12:42:41 +03:00
|
|
|
|
|
|
|
constructor(path: string, network?: Networkish) {
|
2018-06-13 22:39:39 +03:00
|
|
|
if (path == null) {
|
|
|
|
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
|
|
|
|
}
|
|
|
|
|
|
|
|
super('ipc://' + path, network);
|
2018-06-14 04:10:41 +03:00
|
|
|
errors.checkNew(this, IpcProvider);
|
|
|
|
|
2018-06-18 12:42:41 +03:00
|
|
|
defineReadOnly(this, 'path', path);
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: Create a connection to the IPC path and use filters instead of polling for block
|
|
|
|
|
2018-06-15 11:18:17 +03:00
|
|
|
send(method: string, params: any): Promise<any> {
|
2018-06-13 22:39:39 +03:00
|
|
|
// This method is very simple right now. We create a new socket
|
|
|
|
// connection each time, which may be slower, but the main
|
|
|
|
// advantage we are aiming for now is security. This simplifies
|
|
|
|
// multiplexing requests (since we do not need to multiplex).
|
|
|
|
|
|
|
|
var payload = JSON.stringify({
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: 42,
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
var stream = net.connect(this.path);
|
|
|
|
stream.on('data', function(data) {
|
|
|
|
try {
|
|
|
|
resolve(JSON.parse(data.toString('utf8')).result);
|
|
|
|
// @TODO: Better pull apart the error
|
|
|
|
stream.destroy();
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
stream.destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('end', function() {
|
|
|
|
stream.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('error', function(error) {
|
|
|
|
reject(error);
|
|
|
|
stream.destroy();
|
|
|
|
});
|
|
|
|
stream.write(payload);
|
|
|
|
stream.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|