wallaby-cash 1.7.5 → 1.7.8

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.cjs.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toString
5
5
  const ethCryptoJs = require('eth-crypto-js');
6
6
  const axios = require('axios');
7
7
  const cryptoJs = require('crypto-js');
8
+ const jwtDecode = require('jwt-decode');
8
9
  const uuid = require('uuid');
9
10
 
10
11
  const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e : { default: e };
@@ -4490,6 +4491,7 @@ var defaultEnvironment = (_a = browser$1$1.env.NODE_ENV) !== null && _a !== void
4490
4491
  * Please pass the network value in the WallabyConfigOptions object to override it for production-mainnet.
4491
4492
  */
4492
4493
  var defaultNetwork = 'testnet';
4494
+ var isTest = browser$1$1.env.NODE_ENV === 'test';
4493
4495
 
4494
4496
  var StorageService = /** @class */ (function () {
4495
4497
  function StorageService(_a) {
@@ -4941,6 +4943,32 @@ function objectToQueryParams(obj) {
4941
4943
  return params.toString();
4942
4944
  }
4943
4945
 
4946
+ /**
4947
+ * Checks if a given JWT has expired.
4948
+ *
4949
+ * @param jwt - The JWT string to check. If null, the function will return true.
4950
+ * @returns `true` if the JWT has expired or is null, otherwise `false`.
4951
+ */
4952
+ function jwtHasExpired(jwt) {
4953
+ if (!jwt) {
4954
+ return true;
4955
+ }
4956
+ return millisecondsToJWTTimeout(jwt) <= 0;
4957
+ }
4958
+ /**
4959
+ * Calculates the remaining time in milliseconds until a given JWT expires.
4960
+ *
4961
+ * @param jwt - The JWT string to decode and check.
4962
+ * @returns The number of milliseconds until the JWT expires. If the JWT has already expired, the returned value will be less than or equal to 0.
4963
+ */
4964
+ function millisecondsToJWTTimeout(jwt) {
4965
+ var jwtDecoded = jwtDecode.jwtDecode(jwt);
4966
+ var jwtExpirationTimeSeconds = jwtDecoded.exp;
4967
+ var jwtExpirationTimeMilliseconds = (jwtExpirationTimeSeconds !== null && jwtExpirationTimeSeconds !== void 0 ? jwtExpirationTimeSeconds : 0) * 1000;
4968
+ var timeNowMilliseconds = Date.now();
4969
+ return jwtExpirationTimeMilliseconds - timeNowMilliseconds;
4970
+ }
4971
+
4944
4972
  var Base = /** @class */ (function () {
4945
4973
  function Base(config) {
4946
4974
  var _a;
@@ -5125,6 +5153,9 @@ var Base = /** @class */ (function () {
5125
5153
  return __generator(this, function (_f) {
5126
5154
  switch (_f.label) {
5127
5155
  case 0:
5156
+ if (!isTest && jwtHasExpired(this.clientAuthToken)) {
5157
+ throw new Error('Auth token has expired - blocking API call.');
5158
+ }
5128
5159
  _d = {
5129
5160
  'Content-Type': 'application/json',
5130
5161
  'x-api-key': this.settings.apiKey
@@ -5207,6 +5238,11 @@ var WalletRoutes = {
5207
5238
  method: exports.RequestMethod.Get,
5208
5239
  requiresAccessToken: true,
5209
5240
  },
5241
+ addTokenToWallet: {
5242
+ fullPath: '/users/assets/add',
5243
+ method: exports.RequestMethod.Post,
5244
+ requiresAccessToken: true,
5245
+ },
5210
5246
  getUserWallets: {
5211
5247
  fullPath: function (_a) {
5212
5248
  var userId = _a.userId;
@@ -5292,6 +5328,11 @@ var SystemRoutes = {
5292
5328
  method: exports.RequestMethod.Get,
5293
5329
  requiresAccessToken: false,
5294
5330
  },
5331
+ fetchTokenInfo: {
5332
+ fullPath: function () { return "/query/fetch-token-info"; },
5333
+ method: exports.RequestMethod.Get,
5334
+ requiresAccessToken: false,
5335
+ },
5295
5336
  };
5296
5337
 
5297
5338
  var Auth = /** @class */ (function (_super) {
@@ -5609,6 +5650,25 @@ var Auth = /** @class */ (function (_super) {
5609
5650
  return Auth;
5610
5651
  }(Base));
5611
5652
 
5653
+ /**
5654
+ * Sanitizes a Sui object ID by removing any part of the string after '::'.
5655
+ *
5656
+ * @param objectId - The Sui object ID to sanitize. Example input: "0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07::icorgi::ICORGI"
5657
+ * @returns The sanitized Sui object ID. Example output: "0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07"
5658
+ */
5659
+ var sanitizeSuiObjectId = function (objectId) {
5660
+ if (!objectId) {
5661
+ throw new Error('Sui object ID is required');
5662
+ }
5663
+ if (!objectId.startsWith('0x')) {
5664
+ throw new Error('Invalid Sui object ID');
5665
+ }
5666
+ if (!objectId.includes('::')) {
5667
+ return objectId;
5668
+ }
5669
+ return objectId.split('::')[0];
5670
+ };
5671
+
5612
5672
  var Wallet = /** @class */ (function (_super) {
5613
5673
  __extends(Wallet, _super);
5614
5674
  function Wallet(config) {
@@ -6382,9 +6442,54 @@ var Wallet = /** @class */ (function (_super) {
6382
6442
  });
6383
6443
  });
6384
6444
  };
6445
+ Wallet.prototype.addTokenToWallet = function (_a) {
6446
+ return __awaiter(this, arguments, void 0, function (_b) {
6447
+ var contractAddress, response, error_16;
6448
+ var assetConfigId = _b.assetConfigId, walletId = _b.walletId, userId = _b.userId, config = _b.config;
6449
+ return __generator(this, function (_c) {
6450
+ switch (_c.label) {
6451
+ case 0:
6452
+ _c.trys.push([0, 2, , 3]);
6453
+ if (!assetConfigId || !walletId || !userId || !config) {
6454
+ throw new Error('Missing required parameters');
6455
+ }
6456
+ if (!assetConfigId && !config) {
6457
+ throw new Error('Either assetConfigId or config is required');
6458
+ }
6459
+ contractAddress = config.contractAddress;
6460
+ if (config.chainName === exports.BlockchainName.Sui) {
6461
+ if (!config.suiObjectId || !!config.contractAddress) {
6462
+ throw new Error('`suiObjectId` instead of `contractAddress` is required for Sui chain');
6463
+ }
6464
+ contractAddress = sanitizeSuiObjectId(config.suiObjectId);
6465
+ }
6466
+ return [4 /*yield*/, this.request(WalletRoutes.addTokenToWallet, {
6467
+ method: WalletRoutes.addTokenToWallet.method,
6468
+ payload: {
6469
+ assetConfigId: assetConfigId,
6470
+ walletId: walletId,
6471
+ userId: userId,
6472
+ config: __assign(__assign({}, config), { contractAddress: contractAddress }),
6473
+ },
6474
+ })];
6475
+ case 1:
6476
+ response = _c.sent();
6477
+ if (!response || response.errors) {
6478
+ throw new Error(response.errors);
6479
+ }
6480
+ return [2 /*return*/, response];
6481
+ case 2:
6482
+ error_16 = _c.sent();
6483
+ logger('SDK::Wallet::addTokenToWallet::error', error_16);
6484
+ return [2 /*return*/, Promise.reject(error_16)];
6485
+ case 3: return [2 /*return*/];
6486
+ }
6487
+ });
6488
+ });
6489
+ };
6385
6490
  Wallet.prototype.initRecoveryFlow = function (_a) {
6386
6491
  return __awaiter(this, arguments, void 0, function (_b) {
6387
- var initRecovery, error_16;
6492
+ var initRecovery, error_17;
6388
6493
  var clientAuthPubKey = _b.clientAuthPubKey;
6389
6494
  return __generator(this, function (_c) {
6390
6495
  switch (_c.label) {
@@ -6404,9 +6509,9 @@ var Wallet = /** @class */ (function (_super) {
6404
6509
  }
6405
6510
  return [2 /*return*/, { signatureMessage: initRecovery.signatureMessage, wallabyAuthPubKey: initRecovery.wallabyAuthPubKey }];
6406
6511
  case 2:
6407
- error_16 = _c.sent();
6408
- logger('SDK::Wallet::initRecoveryFlow::error', error_16);
6409
- return [2 /*return*/, Promise.reject(error_16)];
6512
+ error_17 = _c.sent();
6513
+ logger('SDK::Wallet::initRecoveryFlow::error', error_17);
6514
+ return [2 /*return*/, Promise.reject(error_17)];
6410
6515
  case 3: return [2 /*return*/];
6411
6516
  }
6412
6517
  });
@@ -6926,6 +7031,95 @@ var System = /** @class */ (function (_super) {
6926
7031
  });
6927
7032
  });
6928
7033
  };
7034
+ /**
7035
+ * Validates the contract address or SUI object ID for a given blockchain.
7036
+ *
7037
+ * @public
7038
+ * @param {BlockchainName} blockchain - The name of the blockchain.
7039
+ * @param {string} contractAddress - The contract address (optional for SUI blockchain).
7040
+ * @param {string} suiObjectId - The SUI object ID (optional for SUI blockchain).
7041
+ *
7042
+ * @returns {string} The validated contract address or SUI object ID.
7043
+ *
7044
+ * @throws Will throw an error if both `contractAddress` and `suiObjectId` are provided for SUI blockchain.
7045
+ * @throws Will throw an error if neither `contractAddress` nor `suiObjectId` are provided for SUI blockchain.
7046
+ * @throws Will throw an error if `contractAddress` is not provided for non-SUI blockchains.
7047
+ * @throws Will throw an error if the blockchain is not supported.
7048
+ */
7049
+ System.prototype.validateContractAddress = function (_a) {
7050
+ var blockchain = _a.blockchain, contractAddress = _a.contractAddress, suiObjectId = _a.suiObjectId;
7051
+ switch (blockchain) {
7052
+ case exports.BlockchainName.Algorand:
7053
+ case exports.BlockchainName.Bitcoin:
7054
+ throw new Error('This blockchain is not supported');
7055
+ case exports.BlockchainName.Sui:
7056
+ if (suiObjectId && contractAddress) {
7057
+ throw new Error('You can only provide either suiObjectId or contractAddress');
7058
+ }
7059
+ if (contractAddress) {
7060
+ throw new Error('You can only provide suiObjectId for SUI blockchain');
7061
+ }
7062
+ if (!suiObjectId) {
7063
+ throw new Error('You must provide suiObjectId for SUI blockchain');
7064
+ }
7065
+ return sanitizeSuiObjectId(suiObjectId);
7066
+ default:
7067
+ if (!contractAddress) {
7068
+ throw new Error('You must provide contractAddress for non-SUI blockchains');
7069
+ }
7070
+ return contractAddress;
7071
+ }
7072
+ };
7073
+ /**
7074
+ * Fetches token information for a given blockchain and contract address or SUI object ID.
7075
+ *
7076
+ * @public
7077
+ * @param {BlockchainName} blockchain - The name of the blockchain.
7078
+ * @param {string} contractAddress - The contract address of the token (optional for non-SUI blockchains).
7079
+ * @param {string} suiObjectId - The SUI object ID of the token (optional for SUI blockchain).
7080
+ *
7081
+ * @returns {Promise<{ decimals: number; symbol: string; name: string; tokenContractAddress: string }>} A promise that resolves to an object containing token information.
7082
+ *
7083
+ * @throws Will throw an error if both `contractAddress` and `suiObjectId` are provided for SUI blockchain.
7084
+ * @throws Will throw an error if neither `contractAddress` nor `suiObjectId` are provided for SUI blockchain.
7085
+ * @throws Will throw an error if `contractAddress` is not provided for non-SUI blockchains.
7086
+ * @throws Will throw an error if the response is invalid or contains an error.
7087
+ */
7088
+ System.prototype.fetchTokenInfo = function (_a) {
7089
+ return __awaiter(this, arguments, void 0, function (_b) {
7090
+ var validatedContractAddress, response, error_2;
7091
+ var blockchain = _b.blockchain, contractAddress = _b.contractAddress, suiObjectId = _b.suiObjectId;
7092
+ return __generator(this, function (_c) {
7093
+ switch (_c.label) {
7094
+ case 0:
7095
+ _c.trys.push([0, 2, , 3]);
7096
+ validatedContractAddress = this.validateContractAddress({
7097
+ blockchain: blockchain,
7098
+ contractAddress: contractAddress,
7099
+ suiObjectId: suiObjectId,
7100
+ });
7101
+ return [4 /*yield*/, this.request(SystemRoutes.fetchTokenInfo, {
7102
+ method: SystemRoutes.fetchTokenInfo.method,
7103
+ queryParams: {
7104
+ chainName: blockchain,
7105
+ tokenContractAddress: validatedContractAddress,
7106
+ },
7107
+ })];
7108
+ case 1:
7109
+ response = _c.sent();
7110
+ if (!response || response.error) {
7111
+ throw new Error(response);
7112
+ }
7113
+ return [2 /*return*/, response];
7114
+ case 2:
7115
+ error_2 = _c.sent();
7116
+ logger('SDK::Wallet::fetchTokenInfo::error', error_2);
7117
+ return [2 /*return*/, Promise.reject(error_2)];
7118
+ case 3: return [2 /*return*/];
7119
+ }
7120
+ });
7121
+ });
7122
+ };
6929
7123
  /**
6930
7124
  * @public
6931
7125
  * Get all supported assets configuration
@@ -6934,7 +7128,7 @@ var System = /** @class */ (function (_super) {
6934
7128
  */
6935
7129
  System.prototype.getSupportedTokens = function () {
6936
7130
  return __awaiter(this, void 0, void 0, function () {
6937
- var response, error_2;
7131
+ var response, error_3;
6938
7132
  return __generator(this, function (_a) {
6939
7133
  switch (_a.label) {
6940
7134
  case 0:
@@ -6950,9 +7144,9 @@ var System = /** @class */ (function (_super) {
6950
7144
  }
6951
7145
  return [2 /*return*/, response.assets];
6952
7146
  case 2:
6953
- error_2 = _a.sent();
6954
- logger('SDK::System::getSupportedTokens::error', error_2);
6955
- return [2 /*return*/, Promise.reject(error_2)];
7147
+ error_3 = _a.sent();
7148
+ logger('SDK::System::getSupportedTokens::error', error_3);
7149
+ return [2 /*return*/, Promise.reject(error_3)];
6956
7150
  case 3: return [2 /*return*/];
6957
7151
  }
6958
7152
  });
@@ -6965,7 +7159,7 @@ var System = /** @class */ (function (_super) {
6965
7159
  */
6966
7160
  System.prototype.getUserData = function (userId) {
6967
7161
  return __awaiter(this, void 0, void 0, function () {
6968
- var response, error_3;
7162
+ var response, error_4;
6969
7163
  return __generator(this, function (_a) {
6970
7164
  switch (_a.label) {
6971
7165
  case 0:
@@ -6981,9 +7175,9 @@ var System = /** @class */ (function (_super) {
6981
7175
  }
6982
7176
  return [2 /*return*/, response.user];
6983
7177
  case 2:
6984
- error_3 = _a.sent();
6985
- logger('SDK::System::getUserData::error', error_3);
6986
- return [2 /*return*/, Promise.reject(error_3)];
7178
+ error_4 = _a.sent();
7179
+ logger('SDK::System::getUserData::error', error_4);
7180
+ return [2 /*return*/, Promise.reject(error_4)];
6987
7181
  case 3: return [2 /*return*/];
6988
7182
  }
6989
7183
  });
@@ -6997,7 +7191,7 @@ var System = /** @class */ (function (_super) {
6997
7191
  */
6998
7192
  System.prototype.getUserByExternalUserId = function (externalUserId) {
6999
7193
  return __awaiter(this, void 0, void 0, function () {
7000
- var response, error_4;
7194
+ var response, error_5;
7001
7195
  return __generator(this, function (_a) {
7002
7196
  switch (_a.label) {
7003
7197
  case 0:
@@ -7013,9 +7207,9 @@ var System = /** @class */ (function (_super) {
7013
7207
  }
7014
7208
  return [2 /*return*/, response.user];
7015
7209
  case 2:
7016
- error_4 = _a.sent();
7017
- logger('SDK::System::getUserByExternalUserId::error', error_4);
7018
- return [2 /*return*/, Promise.reject(error_4)];
7210
+ error_5 = _a.sent();
7211
+ logger('SDK::System::getUserByExternalUserId::error', error_5);
7212
+ return [2 /*return*/, Promise.reject(error_5)];
7019
7213
  case 3: return [2 /*return*/];
7020
7214
  }
7021
7215
  });
@@ -7122,7 +7316,7 @@ var Transactions = /** @class */ (function (_super) {
7122
7316
  */
7123
7317
  Transactions.prototype.transfer = function (_a) {
7124
7318
  return __awaiter(this, arguments, void 0, function (_b) {
7125
- var signatureMessage, transportSeedphrase, message, signature, clientAuthPubKey, response, error_2;
7319
+ var signatureMessage, transportSeedPhrase, message, signature, clientAuthPubKey, response, error_2;
7126
7320
  var originAddress = _b.originAddress, recipientAddress = _b.recipientAddress, amount = _b.amount, assetId = _b.assetId, reference = _b.reference, memo = _b.memo, password = _b.password;
7127
7321
  return __generator(this, function (_c) {
7128
7322
  switch (_c.label) {
@@ -7133,7 +7327,7 @@ var Transactions = /** @class */ (function (_super) {
7133
7327
  signatureMessage = _c.sent();
7134
7328
  return [4 /*yield*/, this.utils.prepSeedPhraseForTransport({ password: password })];
7135
7329
  case 2:
7136
- transportSeedphrase = _c.sent();
7330
+ transportSeedPhrase = _c.sent();
7137
7331
  message = signatureMessage.message, signature = signatureMessage.signature, clientAuthPubKey = signatureMessage.clientAuthPubKey;
7138
7332
  return [4 /*yield*/, this.request(TransactionsRoutes.transfer, {
7139
7333
  method: TransactionsRoutes.transfer.method,
@@ -7141,7 +7335,7 @@ var Transactions = /** @class */ (function (_super) {
7141
7335
  message: message,
7142
7336
  signature: signature,
7143
7337
  clientAuthPubKey: clientAuthPubKey,
7144
- encryptedSeed: transportSeedphrase,
7338
+ encryptedSeed: transportSeedPhrase,
7145
7339
  originAddress: originAddress,
7146
7340
  recipientAddress: recipientAddress,
7147
7341
  assetId: assetId,
@@ -7252,6 +7446,7 @@ var Transactions = /** @class */ (function (_super) {
7252
7446
  * @param {string} amount
7253
7447
  * @param {string} assetId
7254
7448
  * @param {string} contractAddress
7449
+ * @param {string} suiObjectId
7255
7450
  * @param {string} reference
7256
7451
  * @param {string} memo
7257
7452
  * @returns {CreateAllowanceResponse}
@@ -7259,18 +7454,21 @@ var Transactions = /** @class */ (function (_super) {
7259
7454
  */
7260
7455
  Transactions.prototype.createAllowance = function (_a) {
7261
7456
  return __awaiter(this, arguments, void 0, function (_b) {
7262
- var signatureMessage, transportSeedphrase, message, signature, clientAuthPubKey, response, error_5;
7263
- var password = _b.password, ownerAddress = _b.ownerAddress, spenderAddress = _b.spenderAddress, amount = _b.amount, assetId = _b.assetId, contractAddress = _b.contractAddress, reference = _b.reference, memo = _b.memo;
7457
+ var signatureMessage, transportSeedPhrase, message, signature, clientAuthPubKey, response, error_5;
7458
+ var password = _b.password, ownerAddress = _b.ownerAddress, spenderAddress = _b.spenderAddress, amount = _b.amount, assetId = _b.assetId, suiObjectId = _b.suiObjectId, contractAddress = _b.contractAddress, reference = _b.reference, memo = _b.memo;
7264
7459
  return __generator(this, function (_c) {
7265
7460
  switch (_c.label) {
7266
7461
  case 0:
7267
7462
  _c.trys.push([0, 4, , 5]);
7463
+ if (!suiObjectId || !contractAddress || (!!suiObjectId && !!contractAddress)) {
7464
+ throw new Error('Either `suiObjectId` or `contractAddress` are required for this transaction.');
7465
+ }
7268
7466
  return [4 /*yield*/, this.auth.signatureMessage({ password: password })];
7269
7467
  case 1:
7270
7468
  signatureMessage = _c.sent();
7271
7469
  return [4 /*yield*/, this.utils.prepSeedPhraseForTransport({ password: password })];
7272
7470
  case 2:
7273
- transportSeedphrase = _c.sent();
7471
+ transportSeedPhrase = _c.sent();
7274
7472
  message = signatureMessage.message, signature = signatureMessage.signature, clientAuthPubKey = signatureMessage.clientAuthPubKey;
7275
7473
  return [4 /*yield*/, this.request(TransactionsRoutes.createAllowance, {
7276
7474
  method: TransactionsRoutes.createAllowance.method,
@@ -7278,12 +7476,12 @@ var Transactions = /** @class */ (function (_super) {
7278
7476
  message: message,
7279
7477
  signature: signature,
7280
7478
  clientAuthPubKey: clientAuthPubKey,
7281
- encryptedSeed: transportSeedphrase,
7479
+ encryptedSeed: transportSeedPhrase,
7282
7480
  ownerAddress: ownerAddress,
7283
7481
  spenderAddress: spenderAddress,
7284
7482
  amount: amount,
7285
7483
  assetId: assetId,
7286
- contractAddress: contractAddress,
7484
+ contractAddress: contractAddress !== null && contractAddress !== void 0 ? contractAddress : suiObjectId,
7287
7485
  reference: reference,
7288
7486
  memo: memo,
7289
7487
  },