mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Update to use checksum instead of code_id
This commit is contained in:
parent
ae587c40d4
commit
64cf77532a
@ -1,6 +1,7 @@
|
|||||||
import { fromBech32, fromUtf8, toAscii, toBech32, toHex, toUtf8 } from "@cosmjs/encoding";
|
import { fromBech32, fromHex, fromUtf8, toAscii, toBech32, toHex, toUtf8 } from "@cosmjs/encoding";
|
||||||
import { Sha256, sha256 } from "@cosmjs/crypto";
|
import { Sha256, sha256 } from "@cosmjs/crypto";
|
||||||
import { Uint53, Uint64 } from "@cosmjs/math";
|
import { Uint53 } from "@cosmjs/math";
|
||||||
|
import { assert } from "@cosmjs/utils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The "Basic Address" Hash from
|
* The "Basic Address" Hash from
|
||||||
@ -22,15 +23,16 @@ function toUint8(int: number): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** See https://github.com/CosmWasm/wasmd/issues/942 */
|
/** See https://github.com/CosmWasm/wasmd/issues/942 */
|
||||||
function deterministicContractAddress(codeId: number, creator: string, label: string, prefix: string) {
|
function deterministicContractAddress(checksum: Uint8Array, creator: string, label: string, prefix: string) {
|
||||||
const codeIdUint64 = Uint64.fromNumber(codeId);
|
assert(checksum.length === 32);
|
||||||
const creatorData = fromBech32(creator).data;
|
const creatorData = fromBech32(creator).data;
|
||||||
const labelData = toUtf8(label);
|
const labelData = toUtf8(label);
|
||||||
|
|
||||||
const key = new Uint8Array([
|
const key = new Uint8Array([
|
||||||
...toAscii("wasm"),
|
...toAscii("wasm"),
|
||||||
0x00,
|
0x00,
|
||||||
...codeIdUint64.toBytesBigEndian(),
|
toUint8(checksum.length),
|
||||||
|
...checksum,
|
||||||
toUint8(creatorData.length),
|
toUint8(creatorData.length),
|
||||||
...creatorData,
|
...creatorData,
|
||||||
toUint8(labelData.length),
|
toUint8(labelData.length),
|
||||||
@ -43,28 +45,33 @@ function deterministicContractAddress(codeId: number, creator: string, label: st
|
|||||||
function makeTestingAddress(length: number): string {
|
function makeTestingAddress(length: number): string {
|
||||||
let data = new Uint8Array(length);
|
let data = new Uint8Array(length);
|
||||||
data.fill(0x99, 0);
|
data.fill(0x99, 0);
|
||||||
data.fill(0xAA, 5);
|
data.fill(0xaa, 5);
|
||||||
data.fill(0xBB, 10);
|
data.fill(0xbb, 10);
|
||||||
data.fill(0xCC, 15);
|
data.fill(0xcc, 15);
|
||||||
data.fill(0xDD, 20);
|
data.fill(0xdd, 20);
|
||||||
data.fill(0xEE, 25);
|
data.fill(0xee, 25);
|
||||||
data.fill(0xFF, 30);
|
data.fill(0xff, 30);
|
||||||
return toBech32("purple", data);
|
return toBech32("purple", data);
|
||||||
}
|
}
|
||||||
|
|
||||||
let out: Array<any> = [];
|
let out: Array<any> = [];
|
||||||
|
|
||||||
for (let codeId of [1, Number.MAX_SAFE_INTEGER]) {
|
const checksums = [
|
||||||
|
fromHex("13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2a5"),
|
||||||
|
fromHex("1da6c16de2cbaf7ad8cbb66f0925ba33f5c278cb2491762d04658c1480ea229b"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (let checksum of checksums) {
|
||||||
for (let creator of [makeTestingAddress(20), makeTestingAddress(32)]) {
|
for (let creator of [makeTestingAddress(20), makeTestingAddress(32)]) {
|
||||||
for (let label of ["instance 1", "instance 2"]) {
|
for (let label of ["instance 1", "instance 2"]) {
|
||||||
const contractAddress = deterministicContractAddress(codeId, creator, label, "purple");
|
const contractAddress = deterministicContractAddress(checksum, creator, label, "purple");
|
||||||
|
|
||||||
out.push({
|
out.push({
|
||||||
in: {
|
in: {
|
||||||
codeId,
|
checksum: toHex(checksum).toUpperCase(),
|
||||||
label,
|
|
||||||
creator,
|
creator,
|
||||||
creatorData: toHex(fromBech32(creator).data).toUpperCase(),
|
creatorData: toHex(fromBech32(creator).data).toUpperCase(),
|
||||||
|
label,
|
||||||
},
|
},
|
||||||
out: {
|
out: {
|
||||||
contractAddress,
|
contractAddress,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user