@cfxdevkit/services 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/CHANGELOG.md +35 -0
- package/LICENSE +72 -0
- package/README.md +257 -0
- package/dist/index.cjs +1097 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1076 -0
- package/dist/index.js.map +1 -0
- package/dist/services/index.cjs +1097 -0
- package/dist/services/index.cjs.map +1 -0
- package/dist/services/index.d.cts +566 -0
- package/dist/services/index.d.ts +566 -0
- package/dist/services/index.js +1076 -0
- package/dist/services/index.js.map +1 -0
- package/package.json +97 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@cfxdevkit/sdk` will be documented here.
|
|
4
|
+
|
|
5
|
+
Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## [0.1.0] - 2026-02-19
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- Initial SDK extraction from `conflux-devkit` monorepo
|
|
13
|
+
- `clients`: `ClientManager`, `CoreClient`, `EspaceClient` (+ wallet/test variants)
|
|
14
|
+
- `config`: Chain definitions for Conflux Core Space and eSpace (local/testnet/mainnet)
|
|
15
|
+
- `types`: `ChainType`, `UnifiedAccount`, `Address`, `ChainConfig` and all client interfaces
|
|
16
|
+
- `utils`: Simple structured logger
|
|
17
|
+
- `wallet/derivation`: BIP32/BIP39 HD wallet derivation for both Core and eSpace addresses
|
|
18
|
+
- `wallet/session-keys`: `SessionKeyManager` – temporary delegated signing
|
|
19
|
+
- `wallet/batching`: `TransactionBatcher` – multi-tx execution
|
|
20
|
+
- `wallet/embedded`: `EmbeddedWalletManager` – server-side custody
|
|
21
|
+
- `contracts/abis`: ERC-20, ERC-721, ERC-1155 ABIs
|
|
22
|
+
- `contracts/deployer`: `ContractDeployer` – deploy to Core or eSpace
|
|
23
|
+
- `contracts/interaction`: `ContractReader`, `ContractWriter`
|
|
24
|
+
- `services/swap`: `SwapService` – Swappi DEX integration (Uniswap V2)
|
|
25
|
+
- `services/keystore`: `KeystoreService` – encrypted HD wallet storage (AES-256-GCM + PBKDF2)
|
|
26
|
+
- `services/encryption`: `EncryptionService` – AES-256-GCM crypto primitives
|
|
27
|
+
|
|
28
|
+
### Changed (from conflux-devkit)
|
|
29
|
+
- Removed DevKit backend coupling from all services; services are now standalone classes
|
|
30
|
+
- Removed Solidity compiler wrapper (`solc` dependency)
|
|
31
|
+
- Removed `ui-headless` hooks (were DevKit-API coupled, not wagmi-native)
|
|
32
|
+
- Removed DevNode plugin, CLI binary, MCP server, WebSocket streaming server
|
|
33
|
+
- Fixed `@scure/bip39/wordlists/english` import to use `.js` extension for ESM compatibility
|
|
34
|
+
- Keystore backwards-compatibility block removed (DevKit-specific legacy)
|
|
35
|
+
- `moduleResolution` switched from `node` to `bundler` for correct ESM subpath resolution
|
package/LICENSE
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity.
|
|
18
|
+
|
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
20
|
+
exercising permissions granted by this License.
|
|
21
|
+
|
|
22
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
23
|
+
including but not limited to software source code, documentation
|
|
24
|
+
source, and configuration files.
|
|
25
|
+
|
|
26
|
+
"Object" form shall mean any form resulting from mechanical
|
|
27
|
+
transformation or translation of a Source form, including but
|
|
28
|
+
not limited to compiled object code, generated documentation,
|
|
29
|
+
and conversions to other media types.
|
|
30
|
+
|
|
31
|
+
"Work" shall mean the work of authorship made available under
|
|
32
|
+
the License, as indicated by a copyright notice included in or
|
|
33
|
+
attached to the work.
|
|
34
|
+
|
|
35
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
36
|
+
form, that is based on (or derived from) the Work and for which the
|
|
37
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
38
|
+
represent, as a whole, an original work of authorship.
|
|
39
|
+
|
|
40
|
+
"Contribution" shall mean any work of authorship, submitted to the
|
|
41
|
+
Licensor for inclusion in the Work.
|
|
42
|
+
|
|
43
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
44
|
+
whom a Contribution has been received by the Licensor and included
|
|
45
|
+
within the Work.
|
|
46
|
+
|
|
47
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
48
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
49
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
50
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
51
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
52
|
+
Work and such Derivative Works in Source or Object form.
|
|
53
|
+
|
|
54
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
patent license to make, use, sell, offer for sale, import, and
|
|
58
|
+
otherwise transfer the Work.
|
|
59
|
+
|
|
60
|
+
Copyright 2025-2026 cfxdevkit
|
|
61
|
+
|
|
62
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
63
|
+
you may not use this file except in compliance with the License.
|
|
64
|
+
You may obtain a copy of the License at
|
|
65
|
+
|
|
66
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
67
|
+
|
|
68
|
+
Unless required by applicable law or agreed to in writing, software
|
|
69
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
70
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
71
|
+
See the License for the specific language governing permissions and
|
|
72
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# @cfxdevkit/services
|
|
2
|
+
|
|
3
|
+
> Conflux SDK · blockchain client library for Conflux Core Space & eSpace
|
|
4
|
+
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
|
|
8
|
+
Single-package SDK extracted from [conflux-devkit](https://github.com/cfxdevkit/conflux-devkit).
|
|
9
|
+
Provides chain clients, contract utilities, wallet derivation, swap services, and keystore management for building on Conflux.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @cfxdevkit/sdk
|
|
17
|
+
# or
|
|
18
|
+
npm install @cfxdevkit/sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Peer dependencies (install if using React hooks):
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pnpm add react react-dom
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Package Structure
|
|
30
|
+
|
|
31
|
+
| Subpath | Contents |
|
|
32
|
+
|---------|----------|
|
|
33
|
+
| `@cfxdevkit/sdk` | Full barrel – everything |
|
|
34
|
+
| `@cfxdevkit/sdk/clients` | `ClientManager`, `CoreClient`, `EspaceClient` |
|
|
35
|
+
| `@cfxdevkit/sdk/config` | Chain definitions for Core + eSpace |
|
|
36
|
+
| `@cfxdevkit/sdk/types` | Shared TypeScript types |
|
|
37
|
+
| `@cfxdevkit/sdk/utils` | Logger |
|
|
38
|
+
| `@cfxdevkit/sdk/wallet` | HD derivation, session keys, batching, embedded wallets |
|
|
39
|
+
| `@cfxdevkit/sdk/contracts` | `ContractDeployer`, `ContractReader`, `ContractWriter`, ERC ABIs |
|
|
40
|
+
| `@cfxdevkit/sdk/services` | `SwapService` (Swappi DEX), `KeystoreService`, `EncryptionService` |
|
|
41
|
+
| `@cfxdevkit/sdk/automation` | `SafetyGuard`, `RetryQueue`, `PriceChecker`, `AUTOMATION_MANAGER_ABI`, types |
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Quick Start
|
|
46
|
+
|
|
47
|
+
### Connect to Conflux eSpace
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { ClientManager, EVM_MAINNET } from '@cfxdevkit/sdk';
|
|
51
|
+
|
|
52
|
+
const manager = new ClientManager({
|
|
53
|
+
evm: { chain: EVM_MAINNET },
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await manager.connect();
|
|
57
|
+
const block = await manager.evm.publicClient.getBlockNumber();
|
|
58
|
+
console.log('Current block:', block);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Connect to Conflux Core Space
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { ClientManager, CORE_MAINNET } from '@cfxdevkit/sdk';
|
|
65
|
+
|
|
66
|
+
const manager = new ClientManager({
|
|
67
|
+
core: { chain: CORE_MAINNET },
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
await manager.connect();
|
|
71
|
+
const epochNumber = await manager.core.publicClient.getEpochNumber();
|
|
72
|
+
console.log('Current epoch:', epochNumber);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### HD Wallet Derivation
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { generateMnemonic, deriveAccounts } from '@cfxdevkit/sdk/wallet';
|
|
79
|
+
|
|
80
|
+
// Generate new wallet
|
|
81
|
+
const mnemonic = generateMnemonic();
|
|
82
|
+
|
|
83
|
+
// Derive 5 accounts (both Core & eSpace addresses)
|
|
84
|
+
const accounts = deriveAccounts(mnemonic, { count: 5 });
|
|
85
|
+
|
|
86
|
+
for (const account of accounts) {
|
|
87
|
+
console.log(`[${account.index}] Core: ${account.coreAddress}`);
|
|
88
|
+
console.log(`[${account.index}] eSpace: ${account.evmAddress}`);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Read an ERC-20 token
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { ClientManager, EVM_MAINNET } from '@cfxdevkit/sdk/clients';
|
|
96
|
+
import { ContractReader } from '@cfxdevkit/sdk/contracts';
|
|
97
|
+
import { ERC20_ABI } from '@cfxdevkit/sdk/contracts';
|
|
98
|
+
|
|
99
|
+
const manager = new ClientManager({ evm: { chain: EVM_MAINNET } });
|
|
100
|
+
await manager.connect();
|
|
101
|
+
|
|
102
|
+
const reader = new ContractReader(manager);
|
|
103
|
+
const balance = await reader.read({
|
|
104
|
+
address: '0x14b2d3bc65e74dae1030eafd8ac30c533c976a9b', // WCFX
|
|
105
|
+
abi: ERC20_ABI,
|
|
106
|
+
functionName: 'balanceOf',
|
|
107
|
+
args: ['0xYourAddress'],
|
|
108
|
+
chain: 'evm',
|
|
109
|
+
});
|
|
110
|
+
console.log('Balance:', balance);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Query a DEX swap quote (Swappi)
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { ClientManager, EVM_MAINNET } from '@cfxdevkit/sdk/clients';
|
|
117
|
+
import { SwapService } from '@cfxdevkit/sdk/services';
|
|
118
|
+
|
|
119
|
+
const manager = new ClientManager({ evm: { chain: EVM_MAINNET } });
|
|
120
|
+
await manager.connect();
|
|
121
|
+
|
|
122
|
+
const swap = new SwapService(manager.evm.publicClient, 'mainnet');
|
|
123
|
+
const quote = await swap.getQuote({
|
|
124
|
+
tokenIn: 'WCFX',
|
|
125
|
+
tokenOut: 'USDT',
|
|
126
|
+
amountIn: '1', // 1 WCFX
|
|
127
|
+
slippageBps: 50, // 0.5%
|
|
128
|
+
});
|
|
129
|
+
console.log('Expected out:', quote.amountOut);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Encrypted Keystore
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { KeystoreService } from '@cfxdevkit/sdk/services';
|
|
136
|
+
|
|
137
|
+
const keystore = new KeystoreService('/path/to/.keystore.json');
|
|
138
|
+
await keystore.setup({ password: 'my-password' });
|
|
139
|
+
await keystore.addMnemonic({ mnemonic: 'word1 word2 ...', label: 'main' });
|
|
140
|
+
|
|
141
|
+
// Later – unlock and derive
|
|
142
|
+
await keystore.unlockKeystore('my-password');
|
|
143
|
+
const accounts = await keystore.deriveAccountsFromMnemonic(
|
|
144
|
+
mnemonic, 'espace', 5, 0
|
|
145
|
+
);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Automation (SafetyGuard · RetryQueue · PriceChecker)
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import {
|
|
152
|
+
SafetyGuard, RetryQueue, PriceChecker, AUTOMATION_MANAGER_ABI,
|
|
153
|
+
} from '@cfxdevkit/sdk/automation';
|
|
154
|
+
|
|
155
|
+
// Injectable logger — pass pino/winston/console or omit for silence
|
|
156
|
+
const guard = new SafetyGuard({ maxSwapUsd: 5_000 }, myLogger);
|
|
157
|
+
const queue = new RetryQueue({ baseDelayMs: 5_000 }, myLogger);
|
|
158
|
+
const checker = new PriceChecker(myPriceSource, tokenPricesUsd, myLogger);
|
|
159
|
+
|
|
160
|
+
// Pre-flight check before sending a transaction
|
|
161
|
+
const result = guard.check(job, { swapUsd: estimatedUsd });
|
|
162
|
+
if (!result.ok) {
|
|
163
|
+
queue.enqueue(job); // schedule retry with exponential backoff
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Check price condition
|
|
167
|
+
const { conditionMet, swapUsd } = await checker.checkLimitOrder(job);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Architecture
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
@cfxdevkit/sdk
|
|
176
|
+
│
|
|
177
|
+
├── clients/ ← cive (Core) + viem (eSpace) thin wrappers + ClientManager
|
|
178
|
+
├── config/ ← Chain definitions: local / testnet / mainnet for both spaces
|
|
179
|
+
├── types/ ← ChainType, UnifiedAccount, Address, etc.
|
|
180
|
+
├── utils/ ← Logger
|
|
181
|
+
│
|
|
182
|
+
├── wallet/
|
|
183
|
+
│ ├── derivation.ts ← BIP32/BIP39 HD derivation (Core ↔ eSpace)
|
|
184
|
+
│ ├── session-keys/ ← Temporary delegated signing
|
|
185
|
+
│ ├── batching/ ← Multi-tx batching
|
|
186
|
+
│ └── embedded/ ← Server-side custody
|
|
187
|
+
│
|
|
188
|
+
├── contracts/
|
|
189
|
+
│ ├── abis/ ← ERC20, ERC721, ERC1155
|
|
190
|
+
│ ├── deployer/ ← Deploy to Core or eSpace
|
|
191
|
+
│ └── interaction/ ← ContractReader, ContractWriter
|
|
192
|
+
│
|
|
193
|
+
├── services/
|
|
194
|
+
│ ├── swap.ts ← Swappi DEX integration (Uniswap V2 style)
|
|
195
|
+
│ ├── keystore.ts ← Encrypted HD wallet storage (AES-256-GCM)
|
|
196
|
+
│ └── encryption.ts ← AES-256-GCM + PBKDF2 primitives
|
|
197
|
+
│
|
|
198
|
+
└── automation/ ← Off-chain automation primitives (no pino dep; injectable logger)
|
|
199
|
+
├── types.ts ← Job + safety types (JobStatus, SafetyConfig, …)
|
|
200
|
+
├── safety-guard.ts ← SafetyGuard — off-chain pre-flight checks
|
|
201
|
+
├── retry-queue.ts ← RetryQueue — exponential backoff scheduling
|
|
202
|
+
├── price-checker.ts ← PriceChecker — price-source abstraction
|
|
203
|
+
├── abi.ts ← AUTOMATION_MANAGER_ABI (viem `as const`)
|
|
204
|
+
├── keeper-interface.ts ← KeeperClient interface
|
|
205
|
+
└── index.ts ← Barrel export
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## Dual-Space Architecture
|
|
211
|
+
|
|
212
|
+
Conflux has two parallel spaces on the same chain:
|
|
213
|
+
|
|
214
|
+
| | Core Space | eSpace |
|
|
215
|
+
|--|--|--|
|
|
216
|
+
| **API** | Conflux RPC (Epoch-based) | EVM-compatible JSON-RPC |
|
|
217
|
+
| **Addresses** | `cfx:aaa...` (base32) | `0x...` (hex) |
|
|
218
|
+
| **SDK Client** | `CoreClient` (via `cive`) | `EspaceClient` (via `viem`) |
|
|
219
|
+
| **Native token** | CFX | CFX (same) |
|
|
220
|
+
|
|
221
|
+
Use `ClientManager` to manage both simultaneously.
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## Conflux Networks
|
|
226
|
+
|
|
227
|
+
| Network | Core Chain ID | eSpace Chain ID |
|
|
228
|
+
|---------|--------------|----------------|
|
|
229
|
+
| Mainnet | 1029 | 1030 |
|
|
230
|
+
| Testnet | 1 | 71 |
|
|
231
|
+
| Local | 2029 | 2030 |
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## Development
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
pnpm install
|
|
239
|
+
pnpm build # ESM + CJS + .d.ts
|
|
240
|
+
pnpm type-check # tsc --noEmit
|
|
241
|
+
pnpm test # vitest
|
|
242
|
+
pnpm test:coverage
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
Apache-2.0 — see [LICENSE](LICENSE)
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## Relation to conflux-devkit
|
|
254
|
+
|
|
255
|
+
This library was extracted from the [conflux-devkit](https://github.com/cfxdevkit/conflux-devkit) monorepo.
|
|
256
|
+
It contains only production-usable, framework-agnostic code.
|
|
257
|
+
The DevNode management CLI, MCP server, and dashboard are **not** included.
|