@neuraiproject/neurai-rpc 0.4.7 → 0.5.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 +80 -0
- package/DEPIN_EXAMPLES.md +148 -220
- package/DEPIN_IMPLEMENTATION_GUIDE_EN.md +239 -70
- package/README.md +98 -46
- package/depin.ts +465 -214
- package/dist/depin.cjs +305 -0
- package/dist/depin.d.cts +96 -0
- package/dist/depin.d.ts +96 -0
- package/dist/depin.mjs +280 -0
- package/dist/index.cjs +5251 -4941
- package/dist/{types.d.ts → index.d.cts} +10 -63
- package/dist/index.d.ts +200 -0
- package/dist/index.mjs +5224 -4933
- package/docs.json +140 -134
- package/docs.ts +512 -64
- package/index.ts +13 -9
- package/neurai_methods.md +482 -64
- package/package.json +39 -11
- package/createDocs.mjs +0 -61
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/test-depin.ts +0 -187
- package/test.js +0 -56
- package/tsconfig.json +0 -26
package/test-depin.ts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DePIN RPC Test Example
|
|
3
|
-
*
|
|
4
|
-
* This is an example test file showing how to use getDePinRPC.
|
|
5
|
-
* To run actual tests, you need:
|
|
6
|
-
* 1. A running Neurai node with DePIN enabled
|
|
7
|
-
* 2. A valid DePIN token
|
|
8
|
-
* 3. A way to sign messages (wallet with private key)
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { getDePinRPC, requestDePinChallenge } from "./index";
|
|
12
|
-
|
|
13
|
-
// Mock signing function for testing
|
|
14
|
-
async function mockSignMessage(message: string): Promise<string> {
|
|
15
|
-
// In production, this would call your actual signing function
|
|
16
|
-
// For example: neurai-cli signmessage "address" "message"
|
|
17
|
-
console.log("Mock signing message:", message);
|
|
18
|
-
return "H1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234==";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async function testRequestChallenge() {
|
|
22
|
-
console.log("\n=== Test 1: Request Challenge ===");
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
const challenge = await requestDePinChallenge("http://localhost:19002", {
|
|
26
|
-
token: "TESTTOKEN",
|
|
27
|
-
address: "NXtestAddress123",
|
|
28
|
-
signMessage: mockSignMessage,
|
|
29
|
-
mode: "SEND"
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
console.log("✓ Challenge received:");
|
|
33
|
-
console.log(" Challenge:", challenge.challenge);
|
|
34
|
-
console.log(" Timeout:", challenge.timeout, "seconds");
|
|
35
|
-
console.log(" Message to sign:", challenge.messageToSign);
|
|
36
|
-
} catch (error: any) {
|
|
37
|
-
console.error("✗ Failed to request challenge:", error.message);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function testSendMessage() {
|
|
42
|
-
console.log("\n=== Test 2: Send DePIN Message ===");
|
|
43
|
-
|
|
44
|
-
const depinRpc = getDePinRPC("http://localhost:19002", {
|
|
45
|
-
token: "TESTTOKEN",
|
|
46
|
-
address: "NXsenderAddress123",
|
|
47
|
-
signMessage: mockSignMessage,
|
|
48
|
-
mode: "SEND"
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
const result = await depinRpc("depinsendmsg", [
|
|
53
|
-
"TESTTOKEN",
|
|
54
|
-
"localhost",
|
|
55
|
-
"Hello from test!",
|
|
56
|
-
"NXsenderAddress123"
|
|
57
|
-
]);
|
|
58
|
-
|
|
59
|
-
console.log("✓ Message sent successfully:");
|
|
60
|
-
console.log(" Result:", result);
|
|
61
|
-
} catch (error: any) {
|
|
62
|
-
console.error("✗ Failed to send message:");
|
|
63
|
-
console.error(" Error:", error.error);
|
|
64
|
-
console.error(" Description:", error.description);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async function testGetMessages() {
|
|
69
|
-
console.log("\n=== Test 3: Get DePIN Messages ===");
|
|
70
|
-
|
|
71
|
-
const depinRpc = getDePinRPC("http://localhost:19002", {
|
|
72
|
-
token: "TESTTOKEN",
|
|
73
|
-
address: "NXreceiverAddress123",
|
|
74
|
-
signMessage: mockSignMessage,
|
|
75
|
-
mode: "RECEIVE"
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
const messages = await depinRpc("depingetmsg", [
|
|
80
|
-
"TESTTOKEN",
|
|
81
|
-
"localhost",
|
|
82
|
-
"NXreceiverAddress123"
|
|
83
|
-
]);
|
|
84
|
-
|
|
85
|
-
console.log("✓ Messages retrieved:");
|
|
86
|
-
console.log(" Count:", messages.length);
|
|
87
|
-
if (messages.length > 0) {
|
|
88
|
-
console.log(" First message:", messages[0]);
|
|
89
|
-
}
|
|
90
|
-
} catch (error: any) {
|
|
91
|
-
console.error("✗ Failed to get messages:");
|
|
92
|
-
console.error(" Error:", error.error);
|
|
93
|
-
console.error(" Description:", error.description);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async function testErrorHandling() {
|
|
98
|
-
console.log("\n=== Test 4: Error Handling ===");
|
|
99
|
-
|
|
100
|
-
// Test with invalid URL
|
|
101
|
-
const depinRpc = getDePinRPC("http://invalid-server:19002", {
|
|
102
|
-
token: "TESTTOKEN",
|
|
103
|
-
address: "NXtestAddress123",
|
|
104
|
-
signMessage: mockSignMessage,
|
|
105
|
-
mode: "SEND"
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
await depinRpc("depinsendmsg", [
|
|
110
|
-
"TESTTOKEN",
|
|
111
|
-
"localhost",
|
|
112
|
-
"This should fail",
|
|
113
|
-
"NXtestAddress123"
|
|
114
|
-
]);
|
|
115
|
-
|
|
116
|
-
console.error("✗ Should have thrown an error");
|
|
117
|
-
} catch (error: any) {
|
|
118
|
-
console.log("✓ Error caught as expected:");
|
|
119
|
-
console.log(" Type:", error.type);
|
|
120
|
-
console.log(" Error:", error.error);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async function testChallengeReuse() {
|
|
125
|
-
console.log("\n=== Test 5: Challenge Reuse ===");
|
|
126
|
-
|
|
127
|
-
const depinRpc = getDePinRPC("http://localhost:19002", {
|
|
128
|
-
token: "TESTTOKEN",
|
|
129
|
-
address: "NXtestAddress123",
|
|
130
|
-
signMessage: mockSignMessage,
|
|
131
|
-
mode: "SEND"
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
console.log("Making first call...");
|
|
136
|
-
await depinRpc("depinsendmsg", [
|
|
137
|
-
"TESTTOKEN",
|
|
138
|
-
"localhost",
|
|
139
|
-
"First message",
|
|
140
|
-
"NXtestAddress123"
|
|
141
|
-
]);
|
|
142
|
-
|
|
143
|
-
console.log("Making second call (should reuse challenge)...");
|
|
144
|
-
await depinRpc("depinsendmsg", [
|
|
145
|
-
"TESTTOKEN",
|
|
146
|
-
"localhost",
|
|
147
|
-
"Second message",
|
|
148
|
-
"NXtestAddress123"
|
|
149
|
-
]);
|
|
150
|
-
|
|
151
|
-
console.log("✓ Challenge reused successfully");
|
|
152
|
-
} catch (error: any) {
|
|
153
|
-
console.error("✗ Failed:", error.error);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Main test runner
|
|
158
|
-
async function runTests() {
|
|
159
|
-
console.log("==============================================");
|
|
160
|
-
console.log("DePIN RPC Client Test Suite");
|
|
161
|
-
console.log("==============================================");
|
|
162
|
-
console.log("\nNote: These tests require a running Neurai node");
|
|
163
|
-
console.log("with DePIN enabled on port 19002");
|
|
164
|
-
console.log("==============================================");
|
|
165
|
-
|
|
166
|
-
await testRequestChallenge();
|
|
167
|
-
await testSendMessage();
|
|
168
|
-
await testGetMessages();
|
|
169
|
-
await testErrorHandling();
|
|
170
|
-
await testChallengeReuse();
|
|
171
|
-
|
|
172
|
-
console.log("\n==============================================");
|
|
173
|
-
console.log("Tests completed");
|
|
174
|
-
console.log("==============================================\n");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Uncomment to run tests
|
|
178
|
-
// runTests().catch(console.error);
|
|
179
|
-
|
|
180
|
-
export {
|
|
181
|
-
testRequestChallenge,
|
|
182
|
-
testSendMessage,
|
|
183
|
-
testGetMessages,
|
|
184
|
-
testErrorHandling,
|
|
185
|
-
testChallengeReuse,
|
|
186
|
-
runTests
|
|
187
|
-
};
|
package/test.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
const NeuraiRPC = require("./dist/index.cjs");
|
|
2
|
-
|
|
3
|
-
test("3rd party service getblockcount", async () => {
|
|
4
|
-
const rpc = NeuraiRPC.getRPC(
|
|
5
|
-
"anon",
|
|
6
|
-
"anon",
|
|
7
|
-
"https://rpc-main.neurai.org/rpc"
|
|
8
|
-
);
|
|
9
|
-
let count = 0;
|
|
10
|
-
try {
|
|
11
|
-
count = await rpc("getblockcount", []);
|
|
12
|
-
} catch (e) {}
|
|
13
|
-
|
|
14
|
-
const countMoreThanZero = count > 0;
|
|
15
|
-
expect(countMoreThanZero).toBe(true);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("3rd party service incorrect method", async () => {
|
|
19
|
-
const rpc = NeuraiRPC.getRPC(
|
|
20
|
-
"anon",
|
|
21
|
-
"anon",
|
|
22
|
-
"https://rpc-main.neurai.org/rpc"
|
|
23
|
-
);
|
|
24
|
-
const method = "getblockcountWRONG";
|
|
25
|
-
const params = [];
|
|
26
|
-
|
|
27
|
-
let theError = null;
|
|
28
|
-
try {
|
|
29
|
-
const count = await rpc(method, params);
|
|
30
|
-
} catch (e) {
|
|
31
|
-
theError = e;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
expect(!!theError).toBe(true);
|
|
35
|
-
expect(theError.error).toBe("Not in whitelist");
|
|
36
|
-
expect(theError.description).toBe(
|
|
37
|
-
"Method getblockcountWRONG is not supported"
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("Non existing 3rd party service getblockcount", async () => {
|
|
42
|
-
const rpc = NeuraiRPC.getRPC(
|
|
43
|
-
"anon",
|
|
44
|
-
"anon",
|
|
45
|
-
"https://rpc-main.neurai.org/rpc"
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
let error = null;
|
|
49
|
-
try {
|
|
50
|
-
const count = await rpc("getblockcount", []);
|
|
51
|
-
} catch (e) {
|
|
52
|
-
error = e;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
expect(!!error).toBe(true);
|
|
56
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2015",
|
|
4
|
-
"lib": ["ES2015", "DOM"],
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"moduleResolution": "node",
|
|
7
|
-
"esModuleInterop": true,
|
|
8
|
-
"skipLibCheck": true,
|
|
9
|
-
"strict": false,
|
|
10
|
-
"resolveJsonModule": true,
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"declarationMap": true,
|
|
13
|
-
"sourceMap": true,
|
|
14
|
-
"outDir": "./dist",
|
|
15
|
-
"types": ["node"]
|
|
16
|
-
},
|
|
17
|
-
"include": [
|
|
18
|
-
"index.ts",
|
|
19
|
-
"depin.ts",
|
|
20
|
-
"docs.ts"
|
|
21
|
-
],
|
|
22
|
-
"exclude": [
|
|
23
|
-
"node_modules",
|
|
24
|
-
"dist"
|
|
25
|
-
]
|
|
26
|
-
}
|