@atomicfinance/jsonrpc-provider 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @atomicfinance/jsonrpc-provider
2
+
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 2edf70f: Port over bitcoin RPC related providers
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [a06082e]
12
+ - @atomicfinance/errors@3.0.0
13
+ - @atomicfinance/node-provider@3.0.0
@@ -0,0 +1,12 @@
1
+ import { AxiosResponse, NodeProvider } from '@atomicfinance/node-provider';
2
+ export default class JsonRpcProvider extends NodeProvider {
3
+ constructor(uri: string, username?: string, password?: string);
4
+ _prepareRequest(method: string, params: any[]): {
5
+ id: number;
6
+ method: string;
7
+ jsonrpc: string;
8
+ params: any[];
9
+ };
10
+ _parseResponse(_data: AxiosResponse): any;
11
+ jsonrpc(method: string, ...params: any[]): Promise<any>;
12
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const errors_1 = require("@atomicfinance/errors");
7
+ const node_provider_1 = require("@atomicfinance/node-provider");
8
+ const json_bigint_1 = __importDefault(require("json-bigint"));
9
+ const lodash_1 = require("lodash");
10
+ const { parse, stringify } = json_bigint_1.default({
11
+ storeAsString: true,
12
+ strict: true,
13
+ useNativeBigInt: true,
14
+ });
15
+ class JsonRpcProvider extends node_provider_1.NodeProvider {
16
+ constructor(uri, username, password) {
17
+ const config = {
18
+ baseURL: uri,
19
+ responseType: 'text',
20
+ transformResponse: undefined,
21
+ validateStatus: () => true,
22
+ };
23
+ if (username || password) {
24
+ config.auth = { username, password };
25
+ }
26
+ super(config);
27
+ }
28
+ _prepareRequest(method, params) {
29
+ const id = Date.now();
30
+ const jsonrpc = '2.0';
31
+ const req = { id, method, jsonrpc, params };
32
+ return req;
33
+ }
34
+ _parseResponse(_data) {
35
+ const dataString = typeof _data !== 'string' ? stringify(_data) : _data;
36
+ const data = parse(dataString);
37
+ const { error } = data;
38
+ if (error != null) {
39
+ throw new errors_1.NodeError(error.message || error);
40
+ }
41
+ if (!lodash_1.has(data, 'result')) {
42
+ throw new errors_1.NodeError('Missing `result` on the RPC call result');
43
+ }
44
+ return data.result;
45
+ }
46
+ async jsonrpc(method, ...params) {
47
+ const data = await super.nodePost('', this._prepareRequest(method, params));
48
+ return this._parseResponse(data);
49
+ }
50
+ }
51
+ exports.default = JsonRpcProvider;
52
+ //# sourceMappingURL=JsonRpcProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonRpcProvider.js","sourceRoot":"","sources":["../lib/JsonRpcProvider.ts"],"names":[],"mappings":";;;;;AAAA,kDAAkD;AAClD,gEAIsC;AACtC,8DAAqC;AACrC,mCAA6B;AAE7B,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,qBAAU,CAAC;IACtC,aAAa,EAAE,IAAI;IACnB,MAAM,EAAE,IAAI;IACZ,eAAe,EAAE,IAAI;CACtB,CAAC,CAAC;AAEH,MAAqB,eAAgB,SAAQ,4BAAY;IACvD,YAAY,GAAW,EAAE,QAAiB,EAAE,QAAiB;QAC3D,MAAM,MAAM,GAAuB;YACjC,OAAO,EAAE,GAAG;YACZ,YAAY,EAAE,MAAM;YACpB,iBAAiB,EAAE,SAAS;YAC5B,cAAc,EAAE,GAAG,EAAE,CAAC,IAAI;SAC3B,CAAC;QAEF,IAAI,QAAQ,IAAI,QAAQ,EAAE;YACxB,MAAM,CAAC,IAAI,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC;SACtC;QAED,KAAK,CAAC,MAAM,CAAC,CAAC;IAChB,CAAC;IAED,eAAe,CAAC,MAAc,EAAE,MAAa;QAC3C,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACtB,MAAM,OAAO,GAAG,KAAK,CAAC;QACtB,MAAM,GAAG,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;QAE5C,OAAO,GAAG,CAAC;IACb,CAAC;IAED,cAAc,CAAC,KAAoB;QACjC,MAAM,UAAU,GACd,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QAEvD,MAAM,IAAI,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;QAE/B,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;QAEvB,IAAI,KAAK,IAAI,IAAI,EAAE;YACjB,MAAM,IAAI,kBAAS,CAAC,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,CAAC;SAC7C;QAED,IAAI,CAAC,YAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE;YACxB,MAAM,IAAI,kBAAS,CAAC,yCAAyC,CAAC,CAAC;SAChE;QAED,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,MAAc,EAAE,GAAG,MAAa;QAC5C,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;QAE5E,OAAO,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;CACF;AAhDD,kCAgDC"}
@@ -0,0 +1,2 @@
1
+ import JsonRpcProvider from './JsonRpcProvider';
2
+ export { JsonRpcProvider };
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.JsonRpcProvider = void 0;
7
+ const JsonRpcProvider_1 = __importDefault(require("./JsonRpcProvider"));
8
+ exports.JsonRpcProvider = JsonRpcProvider_1.default;
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;AAAA,wEAAgD;AAEvC,0BAFF,yBAAe,CAEE"}
@@ -0,0 +1,64 @@
1
+ import { NodeError } from '@atomicfinance/errors';
2
+ import {
3
+ AxiosRequestConfig,
4
+ AxiosResponse,
5
+ NodeProvider,
6
+ } from '@atomicfinance/node-provider';
7
+ import JSONBigInt from 'json-bigint';
8
+ import { has } from 'lodash';
9
+
10
+ const { parse, stringify } = JSONBigInt({
11
+ storeAsString: true,
12
+ strict: true,
13
+ useNativeBigInt: true,
14
+ });
15
+
16
+ export default class JsonRpcProvider extends NodeProvider {
17
+ constructor(uri: string, username?: string, password?: string) {
18
+ const config: AxiosRequestConfig = {
19
+ baseURL: uri,
20
+ responseType: 'text',
21
+ transformResponse: undefined, // https://github.com/axios/axios/issues/907,
22
+ validateStatus: () => true,
23
+ };
24
+
25
+ if (username || password) {
26
+ config.auth = { username, password };
27
+ }
28
+
29
+ super(config);
30
+ }
31
+
32
+ _prepareRequest(method: string, params: any[]) {
33
+ const id = Date.now();
34
+ const jsonrpc = '2.0';
35
+ const req = { id, method, jsonrpc, params };
36
+
37
+ return req;
38
+ }
39
+
40
+ _parseResponse(_data: AxiosResponse): any {
41
+ const dataString: string =
42
+ typeof _data !== 'string' ? stringify(_data) : _data;
43
+
44
+ const data = parse(dataString);
45
+
46
+ const { error } = data;
47
+
48
+ if (error != null) {
49
+ throw new NodeError(error.message || error);
50
+ }
51
+
52
+ if (!has(data, 'result')) {
53
+ throw new NodeError('Missing `result` on the RPC call result');
54
+ }
55
+
56
+ return data.result;
57
+ }
58
+
59
+ async jsonrpc(method: string, ...params: any[]) {
60
+ const data = await super.nodePost('', this._prepareRequest(method, params));
61
+
62
+ return this._parseResponse(data);
63
+ }
64
+ }
package/lib/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import JsonRpcProvider from './JsonRpcProvider';
2
+
3
+ export { JsonRpcProvider };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@atomicfinance/jsonrpc-provider",
3
+ "version": "3.0.0",
4
+ "description": "",
5
+ "module": "dist/index.js",
6
+ "main": "dist/index.js",
7
+ "directories": {
8
+ "dist": "dist",
9
+ "src": "lib"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "lib"
14
+ ],
15
+ "scripts": {
16
+ "build": "../../node_modules/.bin/tsc --project tsconfig.json",
17
+ "lint": "../../node_modules/.bin/eslint --ignore-path ../../.eslintignore -c ../../.eslintrc.js .",
18
+ "lint:fix": "../../node_modules/.bin/eslint --fix --ignore-path ../../.eslintignore -c ../../.eslintrc.js ."
19
+ },
20
+ "author": "Atomic Finance <info@atomic.finance>",
21
+ "license": "MIT",
22
+ "engines": {
23
+ "node": ">=14"
24
+ },
25
+ "dependencies": {
26
+ "@atomicfinance/errors": "^3.0.0",
27
+ "@atomicfinance/node-provider": "^3.0.0",
28
+ "@babel/runtime": "^7.12.1",
29
+ "@types/lodash": "^4.14.168",
30
+ "json-bigint": "^1.0.0",
31
+ "lodash": "^4.17.20"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "devDependencies": {
37
+ "@types/json-bigint": "^1.0.0"
38
+ },
39
+ "sideEffects": false
40
+ }