Skip to content

Commit

Permalink
[Feature] Remove null property from wasm execute msg (#120)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yys authored Aug 20, 2021
1 parent e75356f commit 8998d13
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
5 changes: 3 additions & 2 deletions src/core/wasm/msgs/MsgExecuteContract.ts
Original file line number Diff line number Diff line change
@@ -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<MsgExecuteContract.Data> {
Expand Down Expand Up @@ -34,12 +34,13 @@ export class MsgExecuteContract extends JSONSerializable<MsgExecuteContract.Data

public toData(): MsgExecuteContract.Data {
const { sender, contract, execute_msg, coins } = this;

return {
type: 'wasm/MsgExecuteContract',
value: {
sender,
contract,
execute_msg,
execute_msg: removeNull(execute_msg),
coins: coins.toData(),
},
};
Expand Down
6 changes: 3 additions & 3 deletions src/core/wasm/msgs/MsgInstantiateContract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONSerializable } from '../../../util/json';
import { JSONSerializable, removeNull } from '../../../util/json';
import { AccAddress } from '../../bech32';
import { Coins } from '../../Coins';

Expand Down Expand Up @@ -44,9 +44,9 @@ export class MsgInstantiateContract extends JSONSerializable<MsgInstantiateContr
type: 'wasm/MsgInstantiateContract',
value: {
sender,
admin,
admin: admin === '' || admin === null ? undefined : admin,
code_id: code_id.toFixed(),
init_msg,
init_msg: removeNull(init_msg),
init_coins: init_coins.toData(),
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/core/wasm/msgs/MsgMigrateContract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONSerializable } from '../../../util/json';
import { JSONSerializable, removeNull } from '../../../util/json';
import { AccAddress } from '../../bech32';
export class MsgMigrateContract extends JSONSerializable<MsgMigrateContract.Data> {
/**
Expand Down Expand Up @@ -36,7 +36,7 @@ export class MsgMigrateContract extends JSONSerializable<MsgMigrateContract.Data
admin,
contract,
new_code_id: new_code_id.toFixed(),
migrate_msg,
migrate_msg: removeNull(migrate_msg),
},
};
}
Expand Down
36 changes: 36 additions & 0 deletions src/util/json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { removeNull } from './json';

describe('removeNull', () => {
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,
},
});
});
});
14 changes: 14 additions & 0 deletions src/util/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ export abstract class JSONSerializable<T> {
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;
}

0 comments on commit 8998d13

Please sign in to comment.