diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 000000000..8229ba906 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +src/util/jsonabc.js +src/util/jsonabc.d.ts \ No newline at end of file diff --git a/package.json b/package.json index aa1881d0c..0b2ff1a38 100644 --- a/package.json +++ b/package.json @@ -83,10 +83,8 @@ "@polkadot/types-known": "^1.23.1", "@polkadot/util": "^2.17.1", "@polkadot/util-crypto": "^2.17.1", - "@types/jsonabc": "^2.3.1", "ajv": "^6.12.2", "bn.js": "^5.1.2", - "jsonabc": "^2.3.1", "typescript-logging": "^0.6.4", "uuid": "^8.1.0" }, diff --git a/src/claim/Claim.utils.ts b/src/claim/Claim.utils.ts index 34c3f11ff..885c48df1 100644 --- a/src/claim/Claim.utils.ts +++ b/src/claim/Claim.utils.ts @@ -4,7 +4,7 @@ * @preferred */ -import * as jsonabc from 'jsonabc' +import jsonabc from '../util/jsonabc' import * as SDKErrors from '../errorhandling/SDKErrors' import IClaim, { CompressedClaim } from '../types/Claim' import { validateAddress, validateHash } from '../util/DataUtils' diff --git a/src/crypto/Crypto.ts b/src/crypto/Crypto.ts index 92938726b..bcb894797 100644 --- a/src/crypto/Crypto.ts +++ b/src/crypto/Crypto.ts @@ -22,8 +22,8 @@ import { signatureVerify } from '@polkadot/util-crypto' import blake2AsU8a from '@polkadot/util-crypto/blake2/asU8a' import naclDecrypt from '@polkadot/util-crypto/nacl/decrypt' import naclEncrypt from '@polkadot/util-crypto/nacl/encrypt' -import * as jsonabc from 'jsonabc' import nacl from 'tweetnacl' +import jsonabc from '../util/jsonabc' export { encodeAddress, decodeAddress, u8aToHex, u8aConcat } diff --git a/src/ctype/CType.utils.ts b/src/ctype/CType.utils.ts index 6ead5c669..9248dbc08 100644 --- a/src/ctype/CType.utils.ts +++ b/src/ctype/CType.utils.ts @@ -5,7 +5,7 @@ */ import Ajv from 'ajv' -import * as jsonabc from 'jsonabc' +import jsonabc from '../util/jsonabc' import Crypto from '../crypto' import * as SDKErrors from '../errorhandling/SDKErrors' import IClaim from '../types/Claim' diff --git a/src/requestforattestation/RequestForAttestation.utils.ts b/src/requestforattestation/RequestForAttestation.utils.ts index 2fed92040..9c6649fea 100644 --- a/src/requestforattestation/RequestForAttestation.utils.ts +++ b/src/requestforattestation/RequestForAttestation.utils.ts @@ -4,7 +4,7 @@ * @preferred */ -import * as jsonabc from 'jsonabc' +import jsonabc from '../util/jsonabc' import AttestedClaimUtils from '../attestedclaim/AttestedClaim.utils' import ClaimUtils from '../claim/Claim.utils' import * as SDKErrors from '../errorhandling/SDKErrors' diff --git a/src/util/jsonabc.d.ts b/src/util/jsonabc.d.ts new file mode 100644 index 000000000..4b8074c7f --- /dev/null +++ b/src/util/jsonabc.d.ts @@ -0,0 +1,21 @@ +// Type definitions for jsonabc 2.3 +// Project: http://novicelab.org/jsonabc +// Definitions by: Florian Keller +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 +/* eslint-disable */ + +/** + * Sort the JSON (clean, parse, sort, stringify). + * @param noArray Sort or don't sort arrays + */ +export function sort(inputStr: string, noArray?: boolean): string; + +/** + * Sort the JSON (clean, parse, sort, stringify). + * @param noArray Sort or don't sort arrays + */ +export function sortObj(input: T, noArray?: boolean): T; + +/** Remove trailing commas */ +export function cleanJSON(input: string): string; diff --git a/src/util/jsonabc.js b/src/util/jsonabc.js new file mode 100644 index 000000000..6cc97730f --- /dev/null +++ b/src/util/jsonabc.js @@ -0,0 +1,93 @@ +// Taken from https://github.com/ShivrajRath/jsonabc/blob/2ccf15f967f0e44e48fb7b163aebef43c0047166/index.js +// Copied here, because the package defines a browser compatible script, but doesn't create it, +// which leads to it failing in react-native. See https://github.com/ShivrajRath/jsonabc/issues/18 +/* eslint-disable */ + + +/*! + JSON ABC | License: MIT. +*/ + +module.exports = { + sort: sort, + sortObj: sortObj, + cleanJSON: cleanJSON +}; + +// Is a value an array? +function isArray (val) { + return Object.prototype.toString.call(val) === '[object Array]'; +} + +// Is a value an Object? +function isPlainObject (val) { + return Object.prototype.toString.call(val) === '[object Object]'; +} + +// Sorting Logic +function sortObj (un, noarray) { + noarray = noarray || false; + + var or = {}; + + if (isArray(un)) { + // Sort or don't sort arrays + if (noarray) { + or = un; + } else { + or = un.sort(); + } + + or.forEach(function (v, i) { + or[i] = sortObj(v, noarray); + }); + + if (!noarray) { + or = or.sort(function (a, b) { + a = JSON.stringify(a); + b = JSON.stringify(b); + return a < b ? -1 : (a > b ? 1 : 0); + }); + } + } else if (isPlainObject(un)) { + or = {}; + Object.keys(un).sort(function (a, b) { + if (a.toLowerCase() < b.toLowerCase()) return -1; + if (a.toLowerCase() > b.toLowerCase()) return 1; + return 0; + }).forEach(function (key) { + or[key] = sortObj(un[key], noarray); + }); + } else { + or = un; + } + + return or; +} + +// Remove trailing commas +function cleanJSON (input) { + input = input.replace(/,[ \t\r\n]+}/g, '}'); + input = input.replace(/,[ \t\r\n]+\]/g, ']'); + return input; +} + +// Sort the JSON (clean, parse, sort, stringify). +function sort (inputStr, noarray) { + var output, obj, r; + + if (inputStr) { + try { + inputStr = cleanJSON(inputStr); + obj = JSON.parse(inputStr); + r = sortObj(obj, noarray); + output = JSON.stringify(r, null, 4); + } catch (ex) { + console.error('jsonabc: Incorrect JSON object.', [], ex); + throw ex; + } + } + return output; +} + +// End. diff --git a/yarn.lock b/yarn.lock index 132ee6b3a..918ee931b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -879,11 +879,6 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= -"@types/jsonabc@^2.3.1": - version "2.3.1" - resolved "https://registry.yarnpkg.com/@types/jsonabc/-/jsonabc-2.3.1.tgz#3aa5d3f938331ef2472d3ea56c6b4c805bef6198" - integrity sha512-SOR8DMQQVUmk3JnNp7FU75U+vp/VcFcghvS68mUQEuq4FKDANIQ9dxDA5qEQMDahBCd5Vyhk81ywn0JbcLpZZw== - "@types/minimist@^1.2.0": version "1.2.0" resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6" @@ -3693,11 +3688,6 @@ json5@^1.0.1: dependencies: minimist "^1.2.0" -jsonabc@^2.3.1: - version "2.3.1" - resolved "https://registry.yarnpkg.com/jsonabc/-/jsonabc-2.3.1.tgz#800a1bd158fb30ace2b4613f8725adfcb040bfb4" - integrity sha1-gAob0Vj7MKzitGE/hyWt/LBAv7Q= - jsonfile@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"