mmn-client-js 1.0.10 → 1.0.11

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/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,15 @@ 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>;
249
262
  /**
250
263
  * Get current nonce for an account
251
264
  */
252
265
  getCurrentNonce(userId: string, tag?: 'latest' | 'pending'): Promise<GetCurrentNonceResponse>;
253
266
  getAccountByUserId(userId: string): Promise<GetAccountByAddressResponse>;
254
267
  scaleAmountToDecimals(originalAmount: string | number, decimals?: number): string;
268
+ validateAddress(addr: string): boolean;
269
+ validateAmount(balance: string, amount: string | number): boolean;
255
270
  }
256
271
  declare function createMmnClient(config: MmnClientConfig): MmnClient;
257
272
 
@@ -261,13 +276,8 @@ declare class ZkClient {
261
276
  private headers;
262
277
  constructor(config: ZkClientConfig);
263
278
  private makeRequest;
264
- getZkProofs({ userId, ephemeralPublicKey, jwt, address, }: {
265
- userId: string;
266
- ephemeralPublicKey: string;
267
- jwt: string;
268
- address: string;
269
- }): Promise<IZkProof>;
279
+ getZkProofs({ userId, ephemeralPublicKey, jwt, address, clientType, }: GetZkProofRequest): Promise<IZkProof>;
270
280
  }
271
281
 
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 };
282
+ export { ETransferType, EZkClientType, IndexerClient, MmnClient, ZkClient, createMmnClient };
283
+ 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
@@ -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 || '',
@@ -8021,6 +8028,17 @@ 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);
8033
+ const signedTx = this.createAndSignTx({
8034
+ ...params,
8035
+ type: TX_TYPE.TRANSFER,
8036
+ sender: fromAddress,
8037
+ recipient: toAddress,
8038
+ });
8039
+ return this.addTx(signedTx);
8040
+ }
8041
+ async sendTransactionByAddress(params) {
8024
8042
  const signedTx = this.createAndSignTx({
8025
8043
  ...params,
8026
8044
  type: TX_TYPE.TRANSFER,
@@ -8047,6 +8065,19 @@ class MmnClient {
8047
8065
  }
8048
8066
  return scaledAmount.toString();
8049
8067
  }
8068
+ validateAddress(addr) {
8069
+ const decoded = bs58.decode(addr);
8070
+ if (!decoded ||
8071
+ decoded.length !== CRYPTO_CONSTANTS.ED25519_PUBLIC_KEY_LENGTH) {
8072
+ return false;
8073
+ }
8074
+ return true;
8075
+ }
8076
+ validateAmount(balance, amount) {
8077
+ const bigBalance = BigInt(balance);
8078
+ const bigAmount = BigInt(typeof amount === 'number' ? this.scaleAmountToDecimals(amount) : amount);
8079
+ return bigAmount <= bigBalance;
8080
+ }
8050
8081
  }
8051
8082
  function createMmnClient(config) {
8052
8083
  return new MmnClient(config);
@@ -8059,6 +8090,11 @@ var ETransferType;
8059
8090
  ETransferType["TransferToken"] = "transfer_token";
8060
8091
  ETransferType["UnlockItem"] = "unlock_item";
8061
8092
  })(ETransferType || (ETransferType = {}));
8093
+ var EZkClientType;
8094
+ (function (EZkClientType) {
8095
+ EZkClientType["MEZON"] = "mezon";
8096
+ EZkClientType["OAUTH"] = "oauth";
8097
+ })(EZkClientType || (EZkClientType = {}));
8062
8098
 
8063
8099
  class ZkClient {
8064
8100
  constructor(config) {
@@ -8114,17 +8150,18 @@ class ZkClient {
8114
8150
  throw new Error('Request failed');
8115
8151
  }
8116
8152
  }
8117
- async getZkProofs({ userId, ephemeralPublicKey, jwt, address, }) {
8153
+ async getZkProofs({ userId, ephemeralPublicKey, jwt, address, clientType = EZkClientType.MEZON, }) {
8118
8154
  const path = `prove`;
8119
8155
  const res = await this.makeRequest('POST', path, undefined, {
8120
8156
  user_id: userId,
8121
8157
  ephemeral_pk: ephemeralPublicKey,
8122
8158
  jwt,
8123
8159
  address,
8160
+ client_type: clientType,
8124
8161
  });
8125
8162
  return res.data;
8126
8163
  }
8127
8164
  }
8128
8165
 
8129
- export { ETransferType, IndexerClient, MmnClient, ZkClient, createMmnClient };
8166
+ export { ETransferType, EZkClientType, IndexerClient, MmnClient, ZkClient, createMmnClient };
8130
8167
  //# sourceMappingURL=index.esm.js.map