@steemit/steem-js 1.0.10 → 1.0.11

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 CHANGED
@@ -14193,6 +14193,77 @@ function sign$3(curve, hash, d, nonce) {
14193
14193
  const finalS = s.gt(N_OVER_TWO) ? n.sub(s) : s;
14194
14194
  return new ECSignature(r, finalS);
14195
14195
  }
14196
+ /**
14197
+ * Recover a public key from a signature.
14198
+ *
14199
+ * See SEC 1: Elliptic Curve Cryptography, section 4.1.6, "Public
14200
+ * Key Recovery Operation".
14201
+ *
14202
+ * http://www.secg.org/download/aid-780/sec1-v2.pdf
14203
+ */
14204
+ function recoverPubKey(curve, e, signature, i) {
14205
+ if ((i & 3) !== i) {
14206
+ throw new Error('Recovery param is more than two bits');
14207
+ }
14208
+ const n = new BN(curve.n.toString());
14209
+ const r = signature.r;
14210
+ const s = signature.s;
14211
+ if (r.isNeg() || r.isZero() || !r.lt(n))
14212
+ throw new Error('Invalid r value');
14213
+ if (s.isNeg() || s.isZero() || !s.lt(n))
14214
+ throw new Error('Invalid s value');
14215
+ // Try using elliptic's built-in recoverPubKey method
14216
+ // It expects: msg (Buffer), signature ({r: BN, s: BN}), j (recovery param)
14217
+ try {
14218
+ // Convert e (BN) to Buffer
14219
+ const msgBuffer = e.toArrayLike(Buffer, 'be', 32);
14220
+ // Create signature object compatible with elliptic's recoverPubKey
14221
+ // elliptic expects {r: BN, s: BN} format
14222
+ const sigObj = { r: r, s: s };
14223
+ // Use elliptic's built-in method
14224
+ const Q = curve.recoverPubKey(msgBuffer, sigObj, i);
14225
+ return Q;
14226
+ }
14227
+ catch (error) {
14228
+ // Fallback to manual implementation if elliptic's method fails
14229
+ const G = curve.g;
14230
+ // A set LSB signifies that the y-coordinate is odd
14231
+ const isYOdd = !!(i & 1);
14232
+ // The more significant bit specifies whether we should use the
14233
+ // first or second candidate key.
14234
+ const isSecondKey = i >> 1;
14235
+ // 1.1 Let x = r + jn
14236
+ const x = isSecondKey ? r.add(n) : r;
14237
+ // pointFromX expects a hex string (not BN object)
14238
+ // Convert BN to hex string and ensure proper padding
14239
+ const xHex = x.toString(16);
14240
+ // Ensure hex string is properly padded to 64 characters (32 bytes)
14241
+ const xHexPadded = xHex.padStart(64, '0');
14242
+ // pointFromX may also accept a Buffer, but hex string is more reliable
14243
+ let R;
14244
+ try {
14245
+ R = curve.curve.pointFromX(xHexPadded, isYOdd);
14246
+ }
14247
+ catch (error) {
14248
+ // If hex string fails, try with Buffer
14249
+ const xBuffer = x.toArrayLike(Buffer, 'be', 32);
14250
+ R = curve.curve.pointFromX(xBuffer, isYOdd);
14251
+ }
14252
+ // 1.4 Check that nR is at infinity
14253
+ const nR = R.mul(n);
14254
+ if (!nR.isInfinity())
14255
+ throw new Error('nR is not a valid curve point');
14256
+ // Compute -e from e
14257
+ const eNeg = e.neg().mod(n);
14258
+ // 1.6.1 Compute Q = r^-1 (sR - eG)
14259
+ // Q = r^-1 (sR + -eG)
14260
+ const rInv = r.invm(n);
14261
+ const sR = R.mul(s);
14262
+ const eGNeg = G.mul(eNeg);
14263
+ const Q = sR.add(eGNeg).mul(rInv);
14264
+ return Q;
14265
+ }
14266
+ }
14196
14267
  /**
14197
14268
  * Calculate pubkey extraction parameter.
14198
14269
  *
@@ -14207,11 +14278,15 @@ function sign$3(curve, hash, d, nonce) {
14207
14278
  function calcPubKeyRecoveryParam(curve, e, signature, Q) {
14208
14279
  for (let i = 0; i < 4; i++) {
14209
14280
  try {
14210
- // Use elliptic's built-in recovery method
14211
- const Qprime = curve.recoverPubKey(e, signature, i);
14281
+ // Use our own recoverPubKey function instead of curve.recoverPubKey
14282
+ const Qprime = recoverPubKey(curve, e, signature, i);
14212
14283
  // 1.6.2 Verify Q = Q'
14213
- // Compare points by checking if they are the same point
14214
- if (Qprime.eq(Q)) {
14284
+ // Compare points by checking coordinates (more reliable than eq method)
14285
+ const Qx = Q.getX().toString(16);
14286
+ const Qy = Q.getY().toString(16);
14287
+ const QprimeX = Qprime.getX().toString(16);
14288
+ const QprimeY = Qprime.getY().toString(16);
14289
+ if (Qx === QprimeX && Qy === QprimeY) {
14215
14290
  return i;
14216
14291
  }
14217
14292
  }
@@ -14245,7 +14320,13 @@ class Signature {
14245
14320
  throw new Error('Invalid signature length');
14246
14321
  }
14247
14322
  const i = buffer.readUInt8(0);
14248
- if ((i - 27) !== ((i - 27) & 7)) {
14323
+ // Support both formats: 27-30 (old/legacy) and 31-34 (dsteem compatible)
14324
+ // Check if it's in the old format (27-30) or new format (31-34)
14325
+ const recoveryOld = i - 27;
14326
+ const recoveryNew = i - 31;
14327
+ const isValidOld = recoveryOld >= 0 && recoveryOld <= 3 && (recoveryOld === (recoveryOld & 7));
14328
+ const isValidNew = recoveryNew >= 0 && recoveryNew <= 3 && (recoveryNew === (recoveryNew & 7));
14329
+ if (!isValidOld && !isValidNew) {
14249
14330
  throw new Error('Invalid signature parameter');
14250
14331
  }
14251
14332
  const r = new BN(buffer.slice(1, 33));
@@ -14294,7 +14375,9 @@ class Signature {
14294
14375
  }
14295
14376
  }
14296
14377
  const i = calcPubKeyRecoveryParam(secp256k1, new BN(buf_sha256), ecsignature, privKey.toPublic().Q);
14297
- return new Signature(ecsignature.r, ecsignature.s, i + 27);
14378
+ // Use recovery byte 31-34 (instead of 27-30) to be compatible with dsteem
14379
+ // dsteem expects: recovery = byte - 31, so byte = recovery + 31
14380
+ return new Signature(ecsignature.r, ecsignature.s, i + 31);
14298
14381
  }
14299
14382
  static isCanonical(r, s) {
14300
14383
  // See libraries/fc/src/crypto/elliptic_common.cpp is_fc_canonical
@@ -25754,7 +25837,7 @@ const steem = {
25754
25837
  operations,
25755
25838
  serializer,
25756
25839
  utils: utils$3,
25757
- version: '1.0.10',
25840
+ version: '1.0.11',
25758
25841
  config: {
25759
25842
  set: (options) => {
25760
25843
  // If nodes is provided, extract the first node as url for API
@@ -25801,6 +25884,8 @@ exports.hmacSha256 = hmacSha256;
25801
25884
  exports.ripemd160 = ripemd160;
25802
25885
  exports.sha256 = sha256;
25803
25886
  exports.sign = sign;
25887
+ exports.signRequest = sign$2;
25804
25888
  exports.steem = steem;
25889
+ exports.validateRequest = validate;
25805
25890
  exports.verify = verify;
25806
25891
  //# sourceMappingURL=index.cjs.map