evernode-js-client 0.6.53 → 0.6.55

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/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
  }
@@ -60617,7 +60617,9 @@ class BaseEvernodeClient {
60617
60617
  const addrStateData = addrLedgerEntry?.HookStateData;
60618
60618
 
60619
60619
  if (addrStateData) {
60620
- const addrStateDecoded = StateHelpers.decodeReputationHostAddressState(Buffer.from(addrStateKey, 'hex'), Buffer.from(addrStateData, 'hex'));
60620
+ let addrStateDecoded = StateHelpers.decodeReputationHostAddressState(Buffer.from(addrStateKey, 'hex'), Buffer.from(addrStateData, 'hex'));
60621
+ const curMoment = await this.getMoment();
60622
+ addrStateDecoded.valid = !!(addrStateDecoded.lastScoredMoment && (curMoment - addrStateDecoded.lastScoredMoment) <= ReputationConstants.SCORE_EXPIRY_MOMENT_COUNT);
60621
60623
  return addrStateDecoded;
60622
60624
  }
60623
60625
  }
@@ -61224,6 +61226,7 @@ const { XflHelpers } = __nccwpck_require__(3972);
61224
61226
  const { EvernodeHelpers } = __nccwpck_require__(6384);
61225
61227
  const { StateHelpers } = __nccwpck_require__(3390);
61226
61228
  const { TransactionHelper } = __nccwpck_require__(5358);
61229
+ const { UtilHelpers } = __nccwpck_require__(3726);
61227
61230
 
61228
61231
  const OFFER_WAIT_TIMEOUT = 60;
61229
61232
 
@@ -61684,19 +61687,21 @@ class HostClient extends BaseEvernodeClient {
61684
61687
  * @param {object} scores [Optional] Score object in { host: score } format.
61685
61688
  */
61686
61689
  async sendReputations(scores = null, options = {}) {
61687
- let buffer = null;
61690
+ let buffer = Buffer.alloc(1);
61688
61691
  if (scores) {
61689
61692
  const preparedScores = await this.prepareHostReputationScores(scores);
61690
61693
  if (preparedScores) {
61691
- buffer = Buffer.alloc(64, 0);
61694
+ buffer = Buffer.alloc(65, 0);
61692
61695
  let i = 0;
61693
61696
  for (const reputationScore of preparedScores) {
61694
- buffer.writeUIntLE(Number(reputationScore.scoreValue), i, 1);
61697
+ buffer.writeUIntLE(Number(reputationScore.scoreValue), i + 1, 1);
61695
61698
  i++;
61696
61699
  }
61697
61700
  }
61698
61701
  }
61699
61702
 
61703
+ buffer.writeUIntLE(ReputationConstants.SCORE_VERSION, 0, 1);
61704
+
61700
61705
  const paramData = codec.decodeAccountID(this.xrplAcc.address);
61701
61706
 
61702
61707
  await this.reputationAcc.invoke(this.config.reputationAddress,
@@ -61720,81 +61725,109 @@ class HostClient extends BaseEvernodeClient {
61720
61725
  * @param {string} outboundIPAddress Assigned IP Address.
61721
61726
  */
61722
61727
  async offerLease(leaseIndex, leaseAmount, tosHash, outboundIPAddress = null, options = {}) {
61728
+ const leases = await this.getLeases();
61729
+ const decoded = (leases ?? []).map((l) => {
61730
+ return {
61731
+ token: l,
61732
+ info: UtilHelpers.decodeLeaseTokenUri(l.URI)
61733
+ }
61734
+ });
61735
+ let existing = decoded.find(l => l.info.leaseIndex == leaseIndex);
61736
+
61737
+ let uriToken;
61738
+ if (!existing) {
61739
+ // <prefix><version tag ("LTV"+uint8)><lease index (uint16)><half of tos hash><lease amount (int64)><identifier (uint32)><ip data>
61740
+ // Lengths of sub sections.
61741
+ const prefixLen = EvernodeConstants.LEASE_TOKEN_PREFIX_HEX.length / 2;
61742
+ const versionPrefixLen = EvernodeConstants.LEASE_TOKEN_VERSION_PREFIX_HEX.length / 2;
61743
+ const versionLen = versionPrefixLen + 2; // ("LTV"<Version Number>)
61744
+ const indexLen = 2;
61745
+ const halfToSLen = tosHash.length / 4;
61746
+ const leaseAmountLen = 8;
61747
+ const identifierLen = 4;
61748
+ const ipDataLen = 17;
61749
+
61750
+ // Offsets of sub sections
61751
+ const versionPrefixOffset = prefixLen;
61752
+ const versionOffset = prefixLen + versionPrefixLen;
61753
+ const indexOffset = prefixLen + versionLen;
61754
+ const halfTosHashOffset = prefixLen + versionLen + indexLen;
61755
+ const leaseAmountOffset = prefixLen + versionLen + indexLen + halfToSLen;
61756
+ const identifierOffset = prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen;
61757
+ const ipDataOffset = prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen + identifierLen;
61758
+
61759
+ const uriBuf = Buffer.alloc((prefixLen + versionLen + indexLen + halfToSLen + leaseAmountLen + identifierLen + ipDataLen));
61760
+
61761
+ Buffer.from(EvernodeConstants.LEASE_TOKEN_PREFIX_HEX, 'hex').copy(uriBuf);
61762
+ Buffer.from(EvernodeConstants.LEASE_TOKEN_VERSION_PREFIX_HEX, 'hex').copy(uriBuf, versionPrefixOffset, 0, versionPrefixLen);
61763
+ uriBuf.writeUInt16BE(EvernodeConstants.LEASE_TOKEN_VERSION, versionOffset);
61764
+ uriBuf.writeUInt16BE(leaseIndex, indexOffset);
61765
+ Buffer.from(tosHash, 'hex').copy(uriBuf, halfTosHashOffset, 0, halfToSLen);
61766
+ uriBuf.writeBigInt64BE(XflHelpers.getXfl(leaseAmount.toString()), leaseAmountOffset);
61767
+ uriBuf.writeUInt32BE((await this.xrplAcc.getSequence()), identifierOffset);
61768
+
61769
+ if (outboundIPAddress) {
61770
+ if (outboundIPAddress.includes(":")) {
61771
+ uriBuf.writeUInt8(IPV6_FAMILY, ipDataOffset);
61772
+ const ipBuf = Buffer.from(outboundIPAddress.split(':').map(v => {
61773
+ const bytes = [];
61774
+ for (let i = 0; i < v.length; i += 2) {
61775
+ bytes.push(parseInt(v.substr(i, 2), 16));
61776
+ }
61777
+ return bytes;
61778
+ }).flat());
61723
61779
 
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));
61780
+ ipBuf.copy(uriBuf, ipDataOffset + 1, 0, ipDataLen);
61781
+ } else {
61782
+ throw "Invalid outbound IP address was provided";
61783
+ }
61784
+ }
61745
61785
 
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);
61786
+ const uri = uriBuf.toString('base64');
61753
61787
 
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());
61788
+ try {
61789
+ await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61790
+ await this.xrplAcc.mintURIToken(uri, null, { isBurnable: true, isHexUri: false }, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61791
+ }, { ...(options.retryOptions ? options.retryOptions : {}), submissionRef: options.submissionRef });
61792
+ } catch (e) {
61793
+ // Re-minting the URIToken after burning that sold URIToken.
61794
+ if (e.code === "tecDUPLICATE") {
61795
+ const uriTokenId = this.xrplAcc.generateIssuedURITokenId(uri);
61796
+ console.log(`Burning URIToken related to a previously sold lease.`);
61797
+ await this.xrplAcc.burnURIToken(uriTokenId, { maxLedgerIndex: this.#getMaxLedgerSequence() });
61798
+ console.log("Re-mint the URIToken for the new lease offer.")
61799
+ await this.xrplAcc.mintURIToken(uri, null, { isBurnable: true, isHexUri: false }, { maxLedgerIndex: this.#getMaxLedgerSequence() });
61800
+ }
61801
+ }
61764
61802
 
61765
- ipBuf.copy(uriBuf, ipDataOffset + 1, 0, ipDataLen);
61766
- } else {
61767
- throw "Invalid outbound IP address was provided";
61803
+ uriToken = await this.xrplAcc.getURITokenByUri(uri);
61804
+ // If uri token is not found in first try, Retry again.
61805
+ if (!uriToken) {
61806
+ console.log(`URI token not found, Retrying in 1 second.`)
61807
+ await new Promise(resolve => setTimeout(resolve, 1000));
61808
+ uriToken = await this.xrplAcc.getURITokenByUri(uri);
61768
61809
  }
61769
61810
  }
61811
+ else {
61812
+ uriToken = existing.token;
61813
+ console.log(`Found exiting lease for index ${leaseIndex}. Minting skipped.`);
61814
+ }
61770
61815
 
61771
- const uri = uriBuf.toString('base64');
61816
+ // Throw if still not found.
61817
+ if (!uriToken)
61818
+ throw "Offer lease URI token not found.";
61772
61819
 
61773
- try {
61820
+ if (!uriToken.Amount) {
61774
61821
  await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61775
- await this.xrplAcc.mintURIToken(uri, null, { isBurnable: true, isHexUri: false }, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61822
+ await this.xrplAcc.sellURIToken(uriToken.index,
61823
+ leaseAmount.toString(),
61824
+ EvernodeConstants.EVR,
61825
+ this.config.evrIssuerAddress, null, null, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61776
61826
  }, { ...(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
61827
  }
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 });
61828
+ else {
61829
+ console.log(`Found exiting offer for the lease ${uriToken.uriTokenId}. Offer skipped.`);
61830
+ }
61798
61831
  }
61799
61832
 
61800
61833
  /**
@@ -61889,9 +61922,15 @@ class HostClient extends BaseEvernodeClient {
61889
61922
  * @param {string} uriTokenId Hex URI token id of the lease.
61890
61923
  */
61891
61924
  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 });
61925
+ const uriToken = await this.xrplApi.getURITokenByIndex(uriTokenId);
61926
+ if (uriToken) {
61927
+ await this.#submitWithRetry(async (feeUplift, submissionRef) => {
61928
+ await this.xrplAcc.burnURIToken(uriTokenId, { maxLedgerIndex: this.#getMaxLedgerSequence(), feeUplift: feeUplift, submissionRef: submissionRef });
61929
+ }, { ...(options.retryOptions ? options.retryOptions : {}), submissionRef: options.submissionRef });
61930
+ }
61931
+ else {
61932
+ console.log(`Uri token ${uriTokenId} not found or already burned. Burn skipped.`);
61933
+ }
61895
61934
  }
61896
61935
 
61897
61936
  /**
@@ -63538,10 +63577,12 @@ const ErrorReasons = {
63538
63577
  }
63539
63578
 
63540
63579
  const ReputationConstants = {
63580
+ SCORE_VERSION: 1,
63541
63581
  REP_INFO_BUFFER_SIZE: 43,
63542
63582
  REP_INFO_PUBKEY_OFFSET: 0,
63543
63583
  REP_INFO_PEER_PORT_OFFSET: 33,
63544
- REP_INFO_MOMENT_OFFSET: 35
63584
+ REP_INFO_MOMENT_OFFSET: 35,
63585
+ SCORE_EXPIRY_MOMENT_COUNT: 2
63545
63586
  }
63546
63587
 
63547
63588
  // All keys are prefixed with 'EVR' (0x455652)
@@ -63930,6 +63971,10 @@ const HOST_ACCUMULATED_REWARD_OFFSET = 116;
63930
63971
  const HOST_REP_LAST_REG_MOMENT_OFFSET = 0;
63931
63972
  const HOST_REP_SCORE_NUMERATOR_OFFSET = 8;
63932
63973
  const HOST_REP_SCORE_DENOMINATOR_OFFSET = 16;
63974
+ const HOST_REP_SCORE_OFFSET = 24;
63975
+ const HOST_REP_LAST_RESET_MOMENT_OFFSET = 32;
63976
+ const HOST_REP_LAST_SCORED_MOMENT_OFFSET = 40;
63977
+ const HOST_REP_LAST_UNIVERSE_SIZE_OFFSET = 48;
63933
63978
 
63934
63979
  const PREV_HOST_ADDRESS_OFFSET = 0;
63935
63980
  const TRANSFER_LEDGER_IDX_OFFSET = 20;
@@ -64019,7 +64064,11 @@ class StateHelpers {
64019
64064
  hostAddress: codec.encodeAccountID(stateKeyBuf.slice(keyOffset)),
64020
64065
  lastRegisteredMoment: Number(stateDataBuf.readBigUInt64LE(HOST_REP_LAST_REG_MOMENT_OFFSET)),
64021
64066
  scoreNumerator: Number(stateDataBuf.readBigUInt64LE(HOST_REP_SCORE_NUMERATOR_OFFSET)),
64022
- scoreDenominator: Number(stateDataBuf.readBigUInt64LE(HOST_REP_SCORE_DENOMINATOR_OFFSET))
64067
+ scoreDenominator: Number(stateDataBuf.readBigUInt64LE(HOST_REP_SCORE_DENOMINATOR_OFFSET)),
64068
+ score: stateDataBuf.length > HOST_REP_SCORE_OFFSET ? Number(stateDataBuf.readBigUInt64LE(HOST_REP_SCORE_OFFSET)) : null,
64069
+ lastResetMoment: stateDataBuf.length > HOST_REP_LAST_RESET_MOMENT_OFFSET ? Number(stateDataBuf.readBigUInt64LE(HOST_REP_LAST_RESET_MOMENT_OFFSET)) : null,
64070
+ lastScoredMoment: stateDataBuf.length > HOST_REP_LAST_SCORED_MOMENT_OFFSET ? Number(stateDataBuf.readBigUInt64LE(HOST_REP_LAST_SCORED_MOMENT_OFFSET)) : null,
64071
+ lastUniverseSize: stateDataBuf.length > HOST_REP_LAST_UNIVERSE_SIZE_OFFSET ? Number(stateDataBuf.readBigUInt64LE(HOST_REP_LAST_UNIVERSE_SIZE_OFFSET)) : null,
64023
64072
  }
64024
64073
  return data;
64025
64074
  }
@@ -65660,23 +65709,18 @@ class XrplAccount {
65660
65709
  }
65661
65710
 
65662
65711
  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);
65712
+ const index = this.generateIssuedURITokenId(uri, isHexUri);
65713
+ return await this.xrplApi.getURITokenByIndex(index);
65666
65714
  }
65667
65715
 
65668
65716
 
65669
- generateIssuedURITokenId(uri) {
65717
+ generateIssuedURITokenId(uri, isHexUri = false) {
65670
65718
  if (uri.length < 1 || uri.length > 256)
65671
65719
  throw 'Invalid URI';
65672
65720
 
65673
65721
  const URITOKEN_LEDGER_TYPE_PREFIX = 85; // Decimal value of ASCII 'U'
65674
65722
  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
- }
65723
+ const uriHex = isHexUri ? uri : TransactionHelper.asciiToHex(uri).toUpperCase();
65680
65724
 
65681
65725
  let hash = crypto.createHash('sha512');
65682
65726
 
@@ -66307,6 +66351,13 @@ class XrplApi {
66307
66351
  }
66308
66352
  }
66309
66353
 
66354
+ async getURITokenByIndex(index) {
66355
+ const entry = await this.getLedgerEntry(index);
66356
+ if (!entry || entry.LedgerEntryType !== 'URIToken')
66357
+ return null;
66358
+ return entry;
66359
+ }
66360
+
66310
66361
  async getTxnInfo(txnHash, options) {
66311
66362
  const resp = (await this.#handleClientRequest({ command: 'tx', transaction: txnHash, binary: false, ...options }));
66312
66363
  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.55",
10
10
  "dependencies": {
11
11
  "elliptic": "6.5.4",
12
12
  "libsodium-wrappers": "0.7.10",
Binary file
Binary file