-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
util.ts
50 lines (43 loc) · 1.51 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { BytesLike, DataOptions } from '@ethersproject/bytes';
import { hexlify, arrayify, concat } from '@ethersproject/bytes';
import { sha256 } from '@ethersproject/sha2';
import { calcRoot } from '@fuel-ts/merkle';
import SparseMerkleTree from '@fuel-ts/sparsemerkle';
import type { StorageSlot } from '@fuel-ts/transactions';
export const getContractRoot = (bytecode: Uint8Array): string => {
const chunkSize = 8;
const chunks: Uint8Array[] = [];
for (let offset = 0; offset < bytecode.length; offset += chunkSize) {
const chunk = new Uint8Array(chunkSize);
chunk.set(bytecode.slice(offset, offset + chunkSize));
chunks.push(chunk);
}
return calcRoot(chunks.map((c) => hexlify(c)));
};
export const getContractStorageRoot = (storageSlots: StorageSlot[]): string => {
const tree = new SparseMerkleTree();
storageSlots.forEach(({ key, value }) => tree.update(key, value));
return tree.root;
};
export const getContractId = (
bytecode: BytesLike,
salt: BytesLike,
stateRoot: BytesLike
): string => {
const root = getContractRoot(arrayify(bytecode));
const contractId = sha256(concat(['0x4655454C', salt, root, stateRoot]));
return contractId;
};
/**
* Generic assert function to avoid undesirable errors
*/
export function assert(condition: unknown, message: string): asserts condition {
if (!condition) {
throw new Error(message);
}
}
export const includeHexPrefix = (value: string, options?: DataOptions) =>
hexlify(value, {
...options,
allowMissingPrefix: true,
});