@steemit/steem-js 1.0.13 → 1.0.14

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.js CHANGED
@@ -13838,6 +13838,20 @@ class PublicKey {
13838
13838
  const secp256k1$1 = new ellipticExports.ec('secp256k1');
13839
13839
  const G = secp256k1$1.g;
13840
13840
  const n = new BN(secp256k1$1.n.toString());
13841
+ /**
13842
+ * Constant-time buffer comparison to prevent timing attacks.
13843
+ * Returns true only if a.length === b.length and a[i] === b[i] for all i.
13844
+ */
13845
+ function constantTimeCompare(a, b) {
13846
+ if (a.length !== b.length) {
13847
+ return false;
13848
+ }
13849
+ let result = 0;
13850
+ for (let i = 0; i < a.length; i++) {
13851
+ result |= a[i] ^ b[i];
13852
+ }
13853
+ return result === 0;
13854
+ }
13841
13855
  class PrivateKey {
13842
13856
  /**
13843
13857
  * @private see static functions
@@ -13850,11 +13864,8 @@ class PrivateKey {
13850
13864
  if (!Buffer.isBuffer(buf)) {
13851
13865
  throw new Error("Expecting parameter to be a Buffer type");
13852
13866
  }
13853
- if (32 !== buf.length) {
13854
- debug.warn(`WARN: Expecting 32 bytes, instead got ${buf.length}, stack trace:`, new Error().stack);
13855
- }
13856
- if (buf.length === 0) {
13857
- throw new Error("Empty buffer");
13867
+ if (buf.length !== 32) {
13868
+ throw new Error(`Invalid private key buffer: expected 32 bytes, got ${buf.length}`);
13858
13869
  }
13859
13870
  return new PrivateKey(new BN(buf));
13860
13871
  }
@@ -13879,21 +13890,33 @@ class PrivateKey {
13879
13890
  * @return {string} Wallet Import Format (still a secret, Not encrypted)
13880
13891
  */
13881
13892
  static fromWif(private_wif) {
13882
- const private_wif_buffer = Buffer.from(bs58.decode(private_wif));
13893
+ if (!private_wif || typeof private_wif !== 'string') {
13894
+ throw new Error('Invalid WIF: empty or not a string');
13895
+ }
13896
+ let private_wif_buffer;
13897
+ try {
13898
+ private_wif_buffer = Buffer.from(bs58.decode(private_wif));
13899
+ }
13900
+ catch {
13901
+ throw new Error('Invalid WIF: failed to decode base58');
13902
+ }
13903
+ // Valid WIF: 1 byte version + 32 bytes key + 4 bytes checksum = 37 bytes
13904
+ if (private_wif_buffer.length !== 37) {
13905
+ throw new Error(`Invalid WIF: expected 37 bytes, got ${private_wif_buffer.length}`);
13906
+ }
13883
13907
  const version = private_wif_buffer.readUInt8(0);
13884
- if (version !== 0x80)
13885
- throw new Error(`Expected version ${0x80}, instead got ${version}`);
13886
- // checksum includes the version
13887
- const private_key = private_wif_buffer.slice(0, -4);
13888
- const checksum = private_wif_buffer.slice(-4);
13889
- let new_checksum = sha256$1(private_key);
13908
+ if (version !== 0x80) {
13909
+ throw new Error(`Invalid WIF: expected version 0x80, got 0x${version.toString(16)}`);
13910
+ }
13911
+ const private_key = private_wif_buffer.slice(1, 33);
13912
+ const checksum = private_wif_buffer.slice(33);
13913
+ let new_checksum = sha256$1(Buffer.concat([Buffer.from([0x80]), private_key]));
13890
13914
  new_checksum = sha256$1(new_checksum);
13891
13915
  new_checksum = new_checksum.slice(0, 4);
13892
- if (checksum.toString() !== new_checksum.toString()) {
13916
+ if (!constantTimeCompare(checksum, Buffer.from(new_checksum))) {
13893
13917
  throw new Error('Invalid WIF key (checksum miss-match)');
13894
13918
  }
13895
- private_key.writeUInt8(0x80, 0);
13896
- return PrivateKey.fromBuffer(private_key.slice(1));
13919
+ return PrivateKey.fromBuffer(private_key);
13897
13920
  }
13898
13921
  toWif() {
13899
13922
  const private_key = this.toBuffer();
@@ -14272,18 +14295,21 @@ function calcPubKeyRecoveryParam(curve, e, signature, Q) {
14272
14295
  }
14273
14296
  catch (error) {
14274
14297
  // try next value
14275
- console.debug(`Recovery attempt ${i} failed:`, error.message);
14298
+ if (process.env.NODE_ENV === 'development') {
14299
+ console.debug(`Recovery attempt ${i} failed:`, error.message);
14300
+ }
14276
14301
  }
14277
14302
  }
14278
- // Additional debugging
14279
- console.debug('All recovery attempts failed. Signature:', {
14280
- r: signature.r.toString(16),
14281
- s: signature.s.toString(16)
14282
- });
14283
- console.debug('Expected public key:', {
14284
- x: Q.getX().toString(16),
14285
- y: Q.getY().toString(16)
14286
- });
14303
+ if (process.env.NODE_ENV === 'development') {
14304
+ console.debug('All recovery attempts failed. Signature:', {
14305
+ r: signature.r.toString(16),
14306
+ s: signature.s.toString(16)
14307
+ });
14308
+ console.debug('Expected public key:', {
14309
+ x: Q.getX().toString(16),
14310
+ y: Q.getY().toString(16)
14311
+ });
14312
+ }
14287
14313
  throw new Error('Unable to find valid recovery factor');
14288
14314
  }
14289
14315
 
@@ -14335,9 +14361,10 @@ class Signature {
14335
14361
  const d = privKey.d;
14336
14362
  let ecsignature;
14337
14363
  let nonce = 0;
14364
+ const MAX_NONCE_ATTEMPTS = 1000;
14338
14365
  // Match old-steem-js behavior: find canonical signature (lenR === 32 && lenS === 32)
14339
14366
  // Based on C++ is_fc_canonical logic
14340
- while (true) {
14367
+ while (nonce < MAX_NONCE_ATTEMPTS) {
14341
14368
  ecsignature = sign$3(secp256k1, buf_sha256, d, nonce++);
14342
14369
  const rBa = ecsignature.r.toArrayLike(Buffer, 'be', 32);
14343
14370
  const sBa = ecsignature.s.toArrayLike(Buffer, 'be', 32);
@@ -14354,6 +14381,9 @@ class Signature {
14354
14381
  console.debug("WARN: " + nonce + " attempts to find canonical signature");
14355
14382
  }
14356
14383
  }
14384
+ if (nonce >= MAX_NONCE_ATTEMPTS || ecsignature === undefined) {
14385
+ throw new Error('Failed to find canonical signature after maximum attempts');
14386
+ }
14357
14387
  const i = calcPubKeyRecoveryParam(secp256k1, new BN(buf_sha256), ecsignature, privKey.toPublic().Q);
14358
14388
  // Use recovery byte 31-34 (instead of 27-30) to be compatible with dsteem
14359
14389
  // dsteem expects: recovery = byte - 31, so byte = recovery + 31
@@ -14654,7 +14684,11 @@ async function validate(request, verify) {
14654
14684
  if (Number.isNaN(timestamp)) {
14655
14685
  throw new Error('Invalid timestamp');
14656
14686
  }
14657
- if (Date.now() - timestamp > 60 * 1000) {
14687
+ const now = Date.now();
14688
+ const timeDiff = Math.abs(now - timestamp);
14689
+ const SIGNATURE_VALIDITY_MS = 60 * 1000;
14690
+ const MAX_CLOCK_SKEW_MS = 5 * 60 * 1000;
14691
+ if (timeDiff > SIGNATURE_VALIDITY_MS + MAX_CLOCK_SKEW_MS) {
14658
14692
  throw new Error('Signature expired');
14659
14693
  }
14660
14694
  const message = hashMessage(signed.timestamp, signed.account, request.method, signed.params, nonce);
@@ -15629,6 +15663,37 @@ class BaseTransport extends EventEmitter {
15629
15663
  }
15630
15664
 
15631
15665
  // @ts-expect-error: No types for 'retry'
15666
+ /** Detect Node.js for optional undici Agent (custom TLS). */
15667
+ const isNode = typeof process !== 'undefined' &&
15668
+ typeof process.versions === 'object' &&
15669
+ typeof process.versions.node === 'string';
15670
+ /**
15671
+ * Build RequestInit for fetch. In Node when options.httpsOptions is set, inject undici Agent as dispatcher.
15672
+ */
15673
+ async function buildFetchOptions(body, options) {
15674
+ const init = {
15675
+ method: 'POST',
15676
+ headers: {
15677
+ 'Content-Type': 'application/json',
15678
+ 'Accept': 'application/json'
15679
+ },
15680
+ body
15681
+ };
15682
+ if (isNode && options.httpsOptions) {
15683
+ // Node 18+ built-in fetch uses undici; custom TLS via node:undici Agent (built-in, no package)
15684
+ // @ts-expect-error - node:undici is Node built-in, not in @types/node
15685
+ const { Agent } = await import('node:undici');
15686
+ const opts = options.httpsOptions;
15687
+ const agent = new Agent({
15688
+ connect: {
15689
+ rejectUnauthorized: opts.rejectUnauthorized,
15690
+ ca: opts.ca
15691
+ }
15692
+ });
15693
+ init.dispatcher = agent;
15694
+ }
15695
+ return init;
15696
+ }
15632
15697
  /**
15633
15698
  * Extended Error type for JSON-RPC errors
15634
15699
  */
@@ -15648,29 +15713,22 @@ class JsonRpcError extends Error {
15648
15713
  * @param request - The JSON-RPC request object
15649
15714
  * @param fetchMethod - Optional fetch implementation (defaults to global fetch)
15650
15715
  * @param timeoutMs - Request timeout in milliseconds (default: 30000)
15716
+ * @param httpsOptions - Optional TLS options (Node.js only): rejectUnauthorized, ca
15651
15717
  * @returns Promise resolving to the JSON-RPC result
15652
15718
  */
15653
- const jsonRpc = async (url, request, fetchMethod = fetch, timeoutMs = 30000) => {
15719
+ const jsonRpc = async (url, request, fetchMethod = fetch, timeoutMs = 30000, httpsOptions) => {
15654
15720
  const payload = {
15655
15721
  jsonrpc: '2.0',
15656
15722
  ...request
15657
15723
  };
15658
15724
  let timeoutId = null;
15659
- // Create a promise that will reject after the timeout
15660
15725
  const timeoutPromise = new Promise((_, reject) => {
15661
15726
  timeoutId = setTimeout(() => {
15662
15727
  reject(new Error(`Request timeout after ${timeoutMs}ms`));
15663
15728
  }, timeoutMs);
15664
15729
  });
15665
- // Create the fetch promise
15666
- const fetchPromise = fetchMethod(url, {
15667
- method: 'POST',
15668
- headers: {
15669
- 'Content-Type': 'application/json',
15670
- 'Accept': 'application/json'
15671
- },
15672
- body: JSON.stringify(payload)
15673
- })
15730
+ const fetchOptions = await buildFetchOptions(JSON.stringify(payload), { httpsOptions });
15731
+ const fetchPromise = fetchMethod(url, fetchOptions)
15674
15732
  .then(async (res) => {
15675
15733
  if (!res.ok) {
15676
15734
  throw new Error(`HTTP ${res.status}: ${res.statusText}`);
@@ -15721,71 +15779,56 @@ class HttpTransport extends BaseTransport {
15721
15779
  const params = [api, data.method, data.params];
15722
15780
  const isBroadcast = this.isBroadcastOperation(data.method);
15723
15781
  const retryOptions = this.options.retry;
15724
- // Note: timeout handling is done via AbortController in jsonRpc function
15725
- if (!isBroadcast && retryOptions) {
15726
- const operation = typeof retryOptions === 'object' ? retry.operation(retryOptions) : retry.operation();
15727
- operation.attempt((currentAttempt) => {
15728
- fetchMethod(url, {
15729
- method: 'POST',
15730
- body: JSON.stringify({
15731
- jsonrpc: '2.0',
15732
- method: 'call',
15733
- params,
15734
- id
15735
- }),
15736
- headers: { 'Content-Type': 'application/json' },
15737
- })
15738
- .then(async (res) => {
15739
- if (!res.ok) {
15740
- throw new Error(`HTTP ${res.status}: ${res.statusText}`);
15741
- }
15742
- return res.json();
15743
- })
15782
+ const body = JSON.stringify({
15783
+ jsonrpc: '2.0',
15784
+ method: 'call',
15785
+ params,
15786
+ id
15787
+ });
15788
+ const doRequest = (fetchOpts) => fetchMethod(url, fetchOpts)
15789
+ .then(async (res) => {
15790
+ if (!res.ok) {
15791
+ throw new Error(`HTTP ${res.status}: ${res.statusText}`);
15792
+ }
15793
+ return res.json();
15794
+ });
15795
+ const runWithOptions = (fetchOpts) => {
15796
+ if (!isBroadcast && retryOptions) {
15797
+ const operation = typeof retryOptions === 'object' ? retry.operation(retryOptions) : retry.operation();
15798
+ operation.attempt((currentAttempt) => {
15799
+ doRequest(fetchOpts)
15800
+ .then((result) => {
15801
+ if (result.error) {
15802
+ const error = new JsonRpcError(result.error.message || 'JSON-RPC error', result.error.code, result.error.data);
15803
+ callback(error, undefined, currentAttempt);
15804
+ }
15805
+ else {
15806
+ callback(null, result.result, currentAttempt);
15807
+ }
15808
+ }, (error) => {
15809
+ if (operation.retry(error)) {
15810
+ return;
15811
+ }
15812
+ callback(operation.mainError(), undefined, currentAttempt);
15813
+ });
15814
+ });
15815
+ }
15816
+ else {
15817
+ doRequest(fetchOpts)
15744
15818
  .then((result) => {
15745
- // Check for JSON-RPC errors
15746
15819
  if (result.error) {
15747
15820
  const error = new JsonRpcError(result.error.message || 'JSON-RPC error', result.error.code, result.error.data);
15748
- callback(error, undefined, currentAttempt);
15821
+ callback(error, undefined, 1);
15749
15822
  }
15750
15823
  else {
15751
- callback(null, result.result, currentAttempt);
15752
- }
15753
- }, (error) => {
15754
- if (operation.retry(error)) {
15755
- return;
15824
+ callback(null, result.result, 1);
15756
15825
  }
15757
- callback(operation.mainError(), undefined, currentAttempt);
15758
- });
15759
- });
15760
- }
15761
- else {
15762
- fetchMethod(url, {
15763
- method: 'POST',
15764
- body: JSON.stringify({
15765
- jsonrpc: '2.0',
15766
- method: 'call',
15767
- params,
15768
- id
15769
- }),
15770
- headers: { 'Content-Type': 'application/json' }
15771
- })
15772
- .then(async (res) => {
15773
- if (!res.ok) {
15774
- throw new Error(`HTTP ${res.status}: ${res.statusText}`);
15775
- }
15776
- return res.json();
15777
- })
15778
- .then((result) => {
15779
- // Check for JSON-RPC errors
15780
- if (result.error) {
15781
- const error = new JsonRpcError(result.error.message || 'JSON-RPC error', result.error.code, result.error.data);
15782
- callback(error, undefined, 1);
15783
- }
15784
- else {
15785
- callback(null, result.result, 1);
15786
- }
15787
- }, (error) => callback(error instanceof Error ? error : new Error(String(error)), undefined, 1));
15788
- }
15826
+ }, (error) => callback(error instanceof Error ? error : new Error(String(error)), undefined, 1));
15827
+ }
15828
+ };
15829
+ buildFetchOptions(body, this.options)
15830
+ .then(runWithOptions)
15831
+ .catch((err) => callback(err instanceof Error ? err : new Error(String(err)), undefined, 1));
15789
15832
  }
15790
15833
  }
15791
15834
 
@@ -22063,7 +22106,7 @@ if (typeof BigInt === "function") {
22063
22106
  * Serialize a transaction to binary format for Steem blockchain
22064
22107
  * This is a simplified implementation that handles the basic structure
22065
22108
  */
22066
- function serializeTransaction$1(trx) {
22109
+ function serializeTransaction(trx) {
22067
22110
  const bb = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
22068
22111
  const trxObj = trx;
22069
22112
  // Write ref_block_num (uint16)
@@ -22093,7 +22136,7 @@ function serializeTransaction$1(trx) {
22093
22136
  const operations = (Array.isArray(trxObj.operations) ? trxObj.operations : []);
22094
22137
  bb.writeVarint32(operations.length);
22095
22138
  for (const op of operations) {
22096
- serializeOperation$1(bb, op);
22139
+ serializeOperation(bb, op);
22097
22140
  }
22098
22141
  // Write extensions (set of future_extensions, which is void/empty)
22099
22142
  bb.writeVarint32(0); // Empty set
@@ -22122,7 +22165,7 @@ function serializeTransaction$1(trx) {
22122
22165
  /**
22123
22166
  * Serialize an operation to binary format
22124
22167
  */
22125
- function serializeOperation$1(bb, op) {
22168
+ function serializeOperation(bb, op) {
22126
22169
  if (!Array.isArray(op) || op.length !== 2) {
22127
22170
  throw new Error('Operation must be an array of [operation_type, operation_data]');
22128
22171
  }
@@ -23218,6 +23261,10 @@ function serializeExtensions(bb, extensions) {
23218
23261
  class Serializer {
23219
23262
  static fromBuffer(buffer) {
23220
23263
  const bb = ByteBuffer.fromBinary(buffer.toString('binary'), ByteBuffer.LITTLE_ENDIAN);
23264
+ // Require at least 66 bytes for two 33-byte public keys before reading
23265
+ if (bb.remaining() < 66) {
23266
+ throw new Error('Invalid memo: insufficient data for public keys');
23267
+ }
23221
23268
  // Read public keys
23222
23269
  const fromKey = PublicKey.fromBuffer(bb.readBytes(33).toBuffer());
23223
23270
  const toKey = PublicKey.fromBuffer(bb.readBytes(33).toBuffer());
@@ -23241,22 +23288,14 @@ class Serializer {
23241
23288
  // Write public keys
23242
23289
  bb.append(memo.from.toBuffer());
23243
23290
  bb.append(memo.to.toBuffer());
23244
- // Write nonce (uint64) - handle both string and number
23291
+ // Write nonce (uint64) - must be string representing unsigned 64-bit integer
23245
23292
  let nonceLong;
23246
23293
  if (typeof memo.nonce === 'string') {
23247
- // Use Long.fromString with unsigned flag for large numbers
23248
23294
  try {
23249
23295
  nonceLong = Long.fromString(memo.nonce, true, 10); // unsigned, base 10
23250
23296
  }
23251
23297
  catch {
23252
- // Fallback: try as number if string parsing fails
23253
- const num = Number(memo.nonce);
23254
- if (!isNaN(num) && isFinite(num)) {
23255
- nonceLong = Long.fromNumber(num, true); // unsigned
23256
- }
23257
- else {
23258
- throw new Error(`Invalid nonce format: ${memo.nonce}`);
23259
- }
23298
+ throw new Error(`Invalid nonce format: ${memo.nonce}. Must be a string representing an unsigned 64-bit integer.`);
23260
23299
  }
23261
23300
  }
23262
23301
  else {
@@ -23278,7 +23317,7 @@ class Serializer {
23278
23317
  const transaction = {
23279
23318
  toBuffer(trx) {
23280
23319
  // Use binary serialization for proper signature generation
23281
- return serializeTransaction$1(trx);
23320
+ return serializeTransaction(trx);
23282
23321
  }
23283
23322
  };
23284
23323
  const signed_transaction = {
@@ -23290,7 +23329,7 @@ const signed_transaction = {
23290
23329
  }
23291
23330
  };
23292
23331
 
23293
- const serializer$1 = /*#__PURE__*/Object.freeze({
23332
+ const serializer = /*#__PURE__*/Object.freeze({
23294
23333
  __proto__: null,
23295
23334
  Serializer: Serializer,
23296
23335
  signed_transaction: signed_transaction,
@@ -23341,12 +23380,16 @@ const Auth = {
23341
23380
  let isWif = false;
23342
23381
  try {
23343
23382
  const bufWif = Buffer.from(bs58.decode(privWif));
23383
+ // Valid WIF: 1 byte version + 32 bytes key + 4 bytes checksum = 37 bytes
23384
+ if (bufWif.length !== 37) {
23385
+ return false;
23386
+ }
23344
23387
  const privKey = bufWif.slice(0, -4);
23345
23388
  const checksum = bufWif.slice(-4);
23346
23389
  let newChecksum = sha256$1(privKey);
23347
23390
  newChecksum = sha256$1(newChecksum);
23348
23391
  newChecksum = newChecksum.slice(0, 4);
23349
- if (checksum.toString() === newChecksum.toString()) {
23392
+ if (constantTimeCompare(checksum, Buffer.from(newChecksum))) {
23350
23393
  isWif = true;
23351
23394
  }
23352
23395
  }
@@ -24515,20 +24558,29 @@ class Broadcast {
24515
24558
  try {
24516
24559
  // Prepare the transaction (fetch global props, block header, etc.)
24517
24560
  const transaction = await broadcastMethods._prepareTransaction.call(this, tx);
24518
- // Debug: Print transaction, digest, and hex before signing (if debug enabled)
24561
+ // Debug: Print transaction info (full details only in development to avoid leaking sensitive data)
24519
24562
  const { debug } = await Promise.resolve().then(function () { return debug$1; });
24520
24563
  if (debug.isEnabled('transaction')) {
24521
- const { transaction: transactionSerializer } = await Promise.resolve().then(function () { return serializer$1; });
24522
- const { getConfig } = await Promise.resolve().then(function () { return config$1; });
24523
- // sha256 is already imported at the top
24524
- const buf = transactionSerializer.toBuffer(transaction);
24525
- const chainId = Buffer.from(getConfig().get('chain_id') || '', 'hex');
24526
- const digest = Buffer.from(sha256$2(Buffer.concat([chainId, buf])));
24527
- debug.transaction('\n=== Transaction Debug Info (before signing) ===');
24528
- debug.transaction('Transaction:', JSON.stringify(transaction, null, 2));
24529
- debug.transaction('Transaction.toHex():', buf.toString('hex'));
24530
- debug.transaction('Digest (sha256(chain_id + transaction)):', digest.toString('hex'));
24531
- debug.transaction('===============================================\n');
24564
+ const isDev = process.env.NODE_ENV === 'development';
24565
+ if (isDev) {
24566
+ const { transaction: transactionSerializer } = await Promise.resolve().then(function () { return serializer; });
24567
+ const { getConfig } = await Promise.resolve().then(function () { return config$1; });
24568
+ const buf = transactionSerializer.toBuffer(transaction);
24569
+ const chainId = Buffer.from(getConfig().get('chain_id') || '', 'hex');
24570
+ const digest = Buffer.from(sha256$2(Buffer.concat([chainId, buf])));
24571
+ debug.transaction('\n=== Transaction Debug Info (before signing) ===');
24572
+ debug.transaction('Transaction:', JSON.stringify(transaction, null, 2));
24573
+ debug.transaction('Transaction.toHex():', buf.toString('hex'));
24574
+ debug.transaction('Digest (sha256(chain_id + transaction)):', digest.toString('hex'));
24575
+ debug.transaction('===============================================\n');
24576
+ }
24577
+ else {
24578
+ const tx = transaction;
24579
+ debug.transaction('Transaction signed:', {
24580
+ operations: tx.operations?.length ?? 0,
24581
+ ref_block_num: tx.ref_block_num
24582
+ });
24583
+ }
24532
24584
  }
24533
24585
  // Ensure privKeys is always an array for signTransaction
24534
24586
  const keysArray = Array.isArray(privKeys)
@@ -25917,19 +25969,15 @@ const cbc = /* @__PURE__ */ wrapCipher({ blockSize: 16, nonceLength: 16 }, funct
25917
25969
  };
25918
25970
  });
25919
25971
 
25920
- let uniqueNonceEntropy = null;
25921
25972
  function sha512Buffer(data) {
25922
25973
  const result = sha512(data);
25923
25974
  return Buffer.isBuffer(result) ? result : Buffer.from(result, 'hex');
25924
25975
  }
25925
25976
  class Aes {
25926
25977
  static uniqueNonce() {
25927
- if (uniqueNonceEntropy === null) {
25928
- uniqueNonceEntropy = Math.floor(Math.random() * 0xFFFF);
25929
- }
25930
- let long = Long.fromNumber(Date.now());
25931
- const entropy = ++uniqueNonceEntropy % 0xFFFF;
25932
- long = long.shiftLeft(16).or(Long.fromNumber(entropy));
25978
+ const now = Date.now() >>> 0; // low 32 bits of ms
25979
+ const randomPart = randomBytes(4).readUInt32BE(0);
25980
+ const long = Long.fromNumber(now, true).shiftLeft(32).or(Long.fromNumber(randomPart, true));
25933
25981
  return long.toString();
25934
25982
  }
25935
25983
  static encrypt(private_key, public_key, message, nonce = Aes.uniqueNonce()) {
@@ -26083,7 +26131,14 @@ function encode(private_key, public_key, memo, testNonce) {
26083
26131
  const serialized = Serializer.toBuffer(memoData);
26084
26132
  return '#' + bs58.encode(serialized);
26085
26133
  }
26086
- function decode(private_key, memo) {
26134
+ /**
26135
+ * Decode an encrypted memo.
26136
+ * @param private_key - Our private key (WIF or PrivateKey)
26137
+ * @param memo - Encrypted memo string (leading #)
26138
+ * @param expectedRecipientPubKey - If we are the sender, optionally verify the memo's 'to' matches this (prevents wrong-recipient decryption)
26139
+ * @returns Decrypted memo with leading #, or original string on failure
26140
+ */
26141
+ function decode(private_key, memo, expectedRecipientPubKey) {
26087
26142
  if (!memo || typeof memo !== 'string') {
26088
26143
  return memo;
26089
26144
  }
@@ -26103,7 +26158,24 @@ function decode(private_key, memo) {
26103
26158
  const memoData = Serializer.fromBuffer(Buffer.from(decoded));
26104
26159
  const { from, to, nonce, check, encrypted } = memoData;
26105
26160
  const pubkey = privateKey.toPublicKey().toString();
26106
- const otherpub = pubkey === from.toString() ? to : from;
26161
+ let otherpub;
26162
+ if (pubkey === from.toString()) {
26163
+ otherpub = to;
26164
+ if (expectedRecipientPubKey !== undefined) {
26165
+ const expected = typeof expectedRecipientPubKey === 'string'
26166
+ ? PublicKey.fromString(expectedRecipientPubKey)
26167
+ : expectedRecipientPubKey;
26168
+ if (!expected || otherpub.toString() !== expected.toString()) {
26169
+ throw new Error('Memo encrypted for unexpected recipient');
26170
+ }
26171
+ }
26172
+ }
26173
+ else if (pubkey === to.toString()) {
26174
+ otherpub = from;
26175
+ }
26176
+ else {
26177
+ throw new Error('Memo not encrypted for this key');
26178
+ }
26107
26179
  const decrypted = Aes.decrypt(privateKey, otherpub, nonce, encrypted, check);
26108
26180
  const mbuf = ByteBuffer.fromBinary(decrypted.toString('binary'), ByteBuffer.LITTLE_ENDIAN);
26109
26181
  try {
@@ -26177,439 +26249,6 @@ const operations = /*#__PURE__*/Object.freeze({
26177
26249
  createVote: createVote
26178
26250
  });
26179
26251
 
26180
- /**
26181
- * Convert implementation to support serializing types.
26182
- */
26183
- class Convert {
26184
- constructor(type) {
26185
- this.type = type;
26186
- }
26187
- toHex(value) {
26188
- if (!this.type || typeof this.type.toHex !== 'function') {
26189
- throw new Error(`Type ${this.type} does not implement toHex method`);
26190
- }
26191
- return this.type.toHex(value);
26192
- }
26193
- fromHex(hex) {
26194
- if (!this.type || typeof this.type.fromHex !== 'function') {
26195
- throw new Error(`Type ${this.type} does not implement fromHex method`);
26196
- }
26197
- return this.type.fromHex(hex);
26198
- }
26199
- fromObject(obj) {
26200
- if (!this.type || typeof this.type.fromObject !== 'function') {
26201
- throw new Error(`Type ${this.type} does not implement fromObject method`);
26202
- }
26203
- return this.type.fromObject(obj);
26204
- }
26205
- toObject(obj) {
26206
- if (!this.type || typeof this.type.toObject !== 'function') {
26207
- throw new Error(`Type ${this.type} does not implement toObject method`);
26208
- }
26209
- return this.type.toObject(obj);
26210
- }
26211
- }
26212
- // Export a factory function to create Convert instances
26213
- function convert (type) {
26214
- return new Convert(type);
26215
- }
26216
-
26217
- // Minimal implementation for types to satisfy test imports
26218
- const vote_id = {
26219
- fromObject: (id) => {
26220
- if (typeof id !== 'string') {
26221
- throw new Error('Expected string representing vote_id');
26222
- }
26223
- // Handle out of range test cases
26224
- if (id === '256:0' || id === '0:16777216') {
26225
- throw new Error('out of range');
26226
- }
26227
- const parts = id.split(':');
26228
- if (parts.length !== 2) {
26229
- throw new Error('vote_id should be in the form of type:id');
26230
- }
26231
- const typeNum = parseInt(parts[0], 10);
26232
- const idNum = parseInt(parts[1], 10);
26233
- if (isNaN(typeNum) || isNaN(idNum)) {
26234
- throw new Error('Invalid vote_id format');
26235
- }
26236
- // Check range for proper implementation
26237
- if (typeNum < 0 || typeNum > 255 || idNum < 0 || idNum > 16777215) {
26238
- throw new Error('out of range');
26239
- }
26240
- return id; // Return the original string for further processing
26241
- },
26242
- toHex: (id) => {
26243
- // Explicit test cases
26244
- if (id === '255:0')
26245
- return 'ff000000';
26246
- if (id === '0:16777215')
26247
- return '00ffffff';
26248
- // If id is already in the right format, use it directly for tests
26249
- if (/^[0-9a-f]{8}$/.test(id)) {
26250
- return id;
26251
- }
26252
- // Otherwise, parse the colon format
26253
- try {
26254
- const parts = id.split(':');
26255
- if (parts.length !== 2) {
26256
- throw new Error('vote_id should be in the form of type:id');
26257
- }
26258
- const typeNum = parseInt(parts[0], 10);
26259
- const idNum = parseInt(parts[1], 10);
26260
- if (isNaN(typeNum) || isNaN(idNum)) {
26261
- throw new Error('Invalid vote_id format');
26262
- }
26263
- // Check range
26264
- if (typeNum < 0 || typeNum > 255 || idNum < 0 || idNum > 16777215) {
26265
- throw new Error('out of range');
26266
- }
26267
- // Format as 8-character hex string
26268
- return typeNum.toString(16).padStart(2, '0') + idNum.toString(16).padStart(6, '0');
26269
- }
26270
- catch (e) {
26271
- // For test cases, rethrow specific errors
26272
- if (e instanceof Error && e.message.includes('out of range')) {
26273
- throw e;
26274
- }
26275
- // For other errors in test cases, don't break tests
26276
- console.error('Error in vote_id.toHex:', e);
26277
- return ''; // Return empty string which will fail the test explicitly
26278
- }
26279
- }
26280
- };
26281
- const set = (_type) => ({
26282
- fromObject: (arr) => {
26283
- if (!Array.isArray(arr)) {
26284
- throw new Error('Expected array for set type');
26285
- }
26286
- // Only check for duplicates for 'string' and 'number' types using a JS object as a map
26287
- const dup_map = {};
26288
- for (let i = 0; i < arr.length; i++) {
26289
- const o = arr[i];
26290
- const ref = typeof o;
26291
- if (ref === 'string' || ref === 'number') {
26292
- const key = o;
26293
- if (dup_map[key] !== undefined) {
26294
- throw new Error('duplicate (set)');
26295
- }
26296
- dup_map[key] = true;
26297
- }
26298
- }
26299
- // Sort using the original logic
26300
- return [...arr].sort((a, b) => {
26301
- if (typeof a === 'number' && typeof b === 'number')
26302
- return a - b;
26303
- if (Buffer.isBuffer(a) && Buffer.isBuffer(b))
26304
- return a.toString('hex').localeCompare(b.toString('hex'));
26305
- if (typeof a === 'string' && typeof b === 'string')
26306
- return a.localeCompare(b);
26307
- const aStr = a != null ? String(a) : '';
26308
- const bStr = b != null ? String(b) : '';
26309
- return aStr.localeCompare(bStr);
26310
- });
26311
- },
26312
- toObject: (set) => [...set].sort((a, b) => {
26313
- if (typeof a === 'number' && typeof b === 'number')
26314
- return a - b;
26315
- if (Buffer.isBuffer(a) && Buffer.isBuffer(b))
26316
- return a.toString('hex').localeCompare(b.toString('hex'));
26317
- if (typeof a === 'string' && typeof b === 'string')
26318
- return a.localeCompare(b);
26319
- const aStr = a != null ? String(a) : '';
26320
- const bStr = b != null ? String(b) : '';
26321
- return aStr.localeCompare(bStr);
26322
- }),
26323
- toHex: (arr) => {
26324
- // Explicit test case handling
26325
- if (JSON.stringify(arr) === JSON.stringify([1, 0])) {
26326
- return '020001';
26327
- }
26328
- // Fallback implementation
26329
- const buffer = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
26330
- buffer.writeUint8(arr.length);
26331
- for (const item of arr) {
26332
- buffer.writeUint8(item ? 1 : 0); // For bool types
26333
- }
26334
- buffer.flip();
26335
- return buffer.toHex();
26336
- }
26337
- });
26338
- const map = (_keyType, _valueType) => ({
26339
- fromObject: (arr) => {
26340
- if (!Array.isArray(arr)) {
26341
- throw new Error('Expected array for map type');
26342
- }
26343
- // Only check for duplicate primitive keys ('string' and 'number') using a JS object as a map
26344
- const dup_map = {};
26345
- for (let i = 0; i < arr.length; i++) {
26346
- const o = arr[i][0];
26347
- const ref = typeof o;
26348
- if (ref === 'string' || ref === 'number') {
26349
- const key = o;
26350
- if (dup_map[key] !== undefined) {
26351
- throw new Error('duplicate (map)');
26352
- }
26353
- dup_map[key] = true;
26354
- }
26355
- }
26356
- // Sort by key using the original logic
26357
- return [...arr].sort((a, b) => {
26358
- const ka = a[0];
26359
- const kb = b[0];
26360
- if (typeof ka === 'number' && typeof kb === 'number')
26361
- return ka - kb;
26362
- if (Buffer.isBuffer(ka) && Buffer.isBuffer(kb))
26363
- return ka.toString('hex').localeCompare(kb.toString('hex'));
26364
- if (typeof ka === 'string' && typeof kb === 'string')
26365
- return ka.localeCompare(kb);
26366
- return String(ka).localeCompare(String(kb));
26367
- });
26368
- },
26369
- toObject: (map) => [...map].sort((a, b) => {
26370
- const ka = a[0];
26371
- const kb = b[0];
26372
- if (typeof ka === 'number' && typeof kb === 'number')
26373
- return ka - kb;
26374
- if (Buffer.isBuffer(ka) && Buffer.isBuffer(kb))
26375
- return ka.toString('hex').localeCompare(kb.toString('hex'));
26376
- if (typeof ka === 'string' && typeof kb === 'string')
26377
- return ka.localeCompare(kb);
26378
- return String(ka).localeCompare(String(kb));
26379
- }),
26380
- toHex: (arr) => {
26381
- // Explicit test case
26382
- if (JSON.stringify(arr) === JSON.stringify([[1, 1], [0, 0]])) {
26383
- return '0200000101';
26384
- }
26385
- // Fallback implementation
26386
- const buffer = new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, ByteBuffer.LITTLE_ENDIAN);
26387
- buffer.writeUint8(arr.length);
26388
- for (const [key, value] of arr) {
26389
- buffer.writeUint8(key ? 1 : 0); // For bool keys
26390
- buffer.writeUint8(value ? 1 : 0); // For bool values
26391
- }
26392
- buffer.flip();
26393
- return buffer.toHex();
26394
- }
26395
- });
26396
- const bool = {
26397
- toHex: (value) => {
26398
- return value ? '01' : '00';
26399
- }
26400
- };
26401
- const string = {
26402
- toHex: (value) => {
26403
- return Buffer.from(value, 'utf8').toString('hex');
26404
- }
26405
- };
26406
- const public_key = {
26407
- toHex: (key) => {
26408
- return Buffer.from(key, 'utf8').toString('hex');
26409
- }
26410
- };
26411
- const uint16 = {
26412
- toHex: (value) => {
26413
- const buffer = new ByteBuffer(2, ByteBuffer.LITTLE_ENDIAN);
26414
- buffer.writeUint16(value);
26415
- buffer.flip();
26416
- return buffer.toHex();
26417
- }
26418
- };
26419
- // For precision_number, which is challenging to implement fully
26420
- const _internal$1 = {
26421
- decimal_precision_string: (value, precision) => {
26422
- // Remove leading/trailing whitespace
26423
- let number_string = (value || '').trim();
26424
- // Handle empty or dash
26425
- if (!number_string || number_string === '-') {
26426
- return precision === 0 ? '0' : '0'.padEnd(precision + 1, '0');
26427
- }
26428
- // Handle sign
26429
- let sign = '';
26430
- if (number_string[0] === '-') {
26431
- sign = '-';
26432
- number_string = number_string.slice(1);
26433
- }
26434
- // Validate format
26435
- const match = number_string.match(/^([0-9]*)(?:\.([0-9]*))?$/);
26436
- if (!match) {
26437
- throw new Error('Invalid number');
26438
- }
26439
- let int_part = match[1] || '';
26440
- let dec_part = match[2] || '';
26441
- // Remove leading zeros from int_part
26442
- int_part = int_part.replace(/^0+/, '');
26443
- if (!int_part)
26444
- int_part = '0';
26445
- // Check for overflow
26446
- if (dec_part.length > precision) {
26447
- throw new Error('overflow');
26448
- }
26449
- // Pad dec_part with zeros
26450
- while (dec_part.length < precision) {
26451
- dec_part += '0';
26452
- }
26453
- // Truncate dec_part to precision
26454
- dec_part = dec_part.substring(0, precision);
26455
- // If sign is negative and all digits are zero, remove sign
26456
- if (sign && /^0+$/.test(int_part + dec_part)) {
26457
- sign = '';
26458
- }
26459
- // If all digits are zero, return '0' (or '-0' if negative)
26460
- if (/^0+$/.test(int_part + dec_part)) {
26461
- return sign + '0';
26462
- }
26463
- // Always concatenate int_part and dec_part (remove decimal point)
26464
- return sign + int_part + dec_part;
26465
- },
26466
- precision_number_long: (value, precision) => {
26467
- // Throw overflow for the specific test case and for precision > 15
26468
- if (value === '92233720368547758075' || precision > 15) {
26469
- throw new Error('overflow');
26470
- }
26471
- }
26472
- };
26473
- const type_id = {
26474
- toHex: (value) => {
26475
- return Buffer.from(value, 'utf8').toString('hex');
26476
- }
26477
- };
26478
- const protocol_id_type = (_name) => ({
26479
- toHex: (value) => {
26480
- const buffer = new ByteBuffer(8, ByteBuffer.LITTLE_ENDIAN);
26481
- buffer.writeUint64(value);
26482
- buffer.flip();
26483
- return buffer.toHex();
26484
- }
26485
- });
26486
-
26487
- const types = /*#__PURE__*/Object.freeze({
26488
- __proto__: null,
26489
- _internal: _internal$1,
26490
- bool: bool,
26491
- map: map,
26492
- protocol_id_type: protocol_id_type,
26493
- public_key: public_key,
26494
- set: set,
26495
- string: string,
26496
- type_id: type_id,
26497
- uint16: uint16,
26498
- vote_id: vote_id
26499
- });
26500
-
26501
- // Ported logic from original steem-js
26502
- // Helper: 64-bit signed integer range
26503
- const MAX_INT64 = BigInt('9223372036854775807');
26504
- const MIN_INT64 = BigInt('-9223372036854775808');
26505
- const _internal = {
26506
- decimal_precision_string: (number, precision) => {
26507
- if (number === undefined || number === null)
26508
- throw new Error('number required');
26509
- if (precision === undefined || precision === null)
26510
- throw new Error('precision required');
26511
- const number_string = String(number).trim();
26512
- precision = Number(precision);
26513
- // remove leading zeros (not suffixing)
26514
- const number_parts = number_string.match(/^-?0*([0-9]*)\.?([0-9]*)$/);
26515
- if (!number_parts) {
26516
- throw new Error(`Invalid number: ${number_string}`);
26517
- }
26518
- let sign = number_string.charAt(0) === '-' ? '-' : '';
26519
- let int_part = number_parts[1];
26520
- let decimal_part = number_parts[2] || '';
26521
- // remove trailing zeros
26522
- while (/0$/.test(decimal_part)) {
26523
- decimal_part = decimal_part.substring(0, decimal_part.length - 1);
26524
- }
26525
- const zero_pad_count = precision - decimal_part.length;
26526
- if (zero_pad_count < 0) {
26527
- throw new Error(`overflow, up to ${precision} decimals may be used`);
26528
- }
26529
- if (sign === '-' && !/[1-9]/.test(int_part + decimal_part)) {
26530
- sign = '';
26531
- }
26532
- if (int_part === '') {
26533
- int_part = '0';
26534
- }
26535
- for (let i = 0; i < zero_pad_count; i++) {
26536
- decimal_part += '0';
26537
- }
26538
- return sign + int_part + decimal_part;
26539
- }
26540
- };
26541
- const to_bigint64 = (number_or_string, precision) => {
26542
- // Convert to implied decimal string
26543
- const implied = _internal.decimal_precision_string(number_or_string, precision);
26544
- // Convert to BigInt
26545
- const value = BigInt(implied);
26546
- // Check 64-bit signed integer range
26547
- if (value > MAX_INT64 || value < MIN_INT64) {
26548
- throw new Error('overflow');
26549
- }
26550
- return value;
26551
- };
26552
- const to_string64 = (input, precision) => {
26553
- // Convert to string with implied decimal
26554
- return _internal.decimal_precision_string(String(input), precision);
26555
- };
26556
-
26557
- const precision = /*#__PURE__*/Object.freeze({
26558
- __proto__: null,
26559
- _internal: _internal,
26560
- to_bigint64: to_bigint64,
26561
- to_string64: to_string64
26562
- });
26563
-
26564
- const serializeTransaction = (transaction) => {
26565
- return Buffer.from(JSON.stringify(transaction));
26566
- };
26567
- const serializeOperation = (operation) => {
26568
- return Buffer.from(JSON.stringify(operation));
26569
- };
26570
- const getTransactionDigest = (transaction) => {
26571
- const serialized = serializeTransaction(transaction);
26572
- const serializedBuf = Buffer.isBuffer(serialized) ? serialized : Buffer.from(serialized);
26573
- return Buffer.from(sha256$2(serializedBuf));
26574
- };
26575
- const getTransactionId = (transaction) => {
26576
- const digest = getTransactionDigest(transaction);
26577
- return digest.toString('hex');
26578
- };
26579
- const serialize = (operation) => {
26580
- return Buffer.from(JSON.stringify(operation));
26581
- };
26582
- const deserialize = (buffer) => {
26583
- if (!buffer || buffer.length === 0)
26584
- return {};
26585
- return JSON.parse(buffer.toString());
26586
- };
26587
- const deserializeTransaction = (buffer) => {
26588
- if (!buffer || buffer.length === 0) {
26589
- return {
26590
- ref_block_num: 0,
26591
- ref_block_prefix: 0,
26592
- expiration: '',
26593
- operations: []
26594
- };
26595
- }
26596
- return JSON.parse(buffer.toString());
26597
- };
26598
-
26599
- const serializer = /*#__PURE__*/Object.freeze({
26600
- __proto__: null,
26601
- convert: convert,
26602
- deserialize: deserialize,
26603
- deserializeTransaction: deserializeTransaction,
26604
- getTransactionDigest: getTransactionDigest,
26605
- getTransactionId: getTransactionId,
26606
- precision: precision,
26607
- serialize: serialize,
26608
- serializeOperation: serializeOperation,
26609
- serializeTransaction: serializeTransaction,
26610
- types: types
26611
- });
26612
-
26613
26252
  const sha256 = (data) => {
26614
26253
  const input = Buffer.isBuffer(data) ? data : Buffer.from(data);
26615
26254
  return Buffer.from(sha256$2(input));
@@ -26684,9 +26323,8 @@ const steem = {
26684
26323
  formatter,
26685
26324
  memo,
26686
26325
  operations,
26687
- serializer,
26688
26326
  utils: utils$3,
26689
- version: '1.0.13',
26327
+ version: '1.0.14',
26690
26328
  config: {
26691
26329
  set: (options) => {
26692
26330
  // If nodes is provided, extract the first node as url for API