Skip to content

Instantly share code, notes, and snippets.

@tubackkhoa
Last active March 12, 2024 05:17
Show Gist options
  • Save tubackkhoa/1cf06f3331975149ac1e2175697ab9f8 to your computer and use it in GitHub Desktop.
Save tubackkhoa/1cf06f3331975149ac1e2175697ab9f8 to your computer and use it in GitHub Desktop.
import { toUtf8, fromBech32, toAscii, toBech32 } from '@cosmjs/encoding';
import { sha256 } from '@cosmjs/crypto';
import { Uint64 } from '@cosmjs/math';
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate';
function increaseBytes(bytes: Uint8Array) {
for (let i = bytes.length - 1; i >= 0; --i) {
if (bytes[i] === 255) {
bytes[i] = 0;
} else {
bytes[i]++;
break;
}
}
}
function toUint64(int: number): Uint8Array {
return Uint64.fromNumber(int).toBytesBigEndian();
}
(async () => {
const prefix = 'orai';
const msg = {};
const saltLength = 8;
const codeId = 1;
const creator = 'orai1xxx';
// get from code id
const client = await CosmWasmClient.connect('https://rpc.orai.io');
const res = await client.getCodeDetails(codeId);
const checksum = Buffer.from(res.checksum, 'hex');
const moduleData = sha256(toAscii('module'));
const wasmData = toAscii('wasm');
const checksumLengthData = toUint64(checksum.length);
const saltLengthData = toUint64(saltLength);
const msgData = toUtf8(JSON.stringify(msg));
const msgLengthData = toUint64(msgData.length);
const creatorData = fromBech32(creator).data;
const creatorLengthData = toUint64(creatorData.length);
const prepareData = new Uint8Array([
...moduleData,
...wasmData,
0x00,
...checksumLengthData,
...checksum,
...creatorLengthData,
...creatorData,
...saltLengthData,
]);
const key = Buffer.from([
...prepareData,
...new Uint8Array(saltLength), // salt buffer
...msgLengthData,
...msgData,
]);
const saltIndex = prepareData.length;
const salt = key.subarray(saltIndex, saltIndex + saltLength);
while (true) {
// change salt part
increaseBytes(salt);
const addressData = sha256(key);
const address = toBech32(prefix, addressData);
// at least 9999 at the end
if (address.match(/9{4,}$/))
console.log(address, 'salt', salt.toString('hex'));
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment