mmn-client-js 1.0.10 → 1.0.12

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 CHANGED
@@ -105,7 +105,8 @@ const walletInfo = await indexerClient.getWalletDetail('wallet-address');
105
105
  // Create ZK client for proof generation
106
106
  const zkClient = new ZkClient({
107
107
  endpoint: 'https://zk.mmn.network',
108
- chainId: 'mmn-mainnet',
108
+ timeout: 30000,
109
+ // chainId is not required for ZK client
109
110
  });
110
111
 
111
112
  // Generate ZK proof for authentication
@@ -178,6 +179,21 @@ const response = await client.sendTransaction({
178
179
  });
179
180
  ```
180
181
 
182
+ **`sendTransactionByPrivateKey(params): Promise<AddTxResponse>`**
183
+ Create, sign (using the provided private key) and submit a transaction without relying on a client-stored keypair.
184
+
185
+ ```typescript
186
+ const response = await client.sendTransactionByPrivateKey({
187
+ sender: 'user123',
188
+ recipient: 'user456',
189
+ amount: '1000000000000000000',
190
+ nonce: 1,
191
+ textData: 'Optional message',
192
+ extraInfo: { type: 'transfer_token', UserSenderId: 'user123', ... },
193
+ privateKey: 'private-key-pkcs8-hex'
194
+ });
195
+ ```
196
+
181
197
  **`getCurrentNonce(address: string, tag?: 'latest' | 'pending'): Promise<GetCurrentNonceResponse>`**
182
198
  Get current nonce for an account.
183
199
 
package/dist/index.d.ts CHANGED
@@ -19,26 +19,23 @@ interface IEphemeralKeyPair {
19
19
  privateKey: string;
20
20
  publicKey: string;
21
21
  }
22
- interface IZkProof {
23
- proof: string;
24
- public_input: string;
25
- }
26
22
  declare enum ETransferType {
27
23
  GiveCoffee = "give_coffee",
28
24
  TransferToken = "transfer_token",
29
25
  UnlockItem = "unlock_item"
30
26
  }
31
27
  interface ExtraInfo {
32
- type: ETransferType;
28
+ type: ETransferType | string;
33
29
  ItemId?: string;
34
30
  ItemType?: string;
35
31
  ClanId?: string;
36
32
  UserSenderId: string;
37
33
  UserSenderUsername: string;
38
- UserReceiverId: string;
34
+ UserReceiverId?: string;
39
35
  ChannelId?: string;
40
36
  MessageRefId?: string;
41
37
  ExtraAttribute?: string;
38
+ [x: string]: string;
42
39
  }
43
40
  interface TxMsg {
44
41
  type: number;
@@ -166,6 +163,21 @@ interface ZkClientConfig {
166
163
  timeout?: number;
167
164
  headers?: Record<string, string>;
168
165
  }
166
+ declare enum EZkClientType {
167
+ MEZON = "mezon",
168
+ OAUTH = "oauth"
169
+ }
170
+ interface GetZkProofRequest {
171
+ userId: string;
172
+ ephemeralPublicKey: string;
173
+ jwt: string;
174
+ address: string;
175
+ clientType?: EZkClientType;
176
+ }
177
+ interface IZkProof {
178
+ proof: string;
179
+ public_input: string;
180
+ }
169
181
 
170
182
  declare class IndexerClient {
171
183
  private endpoint;
@@ -246,12 +258,16 @@ declare class MmnClient {
246
258
  * Send a transaction (create, sign, and submit)
247
259
  */
248
260
  sendTransaction(params: SendTransactionRequest): Promise<AddTxResponse>;
261
+ sendTransactionByAddress(params: SendTransactionRequest): Promise<AddTxResponse>;
262
+ sendTransactionByPrivateKey(params: SendTransactionRequest): Promise<AddTxResponse>;
249
263
  /**
250
264
  * Get current nonce for an account
251
265
  */
252
266
  getCurrentNonce(userId: string, tag?: 'latest' | 'pending'): Promise<GetCurrentNonceResponse>;
253
267
  getAccountByUserId(userId: string): Promise<GetAccountByAddressResponse>;
254
268
  scaleAmountToDecimals(originalAmount: string | number, decimals?: number): string;
269
+ validateAddress(addr: string): boolean;
270
+ validateAmount(balance: string, amount: string | number): boolean;
255
271
  }
256
272
  declare function createMmnClient(config: MmnClientConfig): MmnClient;
257
273
 
@@ -261,13 +277,8 @@ declare class ZkClient {
261
277
  private headers;
262
278
  constructor(config: ZkClientConfig);
263
279
  private makeRequest;
264
- getZkProofs({ userId, ephemeralPublicKey, jwt, address, }: {
265
- userId: string;
266
- ephemeralPublicKey: string;
267
- jwt: string;
268
- address: string;
269
- }): Promise<IZkProof>;
280
+ getZkProofs({ userId, ephemeralPublicKey, jwt, address, clientType, }: GetZkProofRequest): Promise<IZkProof>;
270
281
  }
271
282
 
272
- export { ETransferType, IndexerClient, MmnClient, ZkClient, createMmnClient };
273
- export type { AddTxResponse, ExtraInfo, GetAccountByAddressResponse, GetCurrentNonceResponse, IEphemeralKeyPair, IZkProof, IndexerClientConfig, JsonRpcError, JsonRpcRequest, JsonRpcResponse, ListTransactionResponse, Meta, MmnClientConfig, SendTransactionRequest, SignedTx, Transaction, TransactionDetailResponse, TxMsg, WalletDetail, WalletDetailResponse, ZkClientConfig };
283
+ export { ETransferType, EZkClientType, IndexerClient, MmnClient, ZkClient, createMmnClient };
284
+ export type { AddTxResponse, ExtraInfo, GetAccountByAddressResponse, GetCurrentNonceResponse, GetZkProofRequest, IEphemeralKeyPair, IZkProof, IndexerClientConfig, JsonRpcError, JsonRpcRequest, JsonRpcResponse, ListTransactionResponse, Meta, MmnClientConfig, SendTransactionRequest, SignedTx, Transaction, TransactionDetailResponse, TxMsg, WalletDetail, WalletDetailResponse, ZkClientConfig };
package/dist/index.esm.js CHANGED
@@ -7678,7 +7678,7 @@ const PRNG_CONSTANTS = {
7678
7678
  };
7679
7679
  const TX_TYPE = {
7680
7680
  TRANSFER: 0,
7681
- FAUCET: 1,
7681
+ PRIVATE_KEY: 1,
7682
7682
  };
7683
7683
  const DECIMALS = 6;
7684
7684
  class MmnClient {
@@ -7928,12 +7928,19 @@ class MmnClient {
7928
7928
  * Create and sign a transaction message
7929
7929
  */
7930
7930
  createAndSignTx(params) {
7931
- const fromAddress = this.getAddressFromUserId(params.sender);
7932
- const toAddress = this.getAddressFromUserId(params.recipient);
7931
+ if (!this.validateAddress(params.sender)) {
7932
+ throw new Error('Invalid sender address');
7933
+ }
7934
+ if (!this.validateAddress(params.recipient)) {
7935
+ throw new Error('Invalid recipient address');
7936
+ }
7937
+ if (params.sender === params.recipient) {
7938
+ throw new Error('Sender and recipient addresses cannot be the same');
7939
+ }
7933
7940
  const txMsg = {
7934
7941
  type: params.type,
7935
- sender: fromAddress,
7936
- recipient: toAddress,
7942
+ sender: params.sender,
7943
+ recipient: params.recipient,
7937
7944
  amount: params.amount,
7938
7945
  timestamp: params.timestamp || Date.now(),
7939
7946
  text_data: params.textData || '',
@@ -7990,7 +7997,7 @@ class MmnClient {
7990
7997
  seed.fill(0);
7991
7998
  keyPair.secretKey.fill(0);
7992
7999
  // Return signature based on transaction type
7993
- if (tx.type === TX_TYPE.FAUCET) {
8000
+ if (tx.type === TX_TYPE.PRIVATE_KEY) {
7994
8001
  return bs58.encode(BufferCompat.from(signature));
7995
8002
  }
7996
8003
  // For regular transactions, wrap signature with public key
@@ -8021,9 +8028,27 @@ class MmnClient {
8021
8028
  * Send a transaction (create, sign, and submit)
8022
8029
  */
8023
8030
  async sendTransaction(params) {
8031
+ const fromAddress = this.getAddressFromUserId(params.sender);
8032
+ const toAddress = this.getAddressFromUserId(params.recipient);
8024
8033
  const signedTx = this.createAndSignTx({
8025
8034
  ...params,
8026
8035
  type: TX_TYPE.TRANSFER,
8036
+ sender: fromAddress,
8037
+ recipient: toAddress,
8038
+ });
8039
+ return this.addTx(signedTx);
8040
+ }
8041
+ async sendTransactionByAddress(params) {
8042
+ const signedTx = this.createAndSignTx({
8043
+ ...params,
8044
+ type: TX_TYPE.TRANSFER,
8045
+ });
8046
+ return this.addTx(signedTx);
8047
+ }
8048
+ async sendTransactionByPrivateKey(params) {
8049
+ const signedTx = this.createAndSignTx({
8050
+ ...params,
8051
+ type: TX_TYPE.PRIVATE_KEY,
8027
8052
  });
8028
8053
  return this.addTx(signedTx);
8029
8054
  }
@@ -8047,6 +8072,19 @@ class MmnClient {
8047
8072
  }
8048
8073
  return scaledAmount.toString();
8049
8074
  }
8075
+ validateAddress(addr) {
8076
+ const decoded = bs58.decode(addr);
8077
+ if (!decoded ||
8078
+ decoded.length !== CRYPTO_CONSTANTS.ED25519_PUBLIC_KEY_LENGTH) {
8079
+ return false;
8080
+ }
8081
+ return true;
8082
+ }
8083
+ validateAmount(balance, amount) {
8084
+ const bigBalance = BigInt(balance);
8085
+ const bigAmount = BigInt(typeof amount === 'number' ? this.scaleAmountToDecimals(amount) : amount);
8086
+ return bigAmount <= bigBalance;
8087
+ }
8050
8088
  }
8051
8089
  function createMmnClient(config) {
8052
8090
  return new MmnClient(config);
@@ -8059,6 +8097,11 @@ var ETransferType;
8059
8097
  ETransferType["TransferToken"] = "transfer_token";
8060
8098
  ETransferType["UnlockItem"] = "unlock_item";
8061
8099
  })(ETransferType || (ETransferType = {}));
8100
+ var EZkClientType;
8101
+ (function (EZkClientType) {
8102
+ EZkClientType["MEZON"] = "mezon";
8103
+ EZkClientType["OAUTH"] = "oauth";
8104
+ })(EZkClientType || (EZkClientType = {}));
8062
8105
 
8063
8106
  class ZkClient {
8064
8107
  constructor(config) {
@@ -8114,17 +8157,18 @@ class ZkClient {
8114
8157
  throw new Error('Request failed');
8115
8158
  }
8116
8159
  }
8117
- async getZkProofs({ userId, ephemeralPublicKey, jwt, address, }) {
8160
+ async getZkProofs({ userId, ephemeralPublicKey, jwt, address, clientType = EZkClientType.MEZON, }) {
8118
8161
  const path = `prove`;
8119
8162
  const res = await this.makeRequest('POST', path, undefined, {
8120
8163
  user_id: userId,
8121
8164
  ephemeral_pk: ephemeralPublicKey,
8122
8165
  jwt,
8123
8166
  address,
8167
+ client_type: clientType,
8124
8168
  });
8125
8169
  return res.data;
8126
8170
  }
8127
8171
  }
8128
8172
 
8129
- export { ETransferType, IndexerClient, MmnClient, ZkClient, createMmnClient };
8173
+ export { ETransferType, EZkClientType, IndexerClient, MmnClient, ZkClient, createMmnClient };
8130
8174
  //# sourceMappingURL=index.esm.js.map