@grimoirelabs/core 0.1.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/dist/builders/expressions.d.ts +33 -0
- package/dist/builders/expressions.d.ts.map +1 -0
- package/dist/builders/expressions.js +57 -0
- package/dist/builders/expressions.js.map +1 -0
- package/dist/builders/index.d.ts +44 -0
- package/dist/builders/index.d.ts.map +1 -0
- package/dist/builders/index.js +32 -0
- package/dist/builders/index.js.map +1 -0
- package/dist/builders/spell-builder.d.ts +124 -0
- package/dist/builders/spell-builder.d.ts.map +1 -0
- package/dist/builders/spell-builder.js +299 -0
- package/dist/builders/spell-builder.js.map +1 -0
- package/dist/builders/step-builder.d.ts +212 -0
- package/dist/builders/step-builder.d.ts.map +1 -0
- package/dist/builders/step-builder.js +499 -0
- package/dist/builders/step-builder.js.map +1 -0
- package/dist/compiler/expression-parser.d.ts +14 -0
- package/dist/compiler/expression-parser.d.ts.map +1 -0
- package/dist/compiler/expression-parser.js +460 -0
- package/dist/compiler/expression-parser.js.map +1 -0
- package/dist/compiler/grimoire/ast.d.ts +450 -0
- package/dist/compiler/grimoire/ast.d.ts.map +1 -0
- package/dist/compiler/grimoire/ast.js +19 -0
- package/dist/compiler/grimoire/ast.js.map +1 -0
- package/dist/compiler/grimoire/errors.d.ts +65 -0
- package/dist/compiler/grimoire/errors.d.ts.map +1 -0
- package/dist/compiler/grimoire/errors.js +86 -0
- package/dist/compiler/grimoire/errors.js.map +1 -0
- package/dist/compiler/grimoire/index.d.ts +24 -0
- package/dist/compiler/grimoire/index.d.ts.map +1 -0
- package/dist/compiler/grimoire/index.js +63 -0
- package/dist/compiler/grimoire/index.js.map +1 -0
- package/dist/compiler/grimoire/parser.d.ts +135 -0
- package/dist/compiler/grimoire/parser.d.ts.map +1 -0
- package/dist/compiler/grimoire/parser.js +2148 -0
- package/dist/compiler/grimoire/parser.js.map +1 -0
- package/dist/compiler/grimoire/tokenizer.d.ts +59 -0
- package/dist/compiler/grimoire/tokenizer.d.ts.map +1 -0
- package/dist/compiler/grimoire/tokenizer.js +509 -0
- package/dist/compiler/grimoire/tokenizer.js.map +1 -0
- package/dist/compiler/grimoire/transformer.d.ts +71 -0
- package/dist/compiler/grimoire/transformer.d.ts.map +1 -0
- package/dist/compiler/grimoire/transformer.js +1011 -0
- package/dist/compiler/grimoire/transformer.js.map +1 -0
- package/dist/compiler/index.d.ts +45 -0
- package/dist/compiler/index.d.ts.map +1 -0
- package/dist/compiler/index.js +97 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/compiler/ir-generator.d.ts +16 -0
- package/dist/compiler/ir-generator.d.ts.map +1 -0
- package/dist/compiler/ir-generator.js +997 -0
- package/dist/compiler/ir-generator.js.map +1 -0
- package/dist/compiler/validator.d.ts +15 -0
- package/dist/compiler/validator.d.ts.map +1 -0
- package/dist/compiler/validator.js +401 -0
- package/dist/compiler/validator.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/circuit-breaker.d.ts +59 -0
- package/dist/runtime/circuit-breaker.d.ts.map +1 -0
- package/dist/runtime/circuit-breaker.js +155 -0
- package/dist/runtime/circuit-breaker.js.map +1 -0
- package/dist/runtime/context.d.ts +92 -0
- package/dist/runtime/context.d.ts.map +1 -0
- package/dist/runtime/context.js +219 -0
- package/dist/runtime/context.js.map +1 -0
- package/dist/runtime/error-classifier.d.ts +16 -0
- package/dist/runtime/error-classifier.d.ts.map +1 -0
- package/dist/runtime/error-classifier.js +38 -0
- package/dist/runtime/error-classifier.js.map +1 -0
- package/dist/runtime/expression-evaluator.d.ts +36 -0
- package/dist/runtime/expression-evaluator.d.ts.map +1 -0
- package/dist/runtime/expression-evaluator.js +391 -0
- package/dist/runtime/expression-evaluator.js.map +1 -0
- package/dist/runtime/index.d.ts +12 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +11 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/interpreter.d.ts +59 -0
- package/dist/runtime/interpreter.d.ts.map +1 -0
- package/dist/runtime/interpreter.js +414 -0
- package/dist/runtime/interpreter.js.map +1 -0
- package/dist/runtime/skills/registry.d.ts +11 -0
- package/dist/runtime/skills/registry.d.ts.map +1 -0
- package/dist/runtime/skills/registry.js +73 -0
- package/dist/runtime/skills/registry.js.map +1 -0
- package/dist/runtime/sqlite-state-store.d.ts +28 -0
- package/dist/runtime/sqlite-state-store.d.ts.map +1 -0
- package/dist/runtime/sqlite-state-store.js +180 -0
- package/dist/runtime/sqlite-state-store.js.map +1 -0
- package/dist/runtime/state-store.d.ts +52 -0
- package/dist/runtime/state-store.d.ts.map +1 -0
- package/dist/runtime/state-store.js +32 -0
- package/dist/runtime/state-store.js.map +1 -0
- package/dist/runtime/steps/action.d.ts +17 -0
- package/dist/runtime/steps/action.d.ts.map +1 -0
- package/dist/runtime/steps/action.js +430 -0
- package/dist/runtime/steps/action.js.map +1 -0
- package/dist/runtime/steps/advisory.d.ts +28 -0
- package/dist/runtime/steps/advisory.d.ts.map +1 -0
- package/dist/runtime/steps/advisory.js +209 -0
- package/dist/runtime/steps/advisory.js.map +1 -0
- package/dist/runtime/steps/compute.d.ts +9 -0
- package/dist/runtime/steps/compute.d.ts.map +1 -0
- package/dist/runtime/steps/compute.js +74 -0
- package/dist/runtime/steps/compute.js.map +1 -0
- package/dist/runtime/steps/conditional.d.ts +14 -0
- package/dist/runtime/steps/conditional.d.ts.map +1 -0
- package/dist/runtime/steps/conditional.js +58 -0
- package/dist/runtime/steps/conditional.js.map +1 -0
- package/dist/runtime/steps/emit.d.ts +9 -0
- package/dist/runtime/steps/emit.d.ts.map +1 -0
- package/dist/runtime/steps/emit.js +70 -0
- package/dist/runtime/steps/emit.js.map +1 -0
- package/dist/runtime/steps/halt.d.ts +9 -0
- package/dist/runtime/steps/halt.d.ts.map +1 -0
- package/dist/runtime/steps/halt.js +19 -0
- package/dist/runtime/steps/halt.js.map +1 -0
- package/dist/runtime/steps/loop.d.ts +14 -0
- package/dist/runtime/steps/loop.d.ts.map +1 -0
- package/dist/runtime/steps/loop.js +109 -0
- package/dist/runtime/steps/loop.js.map +1 -0
- package/dist/runtime/steps/parallel.d.ts +9 -0
- package/dist/runtime/steps/parallel.d.ts.map +1 -0
- package/dist/runtime/steps/parallel.js +87 -0
- package/dist/runtime/steps/parallel.js.map +1 -0
- package/dist/runtime/steps/pipeline.d.ts +9 -0
- package/dist/runtime/steps/pipeline.d.ts.map +1 -0
- package/dist/runtime/steps/pipeline.js +125 -0
- package/dist/runtime/steps/pipeline.js.map +1 -0
- package/dist/runtime/steps/try.d.ts +13 -0
- package/dist/runtime/steps/try.d.ts.map +1 -0
- package/dist/runtime/steps/try.js +222 -0
- package/dist/runtime/steps/try.js.map +1 -0
- package/dist/runtime/steps/wait.d.ts +9 -0
- package/dist/runtime/steps/wait.d.ts.map +1 -0
- package/dist/runtime/steps/wait.js +38 -0
- package/dist/runtime/steps/wait.js.map +1 -0
- package/dist/types/actions.d.ts +162 -0
- package/dist/types/actions.d.ts.map +1 -0
- package/dist/types/actions.js +5 -0
- package/dist/types/actions.js.map +1 -0
- package/dist/types/execution.d.ts +276 -0
- package/dist/types/execution.d.ts.map +1 -0
- package/dist/types/execution.js +5 -0
- package/dist/types/execution.js.map +1 -0
- package/dist/types/expressions.d.ts +100 -0
- package/dist/types/expressions.d.ts.map +1 -0
- package/dist/types/expressions.js +48 -0
- package/dist/types/expressions.js.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +5 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/ir.d.ts +187 -0
- package/dist/types/ir.d.ts.map +1 -0
- package/dist/types/ir.js +5 -0
- package/dist/types/ir.js.map +1 -0
- package/dist/types/policy.d.ts +123 -0
- package/dist/types/policy.d.ts.map +1 -0
- package/dist/types/policy.js +5 -0
- package/dist/types/policy.js.map +1 -0
- package/dist/types/primitives.d.ts +76 -0
- package/dist/types/primitives.d.ts.map +1 -0
- package/dist/types/primitives.js +10 -0
- package/dist/types/primitives.js.map +1 -0
- package/dist/types/steps.d.ts +226 -0
- package/dist/types/steps.d.ts.map +1 -0
- package/dist/types/steps.js +5 -0
- package/dist/types/steps.js.map +1 -0
- package/dist/venues/index.d.ts +6 -0
- package/dist/venues/index.d.ts.map +1 -0
- package/dist/venues/index.js +26 -0
- package/dist/venues/index.js.map +1 -0
- package/dist/venues/types.d.ts +40 -0
- package/dist/venues/types.d.ts.map +1 -0
- package/dist/venues/types.js +5 -0
- package/dist/venues/types.js.map +1 -0
- package/dist/wallet/executor.d.ts +109 -0
- package/dist/wallet/executor.d.ts.map +1 -0
- package/dist/wallet/executor.js +354 -0
- package/dist/wallet/executor.js.map +1 -0
- package/dist/wallet/index.d.ts +14 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +13 -0
- package/dist/wallet/index.js.map +1 -0
- package/dist/wallet/keystore.d.ts +44 -0
- package/dist/wallet/keystore.d.ts.map +1 -0
- package/dist/wallet/keystore.js +296 -0
- package/dist/wallet/keystore.js.map +1 -0
- package/dist/wallet/provider.d.ts +111 -0
- package/dist/wallet/provider.d.ts.map +1 -0
- package/dist/wallet/provider.js +309 -0
- package/dist/wallet/provider.js.map +1 -0
- package/dist/wallet/tx-builder.d.ts +85 -0
- package/dist/wallet/tx-builder.d.ts.map +1 -0
- package/dist/wallet/tx-builder.js +290 -0
- package/dist/wallet/tx-builder.js.map +1 -0
- package/dist/wallet/types.d.ts +116 -0
- package/dist/wallet/types.d.ts.map +1 -0
- package/dist/wallet/types.js +86 -0
- package/dist/wallet/types.js.map +1 -0
- package/package.json +39 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Private key management
|
|
3
|
+
*
|
|
4
|
+
* Supports loading keys from:
|
|
5
|
+
* - Environment variables
|
|
6
|
+
* - Raw hex strings (for testing)
|
|
7
|
+
* - BIP-39 mnemonics
|
|
8
|
+
* - Encrypted keystore JSON files
|
|
9
|
+
*/
|
|
10
|
+
import type { Address } from "../types/primitives.js";
|
|
11
|
+
import type { KeyConfig, Wallet } from "./types.js";
|
|
12
|
+
/** Error thrown when key loading fails */
|
|
13
|
+
export declare class KeyLoadError extends Error {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Load a private key from the specified source
|
|
18
|
+
*/
|
|
19
|
+
export declare function loadPrivateKey(config: KeyConfig): `0x${string}`;
|
|
20
|
+
/**
|
|
21
|
+
* Create a wallet from a private key
|
|
22
|
+
*/
|
|
23
|
+
export declare function createWallet(privateKey: `0x${string}`, chainId: number, rpcUrl: string): Wallet;
|
|
24
|
+
/**
|
|
25
|
+
* Create a wallet from a mnemonic phrase
|
|
26
|
+
*/
|
|
27
|
+
export declare function createWalletFromMnemonic(mnemonic: string, chainId: number, rpcUrl: string, derivationPath?: string): Wallet;
|
|
28
|
+
/**
|
|
29
|
+
* Create a wallet from configuration
|
|
30
|
+
*/
|
|
31
|
+
export declare function createWalletFromConfig(config: KeyConfig, chainId: number, rpcUrl: string): Wallet;
|
|
32
|
+
/**
|
|
33
|
+
* Generate a new random private key
|
|
34
|
+
*/
|
|
35
|
+
export declare function generatePrivateKey(): `0x${string}`;
|
|
36
|
+
/**
|
|
37
|
+
* Encrypt a private key into a keystore JSON string
|
|
38
|
+
*/
|
|
39
|
+
export declare function createKeystore(privateKey: `0x${string}`, password: string): Promise<string>;
|
|
40
|
+
/**
|
|
41
|
+
* Get wallet address from a key config without creating full wallet
|
|
42
|
+
*/
|
|
43
|
+
export declare function getAddressFromConfig(config: KeyConfig): Address;
|
|
44
|
+
//# sourceMappingURL=keystore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keystore.d.ts","sourceRoot":"","sources":["../../src/wallet/keystore.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAmBH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAA0C,MAAM,EAAE,MAAM,YAAY,CAAC;AAE5F,0CAA0C;AAC1C,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI5B;AA4BD;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,GAAG,KAAK,MAAM,EAAE,CA4C/D;AAgFD;;GAEG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,KAAK,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAe/F;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,cAAc,CAAC,EAAE,MAAM,GACtB,MAAM,CAgBR;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAejG;AA+ED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,KAAK,MAAM,EAAE,CAElD;AAED;;GAEG;AACH,wBAAsB,cAAc,CAAC,UAAU,EAAE,KAAK,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAMjG;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAiB/D"}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Private key management
|
|
3
|
+
*
|
|
4
|
+
* Supports loading keys from:
|
|
5
|
+
* - Environment variables
|
|
6
|
+
* - Raw hex strings (for testing)
|
|
7
|
+
* - BIP-39 mnemonics
|
|
8
|
+
* - Encrypted keystore JSON files
|
|
9
|
+
*/
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import { Wallet as EthersWallet } from "ethers";
|
|
12
|
+
import { http, createWalletClient, publicActions, } from "viem";
|
|
13
|
+
import { mnemonicToAccount, privateKeyToAccount, generatePrivateKey as viemGeneratePrivateKey, } from "viem/accounts";
|
|
14
|
+
import { arbitrum, base, mainnet, optimism, polygon, sepolia } from "viem/chains";
|
|
15
|
+
/** Error thrown when key loading fails */
|
|
16
|
+
export class KeyLoadError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "KeyLoadError";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** HyperEVM chain definition */
|
|
23
|
+
const hyperEVM = {
|
|
24
|
+
id: 999,
|
|
25
|
+
name: "HyperEVM",
|
|
26
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
27
|
+
rpcUrls: {
|
|
28
|
+
default: { http: ["https://rpc.hyperliquid.xyz/evm"] },
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
/** Map chain IDs to viem chain objects */
|
|
32
|
+
const VIEM_CHAINS = {
|
|
33
|
+
1: mainnet,
|
|
34
|
+
10: optimism,
|
|
35
|
+
137: polygon,
|
|
36
|
+
42161: arbitrum,
|
|
37
|
+
8453: base,
|
|
38
|
+
999: hyperEVM,
|
|
39
|
+
11155111: sepolia,
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Load a private key from the specified source
|
|
43
|
+
*/
|
|
44
|
+
export function loadPrivateKey(config) {
|
|
45
|
+
switch (config.type) {
|
|
46
|
+
case "raw":
|
|
47
|
+
return validatePrivateKey(config.source);
|
|
48
|
+
case "env": {
|
|
49
|
+
const value = process.env[config.source];
|
|
50
|
+
if (!value) {
|
|
51
|
+
throw new KeyLoadError(`Environment variable ${config.source} is not set`);
|
|
52
|
+
}
|
|
53
|
+
return validatePrivateKey(value);
|
|
54
|
+
}
|
|
55
|
+
case "mnemonic": {
|
|
56
|
+
// For mnemonic, the source is the mnemonic phrase
|
|
57
|
+
// Check if it's an env var reference
|
|
58
|
+
let mnemonic = config.source;
|
|
59
|
+
if (!mnemonic.includes(" ")) {
|
|
60
|
+
// Likely an env var name
|
|
61
|
+
const envValue = process.env[config.source];
|
|
62
|
+
if (envValue) {
|
|
63
|
+
mnemonic = envValue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (!isValidMnemonic(mnemonic)) {
|
|
67
|
+
throw new KeyLoadError("Invalid mnemonic phrase");
|
|
68
|
+
}
|
|
69
|
+
const path = (config.derivationPath ?? "m/44'/60'/0'/0/0");
|
|
70
|
+
const _account = mnemonicToAccount(mnemonic, { path });
|
|
71
|
+
// Note: viem doesn't expose the private key directly from mnemonic account
|
|
72
|
+
// We need to derive it differently for raw access
|
|
73
|
+
throw new KeyLoadError("Mnemonic support requires direct account usage. Use createWalletFromMnemonic instead.");
|
|
74
|
+
}
|
|
75
|
+
case "keystore":
|
|
76
|
+
return loadPrivateKeyFromKeystore(config);
|
|
77
|
+
default:
|
|
78
|
+
throw new KeyLoadError(`Unknown key source type: ${config.type}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Validate and normalize a private key
|
|
83
|
+
*/
|
|
84
|
+
function validatePrivateKey(key) {
|
|
85
|
+
// Remove whitespace
|
|
86
|
+
let normalized = key.trim();
|
|
87
|
+
// Add 0x prefix if missing
|
|
88
|
+
if (!normalized.startsWith("0x")) {
|
|
89
|
+
normalized = `0x${normalized}`;
|
|
90
|
+
}
|
|
91
|
+
// Check length (32 bytes = 64 hex chars + 0x prefix)
|
|
92
|
+
if (normalized.length !== 66) {
|
|
93
|
+
throw new KeyLoadError(`Invalid private key length: expected 66 characters, got ${normalized.length}`);
|
|
94
|
+
}
|
|
95
|
+
// Check if valid hex
|
|
96
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(normalized)) {
|
|
97
|
+
throw new KeyLoadError("Invalid private key: must be 32 bytes of hex");
|
|
98
|
+
}
|
|
99
|
+
return normalized;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Load a private key from a keystore JSON source
|
|
103
|
+
*/
|
|
104
|
+
function loadPrivateKeyFromKeystore(config) {
|
|
105
|
+
if (!config.password) {
|
|
106
|
+
throw new KeyLoadError("Keystore password is required");
|
|
107
|
+
}
|
|
108
|
+
const keystoreJson = resolveKeystoreSource(config.source);
|
|
109
|
+
try {
|
|
110
|
+
const wallet = EthersWallet.fromEncryptedJsonSync(keystoreJson, config.password);
|
|
111
|
+
return validatePrivateKey(wallet.privateKey);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw new KeyLoadError(`Failed to decrypt keystore: ${error.message}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Resolve keystore JSON from file path, env var, or raw JSON string
|
|
119
|
+
*/
|
|
120
|
+
function resolveKeystoreSource(source) {
|
|
121
|
+
const trimmed = source.trim();
|
|
122
|
+
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
|
|
123
|
+
return trimmed;
|
|
124
|
+
}
|
|
125
|
+
const envValue = process.env[source];
|
|
126
|
+
if (envValue) {
|
|
127
|
+
return envValue;
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
return readFileSync(source, "utf-8");
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw new KeyLoadError(`Unable to read keystore JSON from '${source}': ${error.message}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Check if a string is a valid BIP-39 mnemonic
|
|
138
|
+
*/
|
|
139
|
+
function isValidMnemonic(phrase) {
|
|
140
|
+
const words = phrase.trim().split(/\s+/);
|
|
141
|
+
// BIP-39 mnemonics are 12, 15, 18, 21, or 24 words
|
|
142
|
+
return [12, 15, 18, 21, 24].includes(words.length);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Create a wallet from a private key
|
|
146
|
+
*/
|
|
147
|
+
export function createWallet(privateKey, chainId, rpcUrl) {
|
|
148
|
+
const account = privateKeyToAccount(privateKey);
|
|
149
|
+
const chain = VIEM_CHAINS[chainId];
|
|
150
|
+
if (!chain) {
|
|
151
|
+
throw new KeyLoadError(`Unsupported chain ID: ${chainId}`);
|
|
152
|
+
}
|
|
153
|
+
const client = createWalletClient({
|
|
154
|
+
account,
|
|
155
|
+
chain,
|
|
156
|
+
transport: http(rpcUrl),
|
|
157
|
+
}).extend(publicActions);
|
|
158
|
+
return new ViemWallet(client, account, chainId);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Create a wallet from a mnemonic phrase
|
|
162
|
+
*/
|
|
163
|
+
export function createWalletFromMnemonic(mnemonic, chainId, rpcUrl, derivationPath) {
|
|
164
|
+
const path = (derivationPath ?? "m/44'/60'/0'/0/0");
|
|
165
|
+
const account = mnemonicToAccount(mnemonic, { path });
|
|
166
|
+
const chain = VIEM_CHAINS[chainId];
|
|
167
|
+
if (!chain) {
|
|
168
|
+
throw new KeyLoadError(`Unsupported chain ID: ${chainId}`);
|
|
169
|
+
}
|
|
170
|
+
const client = createWalletClient({
|
|
171
|
+
account,
|
|
172
|
+
chain,
|
|
173
|
+
transport: http(rpcUrl),
|
|
174
|
+
}).extend(publicActions);
|
|
175
|
+
return new ViemWallet(client, account, chainId);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Create a wallet from configuration
|
|
179
|
+
*/
|
|
180
|
+
export function createWalletFromConfig(config, chainId, rpcUrl) {
|
|
181
|
+
if (config.type === "mnemonic") {
|
|
182
|
+
let mnemonic = config.source;
|
|
183
|
+
// Check if it's an env var reference
|
|
184
|
+
if (!mnemonic.includes(" ")) {
|
|
185
|
+
const envValue = process.env[config.source];
|
|
186
|
+
if (envValue) {
|
|
187
|
+
mnemonic = envValue;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return createWalletFromMnemonic(mnemonic, chainId, rpcUrl, config.derivationPath);
|
|
191
|
+
}
|
|
192
|
+
const privateKey = loadPrivateKey(config);
|
|
193
|
+
return createWallet(privateKey, chainId, rpcUrl);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Wallet implementation using viem
|
|
197
|
+
*/
|
|
198
|
+
class ViemWallet {
|
|
199
|
+
client;
|
|
200
|
+
account;
|
|
201
|
+
chainId;
|
|
202
|
+
constructor(client, account, chainId) {
|
|
203
|
+
this.client = client;
|
|
204
|
+
this.account = account;
|
|
205
|
+
this.chainId = chainId;
|
|
206
|
+
}
|
|
207
|
+
get address() {
|
|
208
|
+
return this.account.address;
|
|
209
|
+
}
|
|
210
|
+
async signTransaction(tx) {
|
|
211
|
+
const request = this.toViemRequest(tx);
|
|
212
|
+
if (!this.account.signTransaction) {
|
|
213
|
+
throw new Error("Account does not support signTransaction");
|
|
214
|
+
}
|
|
215
|
+
return await this.account.signTransaction(request);
|
|
216
|
+
}
|
|
217
|
+
async signMessage(message) {
|
|
218
|
+
return await this.client.signMessage({
|
|
219
|
+
account: this.account,
|
|
220
|
+
message,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
async sendTransaction(tx) {
|
|
224
|
+
const request = this.toViemRequest(tx);
|
|
225
|
+
// Send the transaction
|
|
226
|
+
const hash = await this.client.sendTransaction(request);
|
|
227
|
+
// Wait for confirmation
|
|
228
|
+
const receipt = await this.client.waitForTransactionReceipt({
|
|
229
|
+
hash,
|
|
230
|
+
confirmations: 1,
|
|
231
|
+
});
|
|
232
|
+
return {
|
|
233
|
+
hash: receipt.transactionHash,
|
|
234
|
+
blockNumber: receipt.blockNumber,
|
|
235
|
+
blockHash: receipt.blockHash,
|
|
236
|
+
gasUsed: receipt.gasUsed,
|
|
237
|
+
effectiveGasPrice: receipt.effectiveGasPrice,
|
|
238
|
+
status: receipt.status === "success" ? "success" : "reverted",
|
|
239
|
+
contractAddress: receipt.contractAddress,
|
|
240
|
+
logs: receipt.logs.map((log) => ({
|
|
241
|
+
address: log.address,
|
|
242
|
+
topics: log.topics,
|
|
243
|
+
data: log.data,
|
|
244
|
+
logIndex: log.logIndex,
|
|
245
|
+
})),
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
toViemRequest(tx) {
|
|
249
|
+
return {
|
|
250
|
+
to: tx.to,
|
|
251
|
+
value: tx.value,
|
|
252
|
+
data: tx.data,
|
|
253
|
+
gas: tx.gasLimit,
|
|
254
|
+
maxFeePerGas: tx.maxFeePerGas,
|
|
255
|
+
maxPriorityFeePerGas: tx.maxPriorityFeePerGas,
|
|
256
|
+
nonce: tx.nonce,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Generate a new random private key
|
|
262
|
+
*/
|
|
263
|
+
export function generatePrivateKey() {
|
|
264
|
+
return viemGeneratePrivateKey();
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Encrypt a private key into a keystore JSON string
|
|
268
|
+
*/
|
|
269
|
+
export async function createKeystore(privateKey, password) {
|
|
270
|
+
if (!password) {
|
|
271
|
+
throw new KeyLoadError("Keystore password must not be empty");
|
|
272
|
+
}
|
|
273
|
+
const wallet = new EthersWallet(privateKey);
|
|
274
|
+
return await wallet.encrypt(password);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Get wallet address from a key config without creating full wallet
|
|
278
|
+
*/
|
|
279
|
+
export function getAddressFromConfig(config) {
|
|
280
|
+
if (config.type === "mnemonic") {
|
|
281
|
+
let mnemonic = config.source;
|
|
282
|
+
if (!mnemonic.includes(" ")) {
|
|
283
|
+
const envValue = process.env[config.source];
|
|
284
|
+
if (envValue) {
|
|
285
|
+
mnemonic = envValue;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const path = (config.derivationPath ?? "m/44'/60'/0'/0/0");
|
|
289
|
+
const account = mnemonicToAccount(mnemonic, { path });
|
|
290
|
+
return account.address;
|
|
291
|
+
}
|
|
292
|
+
const privateKey = loadPrivateKey(config);
|
|
293
|
+
const account = privateKeyToAccount(privateKey);
|
|
294
|
+
return account.address;
|
|
295
|
+
}
|
|
296
|
+
//# sourceMappingURL=keystore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keystore.js","sourceRoot":"","sources":["../../src/wallet/keystore.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EACL,IAAI,EAKJ,kBAAkB,EAClB,aAAa,GACd,MAAM,MAAM,CAAC;AACd,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,kBAAkB,IAAI,sBAAsB,GAC7C,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAIlF,0CAA0C;AAC1C,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAED,gCAAgC;AAChC,MAAM,QAAQ,GAAU;IACtB,EAAE,EAAE,GAAG;IACP,IAAI,EAAE,UAAU;IAChB,cAAc,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC9D,OAAO,EAAE;QACP,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,iCAAiC,CAAC,EAAE;KACvD;CACF,CAAC;AAEF,0CAA0C;AAC1C,MAAM,WAAW,GAA0B;IACzC,CAAC,EAAE,OAAO;IACV,EAAE,EAAE,QAAQ;IACZ,GAAG,EAAE,OAAO;IACZ,KAAK,EAAE,QAAQ;IACf,IAA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ection to blockchain nodes with:
|
|
5
|
+
* - Automatic fallback to backup URLs
|
|
6
|
+
* - Retry with exponential backoff
|
|
7
|
+
* - Gas price estimation
|
|
8
|
+
*/
|
|
9
|
+
import { type Abi, type PublicClient } from "viem";
|
|
10
|
+
import type { Address } from "../types/primitives.js";
|
|
11
|
+
import type { GasEstimate, ProviderConfig } from "./types.js";
|
|
12
|
+
/**
|
|
13
|
+
* Provider wrapper with fallback and retry support
|
|
14
|
+
*/
|
|
15
|
+
export declare class Provider {
|
|
16
|
+
private client;
|
|
17
|
+
private config;
|
|
18
|
+
private currentUrlIndex;
|
|
19
|
+
constructor(config: ProviderConfig);
|
|
20
|
+
/**
|
|
21
|
+
* Get the chain ID
|
|
22
|
+
*/
|
|
23
|
+
get chainId(): number;
|
|
24
|
+
/**
|
|
25
|
+
* Get the current RPC URL
|
|
26
|
+
*/
|
|
27
|
+
get rpcUrl(): string;
|
|
28
|
+
/**
|
|
29
|
+
* Get underlying viem client
|
|
30
|
+
*/
|
|
31
|
+
getClient(): PublicClient;
|
|
32
|
+
/**
|
|
33
|
+
* Get the current block number
|
|
34
|
+
*/
|
|
35
|
+
getBlockNumber(): Promise<bigint>;
|
|
36
|
+
/**
|
|
37
|
+
* Get the balance of an address
|
|
38
|
+
*/
|
|
39
|
+
getBalance(address: Address): Promise<bigint>;
|
|
40
|
+
/**
|
|
41
|
+
* Get the nonce for an address
|
|
42
|
+
*/
|
|
43
|
+
getNonce(address: Address): Promise<number>;
|
|
44
|
+
/**
|
|
45
|
+
* Estimate gas for a transaction
|
|
46
|
+
*/
|
|
47
|
+
estimateGas(tx: {
|
|
48
|
+
to: Address;
|
|
49
|
+
data?: string;
|
|
50
|
+
value?: bigint;
|
|
51
|
+
from?: Address;
|
|
52
|
+
}): Promise<bigint>;
|
|
53
|
+
/**
|
|
54
|
+
* Get current gas prices (EIP-1559)
|
|
55
|
+
*/
|
|
56
|
+
getGasPrices(): Promise<{
|
|
57
|
+
maxFeePerGas: bigint;
|
|
58
|
+
maxPriorityFeePerGas: bigint;
|
|
59
|
+
}>;
|
|
60
|
+
/**
|
|
61
|
+
* Get full gas estimate for a transaction
|
|
62
|
+
*/
|
|
63
|
+
getGasEstimate(tx: {
|
|
64
|
+
to: Address;
|
|
65
|
+
data?: string;
|
|
66
|
+
value?: bigint;
|
|
67
|
+
from?: Address;
|
|
68
|
+
}): Promise<GasEstimate>;
|
|
69
|
+
/**
|
|
70
|
+
* Read contract data
|
|
71
|
+
*/
|
|
72
|
+
readContract<T>(params: {
|
|
73
|
+
address: Address;
|
|
74
|
+
abi: Abi;
|
|
75
|
+
functionName: string;
|
|
76
|
+
args?: readonly unknown[];
|
|
77
|
+
}): Promise<T>;
|
|
78
|
+
/**
|
|
79
|
+
* Wait for a transaction receipt
|
|
80
|
+
*/
|
|
81
|
+
waitForTransaction(hash: string, confirmations?: number): Promise<unknown>;
|
|
82
|
+
/**
|
|
83
|
+
* Execute with retry and fallback
|
|
84
|
+
*/
|
|
85
|
+
private withRetry;
|
|
86
|
+
/**
|
|
87
|
+
* Check if we should switch to a different provider
|
|
88
|
+
*/
|
|
89
|
+
private shouldSwitchProvider;
|
|
90
|
+
/**
|
|
91
|
+
* Switch to a different RPC URL
|
|
92
|
+
*/
|
|
93
|
+
private switchToUrl;
|
|
94
|
+
/**
|
|
95
|
+
* Sleep for a duration
|
|
96
|
+
*/
|
|
97
|
+
private sleep;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Create a provider for a specific chain
|
|
101
|
+
*/
|
|
102
|
+
export declare function createProvider(chainId: number, rpcUrl?: string): Provider;
|
|
103
|
+
/**
|
|
104
|
+
* Format wei to human-readable string
|
|
105
|
+
*/
|
|
106
|
+
export declare function formatWei(wei: bigint, decimals?: number): string;
|
|
107
|
+
/**
|
|
108
|
+
* Format gas cost to USD (rough estimate)
|
|
109
|
+
*/
|
|
110
|
+
export declare function formatGasCostUsd(gasUsed: bigint, gasPrice: bigint, ethPriceUsd?: number): string;
|
|
111
|
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