@weblock-wallet/sdk 0.1.71 → 0.1.73

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
@@ -103989,28 +103989,47 @@ var Crypto = {
103989
103989
  encryptShare(share, password, salt) {
103990
103990
  try {
103991
103991
  const key = pbkdf2Sync(password, salt, 1e5, 32, "sha512");
103992
- const iv = randomBytes(16);
103993
- const cipher = createCipheriv("aes-256-cbc", key, iv);
103992
+ const iv = randomBytes(12);
103993
+ const cipher = createCipheriv("aes-256-gcm", key, iv);
103994
103994
  let encrypted = cipher.update(share, "utf8", "hex");
103995
103995
  encrypted += cipher.final("hex");
103996
- return `${iv.toString("hex")}:${encrypted}`;
103996
+ const tag = cipher.getAuthTag();
103997
+ return `gcm:${iv.toString("hex")}:${tag.toString("hex")}:${encrypted}`;
103997
103998
  } catch (e7) {
103998
103999
  console.error("Error during encrypting share:", e7);
103999
104000
  throw e7;
104000
104001
  }
104001
104002
  },
104002
104003
  decryptShare(encryptedShare, password, salt) {
104004
+ const key = pbkdf2Sync(password, salt, 1e5, 32, "sha512");
104003
104005
  try {
104004
- const [ivHex, encrypted] = encryptedShare.split(":");
104006
+ if (encryptedShare.startsWith("gcm:")) {
104007
+ const parts2 = encryptedShare.split(":");
104008
+ if (parts2.length !== 4) throw new Error("Invalid ciphertext format");
104009
+ const [, ivHex2, tagHex, cipherHex] = parts2;
104010
+ const iv2 = Buffer2.from(ivHex2, "hex");
104011
+ const tag = Buffer2.from(tagHex, "hex");
104012
+ const decipher2 = createDecipheriv("aes-256-gcm", key, iv2);
104013
+ decipher2.setAuthTag(tag);
104014
+ let decrypted2 = decipher2.update(cipherHex, "hex", "utf8");
104015
+ decrypted2 += decipher2.final("utf8");
104016
+ return decrypted2;
104017
+ }
104018
+ const parts = encryptedShare.split(":");
104019
+ if (parts.length !== 2) throw new Error("Invalid ciphertext format");
104020
+ const [ivHex, encrypted] = parts;
104005
104021
  const iv = Buffer2.from(ivHex, "hex");
104006
- const key = pbkdf2Sync(password, salt, 1e5, 32, "sha512");
104007
104022
  const decipher = createDecipheriv("aes-256-cbc", key, iv);
104008
104023
  let decrypted = decipher.update(encrypted, "hex", "utf8");
104009
104024
  decrypted += decipher.final("utf8");
104025
+ if (!/^[0-9a-fA-F]+$/.test(decrypted) || decrypted.length % 2 !== 0) {
104026
+ throw new Error("Wrong password");
104027
+ }
104010
104028
  return decrypted;
104011
104029
  } catch (e7) {
104012
104030
  console.error("Error during decrypting share:", e7);
104013
- if (e7.message === "unable to decrypt data") {
104031
+ const msg = String(e7?.message || "").toLowerCase();
104032
+ if (msg.includes("unable to decrypt data") || msg.includes("wrong password") || msg.includes("bad decrypt") || msg.includes("auth") || msg.includes("unsupported state") || msg.includes("invalid tag")) {
104014
104033
  throw new Error("Wrong password");
104015
104034
  }
104016
104035
  throw e7;
@@ -104110,6 +104129,8 @@ var SDKErrorCode = /* @__PURE__ */ ((SDKErrorCode2) => {
104110
104129
  SDKErrorCode2["NETWORK_SWITCH_FAILED"] = "NETWORK_SWITCH_FAILED";
104111
104130
  SDKErrorCode2["TRANSACTION_FAILED"] = "TRANSACTION_FAILED";
104112
104131
  SDKErrorCode2["INVALID_PIN"] = "INVALID_PIN";
104132
+ SDKErrorCode2["RECOVERY_FAILED"] = "RECOVERY_FAILED";
104133
+ SDKErrorCode2["UNKNOWN"] = "UNKNOWN";
104113
104134
  return SDKErrorCode2;
104114
104135
  })(SDKErrorCode || {});
104115
104136
  var SDKError = class extends Error {
@@ -104373,10 +104394,25 @@ var WalletService = class {
104373
104394
  "INVALID_PARAMS" /* INVALID_PARAMS */
104374
104395
  );
104375
104396
  }
104397
+ const walletInfo = await this.walletClient.getWallet();
104398
+ const serverAddr = this.normalizeAddr(walletInfo?.address);
104399
+ const expectedShareLen = String(walletInfo?.share1 ?? "").length;
104376
104400
  const decryptShareOrThrow = (encryptedShare) => {
104377
104401
  try {
104378
- return Crypto.decryptShare(encryptedShare, password, firebaseId);
104402
+ const share = Crypto.decryptShare(
104403
+ encryptedShare,
104404
+ password,
104405
+ firebaseId
104406
+ );
104407
+ if (expectedShareLen > 0 && share.length !== expectedShareLen) {
104408
+ throw new SDKError(
104409
+ "Incorrect PIN code",
104410
+ "INVALID_PASSWORD" /* INVALID_PASSWORD */
104411
+ );
104412
+ }
104413
+ return share;
104379
104414
  } catch (e7) {
104415
+ if (e7 instanceof SDKError) throw e7;
104380
104416
  if (this.isInvalidPasswordError(e7)) {
104381
104417
  throw new SDKError(
104382
104418
  "Incorrect PIN code",
@@ -104387,8 +104423,6 @@ var WalletService = class {
104387
104423
  throw e7;
104388
104424
  }
104389
104425
  };
104390
- const walletInfo = await this.walletClient.getWallet();
104391
- const serverAddr = this.normalizeAddr(walletInfo?.address);
104392
104426
  let share2 = await LocalForage.get(
104393
104427
  STORAGE_KEYS.share2(this.orgHost)
104394
104428
  );
@@ -104411,8 +104445,8 @@ var WalletService = class {
104411
104445
  const derivedAddr2 = this.normalizeAddr(wallet2.address);
104412
104446
  if (this.addressesMismatch(serverAddr, derivedAddr2)) {
104413
104447
  throw new SDKError(
104414
- `Recovered wallet address mismatch. server=${serverAddr} derived=${derivedAddr2}`,
104415
- "WALLET_RECOVERY_FAILED" /* WALLET_RECOVERY_FAILED */
104448
+ "Incorrect PIN code",
104449
+ "INVALID_PASSWORD" /* INVALID_PASSWORD */
104416
104450
  );
104417
104451
  }
104418
104452
  const newShares = await Secrets.split(wallet2.privateKey, 3, 2);
@@ -104447,10 +104481,7 @@ var WalletService = class {
104447
104481
  const wallet = new import_ethers2.Wallet(privateKey);
104448
104482
  const derivedAddr = this.normalizeAddr(wallet.address);
104449
104483
  if (this.addressesMismatch(serverAddr, derivedAddr)) {
104450
- throw new SDKError(
104451
- `Recovered wallet address mismatch. server=${serverAddr} derived=${derivedAddr}`,
104452
- "WALLET_RECOVERY_FAILED" /* WALLET_RECOVERY_FAILED */
104453
- );
104484
+ throw new SDKError("Incorrect PIN code", "INVALID_PASSWORD" /* INVALID_PASSWORD */);
104454
104485
  }
104455
104486
  await this.ensureDeviceEncryptedShare2(share2, firebaseId);
104456
104487
  this.walletAddress = wallet.address;