@effectstream/precompile 0.100.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -0
- package/src/mod.ts +26 -0
package/package.json
ADDED
package/src/mod.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import sha3 from "js-sha3";
|
|
2
|
+
const { keccak_256 } = sha3;
|
|
3
|
+
import type { HexString0x } from "@effectstream/utils";
|
|
4
|
+
|
|
5
|
+
export function generatePrecompile<T extends string>(name: T): HexString0x {
|
|
6
|
+
// trim to 20 bytes to have evm-sized addresses
|
|
7
|
+
const hash = keccak_256(name).slice(0, 40);
|
|
8
|
+
|
|
9
|
+
return `0x${hash}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type EnumToPrecompile<T extends Record<string, string>> = {
|
|
13
|
+
[K in keyof T as T[K]]: HexString0x;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function generatePrecompiles<T extends Record<string, string>>(
|
|
17
|
+
names: T,
|
|
18
|
+
): EnumToPrecompile<T> {
|
|
19
|
+
if (names == null) return {} as EnumToPrecompile<T>; // this happens if you have an empty enum like `enum PrecompileNames {}`
|
|
20
|
+
|
|
21
|
+
const result: Record<string, string> = {};
|
|
22
|
+
for (const val of Object.values(names)) {
|
|
23
|
+
result[val] = generatePrecompile(val);
|
|
24
|
+
}
|
|
25
|
+
return result as EnumToPrecompile<T>;
|
|
26
|
+
}
|