Skip to content

Commit 5a62abb

Browse files
authored
Sanitize BNs to produce decimal strings (#29)
1 parent d2a0265 commit 5a62abb

File tree

5 files changed

+108
-58
lines changed

5 files changed

+108
-58
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polkadot-rpc-proxy",
3-
"version": "0.1.12",
3+
"version": "0.2.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -11,10 +11,10 @@
1111
"author": "",
1212
"license": "GPL-3.0-or-later",
1313
"dependencies": {
14-
"@polkadot/api": "1.9.0-beta.26",
15-
"@polkadot/metadata": "1.9.0-beta.26",
16-
"@polkadot/rpc-provider": "1.9.0-beta.26",
17-
"@polkadot/types": "1.9.0-beta.26",
14+
"@polkadot/api": "1.9.1",
15+
"@polkadot/metadata": "1.9.1",
16+
"@polkadot/rpc-provider": "1.9.1",
17+
"@polkadot/types": "1.9.1",
1818
"@types/body-parser": "^1.19.0",
1919
"@types/express": "^4.17.2",
2020
"body-parser": "^1.19.0",

src/ApiHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ export default class ApiHandler {
131131

132132
const [header, account, locks, sysAccount] = await Promise.all([
133133
api.rpc.chain.getHeader(hash),
134-
api.query.balances.account.at(hash, address),
134+
api.query.balances.account(address),
135135
api.query.balances.locks.at(hash, address),
136136
api.query.system.account.at(hash, address),
137137
]);
138138

139139
const at = {
140140
hash,
141-
height: (header as any).number, // TODO: fix this nasty obvious type erasure
141+
height: header.number.toNumber().toString(10),
142142
};
143143

144144
if (account && locks && sysAccount) {

src/main.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Request, Response } from 'express';
2020
import { ApiPromise } from '@polkadot/api';
2121
import { BlockHash } from '@polkadot/types/interfaces/chain';
2222
import { HttpProvider, WsProvider } from '@polkadot/rpc-provider';
23+
import { sanitizeNumbers } from './utils';
2324
import ApiHandler from './ApiHandler';
2425

2526
const HOST = process.env.BIND_HOST || '127.0.0.1';
@@ -52,10 +53,10 @@ async function main() {
5253
function get(path: string, cb: (params: Params) => Promise<any>) {
5354
app.get(path, async (req, res) => {
5455
try {
55-
res.send(await cb(req.params));
56+
res.send(sanitizeNumbers(await cb(req.params)));
5657
} catch(err) {
5758
if (err && typeof err.error === 'string') {
58-
res.status(500).send(err);
59+
res.status(500).send(sanitizeNumbers(err));
5960
return;
6061
}
6162

@@ -70,10 +71,10 @@ async function main() {
7071
function post(path: string, cb: (params: Params, body: any) => Promise<any>) {
7172
app.post(path, async (req, res) => {
7273
try {
73-
res.send(await cb(req.params, req.body));
74+
res.send(sanitizeNumbers(await cb(req.params, req.body)));
7475
} catch(err) {
7576
if (err && typeof err.error === 'string') {
76-
res.status(500).send(err);
77+
res.status(500).send(sanitizeNumbers(err));
7778
return;
7879
}
7980

src/utils.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import AbstractInt from '@polkadot/types/codec/AbstractInt';
2+
3+
// A sanitizer for arbitrary data that's going to be
4+
// stringified to JSON. We find all instances of `AbstractInt`,
5+
// which is using bn.js as backend, and forcibly serialize it
6+
// to a decimal string.
7+
export function sanitizeNumbers(data: any): any {
8+
if (data instanceof AbstractInt) {
9+
return data.toString(10);
10+
}
11+
12+
if (data instanceof Object) {
13+
if (data.raw instanceof AbstractInt) {
14+
return data.raw.toString(10);
15+
}
16+
17+
if (typeof data.toJSON === "function") {
18+
const json = data.toJSON();
19+
20+
if (Array.isArray(json)) {
21+
return data.map(sanitizeNumbers);
22+
}
23+
24+
if (json instanceof Object) {
25+
const obj = {};
26+
27+
for (const key of Object.keys(json)) {
28+
obj[key] = sanitizeNumbers(data[key]);
29+
}
30+
31+
return obj;
32+
}
33+
34+
return json;
35+
}
36+
37+
if (Array.isArray(data)) {
38+
return data.map(sanitizeNumbers);
39+
}
40+
41+
const obj = {};
42+
for (const key of Object.keys(data)) {
43+
obj[key] = sanitizeNumbers(data[key]);
44+
}
45+
return obj;
46+
}
47+
48+
return data;
49+
}

yarn.lock

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@
99
dependencies:
1010
regenerator-runtime "^0.13.4"
1111

12-
"@polkadot/api-derive@1.9.0-beta.26":
13-
version "1.9.0-beta.26"
14-
resolved "https://registry.yarnpkg.com/@polkadot/api-derive/-/api-derive-1.9.0-beta.26.tgz#6af50d2999fe989dca0a80b5e31d33b86eebce9e"
15-
integrity sha512-M16rvepqDitPBP0OqUus8p8qK/zb5ySYVRrxfiHSDq5nrmD0y6d4GxsdQWBMvKBZklhhxV4FEac6BoXSTfGgMw==
12+
"@polkadot/api-derive@1.9.1":
13+
version "1.9.1"
14+
resolved "https://registry.yarnpkg.com/@polkadot/api-derive/-/api-derive-1.9.1.tgz#0b0fb580d3d5c42570207ee36a8652b21f51a556"
15+
integrity sha512-aktzdVHpEPGqtRNCjjuSFS9Crt5S1JSDYDKq6BvK0tDNAxfNfeD2e73P9kPPNhuC8R50bD5sMmSvsJS0PQTULw==
1616
dependencies:
1717
"@babel/runtime" "^7.9.2"
18-
"@polkadot/api" "1.9.0-beta.26"
19-
"@polkadot/rpc-core" "1.9.0-beta.26"
20-
"@polkadot/rpc-provider" "1.9.0-beta.26"
21-
"@polkadot/types" "1.9.0-beta.26"
18+
"@polkadot/api" "1.9.1"
19+
"@polkadot/rpc-core" "1.9.1"
20+
"@polkadot/rpc-provider" "1.9.1"
21+
"@polkadot/types" "1.9.1"
2222
"@polkadot/util" "^2.7.1"
2323
"@polkadot/util-crypto" "^2.7.1"
2424
bn.js "^5.1.1"
2525
memoizee "^0.4.14"
2626
rxjs "^6.5.4"
2727

28-
"@polkadot/api@1.9.0-beta.26":
29-
version "1.9.0-beta.26"
30-
resolved "https://registry.yarnpkg.com/@polkadot/api/-/api-1.9.0-beta.26.tgz#97bb595df13cdadcca997e50485e0088886e867d"
31-
integrity sha512-g0m2YrIfWELeaDYLQ0Y3M6+mWupA2aoUknLkZHPcFHBQkHBIqSwf5Sq3BsNG/ifXiGM452oWhFoiSBSRY0dqRQ==
28+
"@polkadot/api@1.9.1":
29+
version "1.9.1"
30+
resolved "https://registry.yarnpkg.com/@polkadot/api/-/api-1.9.1.tgz#10956c3c07bf6ba132f94722bd4bc23d536a7296"
31+
integrity sha512-dJlWvpilZCVdRgZ2HsG6fwkNGuHomfqHfjMMPPz1yLdnMybWpsWYxK5cYfYZ3Y9EI0KSv0W+ZU0o6LnQyE/lAA==
3232
dependencies:
3333
"@babel/runtime" "^7.9.2"
34-
"@polkadot/api-derive" "1.9.0-beta.26"
34+
"@polkadot/api-derive" "1.9.1"
3535
"@polkadot/keyring" "^2.7.1"
36-
"@polkadot/metadata" "1.9.0-beta.26"
37-
"@polkadot/rpc-core" "1.9.0-beta.26"
38-
"@polkadot/rpc-provider" "1.9.0-beta.26"
39-
"@polkadot/types" "1.9.0-beta.26"
40-
"@polkadot/types-known" "1.9.0-beta.26"
36+
"@polkadot/metadata" "1.9.1"
37+
"@polkadot/rpc-core" "1.9.1"
38+
"@polkadot/rpc-provider" "1.9.1"
39+
"@polkadot/types" "1.9.1"
40+
"@polkadot/types-known" "1.9.1"
4141
"@polkadot/util" "^2.7.1"
4242
"@polkadot/util-crypto" "^2.7.1"
4343
bn.js "^5.1.1"
@@ -53,63 +53,63 @@
5353
"@polkadot/util" "2.7.1"
5454
"@polkadot/util-crypto" "2.7.1"
5555

56-
"@polkadot/metadata@1.9.0-beta.26":
57-
version "1.9.0-beta.26"
58-
resolved "https://registry.yarnpkg.com/@polkadot/metadata/-/metadata-1.9.0-beta.26.tgz#468f8751803720f38136d3a9b712b2b1fac9a6ba"
59-
integrity sha512-865jF6V4XH+Bh4Zv1duRFXAzEpOQjk0oZnYWxrdzMJYI3gLoazHE4N70cgbMsBl7dKOMCFFqgecuk3URjhIAJQ==
56+
"@polkadot/metadata@1.9.1":
57+
version "1.9.1"
58+
resolved "https://registry.yarnpkg.com/@polkadot/metadata/-/metadata-1.9.1.tgz#8f2453fe981bbbc6a3aba333f209195e5faff166"
59+
integrity sha512-xGDto2AGSXRx3nMfqLIK0D15gJwuowL4D+RdLFC7rA6pMBCM2Ja4uVDD+f76IN0aF+/1p+4dTN1FGkuNMPSqAg==
6060
dependencies:
6161
"@babel/runtime" "^7.9.2"
62-
"@polkadot/types" "1.9.0-beta.26"
63-
"@polkadot/types-known" "1.9.0-beta.26"
62+
"@polkadot/types" "1.9.1"
63+
"@polkadot/types-known" "1.9.1"
6464
"@polkadot/util" "^2.7.1"
6565
"@polkadot/util-crypto" "^2.7.1"
6666
bn.js "^5.1.1"
6767

68-
"@polkadot/rpc-core@1.9.0-beta.26":
69-
version "1.9.0-beta.26"
70-
resolved "https://registry.yarnpkg.com/@polkadot/rpc-core/-/rpc-core-1.9.0-beta.26.tgz#b57adacd8eacd78b155cc25afaeecda5c0d7a40c"
71-
integrity sha512-PaEIxroRa8v9oyfq3FHWs+iXMMDFm8GHuHHrMWgWgMe18ZZHD80f/dV4DzppsVYFHi/bfPGuoJI5i2UZ+EmpCQ==
68+
"@polkadot/rpc-core@1.9.1":
69+
version "1.9.1"
70+
resolved "https://registry.yarnpkg.com/@polkadot/rpc-core/-/rpc-core-1.9.1.tgz#40cab586bba133a0a80481a8a4f81082e62bf08d"
71+
integrity sha512-9CnhCSF4maNzaD6n9O2aYQvbEqqHIZdA2XOFZ+Tjauo2YOvgskC7VAD3gjS2aJ8HcwKY2vaRpgp/lRFnt8+rMg==
7272
dependencies:
7373
"@babel/runtime" "^7.9.2"
74-
"@polkadot/metadata" "1.9.0-beta.26"
75-
"@polkadot/rpc-provider" "1.9.0-beta.26"
76-
"@polkadot/types" "1.9.0-beta.26"
74+
"@polkadot/metadata" "1.9.1"
75+
"@polkadot/rpc-provider" "1.9.1"
76+
"@polkadot/types" "1.9.1"
7777
"@polkadot/util" "^2.7.1"
7878
memoizee "^0.4.14"
7979
rxjs "^6.5.4"
8080

81-
"@polkadot/rpc-provider@1.9.0-beta.26":
82-
version "1.9.0-beta.26"
83-
resolved "https://registry.yarnpkg.com/@polkadot/rpc-provider/-/rpc-provider-1.9.0-beta.26.tgz#8b1f0c46c93506d15bdcc5a846b1019402da9ac1"
84-
integrity sha512-2MmnalVowxXmK+gcv143oiPWNr1Vkg00UcRtjvOcrfZp0WC2T3Zz5VZlRjD49qdsoVDuFpLONlQZPjSNs7ERjQ==
81+
"@polkadot/rpc-provider@1.9.1":
82+
version "1.9.1"
83+
resolved "https://registry.yarnpkg.com/@polkadot/rpc-provider/-/rpc-provider-1.9.1.tgz#06c511a3a71b5ec2c5b15b0e07c6203f01e4c994"
84+
integrity sha512-HW3p9Z3eltqot1qReEwO8EcyBeyh+GZdp3Mb5l1cH9rjKm7GTc7tPpGgLzQP1DBgJMdUpxlPfZ4wZHJO6JresA==
8585
dependencies:
8686
"@babel/runtime" "^7.9.2"
87-
"@polkadot/metadata" "1.9.0-beta.26"
88-
"@polkadot/types" "1.9.0-beta.26"
87+
"@polkadot/metadata" "1.9.1"
88+
"@polkadot/types" "1.9.1"
8989
"@polkadot/util" "^2.7.1"
9090
"@polkadot/util-crypto" "^2.7.1"
9191
bn.js "^5.1.1"
9292
eventemitter3 "^4.0.0"
9393
isomorphic-fetch "^2.2.1"
9494
websocket "^1.0.31"
9595

96-
"@polkadot/types-known@1.9.0-beta.26":
97-
version "1.9.0-beta.26"
98-
resolved "https://registry.yarnpkg.com/@polkadot/types-known/-/types-known-1.9.0-beta.26.tgz#d85476f42922e3a67ef24b4770011bc1371c8511"
99-
integrity sha512-wqkQoSTkXbvJxLMiGeh2IWRk7PM1a4oC/XvxhFNmM6fHJZ7pzTIwNQUy0g1GM2/lzwpCvkE6tnUYENStAyz9RQ==
96+
"@polkadot/types-known@1.9.1":
97+
version "1.9.1"
98+
resolved "https://registry.yarnpkg.com/@polkadot/types-known/-/types-known-1.9.1.tgz#38f1d1ebaf77824066e9b78f82f336c269cc9d89"
99+
integrity sha512-G0YUfEmrr9yW0Iz5SXXsO6zpkALUTLf4pocNY9ISeCjrUcdfk+Rt8OnsI2DRVC8Qjaf9a4rb9lC2HIMTqFJjgQ==
100100
dependencies:
101101
"@babel/runtime" "^7.9.2"
102-
"@polkadot/types" "1.9.0-beta.26"
102+
"@polkadot/types" "1.9.1"
103103
"@polkadot/util" "^2.7.1"
104104
bn.js "^5.1.1"
105105

106-
"@polkadot/types@1.9.0-beta.26":
107-
version "1.9.0-beta.26"
108-
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-1.9.0-beta.26.tgz#a2e17eb7a65178f4259134f029b3606df3ab7694"
109-
integrity sha512-KqOg2oLOHQdLn34JpgdexXcWbvC3FLVQ0r7fFHU0VOqPSCIfolyVyZR7F7XUYLDiGTZaRDWYtUS4oEEKYlnHMw==
106+
"@polkadot/types@1.9.1":
107+
version "1.9.1"
108+
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-1.9.1.tgz#88d426241ace9e6ec3a2ce3e1a51d8333ad28822"
109+
integrity sha512-c1mzuxJiOPIWrkEWMRdT2seW21lEC3C+EetshgCMXpcsAv5iQeB59J1fTbRiSsQIESKIiP/zv+Igpj7wO/2hMw==
110110
dependencies:
111111
"@babel/runtime" "^7.9.2"
112-
"@polkadot/metadata" "1.9.0-beta.26"
112+
"@polkadot/metadata" "1.9.1"
113113
"@polkadot/util" "^2.7.1"
114114
"@polkadot/util-crypto" "^2.7.1"
115115
"@types/bn.js" "^4.11.6"

0 commit comments

Comments
 (0)