@suxinmin/plugin-casper 1.0.2 → 1.0.4
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/README.md +15 -26
- package/dist/actions.js +5 -9
- package/dist/client.d.ts +1 -0
- package/dist/client.js +31 -5
- package/dist/config.d.ts +6 -0
- package/dist/config.js +21 -0
- package/dist/providers.js +4 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,6 @@ Casper blockchain plugin for Eliza AI agent framework. This plugin enables AI ag
|
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- 🏦 **Wallet Management**: Generate new wallets and restore existing ones
|
|
8
7
|
- 💰 **Balance Queries**: Check CSPR token balances for any account
|
|
9
8
|
- 💸 **Token Transfers**: Send CSPR tokens between accounts
|
|
10
9
|
- 📊 **Transaction Status**: Monitor transaction status by deploy hash
|
|
@@ -23,7 +22,10 @@ Add the following environment variables to your `.env` file:
|
|
|
23
22
|
|
|
24
23
|
```env
|
|
25
24
|
# Casper Node URL (testnet or mainnet)
|
|
26
|
-
|
|
25
|
+
# 使用公共节点(无需 API Key)
|
|
26
|
+
CASPER_NODE_URL=https://rpc.testnet.cspr.cloud:443/rpc
|
|
27
|
+
# 或者使用其他公共节点
|
|
28
|
+
# CASPER_NODE_URL=https://node.testnet.casperlabs.org:7777/rpc
|
|
27
29
|
|
|
28
30
|
# Optional: Default wallet configuration
|
|
29
31
|
CASPER_PUBLIC_KEY=your_public_key_here
|
|
@@ -32,8 +34,17 @@ CASPER_PRIVATE_KEY=your_private_key_here
|
|
|
32
34
|
|
|
33
35
|
### Network URLs
|
|
34
36
|
|
|
35
|
-
|
|
36
|
-
-
|
|
37
|
+
**Testnet 公共节点:**
|
|
38
|
+
- `https://rpc.testnet.cspr.cloud:443/rpc` (推荐)
|
|
39
|
+
- `https://node.testnet.casperlabs.org:7777/rpc`
|
|
40
|
+
- `https://testnet.cspr.cloud:443/rpc`
|
|
41
|
+
|
|
42
|
+
**Mainnet 公共节点:**
|
|
43
|
+
- `https://rpc.cspr.cloud:443/rpc` (推荐)
|
|
44
|
+
- `https://node.mainnet.casperlabs.org:7777/rpc`
|
|
45
|
+
- `https://cspr.cloud:443/rpc`
|
|
46
|
+
|
|
47
|
+
**注意:** 某些节点(如 `node.testnet.cspr.cloud`)可能需要 API Key。建议使用上面列出的公共 RPC 端点。
|
|
37
48
|
|
|
38
49
|
## Usage
|
|
39
50
|
|
|
@@ -51,20 +62,7 @@ const agent = new Agent({
|
|
|
51
62
|
|
|
52
63
|
### Available Actions
|
|
53
64
|
|
|
54
|
-
#### 1. Generate Wallet
|
|
55
|
-
|
|
56
|
-
Create a new Casper wallet with public/private key pair.
|
|
57
|
-
|
|
58
|
-
**Example:**
|
|
59
|
-
```
|
|
60
|
-
User: Create a new Casper wallet
|
|
61
|
-
Agent: I'll generate a new Casper wallet for you...
|
|
62
|
-
|
|
63
|
-
📍 Address: account-hash-xxx
|
|
64
|
-
🔑 Public Key: 02a1b2c3d4e5f6...
|
|
65
|
-
🔐 Private Key: xxxxx
|
|
66
65
|
|
|
67
|
-
⚠️ IMPORTANT: Store your private key securely! Never share it with anyone.
|
|
68
66
|
```
|
|
69
67
|
|
|
70
68
|
#### 2. Check Balance
|
|
@@ -137,15 +135,6 @@ const client = new CasperClient({
|
|
|
137
135
|
|
|
138
136
|
#### Methods
|
|
139
137
|
|
|
140
|
-
##### `generateWallet(): WalletInfo`
|
|
141
|
-
|
|
142
|
-
Generate a new wallet with key pair.
|
|
143
|
-
|
|
144
|
-
```typescript
|
|
145
|
-
const wallet = client.generateWallet();
|
|
146
|
-
console.log(wallet.address); // Account address
|
|
147
|
-
console.log(wallet.publicKey); // Public key
|
|
148
|
-
console.log(wallet.privateKey); // Private key
|
|
149
138
|
```
|
|
150
139
|
|
|
151
140
|
##### `getBalance(publicKey: string): Promise<string>`
|
package/dist/actions.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getDeployStatusAction = exports.transferAction = exports.getBalanceAction = exports.generateWalletAction = void 0;
|
|
4
|
-
const
|
|
4
|
+
const config_1 = require("./config");
|
|
5
5
|
/**
|
|
6
6
|
* 生成钱包 Action
|
|
7
7
|
*/
|
|
@@ -14,8 +14,7 @@ exports.generateWalletAction = {
|
|
|
14
14
|
},
|
|
15
15
|
handler: async (runtime, _message, state, _options, callback) => {
|
|
16
16
|
try {
|
|
17
|
-
const
|
|
18
|
-
const client = new client_1.CasperClient({ nodeUrl });
|
|
17
|
+
const client = (0, config_1.createCasperClient)(runtime);
|
|
19
18
|
const wallet = client.generateWallet();
|
|
20
19
|
const response = `I've generated a new Casper wallet for you:
|
|
21
20
|
|
|
@@ -66,8 +65,7 @@ exports.getBalanceAction = {
|
|
|
66
65
|
},
|
|
67
66
|
handler: async (runtime, message, _state, _options, callback) => {
|
|
68
67
|
try {
|
|
69
|
-
const
|
|
70
|
-
const client = new client_1.CasperClient({ nodeUrl });
|
|
68
|
+
const client = (0, config_1.createCasperClient)(runtime);
|
|
71
69
|
// 从消息中提取公钥或地址
|
|
72
70
|
const publicKey = extractPublicKey(message.content.text || '');
|
|
73
71
|
if (!publicKey) {
|
|
@@ -121,8 +119,7 @@ exports.transferAction = {
|
|
|
121
119
|
},
|
|
122
120
|
handler: async (runtime, message, _state, _options, callback) => {
|
|
123
121
|
try {
|
|
124
|
-
const
|
|
125
|
-
const client = new client_1.CasperClient({ nodeUrl });
|
|
122
|
+
const client = (0, config_1.createCasperClient)(runtime);
|
|
126
123
|
const privateKey = runtime.getSetting('CASPER_PRIVATE_KEY');
|
|
127
124
|
if (!privateKey) {
|
|
128
125
|
callback({
|
|
@@ -184,8 +181,7 @@ exports.getDeployStatusAction = {
|
|
|
184
181
|
},
|
|
185
182
|
handler: async (runtime, message, _state, _options, callback) => {
|
|
186
183
|
try {
|
|
187
|
-
const
|
|
188
|
-
const client = new client_1.CasperClient({ nodeUrl });
|
|
184
|
+
const client = (0, config_1.createCasperClient)(runtime);
|
|
189
185
|
const deployHash = extractDeployHash(message.content.text || '');
|
|
190
186
|
if (!deployHash) {
|
|
191
187
|
callback({
|
package/dist/client.d.ts
CHANGED
package/dist/client.js
CHANGED
|
@@ -2,10 +2,35 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CasperClient = void 0;
|
|
4
4
|
const casper_js_sdk_1 = require("casper-js-sdk");
|
|
5
|
+
const client_js_1 = require("@open-rpc/client-js");
|
|
6
|
+
class AuthenticatedCasperRpcClient extends casper_js_sdk_1.CasperServiceByJsonRPC {
|
|
7
|
+
constructor(url, headers) {
|
|
8
|
+
super(url);
|
|
9
|
+
const transport = new client_js_1.HTTPTransport(url, { headers });
|
|
10
|
+
this.client.requestManager.transports = [transport];
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function extractBlockHeader(blockInfo) {
|
|
14
|
+
if (blockInfo?.block?.header) {
|
|
15
|
+
return blockInfo.block.header;
|
|
16
|
+
}
|
|
17
|
+
const version2Header = blockInfo?.block_with_signatures?.block?.Version2?.header;
|
|
18
|
+
if (version2Header) {
|
|
19
|
+
return version2Header;
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
5
23
|
class CasperClient {
|
|
6
24
|
constructor(config) {
|
|
7
25
|
this.config = config;
|
|
8
|
-
|
|
26
|
+
if (config.apiKey) {
|
|
27
|
+
this.client = new AuthenticatedCasperRpcClient(config.nodeUrl, {
|
|
28
|
+
Authorization: config.apiKey,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.client = new casper_js_sdk_1.CasperServiceByJsonRPC(config.nodeUrl);
|
|
33
|
+
}
|
|
9
34
|
}
|
|
10
35
|
/**
|
|
11
36
|
* 生成新的钱包密钥对
|
|
@@ -165,10 +190,11 @@ class CasperClient {
|
|
|
165
190
|
async getLatestBlock() {
|
|
166
191
|
try {
|
|
167
192
|
const blockInfo = await this.client.getLatestBlockInfo();
|
|
193
|
+
const header = extractBlockHeader(blockInfo);
|
|
168
194
|
return {
|
|
169
|
-
stateRootHash:
|
|
170
|
-
timestamp:
|
|
171
|
-
height:
|
|
195
|
+
stateRootHash: header?.state_root_hash,
|
|
196
|
+
timestamp: header?.timestamp,
|
|
197
|
+
height: header?.height,
|
|
172
198
|
};
|
|
173
199
|
}
|
|
174
200
|
catch (error) {
|
|
@@ -181,7 +207,7 @@ class CasperClient {
|
|
|
181
207
|
*/
|
|
182
208
|
async getStateRootHash() {
|
|
183
209
|
const blockInfo = await this.client.getLatestBlockInfo();
|
|
184
|
-
return blockInfo
|
|
210
|
+
return extractBlockHeader(blockInfo)?.state_root_hash || '';
|
|
185
211
|
}
|
|
186
212
|
}
|
|
187
213
|
exports.CasperClient = CasperClient;
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { IAgentRuntime } from '@elizaos/core';
|
|
2
|
+
import { CasperClient, CasperConfig } from './client';
|
|
3
|
+
/** Official Casper Association public testnet RPC (no API key required). */
|
|
4
|
+
export declare const DEFAULT_CASPER_NODE_URL = "https://node.testnet.casper.network/rpc";
|
|
5
|
+
export declare function getCasperConfigFromRuntime(runtime: IAgentRuntime): CasperConfig;
|
|
6
|
+
export declare function createCasperClient(runtime: IAgentRuntime): CasperClient;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_CASPER_NODE_URL = void 0;
|
|
4
|
+
exports.getCasperConfigFromRuntime = getCasperConfigFromRuntime;
|
|
5
|
+
exports.createCasperClient = createCasperClient;
|
|
6
|
+
const client_1 = require("./client");
|
|
7
|
+
/** Official Casper Association public testnet RPC (no API key required). */
|
|
8
|
+
exports.DEFAULT_CASPER_NODE_URL = 'https://node.testnet.casper.network/rpc';
|
|
9
|
+
function getCasperConfigFromRuntime(runtime) {
|
|
10
|
+
const nodeUrl = runtime.getSetting('CASPER_NODE_URL') || exports.DEFAULT_CASPER_NODE_URL;
|
|
11
|
+
const apiKey = runtime.getSetting('CASPER_API_KEY');
|
|
12
|
+
const chainName = runtime.getSetting('CASPER_CHAIN_NAME');
|
|
13
|
+
return {
|
|
14
|
+
nodeUrl,
|
|
15
|
+
...(apiKey ? { apiKey } : {}),
|
|
16
|
+
...(chainName ? { chainName } : {}),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function createCasperClient(runtime) {
|
|
20
|
+
return new client_1.CasperClient(getCasperConfigFromRuntime(runtime));
|
|
21
|
+
}
|
package/dist/providers.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.casperGasProvider = exports.casperWalletProvider = exports.casperNetworkProvider = void 0;
|
|
4
|
-
const
|
|
4
|
+
const config_1 = require("./config");
|
|
5
5
|
/**
|
|
6
6
|
* Casper 网络信息 Provider
|
|
7
7
|
*/
|
|
@@ -9,8 +9,8 @@ exports.casperNetworkProvider = {
|
|
|
9
9
|
name: 'casperNetwork',
|
|
10
10
|
get: async (runtime, message, state) => {
|
|
11
11
|
try {
|
|
12
|
-
const nodeUrl =
|
|
13
|
-
const client =
|
|
12
|
+
const { nodeUrl } = (0, config_1.getCasperConfigFromRuntime)(runtime);
|
|
13
|
+
const client = (0, config_1.createCasperClient)(runtime);
|
|
14
14
|
const latestBlock = await client.getLatestBlock();
|
|
15
15
|
return {
|
|
16
16
|
text: `🌐 Casper Network Status:
|
|
@@ -39,8 +39,7 @@ exports.casperWalletProvider = {
|
|
|
39
39
|
text: 'No Casper wallet configured. Set CASPER_PUBLIC_KEY to view wallet info.'
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
-
const
|
|
43
|
-
const client = new client_1.CasperClient({ nodeUrl });
|
|
42
|
+
const client = (0, config_1.createCasperClient)(runtime);
|
|
44
43
|
const balance = await client.getBalance(publicKey);
|
|
45
44
|
const balanceInCSPR = parseInt(balance) / 1000000000;
|
|
46
45
|
return {
|