@weblock-wallet/sdk 0.1.59 → 0.1.61

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
@@ -104595,7 +104595,15 @@ var WalletService = class {
104595
104595
  method: "eth_sendRawTransaction" /* ETH_SEND_RAW_TRANSACTION */,
104596
104596
  params: [signedTx]
104597
104597
  });
104598
- return response.result;
104598
+ const txHash = response?.result;
104599
+ if (!txHash || typeof txHash !== "string") {
104600
+ throw new SDKError(
104601
+ "RPC returned empty tx hash",
104602
+ "TRANSACTION_FAILED" /* TRANSACTION_FAILED */,
104603
+ response
104604
+ );
104605
+ }
104606
+ return txHash;
104599
104607
  }
104600
104608
  async getTransactionReceipt(txHash, chainId) {
104601
104609
  const response = await this.rpcClient.sendRpc({
@@ -104664,40 +104672,48 @@ var WalletService = class {
104664
104672
  if (!from) {
104665
104673
  throw new SDKError("Wallet not found", "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */);
104666
104674
  }
104675
+ const toHexQuantity = (v5) => {
104676
+ if (v5 === void 0 || v5 === null) return "0x0";
104677
+ const s5 = String(v5).trim();
104678
+ if (!s5) return "0x0";
104679
+ if (s5.startsWith("0x") || s5.startsWith("0X")) return s5;
104680
+ try {
104681
+ const bi = BigInt(s5);
104682
+ return "0x" + bi.toString(16);
104683
+ } catch {
104684
+ return s5;
104685
+ }
104686
+ };
104667
104687
  const walletInfo = await this.walletClient.getWallet();
104668
104688
  const share1 = walletInfo?.share1;
104669
104689
  let share2 = await LocalForage.get(
104670
104690
  STORAGE_KEYS.share2(this.orgHost)
104671
104691
  );
104672
104692
  if (!share2) {
104673
- const [firebaseId, deviceSecret, encryptedShare2Device] = await Promise.all([
104674
- LocalForage.get(STORAGE_KEYS.firebaseId(this.orgHost)),
104675
- LocalForage.get(STORAGE_KEYS.deviceSecret(this.orgHost)),
104676
- LocalForage.get(
104693
+ try {
104694
+ const firebaseId = await LocalForage.get(
104695
+ STORAGE_KEYS.firebaseId(this.orgHost)
104696
+ );
104697
+ const encryptedDevice = await LocalForage.get(
104677
104698
  STORAGE_KEYS.encryptedShare2Device(this.orgHost)
104678
- )
104679
- ]);
104680
- if (firebaseId && deviceSecret && encryptedShare2Device) {
104681
- try {
104682
- console.debug(
104683
- "[WalletService] recovering share2 from encryptedShare2_device"
104684
- );
104699
+ );
104700
+ const deviceSecret = await LocalForage.get(
104701
+ STORAGE_KEYS.deviceSecret(this.orgHost)
104702
+ );
104703
+ if (firebaseId && encryptedDevice && deviceSecret) {
104685
104704
  share2 = Crypto.decryptShare(
104686
- encryptedShare2Device,
104705
+ encryptedDevice,
104687
104706
  deviceSecret,
104688
104707
  firebaseId
104689
104708
  );
104690
- } catch (e7) {
104691
- console.warn(
104692
- "[WalletService] failed to decrypt encryptedShare2_device",
104693
- e7
104694
- );
104709
+ await LocalForage.save(STORAGE_KEYS.share2(this.orgHost), share2);
104695
104710
  }
104711
+ } catch {
104696
104712
  }
104697
104713
  }
104698
104714
  if (!share1 || !share2) {
104699
104715
  throw new SDKError(
104700
- "Wallet shares not found. Please unlock the wallet by calling retrieveWallet(PIN) once on this device.",
104716
+ "Wallet shares not found",
104701
104717
  "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */
104702
104718
  );
104703
104719
  }
@@ -104705,19 +104721,48 @@ var WalletService = class {
104705
104721
  const wallet = new import_ethers2.Wallet(privateKey);
104706
104722
  const nonce = params.nonce ?? await this.getTransactionCount(from, params.chainId);
104707
104723
  const gasPrice = params.gasPrice ?? await this.getGasPrice(params.chainId);
104724
+ let gasLimit = params.gasLimit;
104725
+ if (!gasLimit) {
104726
+ try {
104727
+ const est = await this.estimateGas(
104728
+ {
104729
+ from,
104730
+ to: params.to,
104731
+ value: toHexQuantity(params.value),
104732
+ data: params.data || "0x"
104733
+ },
104734
+ params.chainId
104735
+ );
104736
+ const buffered = Math.max(21e3, Math.ceil(est * 1.2));
104737
+ gasLimit = "0x" + buffered.toString(16);
104738
+ } catch (e7) {
104739
+ const data = (params.data || "0x").toLowerCase();
104740
+ const isApprove = data.startsWith("0x095ea7b3");
104741
+ const fallback = isApprove ? 12e4 : data !== "0x" ? 8e5 : 21e3;
104742
+ gasLimit = "0x" + fallback.toString(16);
104743
+ }
104744
+ }
104745
+ const value = params.value ?? "0";
104708
104746
  const signedTx = await wallet.signTransaction({
104709
104747
  to: params.to,
104710
- value: params.value,
104748
+ value,
104711
104749
  data: params.data || "0x",
104712
104750
  chainId: params.chainId,
104713
104751
  nonce,
104714
104752
  gasPrice,
104715
- gasLimit: params.gasLimit
104753
+ gasLimit
104716
104754
  });
104717
- return this.sendRawTransaction(signedTx, params.chainId);
104755
+ const txHash = await this.sendRawTransaction(signedTx, params.chainId);
104756
+ await LocalForage.delete(STORAGE_KEYS.share2(this.orgHost));
104757
+ return txHash;
104718
104758
  } catch (error) {
104759
+ if (error instanceof SDKError) {
104760
+ throw error;
104761
+ }
104762
+ const anyErr = error;
104763
+ const causeMsg = anyErr?.shortMessage || anyErr?.reason || anyErr?.message || anyErr?.error?.message || "Unknown error";
104719
104764
  throw new SDKError(
104720
- "Transaction failed",
104765
+ `Transaction failed: ${causeMsg}`,
104721
104766
  "TRANSACTION_FAILED" /* TRANSACTION_FAILED */,
104722
104767
  error
104723
104768
  );
@@ -105188,9 +105233,19 @@ var RpcClient = class {
105188
105233
  method: request.method,
105189
105234
  params: request.params
105190
105235
  };
105191
- return this.client.post(this.baseUrl, rpcRequest, {
105192
- needsAccessToken: true
105193
- });
105236
+ const res = await this.client.post(
105237
+ this.baseUrl,
105238
+ rpcRequest,
105239
+ {
105240
+ needsAccessToken: true
105241
+ }
105242
+ );
105243
+ if (res?.error) {
105244
+ const err = res.error;
105245
+ const msg = err?.message || "RPC error";
105246
+ throw new SDKError(msg, "REQUEST_FAILED" /* REQUEST_FAILED */, err);
105247
+ }
105248
+ return res;
105194
105249
  }
105195
105250
  };
105196
105251