mmn-client-js 1.0.1 → 1.0.3
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 +165 -32
- package/dist/index.d.ts +52 -8
- package/dist/index.esm.js +228 -22033
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +247 -22032
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/indexer-client.d.ts +0 -12
- package/dist/indexer-client.d.ts.map +0 -1
- package/dist/mmn-client.d.ts +0 -48
- package/dist/mmn-client.d.ts.map +0 -1
- package/dist/types.d.ts +0 -146
- package/dist/types.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
# mmn-client-js
|
|
2
2
|
|
|
3
|
-
A TypeScript client for interacting with the MMN blockchain
|
|
3
|
+
A comprehensive TypeScript client for interacting with the MMN blockchain ecosystem. It provides complete functionality for blockchain transactions, account management, indexer queries, and ZK proof generation.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- 🔐 Transaction signing (Ed25519)
|
|
8
|
-
- 📝
|
|
9
|
-
- 🚀 JSON-RPC client for MMN
|
|
10
|
-
-
|
|
11
|
-
-
|
|
7
|
+
- 🔐 **Transaction signing** (Ed25519 with NaCl)
|
|
8
|
+
- 📝 **Complete transaction lifecycle** (create, sign, submit)
|
|
9
|
+
- 🚀 **JSON-RPC client** for MMN blockchain
|
|
10
|
+
- 📊 **Indexer client** for transaction history and wallet details
|
|
11
|
+
- � **ZK proof generation** for privacy-preserving authentication
|
|
12
|
+
- 💰 **Wallet management** with BIP39 mnemonic support
|
|
13
|
+
- 🎯 **User ID to address conversion** with SHA256 hashing
|
|
14
|
+
- �📦 **Full TypeScript support** with exported types
|
|
15
|
+
- 🌐 **Cross-platform** (Browser & Node.js compatible)
|
|
12
16
|
|
|
13
17
|
## Installation
|
|
14
18
|
|
|
@@ -16,70 +20,199 @@ A TypeScript client for interacting with the MMN blockchain via JSON-RPC. It sup
|
|
|
16
20
|
npm install mmn-client-js
|
|
17
21
|
```
|
|
18
22
|
|
|
23
|
+
Or using yarn:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn add mmn-client-js
|
|
27
|
+
```
|
|
28
|
+
|
|
19
29
|
## Quick Start
|
|
20
30
|
|
|
31
|
+
### Basic MMN Client Usage
|
|
32
|
+
|
|
21
33
|
```typescript
|
|
22
|
-
import { MmnClient } from 'mmn-client-js';
|
|
34
|
+
import { MmnClient, IndexerClient, ZkClient } from 'mmn-client-js';
|
|
23
35
|
|
|
24
|
-
// Create
|
|
25
|
-
const
|
|
36
|
+
// Create MMN client for blockchain operations
|
|
37
|
+
const mmnClient = new MmnClient({
|
|
26
38
|
baseUrl: 'http://localhost:8080',
|
|
27
|
-
timeout: 30000
|
|
39
|
+
timeout: 30000,
|
|
28
40
|
});
|
|
29
41
|
|
|
42
|
+
// Generate ephemeral key pair for ZK authentication
|
|
43
|
+
const keyPair = mmnClient.generateEphemeralKeyPair();
|
|
44
|
+
console.log('Public Key:', keyPair.publicKey);
|
|
45
|
+
|
|
46
|
+
// Convert user ID to blockchain address
|
|
47
|
+
const senderAddress = mmnClient.getAddressFromUserId('user123');
|
|
48
|
+
const recipientAddress = mmnClient.getAddressFromUserId('user456');
|
|
49
|
+
|
|
50
|
+
// Get current nonce for sender
|
|
51
|
+
const nonceResponse = await mmnClient.getCurrentNonce(senderAddress);
|
|
52
|
+
const currentNonce = nonceResponse.nonce;
|
|
53
|
+
|
|
54
|
+
// Get account information by user ID
|
|
55
|
+
const account = await mmnClient.getAccountByUserId('user123');
|
|
56
|
+
console.log('Balance:', account.balance);
|
|
57
|
+
|
|
30
58
|
// Send a transaction
|
|
31
|
-
const response = await
|
|
32
|
-
sender: '
|
|
33
|
-
recipient: '
|
|
59
|
+
const response = await mmnClient.sendTransaction({
|
|
60
|
+
sender: 'user123',
|
|
61
|
+
recipient: 'user456',
|
|
34
62
|
amount: '1000000000000000000',
|
|
63
|
+
nonce: currentNonce + 1,
|
|
35
64
|
textData: 'Hello MMN!',
|
|
36
|
-
|
|
65
|
+
publicKey: keyPair.publicKey,
|
|
66
|
+
privateKey: keyPair.privateKey,
|
|
67
|
+
zkProof: 'zk-proof-string',
|
|
68
|
+
zkPub: 'zk-public-string',
|
|
37
69
|
});
|
|
38
70
|
|
|
39
71
|
if (response.ok) {
|
|
40
72
|
console.log('Transaction Hash:', response.tx_hash);
|
|
41
73
|
} else {
|
|
42
|
-
console.error('
|
|
74
|
+
console.error('Transaction failed:', response.error);
|
|
43
75
|
}
|
|
44
76
|
```
|
|
45
77
|
|
|
78
|
+
### Indexer Client Usage
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// Create indexer client for transaction history
|
|
82
|
+
const indexerClient = new IndexerClient({
|
|
83
|
+
endpoint: 'https://indexer.mmn.network',
|
|
84
|
+
chainId: 'mmn-mainnet',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Get transaction history for a wallet
|
|
88
|
+
const transactions = await indexerClient.getTransactionByWallet(
|
|
89
|
+
'wallet-address',
|
|
90
|
+
1, // page
|
|
91
|
+
50, // limit
|
|
92
|
+
0 // filter: 0=all, 1=received, 2=sent
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// Get specific transaction details
|
|
96
|
+
const tx = await indexerClient.getTransactionByHash('tx-hash');
|
|
97
|
+
|
|
98
|
+
// Get wallet details
|
|
99
|
+
const walletInfo = await indexerClient.getWalletDetail('wallet-address');
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### ZK Proof Client Usage
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Create ZK client for proof generation
|
|
106
|
+
const zkClient = new ZkClient({
|
|
107
|
+
endpoint: 'https://zk.mmn.network',
|
|
108
|
+
chainId: 'mmn-mainnet',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Generate ZK proof for authentication
|
|
112
|
+
const zkProof = await zkClient.getZkProofs({
|
|
113
|
+
userId: 'user123',
|
|
114
|
+
ephemeralPublicKey: keyPair.publicKey,
|
|
115
|
+
jwt: 'jwt-token',
|
|
116
|
+
address: 'wallet-address',
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
46
120
|
## API Reference
|
|
47
121
|
|
|
48
|
-
### Client Configuration
|
|
122
|
+
### MMN Client Configuration
|
|
49
123
|
|
|
50
124
|
```typescript
|
|
51
125
|
interface MmnClientConfig {
|
|
52
|
-
baseUrl: string;
|
|
53
|
-
timeout?: number;
|
|
54
|
-
headers?: Record<string, string>;
|
|
55
|
-
axiosConfig?: AxiosRequestConfig;
|
|
126
|
+
baseUrl: string; // MMN JSON-RPC base URL
|
|
127
|
+
timeout?: number; // Request timeout in ms (default: 30000)
|
|
128
|
+
headers?: Record<string, string>; // Additional headers
|
|
129
|
+
axiosConfig?: AxiosRequestConfig; // Optional axios overrides
|
|
56
130
|
}
|
|
57
131
|
```
|
|
58
132
|
|
|
59
|
-
###
|
|
133
|
+
### Indexer Client Configuration
|
|
60
134
|
|
|
61
|
-
|
|
135
|
+
```typescript
|
|
136
|
+
interface IndexerClientConfig {
|
|
137
|
+
endpoint: string; // Indexer API endpoint
|
|
138
|
+
chainId: string; // Blockchain chain ID
|
|
139
|
+
timeout?: number; // Request timeout in ms (default: 30000)
|
|
140
|
+
headers?: Record<string, string>; // Additional headers
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Core Methods
|
|
145
|
+
|
|
146
|
+
#### MMN Client Methods
|
|
147
|
+
|
|
148
|
+
**`generateEphemeralKeyPair(): IEphemeralKeyPair`**
|
|
149
|
+
Generate a new Ed25519 key pair using BIP39 mnemonic.
|
|
62
150
|
|
|
63
|
-
|
|
151
|
+
```typescript
|
|
152
|
+
const keyPair = client.generateEphemeralKeyPair();
|
|
153
|
+
// Returns: { privateKey: string, publicKey: string }
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
**`getAddressFromUserId(userId: string): string`**
|
|
157
|
+
Convert user ID to blockchain address using SHA256 + Base58.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const address = client.getAddressFromUserId('user123');
|
|
161
|
+
```
|
|
64
162
|
|
|
65
|
-
|
|
66
|
-
Create, sign, and submit a transaction
|
|
163
|
+
**`sendTransaction(params): Promise<AddTxResponse>`**
|
|
164
|
+
Create, sign, and submit a transaction.
|
|
67
165
|
|
|
68
166
|
```typescript
|
|
69
167
|
const response = await client.sendTransaction({
|
|
70
|
-
sender: '
|
|
71
|
-
recipient: '
|
|
168
|
+
sender: 'user123',
|
|
169
|
+
recipient: 'user456',
|
|
72
170
|
amount: '1000000000000000000',
|
|
73
|
-
nonce,
|
|
171
|
+
nonce: 1,
|
|
74
172
|
textData: 'Optional message',
|
|
75
|
-
extraInfo: '
|
|
76
|
-
|
|
173
|
+
extraInfo: { type: 'transfer_token', UserSenderId: 'user123', ... },
|
|
174
|
+
publicKey: 'public-key-base58',
|
|
175
|
+
privateKey: 'private-key-pkcs8-hex',
|
|
176
|
+
zkProof: 'zk-proof-string',
|
|
177
|
+
zkPub: 'zk-public-string'
|
|
77
178
|
});
|
|
78
179
|
```
|
|
79
180
|
|
|
80
|
-
|
|
181
|
+
**`getCurrentNonce(address: string, tag?: 'latest' | 'pending'): Promise<GetCurrentNonceResponse>`**
|
|
182
|
+
Get current nonce for an account.
|
|
81
183
|
|
|
82
|
-
|
|
184
|
+
**`getAccountByUserId(userId: string): Promise<GetAccountByAddressResponse>`**
|
|
185
|
+
Get account details by user ID.
|
|
186
|
+
|
|
187
|
+
**`scaleAmountToDecimals(amount: string | number, decimals: number): string`**
|
|
188
|
+
Scale amount to blockchain decimals.
|
|
189
|
+
|
|
190
|
+
#### Indexer Client Methods
|
|
191
|
+
|
|
192
|
+
**`getTransactionByHash(hash: string): Promise<Transaction>`**
|
|
193
|
+
Get transaction details by hash.
|
|
194
|
+
|
|
195
|
+
**`getTransactionByWallet(wallet: string, page: number, limit: number, filter: number): Promise<ListTransactionResponse>`**
|
|
196
|
+
Get transaction history for a wallet.
|
|
197
|
+
|
|
198
|
+
- `filter`: 0=all, 1=received, 2=sent
|
|
199
|
+
|
|
200
|
+
**`getWalletDetail(wallet: string): Promise<WalletDetail>`**
|
|
201
|
+
Get wallet balance and account information.
|
|
202
|
+
|
|
203
|
+
#### ZK Client Methods
|
|
204
|
+
|
|
205
|
+
**`getZkProofs(params): Promise<IZkProof>`**
|
|
206
|
+
Generate ZK proof for authentication.
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
const zkProof = await zkClient.getZkProofs({
|
|
210
|
+
userId: 'user123',
|
|
211
|
+
ephemeralPublicKey: 'ephemeral-public-key',
|
|
212
|
+
jwt: 'jwt-token',
|
|
213
|
+
address: 'wallet-address',
|
|
214
|
+
});
|
|
215
|
+
```
|
|
83
216
|
|
|
84
217
|
## Contributing
|
|
85
218
|
|
package/dist/index.d.ts
CHANGED
|
@@ -17,10 +17,13 @@ interface JsonRpcResponse<T = unknown> {
|
|
|
17
17
|
error?: JsonRpcError;
|
|
18
18
|
id: string | number;
|
|
19
19
|
}
|
|
20
|
-
interface
|
|
21
|
-
address: string;
|
|
20
|
+
interface IEphemeralKeyPair {
|
|
22
21
|
privateKey: string;
|
|
23
|
-
|
|
22
|
+
publicKey: string;
|
|
23
|
+
}
|
|
24
|
+
interface IZkProof {
|
|
25
|
+
proof: string;
|
|
26
|
+
public_input: string;
|
|
24
27
|
}
|
|
25
28
|
declare enum ETransferType {
|
|
26
29
|
GiveCoffee = "give_coffee",
|
|
@@ -47,6 +50,8 @@ interface TxMsg {
|
|
|
47
50
|
text_data: string;
|
|
48
51
|
nonce: number;
|
|
49
52
|
extra_info: string;
|
|
53
|
+
zk_proof: string;
|
|
54
|
+
zk_pub: string;
|
|
50
55
|
}
|
|
51
56
|
interface SignedTx {
|
|
52
57
|
tx_msg: TxMsg;
|
|
@@ -110,6 +115,7 @@ interface Transaction {
|
|
|
110
115
|
status?: number;
|
|
111
116
|
transaction_timestamp: number;
|
|
112
117
|
text_data: string;
|
|
118
|
+
extra_info: string;
|
|
113
119
|
}
|
|
114
120
|
interface Meta {
|
|
115
121
|
chain_id: number;
|
|
@@ -144,6 +150,11 @@ interface IndexerClientConfig {
|
|
|
144
150
|
timeout?: number;
|
|
145
151
|
headers?: Record<string, string>;
|
|
146
152
|
}
|
|
153
|
+
interface ZkClientConfig {
|
|
154
|
+
endpoint: string;
|
|
155
|
+
timeout?: number;
|
|
156
|
+
headers?: Record<string, string>;
|
|
157
|
+
}
|
|
147
158
|
|
|
148
159
|
declare class IndexerClient {
|
|
149
160
|
private axiosInstance;
|
|
@@ -162,14 +173,31 @@ declare class MmnClient {
|
|
|
162
173
|
private requestId;
|
|
163
174
|
constructor(config: MmnClientConfig);
|
|
164
175
|
private makeRequest;
|
|
176
|
+
/**
|
|
177
|
+
* Securely convert raw Ed25519 private key to PKCS#8 format
|
|
178
|
+
* @param raw - Raw 32-byte Ed25519 private key
|
|
179
|
+
* @returns PKCS#8 formatted private key in hex
|
|
180
|
+
* @throws Error if input validation fails
|
|
181
|
+
*/
|
|
165
182
|
private rawEd25519ToPkcs8Hex;
|
|
166
|
-
|
|
183
|
+
private encodeLength;
|
|
184
|
+
/**
|
|
185
|
+
* Securely generate ephemeral key pair with proper entropy
|
|
186
|
+
* @returns Ephemeral key pair with private and public keys
|
|
187
|
+
* @throws Error if key generation fails
|
|
188
|
+
*/
|
|
189
|
+
generateEphemeralKeyPair(): IEphemeralKeyPair;
|
|
190
|
+
getAddressFromUserId(userId: string): string;
|
|
167
191
|
/**
|
|
168
192
|
* Create and sign a transaction message
|
|
169
193
|
*/
|
|
170
194
|
private createAndSignTx;
|
|
171
195
|
/**
|
|
172
|
-
*
|
|
196
|
+
* Securely sign a transaction with Ed25519
|
|
197
|
+
* @param tx - Transaction message to sign
|
|
198
|
+
* @param privateKeyHex - Private key in PKCS#8 hex format
|
|
199
|
+
* @returns Base58 encoded signature
|
|
200
|
+
* @throws Error if signing fails
|
|
173
201
|
*/
|
|
174
202
|
private signTransaction;
|
|
175
203
|
/**
|
|
@@ -191,16 +219,32 @@ declare class MmnClient {
|
|
|
191
219
|
timestamp?: number;
|
|
192
220
|
textData?: string;
|
|
193
221
|
extraInfo?: ExtraInfo;
|
|
222
|
+
publicKey: string;
|
|
194
223
|
privateKey: string;
|
|
224
|
+
zkProof: string;
|
|
225
|
+
zkPub: string;
|
|
195
226
|
}): Promise<AddTxResponse>;
|
|
196
227
|
/**
|
|
197
228
|
* Get current nonce for an account
|
|
198
229
|
*/
|
|
199
230
|
getCurrentNonce(address: string, tag?: 'latest' | 'pending'): Promise<GetCurrentNonceResponse>;
|
|
200
|
-
|
|
231
|
+
getAccountByUserId(userId: string): Promise<GetAccountByAddressResponse>;
|
|
201
232
|
scaleAmountToDecimals(originalAmount: string | number, decimals: number): string;
|
|
202
233
|
}
|
|
203
234
|
declare function createMmnClient(config: MmnClientConfig): MmnClient;
|
|
204
235
|
|
|
205
|
-
|
|
206
|
-
|
|
236
|
+
declare class ZkClient {
|
|
237
|
+
private axiosInstance;
|
|
238
|
+
private endpoint;
|
|
239
|
+
constructor(config: ZkClientConfig);
|
|
240
|
+
private makeRequest;
|
|
241
|
+
getZkProofs({ userId, ephemeralPublicKey, jwt, address, }: {
|
|
242
|
+
userId: string;
|
|
243
|
+
ephemeralPublicKey: string;
|
|
244
|
+
jwt: string;
|
|
245
|
+
address: string;
|
|
246
|
+
}): Promise<IZkProof>;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export { ETransferType, IndexerClient, MmnClient, ZkClient, createMmnClient };
|
|
250
|
+
export type { AddTxResponse, ExtraInfo, GetAccountByAddressResponse, GetCurrentNonceResponse, IEphemeralKeyPair, IZkProof, IndexerClientConfig, JsonRpcError, JsonRpcRequest, JsonRpcResponse, ListTransactionResponse, Meta, MmnClientConfig, SignedTx, Transaction, TransactionDetailResponse, TxMsg, WalletDetail, WalletDetailResponse, ZkClientConfig };
|