@neuraiproject/neurai-depin-msg 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Neuraiproject - Neurai.org
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,169 @@
1
+ # @neuraiproject/neurai-depin-msg
2
+
3
+ Build, encrypt, sign, and serialize DePIN messages compatible with Neurai Core.
4
+
5
+ This library produces the hex payload for the Neurai RPC method `depinsubmitmsg`.
6
+ It runs fully client-side (browser / local scripts) and does not require a backend.
7
+
8
+ ## What you get
9
+
10
+ Given:
11
+ - A plaintext message
12
+ - Your sender address + private key
13
+ - The compressed public keys of all recipients
14
+
15
+ It returns:
16
+ - `hex`: a serialized `CDepinMessage` ready to submit via `depinsubmitmsg`
17
+ - `messageHash`: the message hash as typically displayed by Neurai Core
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ npm install @neuraiproject/neurai-depin-msg
23
+ ```
24
+
25
+ ## Usage (Browser)
26
+
27
+ Include the bundled build (IIFE). It exposes a global `neuraiDepinMsg` object.
28
+
29
+ ```html
30
+ <script src="./dist/neurai-depin-msg.min.js"></script>
31
+ <script>
32
+ async function build() {
33
+ const res = await neuraiDepinMsg.buildDepinMessage({
34
+ token: 'MYTOKEN',
35
+ senderAddress: 'NxxxxYourAddress',
36
+ senderPubKey: '02abcdef... (66 hex chars)',
37
+ privateKey: 'L... (WIF) OR 64-hex private key',
38
+ timestamp: Math.floor(Date.now() / 1000),
39
+ message: 'Hello from the browser!',
40
+ recipientPubKeys: [
41
+ '02deadbeef... (recipient 1 compressed pubkey)',
42
+ '03cafebabe... (recipient 2 compressed pubkey)'
43
+ ]
44
+ });
45
+
46
+ console.log('depinsubmitmsg hex:', res.hex);
47
+ console.log('messageHash:', res.messageHash);
48
+ }
49
+
50
+ build();
51
+ </script>
52
+ ```
53
+
54
+ Submit it to your node:
55
+
56
+ ```bash
57
+ neurai-cli depinsubmitmsg "<HEX_FROM_LIBRARY>"
58
+ ```
59
+
60
+ ## Usage (Node.js)
61
+
62
+ The published build is browser-style (IIFE) and attaches to `globalThis.neuraiDepinMsg`.
63
+ Node must have WebCrypto available.
64
+
65
+ ```js
66
+ // Node 18+ usually has WebCrypto; if not, enable it explicitly:
67
+ import { webcrypto } from 'node:crypto';
68
+ if (!globalThis.crypto?.subtle) globalThis.crypto = webcrypto;
69
+
70
+ // This executes the IIFE bundle and sets globalThis.neuraiDepinMsg
71
+ import '@neuraiproject/neurai-depin-msg/dist/neurai-depin-msg.js';
72
+
73
+ const { buildDepinMessage } = globalThis.neuraiDepinMsg;
74
+
75
+ const res = await buildDepinMessage({
76
+ token: 'MYTOKEN',
77
+ senderAddress: 'NxxxxYourAddress',
78
+ senderPubKey: '02abcdef...(66 hex chars)',
79
+ privateKey: 'L... (WIF) OR 64-hex private key',
80
+ timestamp: Math.floor(Date.now() / 1000),
81
+ message: 'Hello from Node!',
82
+ recipientPubKeys: ['02deadbeef...']
83
+ });
84
+
85
+ console.log(res.hex);
86
+ ```
87
+
88
+ ## API
89
+
90
+ ### `neuraiDepinMsg.buildDepinMessage(params)`
91
+
92
+ The browser/IIFE global is `neuraiDepinMsg`.
93
+
94
+ Builds a complete serialized `CDepinMessage` (as hex) suitable for `depinsubmitmsg`.
95
+
96
+ #### Parameters
97
+
98
+ | Parameter | Type | Required | Description |
99
+ |-----------|------|----------|-------------|
100
+ | `token` | `string` | yes | Token/asset name for the DePIN channel |
101
+ | `senderAddress` | `string` | yes | Sender Neurai address (string as used by Core RPC) |
102
+ | `senderPubKey` | `string` | yes | Sender compressed public key as hex (66 chars / 33 bytes) |
103
+ | `privateKey` | `string` | yes | Sender private key as **WIF** or **64-hex** (32 bytes) |
104
+ | `timestamp` | `number` | yes | Unix time (seconds) |
105
+ | `message` | `string` | yes | Plaintext message (UTF-8) |
106
+ | `recipientPubKeys` | `string[]` | yes | Recipient compressed pubkeys as hex (66 chars each). The sender pubkey is automatically added if missing so you can decrypt your own messages. |
107
+
108
+ Notes:
109
+ - Public keys must be **compressed** (start with `02` or `03`).
110
+ - This library does not discover recipients for a token; it only encrypts for the pubkeys you provide.
111
+
112
+ #### Returns
113
+
114
+ | Property | Type | Description |
115
+ |----------|------|-------------|
116
+ | `hex` | `string` | Hex-encoded serialized `CDepinMessage` |
117
+ | `messageHash` | `string` | Double-SHA256 signing hash displayed in the typical Core-style (byte-reversed) |
118
+ | `messageHashBytes` | `string` | Raw 32-byte digest as hex (not reversed) |
119
+ | `encryptedSize` | `number` | Size of the serialized `CECIESEncryptedMessage` in bytes |
120
+ | `recipientCount` | `number` | Number of recipients (including sender if auto-added) |
121
+
122
+ ## How it works (Core-compatible)
123
+
124
+ ### Encryption (Hybrid ECIES)
125
+
126
+ This matches Neurai Core's `CECIESEncryptedMessage` format:
127
+
128
+ - Ephemeral keypair is generated per message.
129
+ - Message encryption:
130
+ - AES-256-CBC encrypts plaintext with a derived AES key.
131
+ - Payload is stored as `[IV(16) || ciphertext || HMAC-SHA256(aesKey, ciphertext)]`.
132
+ - Per-recipient key wrapping:
133
+ - ECDH derives a shared secret from ephemeral privkey + recipient pubkey.
134
+ - A per-recipient `encKey` is derived and used to AES-CBC encrypt the 32-byte AES key.
135
+ - Recipient package is `[recipientIV(16) || encryptedAESKey || HMAC-SHA256(encKey, encryptedAESKey)]`.
136
+
137
+ ### Serialization
138
+
139
+ All fields are serialized using Bitcoin-style rules:
140
+ - CompactSize prefixes for vectors/strings
141
+ - Little-endian integers
142
+
143
+ ### Signing
144
+
145
+ The signature hash is:
146
+
147
+ `doubleSHA256( serialize(token) || serialize(senderAddress) || int64(timestamp) || vector(encryptedPayloadBytes) )`
148
+
149
+ `messageHash` is the display-friendly byte-reversed form (similar to how Core prints `uint256`).
150
+
151
+ ## Demo
152
+
153
+ Open `demo.html` in a browser (a local server is recommended):
154
+
155
+ ```bash
156
+ python3 -m http.server 8080
157
+ # Open http://localhost:8080/demo.html
158
+ ```
159
+
160
+ ## Development
161
+
162
+ ```bash
163
+ npm run build:dev # dist/neurai-depin-msg.js
164
+ npm run build # dist/neurai-depin-msg.min.js
165
+ ```
166
+
167
+ ## License
168
+
169
+ MIT