From 8998d13a06c1b2a86587f91102cf9facf4a2e3af Mon Sep 17 00:00:00 2001 From: yys Date: Fri, 20 Aug 2021 17:18:44 +0900 Subject: [PATCH] [Feature] Remove null property from wasm execute msg (#120) * remove null property from wasm msg to prevent signature verification failed * put undefined to instantiate.admin when admin is empty string * update version to v2.0.2 --- package-lock.json | 4 +-- package.json | 2 +- src/core/wasm/msgs/MsgExecuteContract.ts | 5 +-- src/core/wasm/msgs/MsgInstantiateContract.ts | 6 ++-- src/core/wasm/msgs/MsgMigrateContract.ts | 4 +-- src/util/json.spec.ts | 36 ++++++++++++++++++++ src/util/json.ts | 14 ++++++++ 7 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 src/util/json.spec.ts diff --git a/package-lock.json b/package-lock.json index c2b19b515..69ecd61f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@terra-money/terra.js", - "version": "2.0.1", + "version": "2.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@terra-money/terra.js", - "version": "2.0.1", + "version": "2.0.2", "license": "MIT", "dependencies": { "axios": "^0.21.1", diff --git a/package.json b/package.json index e3ccb421d..9c02c65ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@terra-money/terra.js", - "version": "2.0.1", + "version": "2.0.2", "description": "The JavaScript SDK for Terra", "license": "MIT", "author": "Terraform Labs, PTE.", diff --git a/src/core/wasm/msgs/MsgExecuteContract.ts b/src/core/wasm/msgs/MsgExecuteContract.ts index ab2a3437d..0c668a647 100644 --- a/src/core/wasm/msgs/MsgExecuteContract.ts +++ b/src/core/wasm/msgs/MsgExecuteContract.ts @@ -1,4 +1,4 @@ -import { JSONSerializable } from '../../../util/json'; +import { JSONSerializable, removeNull } from '../../../util/json'; import { AccAddress } from '../../bech32'; import { Coins } from '../../Coins'; export class MsgExecuteContract extends JSONSerializable { @@ -34,12 +34,13 @@ export class MsgExecuteContract extends JSONSerializable { /** @@ -36,7 +36,7 @@ export class MsgMigrateContract extends JSONSerializable { + it('remove null ', () => { + expect( + removeNull({ + a: 'abc', + b: { + a: null, + b: 123, + }, + c: null, + d: [123], + e: { + a: { + a: null, + b: 'abc', + }, + b: 123, + }, + }) + ).toEqual({ + a: 'abc', + b: { + b: 123, + }, + d: [123], + e: { + a: { + b: 'abc', + }, + b: 123, + }, + }); + }); +}); diff --git a/src/util/json.ts b/src/util/json.ts index 67da8ef1c..c528f9261 100644 --- a/src/util/json.ts +++ b/src/util/json.ts @@ -24,3 +24,17 @@ export abstract class JSONSerializable { return JSON.stringify(prepareSignBytes(this.toData())); } } + +export function removeNull(obj: any): any { + if (obj !== null && typeof obj === 'object') { + Object.keys(obj).forEach(function (key) { + if (obj[key] === null) { + delete obj[key]; + } else if (typeof obj[key] === 'object') { + removeNull(obj[key]); + } + }); + } + + return obj; +}