ault-sdk-ts 0.16.1 → 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/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/core/user-agent.ts
8
- var SDK_VERSION = "0.16.1";
8
+ var SDK_VERSION = "0.17.1";
9
9
  var USER_AGENT = `ault-sdk-ts/${SDK_VERSION}`;
10
10
  function isServerRuntime() {
11
11
  if (typeof document !== "undefined") return false;
@@ -181,6 +181,16 @@ var TxConfirmationUnavailableError = class extends TxError {
181
181
  this.name = "TxConfirmationUnavailableError";
182
182
  }
183
183
  };
184
+ var SignerMismatchError = class extends Error {
185
+ constructor(params) {
186
+ super(
187
+ 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."
188
+ );
189
+ this.name = "SignerMismatchError";
190
+ this.signerAddress = params.signerAddress;
191
+ this.recoveredAddress = params.recoveredAddress;
192
+ }
193
+ };
184
194
  var MissingMsgResponseError = class extends Error {
185
195
  constructor(typeUrl, txHash) {
186
196
  super(
@@ -34307,7 +34317,9 @@ function createVaultApi(context) {
34307
34317
  }
34308
34318
 
34309
34319
  // src/eip712/sign-and-broadcast.ts
34310
- import { hexToBytes, hashTypedData, recoverPublicKey } from "viem";
34320
+ import { bytesToHex, hexToBytes, hashTypedData, recoverPublicKey, sha256 as sha2562 } from "viem";
34321
+ import { publicKeyToAddress } from "viem/accounts";
34322
+ import { secp256k1 } from "@noble/curves/secp256k1";
34311
34323
 
34312
34324
  // src/proto/tx-encode.ts
34313
34325
  var WIRE_VARINT = 0;
@@ -34910,22 +34922,63 @@ async function resolveDynamicFeeAmount(network, gas, feeMarketCache, fetchFn, fe
34910
34922
  const staticI = BigInt(staticFloor);
34911
34923
  return dynamicI > staticI ? dynamicI.toString() : staticFloor;
34912
34924
  }
34913
- async function recoverPubkeyFromTypedDataSignature(typedData, signature) {
34925
+ async function recoverSignerPubkey(typedData, signature, signerAddress) {
34926
+ const signerEvmAddress = isValidEvmAddress(signerAddress) ? signerAddress : aultToEvm(signerAddress);
34927
+ let sigBytes;
34928
+ try {
34929
+ sigBytes = hexToBytes(signature);
34930
+ } catch {
34931
+ sigBytes = new Uint8Array();
34932
+ }
34933
+ if (sigBytes.length !== 64 && sigBytes.length !== 65) {
34934
+ throw new SignerMismatchError({
34935
+ signerAddress: signerEvmAddress,
34936
+ message: `The wallet returned a ${sigBytes.length}-byte signature for ${signerEvmAddress}; expected 64 or 65 bytes.`
34937
+ });
34938
+ }
34914
34939
  const hash = hashTypedData({
34915
34940
  domain: typedData.domain,
34916
34941
  types: typedData.types,
34917
34942
  primaryType: typedData.primaryType,
34918
34943
  message: typedData.message
34919
34944
  });
34920
- const publicKey = await recoverPublicKey({ hash, signature });
34921
- const uncompressedBytes = hexToBytes(publicKey);
34922
- const x = uncompressedBytes.slice(1, 33);
34923
- const y = uncompressedBytes.slice(33, 65);
34924
- const prefix = y[31] % 2 === 0 ? 2 : 3;
34925
- const compressed = new Uint8Array(33);
34926
- compressed[0] = prefix;
34927
- compressed.set(x, 1);
34928
- return bytesToBase64(compressed);
34945
+ const rs = sigBytes.slice(0, 64);
34946
+ let v = sigBytes[64];
34947
+ if (v === void 0 && rs[32] & 128) {
34948
+ rs[32] &= 127;
34949
+ v = 1;
34950
+ }
34951
+ const r = bytesToHex(rs.slice(0, 32));
34952
+ const s = bytesToHex(rs.slice(32));
34953
+ const parities = v === 1 || v === 28 ? [1, 0] : [0, 1];
34954
+ let recoveredAddress;
34955
+ for (const yParity of parities) {
34956
+ let publicKey;
34957
+ try {
34958
+ publicKey = await recoverPublicKey({ hash, signature: { r, s, yParity } });
34959
+ } catch {
34960
+ continue;
34961
+ }
34962
+ const address = publicKeyToAddress(publicKey);
34963
+ recoveredAddress ?? (recoveredAddress = address);
34964
+ if (!addressEquals(address, signerEvmAddress)) continue;
34965
+ const uncompressed = hexToBytes(publicKey);
34966
+ const compressed = new Uint8Array(33);
34967
+ compressed[0] = uncompressed[64] % 2 === 0 ? 2 : 3;
34968
+ compressed.set(uncompressed.slice(1, 33), 1);
34969
+ return { pubkeyBase64: bytesToBase64(compressed), signature: rs };
34970
+ }
34971
+ throw new SignerMismatchError({ signerAddress: signerEvmAddress, recoveredAddress });
34972
+ }
34973
+ function pubkeyBelongsToSigner(pubkeyBase64, signerAddress) {
34974
+ try {
34975
+ const uncompressed = secp256k1.ProjectivePoint.fromHex(base64ToBytes(pubkeyBase64)).toRawBytes(
34976
+ false
34977
+ );
34978
+ return addressEquals(publicKeyToAddress(bytesToHex(uncompressed)), signerAddress);
34979
+ } catch {
34980
+ return false;
34981
+ }
34929
34982
  }
34930
34983
  function createAccountCache() {
34931
34984
  return { accountNumber: null, pubkeyBase64: null, nextSequence: null };
@@ -34963,6 +35016,34 @@ function getConfirmationUnavailableLog(txHash, error) {
34963
35016
  }
34964
35017
  return `Transaction ${txHash} was broadcast but could not be confirmed.`;
34965
35018
  }
35019
+ async function broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions) {
35020
+ const txHash = normalizeTxHash(sha2562(txRawBytes));
35021
+ try {
35022
+ const result = await postJson(
35023
+ `${network.restUrl}/cosmos/tx/v1beta1/txs`,
35024
+ { tx_bytes: bytesToBase64(txRawBytes), mode: "BROADCAST_MODE_SYNC" },
35025
+ { ...fetchOptions, retries: 0 },
35026
+ fetchFn
35027
+ );
35028
+ const response = result?.tx_response;
35029
+ const code = response?.code;
35030
+ const rawHash = response?.txhash;
35031
+ if (typeof code !== "number" || !Number.isSafeInteger(code) || code < 0 || typeof rawHash !== "string" || !rawHash) {
35032
+ throw new Error("Unrecognized broadcast response");
35033
+ }
35034
+ if (normalizeTxHash(rawHash) !== txHash) {
35035
+ throw new Error("Broadcast response is for a different transaction");
35036
+ }
35037
+ return { txHash, code, rawLog: response?.raw_log };
35038
+ } catch (error) {
35039
+ throw new TxConfirmationUnavailableError({
35040
+ txHash,
35041
+ code: -1,
35042
+ rawLog: error instanceof Error ? error.message : "Broadcast response unavailable",
35043
+ confirmed: void 0
35044
+ });
35045
+ }
35046
+ }
34966
35047
  function _resetIndexingCache() {
34967
35048
  }
34968
35049
  async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs = TIMING_CONSTANTS.TX_POLL_TIMEOUT_MS, intervalMs = 1500) {
@@ -34979,7 +35060,8 @@ async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs
34979
35060
  { ...fetchOptions, retries: 0, timeout: perRequestTimeout },
34980
35061
  fetchFn
34981
35062
  );
34982
- if (typeof result.tx_response?.code !== "number") {
35063
+ const receipt = result?.tx_response;
35064
+ if (!receipt || !Number.isSafeInteger(receipt.code) || receipt.code < 0 || typeof receipt.txhash !== "string" || normalizeTxHash(receipt.txhash) !== normalizeTxHash(txHash) || typeof receipt.height !== "string" || !/^\d+$/.test(receipt.height) || BigInt(receipt.height) <= 0n) {
34983
35065
  return {
34984
35066
  code: -1,
34985
35067
  rawLog: `Transaction ${txHash} was broadcast but the confirmation response was invalid.`,
@@ -34987,9 +35069,9 @@ async function pollTxInclusion(network, txHash, fetchFn, fetchOptions, timeoutMs
34987
35069
  };
34988
35070
  }
34989
35071
  return {
34990
- code: result.tx_response.code,
34991
- rawLog: result.tx_response.raw_log,
34992
- data: result.tx_response.data,
35072
+ code: receipt.code,
35073
+ rawLog: receipt.raw_log,
35074
+ data: receipt.data,
34993
35075
  confirmed: true
34994
35076
  };
34995
35077
  } catch (error) {
@@ -35051,6 +35133,9 @@ async function signAndBroadcastEip712({
35051
35133
  throw new Error("signer is required to sign EIP-712 typed data.");
35052
35134
  }
35053
35135
  const resolvedSignerAddress = signerAddress ? isValidEvmAddress(signerAddress) ? evmToAult(signerAddress) : signerAddress : await resolveSignerAddress(resolvedSigner);
35136
+ if (cache?.pubkeyBase64 && !pubkeyBelongsToSigner(cache.pubkeyBase64, resolvedSignerAddress)) {
35137
+ cache.pubkeyBase64 = null;
35138
+ }
35054
35139
  const cacheIsWarm = cache?.accountNumber != null && cache.nextSequence != null;
35055
35140
  const accountInfo = cacheIsWarm ? null : await queryAccount(network, resolvedSignerAddress, fetchFn, fetchOptions);
35056
35141
  if (cache && accountInfo) {
@@ -35112,13 +35197,12 @@ async function signAndBroadcastEip712({
35112
35197
  };
35113
35198
  const typedData = buildEip712TypedData(context, msgs, network.evmChainId);
35114
35199
  const signature = normalizeSignature(await resolvedSigner.signTypedData(typedData));
35115
- let pubkey = cache?.pubkeyBase64 ?? accountInfo?.pubkeyBase64 ?? null;
35116
- if (!pubkey) {
35117
- pubkey = await recoverPubkeyFromTypedDataSignature(typedData, signature);
35118
- if (cache) cache.pubkeyBase64 = pubkey;
35119
- }
35120
- const sigBytes = hexToBytes(signature);
35121
- const sigWithoutRecovery = sigBytes.slice(0, 64);
35200
+ const { pubkeyBase64: pubkey, signature: sigWithoutRecovery } = await recoverSignerPubkey(
35201
+ typedData,
35202
+ signature,
35203
+ resolvedSignerAddress
35204
+ );
35205
+ if (cache) cache.pubkeyBase64 = pubkey;
35122
35206
  const encodedMessages = msgs.map((msg2) => {
35123
35207
  const encoder = MSG_ENCODERS[msg2.typeUrl];
35124
35208
  if (!encoder) {
@@ -35147,26 +35231,20 @@ async function signAndBroadcastEip712({
35147
35231
  authInfoBytes,
35148
35232
  signatures: [sigWithoutRecovery]
35149
35233
  });
35150
- const txBytesBase64 = bytesToBase64(txRawBytes);
35151
- let result;
35234
+ let checkTx;
35152
35235
  try {
35153
- result = await postJson(
35154
- `${network.restUrl}/cosmos/tx/v1beta1/txs`,
35155
- { tx_bytes: txBytesBase64, mode: "BROADCAST_MODE_SYNC" },
35156
- { ...fetchOptions, retries: 0 },
35157
- fetchFn
35158
- );
35236
+ checkTx = await broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions);
35159
35237
  } catch (error) {
35160
35238
  clearCachedSequence(cache);
35161
35239
  throw error;
35162
35240
  }
35163
- const txHash = normalizeTxHash(result.tx_response.txhash);
35164
- if (result.tx_response.code !== 0) {
35241
+ const { txHash } = checkTx;
35242
+ if (checkTx.code !== 0) {
35165
35243
  clearCachedSequence(cache);
35166
35244
  throw new TxError({
35167
35245
  txHash,
35168
- code: result.tx_response.code,
35169
- rawLog: result.tx_response.raw_log,
35246
+ code: checkTx.code,
35247
+ rawLog: checkTx.rawLog,
35170
35248
  confirmed: true
35171
35249
  });
35172
35250
  }
@@ -35175,7 +35253,7 @@ async function signAndBroadcastEip712({
35175
35253
  return {
35176
35254
  txHash,
35177
35255
  code: 0,
35178
- rawLog: result.tx_response.raw_log,
35256
+ rawLog: checkTx.rawLog,
35179
35257
  confirmed: false
35180
35258
  };
35181
35259
  }
@@ -35197,7 +35275,7 @@ async function signAndBroadcastEip712({
35197
35275
  throw new TxConfirmationUnavailableError({
35198
35276
  txHash,
35199
35277
  code: deliverResult.code,
35200
- rawLog: deliverResult.rawLog ?? result.tx_response.raw_log,
35278
+ rawLog: deliverResult.rawLog ?? checkTx.rawLog,
35201
35279
  confirmed: void 0
35202
35280
  });
35203
35281
  }
@@ -38120,12 +38198,12 @@ function createStaticResolver(optsOrNetwork, legacyManifest) {
38120
38198
  }
38121
38199
 
38122
38200
  // src/agent/attach.ts
38123
- import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
38201
+ import { secp256k1 as secp256k13 } from "@noble/curves/secp256k1";
38124
38202
  import { keccak256 as keccak2562, hexToBytes as hexToBytes4 } from "viem";
38125
38203
  import { toHex as toHex5 } from "@cosmjs/encoding";
38126
38204
 
38127
38205
  // src/agent/sign-and-broadcast.ts
38128
- import { secp256k1 } from "@noble/curves/secp256k1";
38206
+ import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
38129
38207
  import { keccak256, hexToBytes as hexToBytes3 } from "viem";
38130
38208
  var AGENT_DEFAULTS = {
38131
38209
  GAS_LIMIT: "186000",
@@ -38136,7 +38214,7 @@ var AGENT_DEFAULTS = {
38136
38214
  function computeAgentKeyMaterial(agentPrivateKey) {
38137
38215
  const normalized = agentPrivateKey.startsWith("0x") ? agentPrivateKey : `0x${agentPrivateKey}`;
38138
38216
  const privKeyBytes = hexToBytes3(normalized);
38139
- const compressedPubkey = secp256k1.getPublicKey(privKeyBytes, true);
38217
+ const compressedPubkey = secp256k12.getPublicKey(privKeyBytes, true);
38140
38218
  return { privKeyBytes, compressedPubkey };
38141
38219
  }
38142
38220
  function computeTimeoutTimestamp(timeoutSeconds) {
@@ -38217,26 +38295,20 @@ async function signAndBroadcastAgentTx({
38217
38295
  accountNumber: BigInt(accountNumber)
38218
38296
  });
38219
38297
  const hash = hexToBytes3(keccak256(signDocBytes));
38220
- const sig = secp256k1.sign(hash, privKeyBytes);
38298
+ const sig = secp256k12.sign(hash, privKeyBytes);
38221
38299
  const signatureBytes = sig.toCompactRawBytes();
38222
38300
  const txRawBytes = encodeTxRaw({
38223
38301
  bodyBytes,
38224
38302
  authInfoBytes,
38225
38303
  signatures: [signatureBytes]
38226
38304
  });
38227
- const txBytesBase64 = bytesToBase64(txRawBytes);
38228
- const result = await postJson(
38229
- `${network.restUrl}/cosmos/tx/v1beta1/txs`,
38230
- { tx_bytes: txBytesBase64, mode: "BROADCAST_MODE_SYNC" },
38231
- fetchOptions,
38232
- fetchFn
38233
- );
38234
- const txHash = normalizeTxHash(result.tx_response.txhash);
38235
- if (result.tx_response.code !== 0) {
38305
+ const checkTx = await broadcastSignedTx(network, txRawBytes, fetchFn, fetchOptions);
38306
+ const { txHash } = checkTx;
38307
+ if (checkTx.code !== 0) {
38236
38308
  throw new TxError({
38237
38309
  txHash,
38238
- code: result.tx_response.code,
38239
- rawLog: result.tx_response.raw_log,
38310
+ code: checkTx.code,
38311
+ rawLog: checkTx.rawLog,
38240
38312
  confirmed: true
38241
38313
  });
38242
38314
  }
@@ -38244,7 +38316,7 @@ async function signAndBroadcastAgentTx({
38244
38316
  return {
38245
38317
  txHash,
38246
38318
  code: 0,
38247
- rawLog: result.tx_response.raw_log,
38319
+ rawLog: checkTx.rawLog,
38248
38320
  confirmed: false
38249
38321
  };
38250
38322
  }
@@ -38259,7 +38331,7 @@ async function signAndBroadcastAgentTx({
38259
38331
  throw new TxConfirmationUnavailableError({
38260
38332
  txHash,
38261
38333
  code: deliverResult.code,
38262
- rawLog: deliverResult.rawLog ?? result.tx_response.raw_log,
38334
+ rawLog: deliverResult.rawLog ?? checkTx.rawLog,
38263
38335
  confirmed: void 0
38264
38336
  });
38265
38337
  }
@@ -38717,7 +38789,7 @@ function deriveAgentAddress(privateKeyHex) {
38717
38789
  const privKeyBytes = hexToBytes4(
38718
38790
  privateKeyHex.startsWith("0x") ? privateKeyHex : `0x${privateKeyHex}`
38719
38791
  );
38720
- const uncompressedPubkey = secp256k12.getPublicKey(privKeyBytes, false);
38792
+ const uncompressedPubkey = secp256k13.getPublicKey(privKeyBytes, false);
38721
38793
  const pubkeyHash = hexToBytes4(keccak2562(uncompressedPubkey.slice(1)));
38722
38794
  const evmAddressBytes = pubkeyHash.slice(12);
38723
38795
  const evmHex = `0x${toHex5(evmAddressBytes)}`;
@@ -42304,6 +42376,7 @@ export {
42304
42376
  SearchUDFSymbolsQueryParams,
42305
42377
  SearchUDFSymbolsResponse,
42306
42378
  SearchUDFSymbolsResponseItem,
42379
+ SignerMismatchError,
42307
42380
  TIMING_CONSTANTS,
42308
42381
  TX_BASE_FIELDS,
42309
42382
  TimeoutError,
@@ -42324,6 +42397,7 @@ export {
42324
42397
  aultToEvm,
42325
42398
  averagePremium,
42326
42399
  base64ToBytes,
42400
+ broadcastSignedTx,
42327
42401
  buildBorrowMarginMsg,
42328
42402
  buildConditionalOrderMsg,
42329
42403
  buildDepositMarginMsg,