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 +17 -1
- package/dist/index.d.ts +25 -14
- package/dist/index.esm.js +52 -8
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +51 -7
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -7680,7 +7680,7 @@ const PRNG_CONSTANTS = {
|
|
|
7680
7680
|
};
|
|
7681
7681
|
const TX_TYPE = {
|
|
7682
7682
|
TRANSFER: 0,
|
|
7683
|
-
|
|
7683
|
+
PRIVATE_KEY: 1,
|
|
7684
7684
|
};
|
|
7685
7685
|
const DECIMALS = 6;
|
|
7686
7686
|
class MmnClient {
|
|
@@ -7930,12 +7930,19 @@ class MmnClient {
|
|
|
7930
7930
|
* Create and sign a transaction message
|
|
7931
7931
|
*/
|
|
7932
7932
|
createAndSignTx(params) {
|
|
7933
|
-
|
|
7934
|
-
|
|
7933
|
+
if (!this.validateAddress(params.sender)) {
|
|
7934
|
+
throw new Error('Invalid sender address');
|
|
7935
|
+
}
|
|
7936
|
+
if (!this.validateAddress(params.recipient)) {
|
|
7937
|
+
throw new Error('Invalid recipient address');
|
|
7938
|
+
}
|
|
7939
|
+
if (params.sender === params.recipient) {
|
|
7940
|
+
throw new Error('Sender and recipient addresses cannot be the same');
|
|
7941
|
+
}
|
|
7935
7942
|
const txMsg = {
|
|
7936
7943
|
type: params.type,
|
|
7937
|
-
sender:
|
|
7938
|
-
recipient:
|
|
7944
|
+
sender: params.sender,
|
|
7945
|
+
recipient: params.recipient,
|
|
7939
7946
|
amount: params.amount,
|
|
7940
7947
|
timestamp: params.timestamp || Date.now(),
|
|
7941
7948
|
text_data: params.textData || '',
|
|
@@ -7992,7 +7999,7 @@ class MmnClient {
|
|
|
7992
7999
|
seed.fill(0);
|
|
7993
8000
|
keyPair.secretKey.fill(0);
|
|
7994
8001
|
// Return signature based on transaction type
|
|
7995
|
-
if (tx.type === TX_TYPE.
|
|
8002
|
+
if (tx.type === TX_TYPE.PRIVATE_KEY) {
|
|
7996
8003
|
return bs58.encode(BufferCompat.from(signature));
|
|
7997
8004
|
}
|
|
7998
8005
|
// For regular transactions, wrap signature with public key
|
|
@@ -8023,9 +8030,27 @@ class MmnClient {
|
|
|
8023
8030
|
* Send a transaction (create, sign, and submit)
|
|
8024
8031
|
*/
|
|
8025
8032
|
async sendTransaction(params) {
|
|
8033
|
+
const fromAddress = this.getAddressFromUserId(params.sender);
|
|
8034
|
+
const toAddress = this.getAddressFromUserId(params.recipient);
|
|
8026
8035
|
const signedTx = this.createAndSignTx({
|
|
8027
8036
|
...params,
|
|
8028
8037
|
type: TX_TYPE.TRANSFER,
|
|
8038
|
+
sender: fromAddress,
|
|
8039
|
+
recipient: toAddress,
|
|
8040
|
+
});
|
|
8041
|
+
return this.addTx(signedTx);
|
|
8042
|
+
}
|
|
8043
|
+
async sendTransactionByAddress(params) {
|
|
8044
|
+
const signedTx = this.createAndSignTx({
|
|
8045
|
+
...params,
|
|
8046
|
+
type: TX_TYPE.TRANSFER,
|
|
8047
|
+
});
|
|
8048
|
+
return this.addTx(signedTx);
|
|
8049
|
+
}
|
|
8050
|
+
async sendTransactionByPrivateKey(params) {
|
|
8051
|
+
const signedTx = this.createAndSignTx({
|
|
8052
|
+
...params,
|
|
8053
|
+
type: TX_TYPE.PRIVATE_KEY,
|
|
8029
8054
|
});
|
|
8030
8055
|
return this.addTx(signedTx);
|
|
8031
8056
|
}
|
|
@@ -8049,6 +8074,19 @@ class MmnClient {
|
|
|
8049
8074
|
}
|
|
8050
8075
|
return scaledAmount.toString();
|
|
8051
8076
|
}
|
|
8077
|
+
validateAddress(addr) {
|
|
8078
|
+
const decoded = bs58.decode(addr);
|
|
8079
|
+
if (!decoded ||
|
|
8080
|
+
decoded.length !== CRYPTO_CONSTANTS.ED25519_PUBLIC_KEY_LENGTH) {
|
|
8081
|
+
return false;
|
|
8082
|
+
}
|
|
8083
|
+
return true;
|
|
8084
|
+
}
|
|
8085
|
+
validateAmount(balance, amount) {
|
|
8086
|
+
const bigBalance = BigInt(balance);
|
|
8087
|
+
const bigAmount = BigInt(typeof amount === 'number' ? this.scaleAmountToDecimals(amount) : amount);
|
|
8088
|
+
return bigAmount <= bigBalance;
|
|
8089
|
+
}
|
|
8052
8090
|
}
|
|
8053
8091
|
function createMmnClient(config) {
|
|
8054
8092
|
return new MmnClient(config);
|
|
@@ -8061,6 +8099,11 @@ exports.ETransferType = void 0;
|
|
|
8061
8099
|
ETransferType["TransferToken"] = "transfer_token";
|
|
8062
8100
|
ETransferType["UnlockItem"] = "unlock_item";
|
|
8063
8101
|
})(exports.ETransferType || (exports.ETransferType = {}));
|
|
8102
|
+
exports.EZkClientType = void 0;
|
|
8103
|
+
(function (EZkClientType) {
|
|
8104
|
+
EZkClientType["MEZON"] = "mezon";
|
|
8105
|
+
EZkClientType["OAUTH"] = "oauth";
|
|
8106
|
+
})(exports.EZkClientType || (exports.EZkClientType = {}));
|
|
8064
8107
|
|
|
8065
8108
|
class ZkClient {
|
|
8066
8109
|
constructor(config) {
|
|
@@ -8116,13 +8159,14 @@ class ZkClient {
|
|
|
8116
8159
|
throw new Error('Request failed');
|
|
8117
8160
|
}
|
|
8118
8161
|
}
|
|
8119
|
-
async getZkProofs({ userId, ephemeralPublicKey, jwt, address, }) {
|
|
8162
|
+
async getZkProofs({ userId, ephemeralPublicKey, jwt, address, clientType = exports.EZkClientType.MEZON, }) {
|
|
8120
8163
|
const path = `prove`;
|
|
8121
8164
|
const res = await this.makeRequest('POST', path, undefined, {
|
|
8122
8165
|
user_id: userId,
|
|
8123
8166
|
ephemeral_pk: ephemeralPublicKey,
|
|
8124
8167
|
jwt,
|
|
8125
8168
|
address,
|
|
8169
|
+
client_type: clientType,
|
|
8126
8170
|
});
|
|
8127
8171
|
return res.data;
|
|
8128
8172
|
}
|