Make msg a string | null

This commit is contained in:
Simon Warta 2022-10-18 14:42:09 +02:00
parent ee5429d1f4
commit 652c74d159
2 changed files with 12 additions and 7 deletions

View File

@ -31,12 +31,11 @@ for (let checksum of checksums) {
for (let creator of [makeTestingAddress(20), makeTestingAddress(32)]) {
for (let salt of salts) {
for (let msg of msgs) {
const encodedMsg = typeof msg === "string" ? toUtf8(msg) : new Uint8Array();
const { key, addressData, address } = _instantiate2AddressIntermediate(
checksum,
creator,
salt,
encodedMsg,
msg,
"purple",
);
out.push({

View File

@ -1,5 +1,5 @@
import { Sha256, sha256 } from "@cosmjs/crypto";
import { fromBech32, toAscii, toBech32 } from "@cosmjs/encoding";
import { fromBech32, toAscii, toBech32, toUtf8 } from "@cosmjs/encoding";
import { Uint64 } from "@cosmjs/math";
import { assert } from "@cosmjs/utils";
@ -27,12 +27,14 @@ export function _instantiate2AddressIntermediate(
checksum: Uint8Array,
creator: string,
salt: Uint8Array,
msg: Uint8Array,
msg: string | null,
prefix: string,
): { key: Uint8Array; addressData: Uint8Array; address: string } {
assert(checksum.length === 32);
const creatorData = fromBech32(creator).data;
const msgData = typeof msg === "string" ? toUtf8(msg) : new Uint8Array();
// Validate inputs
if (salt.length < 1 || salt.length > 64) throw new Error("Salt must be between 1 and 64 bytes");
@ -45,19 +47,23 @@ export function _instantiate2AddressIntermediate(
...creatorData,
...toUint64(salt.length),
...salt,
...toUint64(msg.length),
...msg,
...toUint64(msgData.length),
...msgData,
]);
const addressData = hash("module", key);
const address = toBech32(prefix, addressData);
return { key, addressData, address };
}
/**
* Predictable address generation for the MsgInstantiateContract2
* introduced with wasmd 0.29.
*/
export function instantiate2Address(
checksum: Uint8Array,
creator: string,
salt: Uint8Array,
msg: Uint8Array,
msg: string | null,
prefix: string,
): string {
return _instantiate2AddressIntermediate(checksum, creator, salt, msg, prefix).address;