mmn-client-js 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) 2024 MezonAI Team
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,90 @@
1
+ # @mezon/mmn-client-js
2
+
3
+ A TypeScript client for interacting with the MMN blockchain via JSON-RPC. It supports creating, signing, and submitting transactions, plus basic account and transaction queries.
4
+
5
+ ## Features
6
+
7
+ - πŸ” Transaction signing (Ed25519)
8
+ - πŸ“ Create and sign transactions
9
+ - πŸš€ JSON-RPC client for MMN
10
+ - πŸ“¦ TypeScript with exported types
11
+ - 🌐 Browser & Node.js compatible
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @mezonai/mmn-client-js
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ```typescript
22
+ import { MmnClient } from '@mezonai/mmn-client-js';
23
+
24
+ // Create a client instance
25
+ const client = new MmnClient({
26
+ baseUrl: 'http://localhost:8080',
27
+ timeout: 30000
28
+ });
29
+
30
+ // Send a transaction
31
+ const response = await client.sendTransaction({
32
+ sender: 'sender-address',
33
+ recipient: 'recipient-address',
34
+ amount: '1000000000000000000',
35
+ textData: 'Hello MMN!',
36
+ privateKey: 'private-key-hex'
37
+ });
38
+
39
+ if (response.ok) {
40
+ console.log('Transaction Hash:', response.tx_hash);
41
+ } else {
42
+ console.error('Error:', response.error);
43
+ }
44
+ ```
45
+
46
+ ## API Reference
47
+
48
+ ### Client Configuration
49
+
50
+ ```typescript
51
+ interface MmnClientConfig {
52
+ baseUrl: string; // MMN JSON-RPC base URL
53
+ timeout?: number; // Request timeout in ms (default: 30000)
54
+ headers?: Record<string, string>; // Additional headers
55
+ axiosConfig?: AxiosRequestConfig; // Optional axios overrides
56
+ }
57
+ ```
58
+
59
+ ### Transaction Signing
60
+
61
+ The client signs transactions with Ed25519. Provide a 64‑byte secret key or a 32‑byte seed (as `Uint8Array` or string hex/base58).
62
+
63
+ ### Transaction Operations
64
+
65
+ #### `sendTransaction(params): Promise<AddTxResponse>`
66
+ Create, sign, and submit a transaction in one call.
67
+
68
+ ```typescript
69
+ const response = await client.sendTransaction({
70
+ sender: 'sender-address',
71
+ recipient: 'recipient-address',
72
+ amount: '1000000000000000000',
73
+ nonce,
74
+ textData: 'Optional message',
75
+ extraInfo: 'Optional extra',
76
+ privateKey: new Uint8Array(/* 64-byte secretKey */)
77
+ });
78
+ ```
79
+
80
+ ## License
81
+
82
+ MIT
83
+
84
+ ## Contributing
85
+
86
+ Contributions are welcome! Please feel free to submit a Pull Request.
87
+
88
+ ## Support
89
+
90
+ For support, please open an issue on the [GitHub repository](https://github.com/mezonai/mmn/issues).
@@ -0,0 +1,206 @@
1
+ import { AxiosRequestConfig } from 'axios';
2
+
3
+ interface JsonRpcRequest {
4
+ jsonrpc: '2.0';
5
+ method: string;
6
+ params?: unknown;
7
+ id: string | number;
8
+ }
9
+ interface JsonRpcError {
10
+ code: number;
11
+ message: string;
12
+ data?: unknown;
13
+ }
14
+ interface JsonRpcResponse<T = unknown> {
15
+ jsonrpc: '2.0';
16
+ result?: T;
17
+ error?: JsonRpcError;
18
+ id: string | number;
19
+ }
20
+ interface IWallet {
21
+ address: string;
22
+ privateKey: string;
23
+ recoveryPhrase: string;
24
+ }
25
+ declare enum ETransferType {
26
+ GiveCoffee = "give_coffee",
27
+ TransferToken = "transfer_token",
28
+ UnlockItem = "unlock_item"
29
+ }
30
+ interface ExtraInfo {
31
+ type: ETransferType;
32
+ ItemId?: string;
33
+ ItemType?: string;
34
+ ClanId?: string;
35
+ UserSenderId: string;
36
+ UserSenderUsername: string;
37
+ UserReceiverId: string;
38
+ ChannelId?: string;
39
+ MessageRefId?: string;
40
+ }
41
+ interface TxMsg {
42
+ type: number;
43
+ sender: string;
44
+ recipient: string;
45
+ amount: string;
46
+ timestamp: number;
47
+ text_data: string;
48
+ nonce: number;
49
+ extra_info: string;
50
+ }
51
+ interface SignedTx {
52
+ tx_msg: TxMsg;
53
+ signature: string;
54
+ }
55
+ interface AddTxResponse {
56
+ ok: boolean;
57
+ tx_hash: string;
58
+ error: string;
59
+ }
60
+ interface GetCurrentNonceResponse {
61
+ address: string;
62
+ nonce: number;
63
+ tag: string;
64
+ error: string;
65
+ }
66
+ interface GetAccountByAddressResponse {
67
+ address: string;
68
+ balance: string;
69
+ nonce: number;
70
+ decimals: number;
71
+ }
72
+ interface MmnClientConfig {
73
+ baseUrl: string;
74
+ timeout?: number;
75
+ headers?: Record<string, string>;
76
+ axiosConfig?: AxiosRequestConfig;
77
+ }
78
+ interface Transaction {
79
+ chain_id: string;
80
+ hash: string;
81
+ nonce: number;
82
+ block_hash: string;
83
+ block_number: number;
84
+ block_timestamp: number;
85
+ transaction_index: number;
86
+ from_address: string;
87
+ to_address: string;
88
+ value: string;
89
+ gas: number;
90
+ gas_price: string;
91
+ data: string;
92
+ function_selector: string;
93
+ max_fee_per_gas: string;
94
+ max_priority_fee_per_gas: string;
95
+ max_fee_per_blob_gas?: string;
96
+ blob_versioned_hashes?: string[];
97
+ transaction_type: number;
98
+ r: string;
99
+ s: string;
100
+ v: string;
101
+ access_list_json?: string;
102
+ authorization_list_json?: string;
103
+ contract_address?: string;
104
+ gas_used?: number;
105
+ cumulative_gas_used?: number;
106
+ effective_gas_price?: string;
107
+ blob_gas_used?: number;
108
+ blob_gas_price?: string;
109
+ logs_bloom?: string;
110
+ status?: number;
111
+ transaction_timestamp: number;
112
+ text_data: string;
113
+ }
114
+ interface Meta {
115
+ chain_id: number;
116
+ address?: string;
117
+ signature?: string;
118
+ page: number;
119
+ limit?: number;
120
+ total_items?: number;
121
+ total_pages?: number;
122
+ }
123
+ interface WalletDetail {
124
+ address: string;
125
+ balance: string;
126
+ account_nonce: number;
127
+ last_balance_update: number;
128
+ }
129
+ interface WalletDetailResponse {
130
+ data: WalletDetail;
131
+ }
132
+ interface ListTransactionResponse {
133
+ meta: Meta;
134
+ data?: Transaction[];
135
+ }
136
+ interface TransactionDetailResponse {
137
+ data: {
138
+ transaction: Transaction;
139
+ };
140
+ }
141
+ interface IndexerClientConfig {
142
+ endpoint: string;
143
+ chainId: string;
144
+ timeout?: number;
145
+ headers?: Record<string, string>;
146
+ }
147
+
148
+ declare class IndexerClient {
149
+ private axiosInstance;
150
+ private endpoint;
151
+ private chainId;
152
+ constructor(config: IndexerClientConfig);
153
+ private makeRequest;
154
+ getTransactionByHash(hash: string): Promise<Transaction>;
155
+ getTransactionByWallet(wallet: string, page: number | undefined, limit: number | undefined, filter: number): Promise<ListTransactionResponse>;
156
+ getWalletDetail(wallet: string): Promise<WalletDetail>;
157
+ }
158
+
159
+ declare class MmnClient {
160
+ private config;
161
+ private axiosInstance;
162
+ private requestId;
163
+ constructor(config: MmnClientConfig);
164
+ private makeRequest;
165
+ private rawEd25519ToPkcs8Hex;
166
+ createWallet(): IWallet;
167
+ /**
168
+ * Create and sign a transaction message
169
+ */
170
+ private createAndSignTx;
171
+ /**
172
+ * Sign a transaction with Ed25519
173
+ */
174
+ private signTransaction;
175
+ /**
176
+ * Serialize transaction for signing
177
+ */
178
+ private serializeTransaction;
179
+ /**
180
+ * Add a signed transaction to the blockchain
181
+ */
182
+ private addTx;
183
+ /**
184
+ * Send a transaction (create, sign, and submit)
185
+ */
186
+ sendTransaction(params: {
187
+ sender: string;
188
+ recipient: string;
189
+ amount: string;
190
+ nonce: number;
191
+ timestamp?: number;
192
+ textData?: string;
193
+ extraInfo?: ExtraInfo;
194
+ privateKey: string;
195
+ }): Promise<AddTxResponse>;
196
+ /**
197
+ * Get current nonce for an account
198
+ */
199
+ getCurrentNonce(address: string, tag?: 'latest' | 'pending'): Promise<GetCurrentNonceResponse>;
200
+ getAccountByAddress(address: string): Promise<GetAccountByAddressResponse>;
201
+ scaleAmountToDecimals(originalAmount: string | number, decimals: number): string;
202
+ }
203
+ declare function createMmnClient(config: MmnClientConfig): MmnClient;
204
+
205
+ export { ETransferType, IndexerClient, MmnClient, createMmnClient };
206
+ export type { AddTxResponse, ExtraInfo, GetAccountByAddressResponse, GetCurrentNonceResponse, IWallet, IndexerClientConfig, JsonRpcError, JsonRpcRequest, JsonRpcResponse, ListTransactionResponse, Meta, MmnClientConfig, SignedTx, Transaction, TransactionDetailResponse, TxMsg, WalletDetail, WalletDetailResponse };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC"}