| 14 | ) {} |
| 15 | |
| 16 | public callMethod(method: string, params: any, callback: CallbackType, walletName?: string) { |
| 17 | const authString = this.username ? `${this.username}:${this.password}@` : ''; |
| 18 | const walletString = walletName ? '/wallet/' + walletName : ''; |
| 19 | request( |
| 20 | { |
| 21 | method: 'POST', |
| 22 | url: `${this.protocol}://${authString}${this.host}:${this.port}${walletString}`, |
| 23 | body: { |
| 24 | jsonrpc: '1.0', |
| 25 | id: Date.now(), |
| 26 | method, |
| 27 | params |
| 28 | }, |
| 29 | json: true |
| 30 | }, |
| 31 | (err, res) => { |
| 32 | if (err) { |
| 33 | return callback(err); |
| 34 | } else if (res) { |
| 35 | if (res.body) { |
| 36 | if (res.body.error) { |
| 37 | return callback(res.body.error); |
| 38 | } else if (res.body.result) { |
| 39 | return callback(null, res.body && res.body.result); |
| 40 | } else { |
| 41 | return callback({ msg: 'No error or body found', body: res.body }); |
| 42 | } |
| 43 | } |
| 44 | } else { |
| 45 | return callback('No response or error returned by rpc call'); |
| 46 | } |
| 47 | } |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | async asyncCall<T>(method: string, params: any[], walletName?: string) { |
| 52 | return new Promise<T>((resolve, reject) => { |