@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.js CHANGED
@@ -104550,7 +104550,15 @@ var WalletService = class {
104550
104550
  method: "eth_sendRawTransaction" /* ETH_SEND_RAW_TRANSACTION */,
104551
104551
  params: [signedTx]
104552
104552
  });
104553
- return response.result;
104553
+ const txHash = response?.result;
104554
+ if (!txHash || typeof txHash !== "string") {
104555
+ throw new SDKError(
104556
+ "RPC returned empty tx hash",
104557
+ "TRANSACTION_FAILED" /* TRANSACTION_FAILED */,
104558
+ response
104559
+ );
104560
+ }
104561
+ return txHash;
104554
104562
  }
104555
104563
  async getTransactionReceipt(txHash, chainId) {
104556
104564
  const response = await this.rpcClient.sendRpc({
@@ -104619,40 +104627,48 @@ var WalletService = class {
104619
104627
  if (!from) {
104620
104628
  throw new SDKError("Wallet not found", "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */);
104621
104629
  }
104630
+ const toHexQuantity = (v5) => {
104631
+ if (v5 === void 0 || v5 === null) return "0x0";
104632
+ const s5 = String(v5).trim();
104633
+ if (!s5) return "0x0";
104634
+ if (s5.startsWith("0x") || s5.startsWith("0X")) return s5;
104635
+ try {
104636
+ const bi = BigInt(s5);
104637
+ return "0x" + bi.toString(16);
104638
+ } catch {
104639
+ return s5;
104640
+ }
104641
+ };
104622
104642
  const walletInfo = await this.walletClient.getWallet();
104623
104643
  const share1 = walletInfo?.share1;
104624
104644
  let share2 = await LocalForage.get(
104625
104645
  STORAGE_KEYS.share2(this.orgHost)
104626
104646
  );
104627
104647
  if (!share2) {
104628
- const [firebaseId, deviceSecret, encryptedShare2Device] = await Promise.all([
104629
- LocalForage.get(STORAGE_KEYS.firebaseId(this.orgHost)),
104630
- LocalForage.get(STORAGE_KEYS.deviceSecret(this.orgHost)),
104631
- LocalForage.get(
104648
+ try {
104649
+ const firebaseId = await LocalForage.get(
104650
+ STORAGE_KEYS.firebaseId(this.orgHost)
104651
+ );
104652
+ const encryptedDevice = await LocalForage.get(
104632
104653
  STORAGE_KEYS.encryptedShare2Device(this.orgHost)
104633
- )
104634
- ]);
104635
- if (firebaseId && deviceSecret && encryptedShare2Device) {
104636
- try {
104637
- console.debug(
104638
- "[WalletService] recovering share2 from encryptedShare2_device"
104639
- );
104654
+ );
104655
+ const deviceSecret = await LocalForage.get(
104656
+ STORAGE_KEYS.deviceSecret(this.orgHost)
104657
+ );
104658
+ if (firebaseId && encryptedDevice && deviceSecret) {
104640
104659
  share2 = Crypto.decryptShare(
104641
- encryptedShare2Device,
104660
+ encryptedDevice,
104642
104661
  deviceSecret,
104643
104662
  firebaseId
104644
104663
  );
104645
- } catch (e7) {
104646
- console.warn(
104647
- "[WalletService] failed to decrypt encryptedShare2_device",
104648
- e7
104649
- );
104664
+ await LocalForage.save(STORAGE_KEYS.share2(this.orgHost), share2);
104650
104665
  }
104666
+ } catch {
104651
104667
  }
104652
104668
  }
104653
104669
  if (!share1 || !share2) {
104654
104670
  throw new SDKError(
104655
- "Wallet shares not found. Please unlock the wallet by calling retrieveWallet(PIN) once on this device.",
104671
+ "Wallet shares not found",
104656
104672
  "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */
104657
104673
  );
104658
104674
  }
@@ -104660,19 +104676,48 @@ var WalletService = class {
104660
104676
  const wallet = new Wallet(privateKey);
104661
104677
  const nonce = params.nonce ?? await this.getTransactionCount(from, params.chainId);
104662
104678
  const gasPrice = params.gasPrice ?? await this.getGasPrice(params.chainId);
104679
+ let gasLimit = params.gasLimit;
104680
+ if (!gasLimit) {
104681
+ try {
104682
+ const est = await this.estimateGas(
104683
+ {
104684
+ from,
104685
+ to: params.to,
104686
+ value: toHexQuantity(params.value),
104687
+ data: params.data || "0x"
104688
+ },
104689
+ params.chainId
104690
+ );
104691
+ const buffered = Math.max(21e3, Math.ceil(est * 1.2));
104692
+ gasLimit = "0x" + buffered.toString(16);
104693
+ } catch (e7) {
104694
+ const data = (params.data || "0x").toLowerCase();
104695
+ const isApprove = data.startsWith("0x095ea7b3");
104696
+ const fallback = isApprove ? 12e4 : data !== "0x" ? 8e5 : 21e3;
104697
+ gasLimit = "0x" + fallback.toString(16);
104698
+ }
104699
+ }
104700
+ const value = params.value ?? "0";
104663
104701
  const signedTx = await wallet.signTransaction({
104664
104702
  to: params.to,
104665
- value: params.value,
104703
+ value,
104666
104704
  data: params.data || "0x",
104667
104705
  chainId: params.chainId,
104668
104706
  nonce,
104669
104707
  gasPrice,
104670
- gasLimit: params.gasLimit
104708
+ gasLimit
104671
104709
  });
104672
- return this.sendRawTransaction(signedTx, params.chainId);
104710
+ const txHash = await this.sendRawTransaction(signedTx, params.chainId);
104711
+ await LocalForage.delete(STORAGE_KEYS.share2(this.orgHost));
104712
+ return txHash;
104673
104713
  } catch (error) {
104714
+ if (error instanceof SDKError) {
104715
+ throw error;
104716
+ }
104717
+ const anyErr = error;
104718
+ const causeMsg = anyErr?.shortMessage || anyErr?.reason || anyErr?.message || anyErr?.error?.message || "Unknown error";
104674
104719
  throw new SDKError(
104675
- "Transaction failed",
104720
+ `Transaction failed: ${causeMsg}`,
104676
104721
  "TRANSACTION_FAILED" /* TRANSACTION_FAILED */,
104677
104722
  error
104678
104723
  );
@@ -105147,9 +105192,19 @@ var RpcClient = class {
105147
105192
  method: request.method,
105148
105193
  params: request.params
105149
105194
  };
105150
- return this.client.post(this.baseUrl, rpcRequest, {
105151
- needsAccessToken: true
105152
- });
105195
+ const res = await this.client.post(
105196
+ this.baseUrl,
105197
+ rpcRequest,
105198
+ {
105199
+ needsAccessToken: true
105200
+ }
105201
+ );
105202
+ if (res?.error) {
105203
+ const err = res.error;
105204
+ const msg = err?.message || "RPC error";
105205
+ throw new SDKError(msg, "REQUEST_FAILED" /* REQUEST_FAILED */, err);
105206
+ }
105207
+ return res;
105153
105208
  }
105154
105209
  };
105155
105210