ault-sdk-ts 0.17.0 → 0.17.1

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/README.md CHANGED
@@ -1390,6 +1390,14 @@ transaction, so look it up by `error.txHash` before you sign and send it again.
1390
1390
  A `TxNotConfirmedError` means the broadcast was accepted but the transaction
1391
1391
  did not show up in a block before the timeout.
1392
1392
 
1393
+ A wallet send can fail before anything is broadcast with `SignerMismatchError`.
1394
+ The SDK checks that the signature the wallet returned was made by the signer's
1395
+ own key. It was not if the wallet signed with another account, hashes EIP-712
1396
+ typed data in a non-standard way, or is a contract wallet that does not produce
1397
+ ECDSA signatures. `error.signerAddress` is the account you asked to sign with
1398
+ and `error.recoveredAddress` is the one the signature belongs to. Nothing was
1399
+ sent, so there is no transaction to look up.
1400
+
1393
1401
  ## Advanced Usage
1394
1402
 
1395
1403
  For advanced use cases, you can access the low-level client:
@@ -69,6 +69,23 @@ export declare class TxNotConfirmedError extends TxError {
69
69
  export declare class TxConfirmationUnavailableError extends TxError {
70
70
  constructor(params: TxErrorParams);
71
71
  }
72
+ /**
73
+ * Thrown before anything is broadcast when the wallet's signature was not made
74
+ * by the signer's key: it signed with another account, hashed the EIP-712
75
+ * payload its own way, or is not an ECDSA wallet at all (a contract wallet).
76
+ * `recoveredAddress` is the address the signature does recover to, when one
77
+ * could be recovered. Nothing was sent, so the sequence is untouched.
78
+ */
79
+ export declare class SignerMismatchError extends Error {
80
+ /** The signer's address, in 0x form. */
81
+ signerAddress: string;
82
+ recoveredAddress?: string;
83
+ constructor(params: {
84
+ signerAddress: string;
85
+ recoveredAddress?: string;
86
+ message?: string;
87
+ });
88
+ }
72
89
  /**
73
90
  * Thrown in wait mode when the tx committed successfully (DeliverTx code 0) but
74
91
  * the expected per-message response was missing from `tx_response.data` — e.g. a
package/dist/index.cjs CHANGED
@@ -261,6 +261,7 @@ __export(index_exports, {
261
261
  SearchUDFSymbolsQueryParams: () => SearchUDFSymbolsQueryParams,
262
262
  SearchUDFSymbolsResponse: () => SearchUDFSymbolsResponse,
263
263
  SearchUDFSymbolsResponseItem: () => SearchUDFSymbolsResponseItem,
264
+ SignerMismatchError: () => SignerMismatchError,
264
265
  TIMING_CONSTANTS: () => TIMING_CONSTANTS,
265
266
  TX_BASE_FIELDS: () => TX_BASE_FIELDS,
266
267
  TimeoutError: () => TimeoutError,
@@ -636,7 +637,7 @@ __export(index_exports, {
636
637
  module.exports = __toCommonJS(index_exports);
637
638
 
638
639
  // src/core/user-agent.ts
639
- var SDK_VERSION = "0.17.0";
640
+ var SDK_VERSION = "0.17.1";
640
641
  var USER_AGENT = `ault-sdk-ts/${SDK_VERSION}`;
641
642
  function isServerRuntime() {
642
643
  if (typeof document !== "undefined") return false;
@@ -812,6 +813,16 @@ var TxConfirmationUnavailableError = class extends TxError {
812
813
  this.name = "TxConfirmationUnavailableError";
813
814
  }
814
815
  };
816
+ var SignerMismatchError = class extends Error {
817
+ constructor(params) {
818
+ super(
819
+ params.message ?? `The wallet's signature does not match ${params.signerAddress}` + (params.recoveredAddress ? `: it recovers to ${params.recoveredAddress}.` : ".") + " The wallet signed with another account or hashed the EIP-712 payload differently."
820
+ );
821
+ this.name = "SignerMismatchError";
822
+ this.signerAddress = params.signerAddress;
823
+ this.recoveredAddress = params.recoveredAddress;
824
+ }
825
+ };
815
826
  var MissingMsgResponseError = class extends Error {
816
827
  constructor(typeUrl, txHash) {
817
828
  super(
@@ -34939,6 +34950,8 @@ function createVaultApi(context) {
34939
34950
 
34940
34951
  // src/eip712/sign-and-broadcast.ts
34941
34952
  var import_viem5 = require("viem");
34953
+ var import_accounts2 = require("viem/accounts");
34954
+ var import_secp256k1 = require("@noble/curves/secp256k1");
34942
34955
 
34943
34956
  // src/proto/tx-encode.ts
34944
34957
  var WIRE_VARINT = 0;
@@ -35541,22 +35554,63 @@ async function resolveDynamicFeeAmount(network, gas, feeMarketCache, fetchFn, fe
35541
35554
  const staticI = BigInt(staticFloor);
35542
35555
  return dynamicI > staticI ? dynamicI.toString() : staticFloor;
35543
35556
  }
35544
- async function recoverPubkeyFromTypedDataSignature(typedData, signature) {
35557
+ async function recoverSignerPubkey(typedData, signature, signerAddress) {
35558
+ const signerEvmAddress = isValidEvmAddress(signerAddress) ? signerAddress : aultToEvm(signerAddress);
35559
+ let sigBytes;
35560
+ try {
35561
+ sigBytes = (0, import_viem5.hexToBytes)(signature);
35562
+ } catch {
35563
+ sigBytes = new Uint8Array();
35564
+ }
35565
+ if (sigBytes.length !== 64 && sigBytes.length !== 65) {
35566
+ throw new SignerMismatchError({
35567
+ signerAddress: signerEvmAddress,
35568
+ message: `The wallet returned a ${sigBytes.length}-byte signature for ${signerEvmAddress}; expected 64 or 65 bytes.`
35569
+ });
35570
+ }
35545
35571
  const hash = (0, import_viem5.hashTypedData)({
35546
35572
  domain: typedData.domain,
35547
35573
  types: typedData.types,
35548
35574
  primaryType: typedData.primaryType,
35549
35575
  message: typedData.message
35550
35576
  });
35551
- const publicKey = await (0, import_viem5.recoverPublicKey)({ hash, signature });
35552
- const uncompressedBytes = (0, import_viem5.hexToBytes)(publicKey);
35553
- const x = uncompressedBytes.slice(1, 33);
35554
- const y = uncompressedBytes.slice(33, 65);
35555
- const prefix = y[31] % 2 === 0 ? 2 : 3;
35556
- const compressed = new Uint8Array(33);
35557
- compressed[0] = prefix;
35558
- compressed.set(x, 1);
35559
- return bytesToBase64(compressed);
35577
+ const rs = sigBytes.slice(0, 64);
35578
+ let v = sigBytes[64];
35579
+ if (v === void 0 && rs[32] & 128) {
35580
+ rs[32] &= 127;
35581
+ v = 1;
35582
+ }
35583
+ const r = (0, import_viem5.bytesToHex)(rs.slice(0, 32));
35584
+ const s = (0, import_viem5.bytesToHex)(rs.slice(32));
35585
+ const parities = v === 1 || v === 28 ? [1, 0] : [0, 1];
35586
+ let recoveredAddress;
35587
+ for (const yParity of parities) {
35588
+ let publicKey;
35589
+ try {
35590
+ publicKey = await (0, import_viem5.recoverPublicKey)({ hash, signature: { r, s, yParity } });
35591
+ } catch {
35592
+ continue;
35593
+ }
35594
+ const address = (0, import_accounts2.publicKeyToAddress)(publicKey);
35595
+ recoveredAddress ?? (recoveredAddress = address);
35596
+ if (!addressEquals(address, signerEvmAddress)) continue;
35597
+ const uncompressed = (0, import_viem5.hexToBytes)(publicKey);
35598
+ const compressed = new Uint8Array(33);
35599
+ compressed[0] = uncompressed[64] % 2 === 0 ? 2 : 3;
35600
+ compressed.set(uncompressed.slice(1, 33), 1);
35601
+ return { pubkeyBase64: bytesToBase64(compressed), signature: rs };
35602
+ }
35603
+ throw new SignerMismatchError({ signerAddress: signerEvmAddress, recoveredAddress });
35604
+ }
35605
+ function pubkeyBelongsToSigner(pubkeyBase64, signerAddress) {
35606
+ try {
35607
+ const uncompressed = import_secp256k1.secp256k1.ProjectivePoint.fromHex(base64ToBytes(pubkeyBase64)).toRawBytes(
35608
+ false
35609
+ );
35610
+ return addressEquals((0, import_accounts2.publicKeyToAddress)((0, import_viem5.bytesToHex)(uncompressed)), signerAddress);
35611
+ } catch {
35612
+ return false;
35613
+ }
35560
35614
  }
35561
35615
  function createAccountCache() {
35562
35616
  return { accountNumber: null, pubkeyBase64: null, nextSequence: null };
@@ -35711,6 +35765,9 @@ async function signAndBroadcastEip712({
35711
35765
  throw new Error("signer is required to sign EIP-712 typed data.");
35712
35766
  }
35713
35767
  const resolvedSignerAddress = signerAddress ? isValidEvmAddress(signerAddress) ? evmToAult(signerAddress) : signerAddress : await resolveSignerAddress(resolvedSigner);
35768
+ if (cache?.pubkeyBase64 && !pubkeyBelongsToSigner(cache.pubkeyBase64, resolvedSignerAddress)) {
35769
+ cache.pubkeyBase64 = null;
35770
+ }
35714
35771
  const cacheIsWarm = cache?.accountNumber != null && cache.nextSequence != null;
35715
35772
  const accountInfo = cacheIsWarm ? null : await queryAccount(network, resolvedSignerAddress, fetchFn, fetchOptions);
35716
35773
  if (cache && accountInfo) {
@@ -35772,13 +35829,12 @@ async function signAndBroadcastEip712({
35772
35829
  };
35773
35830
  const typedData = buildEip712TypedData(context, msgs, network.evmChainId);
35774
35831
  const signature = normalizeSignature(await resolvedSigner.signTypedData(typedData));
35775
- let pubkey = cache?.pubkeyBase64 ?? accountInfo?.pubkeyBase64 ?? null;
35776
- if (!pubkey) {
35777
- pubkey = await recoverPubkeyFromTypedDataSignature(typedData, signature);
35778
- if (cache) cache.pubkeyBase64 = pubkey;
35779
- }
35780
- const sigBytes = (0, import_viem5.hexToBytes)(signature);
35781
- const sigWithoutRecovery = sigBytes.slice(0, 64);
35832
+ const { pubkeyBase64: pubkey, signature: sigWithoutRecovery } = await recoverSignerPubkey(
35833
+ typedData,
35834
+ signature,
35835
+ resolvedSignerAddress
35836
+ );
35837
+ if (cache) cache.pubkeyBase64 = pubkey;
35782
35838
  const encodedMessages = msgs.map((msg2) => {
35783
35839
  const encoder = MSG_ENCODERS[msg2.typeUrl];
35784
35840
  if (!encoder) {
@@ -38774,12 +38830,12 @@ function createStaticResolver(optsOrNetwork, legacyManifest) {
38774
38830
  }
38775
38831
 
38776
38832
  // src/agent/attach.ts
38777
- var import_secp256k12 = require("@noble/curves/secp256k1");
38833
+ var import_secp256k13 = require("@noble/curves/secp256k1");
38778
38834
  var import_viem7 = require("viem");
38779
38835
  var import_encoding9 = require("@cosmjs/encoding");
38780
38836
 
38781
38837
  // src/agent/sign-and-broadcast.ts
38782
- var import_secp256k1 = require("@noble/curves/secp256k1");
38838
+ var import_secp256k12 = require("@noble/curves/secp256k1");
38783
38839
  var import_viem6 = require("viem");
38784
38840
  var AGENT_DEFAULTS = {
38785
38841
  GAS_LIMIT: "186000",
@@ -38790,7 +38846,7 @@ var AGENT_DEFAULTS = {
38790
38846
  function computeAgentKeyMaterial(agentPrivateKey) {
38791
38847
  const normalized = agentPrivateKey.startsWith("0x") ? agentPrivateKey : `0x${agentPrivateKey}`;
38792
38848
  const privKeyBytes = (0, import_viem6.hexToBytes)(normalized);
38793
- const compressedPubkey = import_secp256k1.secp256k1.getPublicKey(privKeyBytes, true);
38849
+ const compressedPubkey = import_secp256k12.secp256k1.getPublicKey(privKeyBytes, true);
38794
38850
  return { privKeyBytes, compressedPubkey };
38795
38851
  }
38796
38852
  function computeTimeoutTimestamp(timeoutSeconds) {
@@ -38871,7 +38927,7 @@ async function signAndBroadcastAgentTx({
38871
38927
  accountNumber: BigInt(accountNumber)
38872
38928
  });
38873
38929
  const hash = (0, import_viem6.hexToBytes)((0, import_viem6.keccak256)(signDocBytes));
38874
- const sig = import_secp256k1.secp256k1.sign(hash, privKeyBytes);
38930
+ const sig = import_secp256k12.secp256k1.sign(hash, privKeyBytes);
38875
38931
  const signatureBytes = sig.toCompactRawBytes();
38876
38932
  const txRawBytes = encodeTxRaw({
38877
38933
  bodyBytes,
@@ -39365,7 +39421,7 @@ function deriveAgentAddress(privateKeyHex) {
39365
39421
  const privKeyBytes = (0, import_viem7.hexToBytes)(
39366
39422
  privateKeyHex.startsWith("0x") ? privateKeyHex : `0x${privateKeyHex}`
39367
39423
  );
39368
- const uncompressedPubkey = import_secp256k12.secp256k1.getPublicKey(privKeyBytes, false);
39424
+ const uncompressedPubkey = import_secp256k13.secp256k1.getPublicKey(privKeyBytes, false);
39369
39425
  const pubkeyHash = (0, import_viem7.hexToBytes)((0, import_viem7.keccak256)(uncompressedPubkey.slice(1)));
39370
39426
  const evmAddressBytes = pubkeyHash.slice(12);
39371
39427
  const evmHex = `0x${(0, import_encoding9.toHex)(evmAddressBytes)}`;
@@ -42953,6 +43009,7 @@ function createDexWs(url, options) {
42953
43009
  SearchUDFSymbolsQueryParams,
42954
43010
  SearchUDFSymbolsResponse,
42955
43011
  SearchUDFSymbolsResponseItem,
43012
+ SignerMismatchError,
42956
43013
  TIMING_CONSTANTS,
42957
43014
  TX_BASE_FIELDS,
42958
43015
  TimeoutError,