mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-02-25 17:25:25 +00:00
* Check in. * Add read only wallet and new types for that. Convert some components to require full wallet. * Fix readonly property, fix uncaught throw. * Disable address only on some tabs. * Use FullWalletOnly render callback to handle signing. * Work around uncertain wallet type. * Fix function args. * Undo bug fix that should be done in another branch. * Disable button while address is bad. * Remove log. * Convert anonymous functions to class functions.
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { randomBytes } from 'crypto';
|
|
import { JsonRpcResponse, RPCRequest } from './types';
|
|
|
|
export default class RPCClient {
|
|
public endpoint: string;
|
|
public headers: object;
|
|
constructor(endpoint: string, headers: object = {}) {
|
|
this.endpoint = endpoint;
|
|
this.headers = headers;
|
|
}
|
|
|
|
public id(): string {
|
|
return randomBytes(16).toString('hex');
|
|
}
|
|
|
|
public decorateRequest = (req: RPCRequest) => ({
|
|
...req,
|
|
id: this.id(),
|
|
jsonrpc: '2.0'
|
|
});
|
|
|
|
public call = (request: RPCRequest | any): Promise<JsonRpcResponse> => {
|
|
return fetch(this.endpoint, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...this.headers
|
|
},
|
|
body: JSON.stringify(this.decorateRequest(request))
|
|
}).then(r => r.json());
|
|
};
|
|
|
|
public batch = (requests: RPCRequest[] | any): Promise<JsonRpcResponse[]> => {
|
|
return fetch(this.endpoint, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...this.headers
|
|
},
|
|
body: JSON.stringify(requests.map(this.decorateRequest))
|
|
}).then(r => r.json());
|
|
};
|
|
}
|