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/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.17.0";
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, sha256 as sha2562 } 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 };
@@ -35080,6 +35133,9 @@ async function signAndBroadcastEip712({
35080
35133
  throw new Error("signer is required to sign EIP-712 typed data.");
35081
35134
  }
35082
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
+ }
35083
35139
  const cacheIsWarm = cache?.accountNumber != null && cache.nextSequence != null;
35084
35140
  const accountInfo = cacheIsWarm ? null : await queryAccount(network, resolvedSignerAddress, fetchFn, fetchOptions);
35085
35141
  if (cache && accountInfo) {
@@ -35141,13 +35197,12 @@ async function signAndBroadcastEip712({
35141
35197
  };
35142
35198
  const typedData = buildEip712TypedData(context, msgs, network.evmChainId);
35143
35199
  const signature = normalizeSignature(await resolvedSigner.signTypedData(typedData));
35144
- let pubkey = cache?.pubkeyBase64 ?? accountInfo?.pubkeyBase64 ?? null;
35145
- if (!pubkey) {
35146
- pubkey = await recoverPubkeyFromTypedDataSignature(typedData, signature);
35147
- if (cache) cache.pubkeyBase64 = pubkey;
35148
- }
35149
- const sigBytes = hexToBytes(signature);
35150
- 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;
35151
35206
  const encodedMessages = msgs.map((msg2) => {
35152
35207
  const encoder = MSG_ENCODERS[msg2.typeUrl];
35153
35208
  if (!encoder) {
@@ -38143,12 +38198,12 @@ function createStaticResolver(optsOrNetwork, legacyManifest) {
38143
38198
  }
38144
38199
 
38145
38200
  // src/agent/attach.ts
38146
- import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
38201
+ import { secp256k1 as secp256k13 } from "@noble/curves/secp256k1";
38147
38202
  import { keccak256 as keccak2562, hexToBytes as hexToBytes4 } from "viem";
38148
38203
  import { toHex as toHex5 } from "@cosmjs/encoding";
38149
38204
 
38150
38205
  // src/agent/sign-and-broadcast.ts
38151
- import { secp256k1 } from "@noble/curves/secp256k1";
38206
+ import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
38152
38207
  import { keccak256, hexToBytes as hexToBytes3 } from "viem";
38153
38208
  var AGENT_DEFAULTS = {
38154
38209
  GAS_LIMIT: "186000",
@@ -38159,7 +38214,7 @@ var AGENT_DEFAULTS = {
38159
38214
  function computeAgentKeyMaterial(agentPrivateKey) {
38160
38215
  const normalized = agentPrivateKey.startsWith("0x") ? agentPrivateKey : `0x${agentPrivateKey}`;
38161
38216
  const privKeyBytes = hexToBytes3(normalized);
38162
- const compressedPubkey = secp256k1.getPublicKey(privKeyBytes, true);
38217
+ const compressedPubkey = secp256k12.getPublicKey(privKeyBytes, true);
38163
38218
  return { privKeyBytes, compressedPubkey };
38164
38219
  }
38165
38220
  function computeTimeoutTimestamp(timeoutSeconds) {
@@ -38240,7 +38295,7 @@ async function signAndBroadcastAgentTx({
38240
38295
  accountNumber: BigInt(accountNumber)
38241
38296
  });
38242
38297
  const hash = hexToBytes3(keccak256(signDocBytes));
38243
- const sig = secp256k1.sign(hash, privKeyBytes);
38298
+ const sig = secp256k12.sign(hash, privKeyBytes);
38244
38299
  const signatureBytes = sig.toCompactRawBytes();
38245
38300
  const txRawBytes = encodeTxRaw({
38246
38301
  bodyBytes,
@@ -38734,7 +38789,7 @@ function deriveAgentAddress(privateKeyHex) {
38734
38789
  const privKeyBytes = hexToBytes4(
38735
38790
  privateKeyHex.startsWith("0x") ? privateKeyHex : `0x${privateKeyHex}`
38736
38791
  );
38737
- const uncompressedPubkey = secp256k12.getPublicKey(privKeyBytes, false);
38792
+ const uncompressedPubkey = secp256k13.getPublicKey(privKeyBytes, false);
38738
38793
  const pubkeyHash = hexToBytes4(keccak2562(uncompressedPubkey.slice(1)));
38739
38794
  const evmAddressBytes = pubkeyHash.slice(12);
38740
38795
  const evmHex = `0x${toHex5(evmAddressBytes)}`;
@@ -42321,6 +42376,7 @@ export {
42321
42376
  SearchUDFSymbolsQueryParams,
42322
42377
  SearchUDFSymbolsResponse,
42323
42378
  SearchUDFSymbolsResponseItem,
42379
+ SignerMismatchError,
42324
42380
  TIMING_CONSTANTS,
42325
42381
  TX_BASE_FIELDS,
42326
42382
  TimeoutError,