Skip to content

Commit

Permalink
replace JSON.stringify with a safe stringify (#2690)
Browse files Browse the repository at this point in the history
* fix: replace JSON.stringify with a safe stringify

* chore: rollback safe stringify in doc files
  • Loading branch information
ac10n authored Oct 6, 2023
1 parent 986549b commit c541a29
Show file tree
Hide file tree
Showing 83 changed files with 702 additions and 634 deletions.
2 changes: 1 addition & 1 deletion apps/taquito-test-dapp/src/lib/TestContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
<div style="word-break:break-word;">
<p>{testResult.title}</p>
{#each Object.entries(testResult.body) as [name, res]}
<p>{name}: {JSON.stringify(res)}</p>
<p>{name}: {stringify(res)}</p>
{/each}
</div>
{/if}
Expand Down
10 changes: 5 additions & 5 deletions apps/taquito-test-dapp/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ const signPayload = async (
};
} catch (error) {
console.log(error);
return { success: false, opHash: "", output: JSON.stringify(error) };
return { success: false, opHash: "", output: stringify(error) };
}
};

Expand Down Expand Up @@ -311,7 +311,7 @@ const signPayloadAndSend = async (
sigDetails: { input, formattedInput, bytes: payload.payload }
};
} catch (error) {
return { success: false, opHash: "", output: JSON.stringify(error) };
return { success: false, opHash: "", output: stringify(error) };
}
};

Expand All @@ -330,11 +330,11 @@ const signFailingNoop = async (
success: true,
opHash: "",
output: signedPayload.signature,
sigDetails: { input, bytes: '03' + signedPayload.bytes, formattedInput: JSON.stringify(signedPayload.signedContent) },
sigDetails: { input, bytes: '03' + signedPayload.bytes, formattedInput: stringify(signedPayload.signedContent) },
};
} catch (error) {
console.log(error);
return { success: false, opHash: "", output: JSON.stringify(error) };
return { success: false, opHash: "", output: stringify(error) };
}
};

Expand Down Expand Up @@ -369,7 +369,7 @@ const verifySignatureWithTaquito = async (
throw "Forged signature is incorrect";
}
} catch (error) {
return { success: false, opHash: "", output: JSON.stringify(error) };
return { success: false, opHash: "", output: stringify(error) };
}
};

Expand Down
9 changes: 5 additions & 4 deletions example/deploy-docs-live-code-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { contractMap8pairs } from './data/contractMap8pairs';
import { char2Bytes } from '@taquito/utils';
import { fa2Contract } from '../integration-tests/data/fa2_contract';
import BigNumber from 'bignumber.js';
import { stringify } from '@taquito/core';


const provider = 'https://ghostnet.ecadinfra.com/';
Expand Down Expand Up @@ -179,7 +180,7 @@ async function originateTheContracts() {
contract_catalogue.forEach((value, key) => {
jsonObject[key] = value;
});
console.log(JSON.stringify(jsonObject));
console.log(stringify(jsonObject));
}
}

Expand Down Expand Up @@ -584,7 +585,7 @@ async function originateTzip16Storage() {

const metadataBigMap = new MichelsonMap();
metadataBigMap.set('', char2Bytes('tezos-storage:here'));
metadataBigMap.set('here', char2Bytes(JSON.stringify(metadataJSON)));
metadataBigMap.set('here', char2Bytes(stringify(metadataJSON)));

const tacoShopStorageMap = new MichelsonMap();

Expand Down Expand Up @@ -696,7 +697,7 @@ async function originateTzip16OnChainJSON() {
try {
const metadataBigMAp = new MichelsonMap();
metadataBigMAp.set('', char2Bytes('tezos-storage:here'));
metadataBigMAp.set('here', char2Bytes(JSON.stringify(metadataViewsExample1)));
metadataBigMAp.set('here', char2Bytes(stringify(metadataViewsExample1)));

const op = await tezos.contract.originate({
code: contractCode,
Expand All @@ -719,7 +720,7 @@ async function originateTzip16OnChainMultiply() {
try {
const metadataBigMAp = new MichelsonMap();
metadataBigMAp.set('', char2Bytes('tezos-storage:here'));
metadataBigMAp.set('here', char2Bytes(JSON.stringify(metadataViewsExample2)));
metadataBigMAp.set('here', char2Bytes(stringify(metadataViewsExample2)));

const op = await tezos.contract.originate({
code: contractCode,
Expand Down
5 changes: 3 additions & 2 deletions example/encode-michelson.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stringify } from '@taquito/core';
import { Parser } from '@taquito/michel-codec'

const example = async () => {
Expand All @@ -15,11 +16,11 @@ const example = async () => {

console.log('Example 1')
const exp1 = p.parseMichelineExpression(ex1)
console.log(JSON.stringify(exp1))
console.log(stringify(exp1))

console.log('Example 2')
const exp2 = p.parseMichelineExpression(ex2)
console.log(JSON.stringify(exp2))
console.log(stringify(exp2))

} catch (ex) {
console.log(ex)
Expand Down
3 changes: 2 additions & 1 deletion example/example-bigmap-keys-as-deep-pair.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stringify } from '@taquito/core';
import { Parser } from '@taquito/michel-codec'
import { Schema } from '@taquito/michelson-encoder';

Expand All @@ -19,7 +20,7 @@ const example = async () => {
const parsed_storage: any = p.parseMichelineExpression(example_storage_in_michelson )

const schema = new Schema(parsed_storage);
console.log(JSON.stringify(schema.ExtractSchema(), null, 2))
console.log(stringify(schema.ExtractSchema(), null, 2))
} catch (ex) {
console.log(ex)
}
Expand Down
3 changes: 2 additions & 1 deletion example/example-bigmap-keys-as-pair.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stringify } from '@taquito/core';
import { Parser } from '@taquito/michel-codec'
import { Schema } from '@taquito/michelson-encoder';

Expand All @@ -19,7 +20,7 @@ const example = async () => {
const parsed_storage: any = p.parseMichelineExpression(example_storage_in_michelson )

const schema = new Schema(parsed_storage);
console.log(JSON.stringify(schema.ExtractSchema(), null, 2))
console.log(stringify(schema.ExtractSchema(), null, 2))
} catch (ex) {
console.log(ex)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { char2Bytes } from '@taquito/utils';
import { tacoContractTzip16 } from "../integration-tests/data/modified-taco-contract"
import { MichelsonMap } from "@taquito/taquito";
import { InMemorySigner } from '@taquito/signer';
import { stringify } from '@taquito/core';

async function example() {
const provider = 'https://ghostnet.ecadinfra.com';
Expand Down Expand Up @@ -34,7 +35,7 @@ async function example() {

const metadataBigMAp = new MichelsonMap();
metadataBigMAp.set("", char2Bytes('tezos-storage:here'));
metadataBigMAp.set("here", char2Bytes(JSON.stringify(metadataJSON)))
metadataBigMAp.set("here", char2Bytes(stringify(metadataJSON)))

const tacoShopStorageMap = new MichelsonMap();

Expand Down
3 changes: 2 additions & 1 deletion example/example-tzip-16-on-chain-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MichelsonMap, TezosToolkit } from '@taquito/taquito';
import { contractCode, metadataViewsExample1 } from '../integration-tests/data/metadataViews';
import { char2Bytes } from '@taquito/utils';
import { InMemorySigner } from '@taquito/signer';
import { stringify } from '@taquito/core';

async function example() {
const provider = 'https://ghostnet.ecadinfra.com';
Expand All @@ -14,7 +15,7 @@ async function example() {

const metadataBigMAp = new MichelsonMap();
metadataBigMAp.set("", char2Bytes('tezos-storage:here'));
metadataBigMAp.set("here", char2Bytes(JSON.stringify(metadataViewsExample1)))
metadataBigMAp.set("here", char2Bytes(stringify(metadataViewsExample1)))

const op = await tezos.contract.originate({
code: contractCode,
Expand Down
3 changes: 2 additions & 1 deletion example/example-tzip-16-on-chain-two.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MichelsonMap, TezosToolkit } from '@taquito/taquito';
import { InMemorySigner } from '@taquito/signer';
import { contractCode, metadataViewsExample2 } from '../integration-tests/data/metadataViews';
import { char2Bytes } from '@taquito/utils';
import { stringify } from '@taquito/core';

async function example() {
const provider = 'https://ghostnet.ecadinfra.com';
Expand All @@ -14,7 +15,7 @@ async function example() {

const metadataBigMAp = new MichelsonMap();
metadataBigMAp.set("", char2Bytes('tezos-storage:here'));
metadataBigMAp.set("here", char2Bytes(JSON.stringify(metadataViewsExample2)))
metadataBigMAp.set("here", char2Bytes(stringify(metadataViewsExample2)))

const op = await tezos.contract.originate({
code: contractCode,
Expand Down
3 changes: 2 additions & 1 deletion example/example-tzip-16-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MichelsonMap, TezosToolkit } from '@taquito/taquito';
import { tacoContractTzip16 } from "../integration-tests/data/modified-taco-contract"
import { char2Bytes } from '@taquito/utils';
import { InMemorySigner } from '@taquito/signer';
import { stringify } from '@taquito/core';

async function example() {
const provider = 'https://ghostnet.ecadinfra.com';
Expand All @@ -25,7 +26,7 @@ async function example() {

const metadataBigMap = new MichelsonMap();
metadataBigMap.set("", char2Bytes('tezos-storage:here'));
metadataBigMap.set("here", char2Bytes(JSON.stringify(metadataJSON)))
metadataBigMap.set("here", char2Bytes(stringify(metadataJSON)))

// Ligo Taco shop contract modified to include metadata in storage
// https://ide.ligolang.org/p/-uS469slzUlSm1zwNqHl1A
Expand Down
3 changes: 2 additions & 1 deletion example/example-tzip-16.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stringify } from "@taquito/core";
import { TezosToolkit } from "@taquito/taquito";
import { tzip16, Tzip16Module } from '@taquito/tzip16';

Expand All @@ -8,7 +9,7 @@ async function example() {
tezos.addExtension(new Tzip16Module());
const contract = await tezos.contract.at("KT1JZVozQHLZN7TaACnX6NGBxUkhNjn6tmTB", tzip16)
const metadata = await contract.tzip16().getMetadata();
console.log(JSON.stringify(metadata, null, 2));
console.log(stringify(metadata, null, 2));

} catch (ex) {
console.error(ex);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CONFIGS } from './config';
import { OpKind } from '@taquito/taquito';
import { ligoSample } from './data/ligo-simple-contract';
import { stringify } from '@taquito/core';

CONFIGS().forEach(({ lib, rpc, setup }) => {

Expand All @@ -27,7 +28,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => {

simpleContractAddress = op.contractAddress!;
} catch(e) {
console.log(JSON.stringify(e));
console.log(stringify(e));
}
done();
});
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/contract-lambda-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { CONFIGS } from './config';
import { tzip7Contract } from './data/tzip_7_contract';
import { testContract } from './data/test_lambda_view';
import { fa2Contract } from './data/fa2_contract';
import { stringify } from '@taquito/core';

CONFIGS().forEach(({ lib, rpc, setup }) => {
const Tezos = lib;
const toJSON = (x: any) => JSON.parse(JSON.stringify(x));
const toJSON = (x: any) => JSON.parse(stringify(x));

describe(`Test contract with lambda view trough contract api using: ${rpc}`, () => {
beforeEach(async done => {
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/contract-override-estimate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stringify } from "@taquito/core";
import { CONFIGS } from "./config";
import { InvalidEstimateValueError } from '@taquito/taquito';

Expand All @@ -14,7 +15,7 @@ CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => {
const account = await createAddress();
pkh = await account.signer.publicKeyHash();
} catch(e) {
console.log(JSON.stringify(e));
console.log(stringify(e));
}

done();
Expand Down
5 changes: 3 additions & 2 deletions integration-tests/contract-permits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { permit_fa12_smartpy } from './data/permit_fa12_smartpy';
import { buf2hex, char2Bytes, hex2buf } from '@taquito/utils';
import { tzip16, Tzip16Module } from '@taquito/tzip16';
import { packDataBytes } from "@taquito/michel-codec"
import { stringify } from '@taquito/core';

const blake = require('blakejs');
const bob_address = 'tz1Xk7HkSwHv6dTEgR7E2WC2yFj4cyyuj2Gh';
Expand Down Expand Up @@ -329,9 +330,9 @@ CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => {
try {
await fail_contract.methods.transfer(bootstrap3_address, bootstrap4_address, 1).send();
} catch (errors) {
let jsonStr: string = JSON.stringify(errors);
let jsonStr: string = stringify(errors);
let jsonObj = JSON.parse(jsonStr);
let error_code = JSON.stringify(jsonObj.errors[1].with.int);
let error_code = stringify(jsonObj.errors[1].with.int);
expect((error_code = '26'));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stringify } from '@taquito/core';
import { CONFIGS } from './config';

CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => {
Expand All @@ -21,7 +22,7 @@ CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => {
await register.confirmation();

} catch(e) {
console.log(JSON.stringify(e));
console.log(stringify(e));
}

done();
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/pack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MichelsonType, MichelsonData, ProtocolID, packDataBytes } from "@taquit
import { MichelsonV1Expression } from "@taquito/rpc";
import fs from "fs";
import path from "path";
import { stringify } from "@taquito/core";

interface TypedTestData {
type?: MichelsonType;
Expand All @@ -29,7 +30,7 @@ CONFIGS().forEach(({ rpc, protocol }) => {
describe("Test pack", () => {
for (const s of src) {
const def = (s.proto === undefined || s.proto === protocol) ? test : test.skip;
def(`Verify that .pack for local pack will return same result as for network pack: ${JSON.stringify(s.data)} (${rpc})`, async done => {
def(`Verify that .pack for local pack will return same result as for network pack: ${stringify(s.data)} (${rpc})`, async done => {
const local = packDataBytes(s.data, s.type);
const rpcResult = await Tezos.rpc.packData({ data: s.data, type: s.type as MichelsonV1Expression }, { block: "head" })
expect(local.bytes).toEqual(rpcResult.packed);
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/prepare-operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OperationContentsBallot, OperationContentsTransaction } from '@taquito/
import { OpKind } from '@taquito/taquito';
import { CONFIGS } from './config';
import { LocalForger } from '@taquito/local-forging';
import { stringify } from '@taquito/core';

CONFIGS().forEach(({ lib, setup, protocol, createAddress }) => {
const Tezos = lib;
Expand All @@ -26,7 +27,7 @@ CONFIGS().forEach(({ lib, setup, protocol, createAddress }) => {
contractAddress = op.contractAddress!;

} catch(e: any) {
console.log('Unable to originate contract: ', JSON.stringify(e));
console.log('Unable to originate contract: ', stringify(e));
}

done();
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/rpc-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { encodeExpr } from '@taquito/utils';
import { Schema } from '@taquito/michelson-encoder';
import { tokenBigmapCode, tokenBigmapStorage } from './data/token_bigmap';
import { ticketCode, ticketStorage } from './data/code_with_ticket';
import { stringify } from '@taquito/core';

CONFIGS().forEach(
({
Expand Down Expand Up @@ -38,7 +39,7 @@ CONFIGS().forEach(
const ticketCallOp = await ticketContract.methods.auto_call(1).send();
await ticketCallOp.confirmation();
} catch (e) {
console.log('Failed to originate ticket contract', JSON.stringify(e));
console.log('Failed to originate ticket contract', stringify(e));
}

done();
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/sandbox-drain-delegate-operation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TezosToolkit } from "@taquito/taquito";
import { CONFIGS, sleep } from "./config";
import { stringify } from "@taquito/core";

CONFIGS().forEach(({ lib, rpc, protocol, setup, createAddress }) => {
const Tezos = lib;
Expand Down Expand Up @@ -36,7 +37,7 @@ CONFIGS().forEach(({ lib, rpc, protocol, setup, createAddress }) => {
await sleep(((constants.preserved_cycles + 2) * constants.blocks_per_cycle * (constants.minimal_block_delay!.toNumber())) * 1000);

} catch (e) {
console.log(JSON.stringify(e));
console.log(stringify(e));
}
done();
})
Expand Down
Loading

0 comments on commit c541a29

Please sign in to comment.