evernode-js-client 0.6.53 → 0.6.54

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -9189,7 +9189,7 @@ module.exports = { mask, unmask };
9189
9189
 
9190
9190
 
9191
9191
  try {
9192
- module.exports = require(__nccwpck_require__.ab + "prebuilds/linux-x64/node.napi1.node");
9192
+ module.exports = require(__nccwpck_require__.ab + "prebuilds/linux-x64/node.napi.node");
9193
9193
  } catch (e) {
9194
9194
  module.exports = __nccwpck_require__(9467);
9195
9195
  }
@@ -38321,7 +38321,7 @@ module.exports = isValidUTF8;
38321
38321
 
38322
38322
 
38323
38323
  try {
38324
- module.exports = require(__nccwpck_require__.ab + "prebuilds/linux-x64/node.napi.node");
38324
+ module.exports = require(__nccwpck_require__.ab + "prebuilds/linux-x64/node.napi1.node");
38325
38325
  } catch (e) {
38326
38326
  module.exports = __nccwpck_require__(6982);
38327
38327
  }
@@ -61224,6 +61224,7 @@ const { XflHelpers } = __nccwpck_require__(3972);
61224
61224
  const { EvernodeHelpers } = __nccwpck_require__(6384);
61225
61225
  const { StateHelpers } = __nccwpck_require__(3390);
61226
61226
  const { TransactionHelper } = __nccwpck_require__(5358);
61227
+ const { UtilHelpers } = __nccwpck_require__(3726);
61227
61228
 
61228
61229
  const OFFER_WAIT_TIMEOUT = 60;
61229
61230
 
@@ -61720,81 +61721,109 @@ class HostClient extends BaseEvernodeClient {
61720
61721
  * @param {string} outboundIPAddress Assigned IP Address.
61721
61722
  */
61722
61723
  async offerLease(leaseIndex, leaseAmount, tosHash, outboundIPAddress = null, options = {}) {
61724
+ const leases = await this.getLeases();
61725
+ const decoded = (leases ?? []).map((l) => {
61726
+ return {
61727
+ token: l,
61728
+ info: UtilHelpers.decodeLeaseTokenUri(l.URI)
61729
+ }
61730
+ });
61731
+ let existing = decoded.find(l => l.info.leaseIndex == leaseIndex);
61732
+
61733
+ let uriToken;
61734
+ if (!existing) {
61735
+ // <prefix><version tag ("LTV"+uint8)><lease index (uint16)><half of tos hash><lease amount (int64)><identifier (uint32)><ip data>
61736
+ // Lengths of sub sections.
61737
+ const prefixLen = EvernodeConstants.LEASE_TOKEN_PREFIX_HEX.length / 2;
61738
+ const versionPrefixLen = EvernodeConstants.LEASE_TOKEN_VERSION_PREFIX_HEX.length / 2;
61739
+ const versionLen = versionPrefixLen + 2; // ("LTV"<Version Number>)
61740
+ const indexLen = 2;
61741
+ const halfToSLen = tosHash.length / 4;
61742
+ const leaseAmountLen = 8;
61743
+ const identifierLen = 4;
61744
+ const ipDataLen = 17;
61745
+
61746
+ // Offsets of sub sections
61747
+ const versionPrefixOffset = prefixLen;
61748
+ const versionOffset = prefixLen + versionPrefixLen;
61749
+ const indexOffset = prefixLen + versionLen;
61750
+ const halfTosHashOffset = prefixLen + versionLen + indexLen;
61751
+ const leaseAmountOffset = prefixLen + versionLen + indexLen + halfToSLen;
61752
+ const identifierOffset = prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen;
61753
+ const ipDataOffset = prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen + identifierLen;
61754
+
61755
+ const uriBuf = Buffer.alloc((prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen + identifierLen + ipDataLen));
61756
+
61757
+ Buffer.from(EvernodeConstants.LEASE_TOKEN_PREFIX_HEX, 'hex').copy(uriBuf);
61758
+ Buffer.from(EvernodeConstants.LEASE_TOKEN_VERSION_PREFIX_HEX, 'hex').copy(uriBuf, versionPrefixOffset, 0, versionPrefixLen);
61759
+ uriBuf.writeUInt16BE(EvernodeConstants.LEASE_TOKEN_VERSION, versionOffset);
61760
+ uriBuf.writeUInt16BE(leaseIndex, indexOffset);
61761
+ Buffer.from(tosHash, 'hex').copy(uriBuf, halfTosHashOffset, 0, halfToSLen);
61762
+ uriBuf.writeBigInt64BE(XflHelpers.getXfl(leaseAmount.toString()), leaseAmountOffset);
61763
+ uriBuf.writeUInt32BE((await this.xrplAcc.getSequence()), identifierOffset);
61764
+
61765
+ if (outboundIPAddress) {
61766
+ if (outboundIPAddress.includes(":")) {
61767
+ uriBuf.writeUInt8(IPV6_FAMILY, ipDataOffset);
61768
+ const ipBuf = Buffer.from(outboundIPAddress.split(':').map(v => {
61769
+ const bytes = [];
61770
+ for (let i = 0; i < v.length; i += 2) {
61771
+ bytes.push(parseInt(v.substr(i, 2), 16));
61772
+ }
61773
+ return bytes;
61774
+ }).flat());
61723
61775
 
61724
- // <prefix><version tag ("LTV"+uint8)><lease index (uint16)><half of tos hash><lease amount (int64)><identifier (uint32)><ip data>
61725
- // Lengths of sub sections.
61726
- const prefixLen = EvernodeConstants.LEASE_TOKEN_PREFIX_HEX.length / 2;
61727
- const versionPrefixLen = EvernodeConstants.LEASE_TOKEN_VERSION_PREFIX_HEX.length / 2;
61728
- const versionLen = versionPrefixLen + 2; // ("LTV"<Version Number>)
61729
- const indexLen = 2;
61730
- const halfToSLen = tosHash.length / 4;
61731
- const leaseAmountLen = 8;
61732
- const identifierLen = 4;
61733
- const ipDataLen = 17;
61734
-
61735
- // Offsets of sub sections
61736
- const versionPrefixOffset = prefixLen;
61737
- const versionOffset = prefixLen + versionPrefixLen;
61738
- const indexOffset = prefixLen + versionLen;
61739
- const halfTosHashOffset = prefixLen + versionLen + indexLen;
61740
- const leaseAmountOffset = prefixLen + versionLen + indexLen + halfToSLen;
61741
- const identifierOffset = prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen;
61742
- const ipDataOffset = prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen + identifierLen;
61743
-
61744
- const uriBuf = Buffer.alloc((prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen + identifierLen + ipDataLen));
61776
+ ipBuf.copy(uriBuf, ipDataOffset + 1, 0, ipDataLen);
61777
+ } else {
61778
+ throw "Invalid outbound IP address was provided";
61779
+ }
61780
+ }
61745
61781
 
61746
- Buffer.from(EvernodeConstants.LEASE_TOKEN_PREFIX_HEX, 'hex').copy(uriBuf);
61747
- Buffer.from(EvernodeConstants.LEASE_TOKEN_VERSION_PREFIX_HEX, 'hex').copy(uriBuf, versionPrefixOffset, 0, versionPrefixLen);
61748
- uriBuf.writeUInt16BE(EvernodeConstants.LEASE_TOKEN_VERSION, versionOffset);
61749
- uriBuf.writeUInt16BE(leaseIndex, indexOffset);
61750
- Buffer.from(tosHash, 'hex').copy(uriBuf, halfTosHashOffset, 0, halfToSLen);
61751
- uriBuf.writeBigInt64BE(XflHelpers.getXfl(leaseAmount.toString()), leaseAmountOffset);
61752
- uriBuf.writeUInt32BE((await this.xrplAcc.getSequence()), identifierOffset);
61782
+ const uri = uriBuf.toString('base64');
61753
61783
 
61754
- if (outboundIPAddress) {
61755
- if (outboundIPAddress.includes(":")) {
61756
- uriBuf.writeUInt8(IPV6_FAMILY, ipDataOffset);
61757
- const ipBuf = Buffer.from(outboundIPAddress.split(':').map(v => {
61758
- const bytes = [];
61759
- for (let i = 0; i < v.length; i += 2) {
61760
- bytes.push(parseInt(v.substr(i, 2), 16));
61761
- }
61762
- return bytes;
61763
- }).flat());
61784
+ try {
61785
+ await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61786
+ await this.xrplAcc.mintURIToken(uri, null, { isBurnable: true, isHexUri: false }, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61787
+ }, { ...(options.retryOptions ? options.retryOptions : {}), submissionRef: options.submissionRef });
61788
+ } catch (e) {
61789
+ // Re-minting the URIToken after burning that sold URIToken.
61790
+ if (e.code === "tecDUPLICATE") {
61791
+ const uriTokenId = this.xrplAcc.generateIssuedURITokenId(uri);
61792
+ console.log(`Burning URIToken related to a previously sold lease.`);
61793
+ await this.xrplAcc.burnURIToken(uriTokenId, { maxLedgerIndex: this.#getMaxLedgerSequence() });
61794
+ console.log("Re-mint the URIToken for the new lease offer.")
61795
+ await this.xrplAcc.mintURIToken(uri, null, { isBurnable: true, isHexUri: false }, { maxLedgerIndex: this.#getMaxLedgerSequence() });
61796
+ }
61797
+ }
61764
61798
 
61765
- ipBuf.copy(uriBuf, ipDataOffset + 1, 0, ipDataLen);
61766
- } else {
61767
- throw "Invalid outbound IP address was provided";
61799
+ uriToken = await this.xrplAcc.getURITokenByUri(uri);
61800
+ // If uri token is not found in first try, Retry again.
61801
+ if (!uriToken) {
61802
+ console.log(`URI token not found, Retrying in 1 second.`)
61803
+ await new Promise(resolve => setTimeout(resolve, 1000));
61804
+ uriToken = await this.xrplAcc.getURITokenByUri(uri);
61768
61805
  }
61769
61806
  }
61807
+ else {
61808
+ uriToken = existing.token;
61809
+ console.log(`Found exiting lease for index ${leaseIndex}. Minting skipped.`);
61810
+ }
61770
61811
 
61771
- const uri = uriBuf.toString('base64');
61812
+ // Throw if still not found.
61813
+ if (!uriToken)
61814
+ throw "Offer lease URI token not found.";
61772
61815
 
61773
- try {
61816
+ if (!uriToken.Amount) {
61774
61817
  await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61775
- await this.xrplAcc.mintURIToken(uri, null, { isBurnable: true, isHexUri: false }, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61818
+ await this.xrplAcc.sellURIToken(uriToken.index,
61819
+ leaseAmount.toString(),
61820
+ EvernodeConstants.EVR,
61821
+ this.config.evrIssuerAddress, null, null, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61776
61822
  }, { ...(options.retryOptions ? options.retryOptions : {}), submissionRef: options.submissionRef });
61777
- } catch (e) {
61778
- // Re-minting the URIToken after burning that sold URIToken.
61779
- if (e.code === "tecDUPLICATE") {
61780
- const uriTokenId = this.xrplAcc.generateIssuedURITokenId(uri);
61781
- console.log(`Burning URIToken related to a previously sold lease.`);
61782
- await this.xrplAcc.burnURIToken(uriTokenId, { maxLedgerIndex: this.#getMaxLedgerSequence() });
61783
- console.log("Re-mint the URIToken for the new lease offer.")
61784
- await this.xrplAcc.mintURIToken(uri, null, { isBurnable: true, isHexUri: false }, { maxLedgerIndex: this.#getMaxLedgerSequence() });
61785
- }
61786
61823
  }
61787
-
61788
- const uriToken = await this.xrplAcc.getURITokenByUri(uri);
61789
- if (!uriToken)
61790
- throw "Offer lease NFT creation error.";
61791
-
61792
- await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61793
- await this.xrplAcc.sellURIToken(uriToken.index,
61794
- leaseAmount.toString(),
61795
- EvernodeConstants.EVR,
61796
- this.config.evrIssuerAddress, null, null, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61797
- }, { ...(options.retryOptions ? options.retryOptions : {}), submissionRef: options.submissionRef });
61824
+ else {
61825
+ console.log(`Found exiting offer for the lease ${uriToken.uriTokenId}. Offer skipped.`);
61826
+ }
61798
61827
  }
61799
61828
 
61800
61829
  /**
@@ -61889,9 +61918,15 @@ class HostClient extends BaseEvernodeClient {
61889
61918
  * @param {string} uriTokenId Hex URI token id of the lease.
61890
61919
  */
61891
61920
  async expireLease(uriTokenId, options = {}) {
61892
- await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61893
- await this.xrplAcc.burnURIToken(uriTokenId, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61894
- }, { ...(options.retryOptions ? options.retryOptions : {}), submissionRef: options.submissionRef });
61921
+ const uriToken = await this.xrplApi.getURITokenByIndex(uriTokenId);
61922
+ if (uriToken) {
61923
+ await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61924
+ await this.xrplAcc.burnURIToken(uriTokenId, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61925
+ }, { ...(options.retryOptions ? options.retryOptions : {}), submissionRef: options.submissionRef });
61926
+ }
61927
+ else {
61928
+ console.log(`Uri token ${uriTokenId} not found or already burned. Burn skipped.`);
61929
+ }
61895
61930
  }
61896
61931
 
61897
61932
  /**
@@ -65660,23 +65695,18 @@ class XrplAccount {
65660
65695
  }
65661
65696
 
65662
65697
  async getURITokenByUri(uri, isHexUri = false) {
65663
- const tokens = await this.getURITokens();
65664
- const hexUri = isHexUri ? uri : TransactionHelper.asciiToHex(uri).toUpperCase();
65665
- return tokens.find(t => t.URI == hexUri);
65698
+ const index = this.generateIssuedURITokenId(uri, isHexUri);
65699
+ return await this.xrplApi.getURITokenByIndex(index);
65666
65700
  }
65667
65701
 
65668
65702
 
65669
- generateIssuedURITokenId(uri) {
65703
+ generateIssuedURITokenId(uri, isHexUri = false) {
65670
65704
  if (uri.length < 1 || uri.length > 256)
65671
65705
  throw 'Invalid URI';
65672
65706
 
65673
65707
  const URITOKEN_LEDGER_TYPE_PREFIX = 85; // Decimal value of ASCII 'U'
65674
65708
  const accIdHex = (codec.decodeAccountID(this.address)).toString('hex').toUpperCase();
65675
- let uriHex = '';
65676
- for (let n in uri) {
65677
- let digit = uri.charCodeAt(n).toString(16).toUpperCase();
65678
- uriHex += (digit.length == 1 ? '0' : '') + digit
65679
- }
65709
+ const uriHex = isHexUri ? uri : TransactionHelper.asciiToHex(uri).toUpperCase();
65680
65710
 
65681
65711
  let hash = crypto.createHash('sha512');
65682
65712
 
@@ -66307,6 +66337,13 @@ class XrplApi {
66307
66337
  }
66308
66338
  }
66309
66339
 
66340
+ async getURITokenByIndex(index) {
66341
+ const entry = await this.getLedgerEntry(index);
66342
+ if (!entry || entry.LedgerEntryType !== 'URIToken')
66343
+ return null;
66344
+ return entry;
66345
+ }
66346
+
66310
66347
  async getTxnInfo(txnHash, options) {
66311
66348
  const resp = (await this.#handleClientRequest({ command: 'tx', transaction: txnHash, binary: false, ...options }));
66312
66349
  return resp?.result;
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  ],
7
7
  "homepage": "https://github.com/HotPocketDev/evernode-js-client",
8
8
  "license": "SEE LICENSE IN https://raw.githubusercontent.com/EvernodeXRPL/evernode-resources/main/license/evernode-license.pdf",
9
- "version": "0.6.53",
9
+ "version": "0.6.54",
10
10
  "dependencies": {
11
11
  "elliptic": "6.5.4",
12
12
  "libsodium-wrappers": "0.7.10",
Binary file
Binary file