@unicitylabs/sphere-sdk 0.7.1 → 0.7.2

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
@@ -894,7 +894,6 @@ var init_network = __esm({
894
894
  // index.ts
895
895
  var index_exports = {};
896
896
  __export(index_exports, {
897
- AuthVerificationError: () => AuthVerificationError,
898
897
  COIN_TYPES: () => COIN_TYPES,
899
898
  CoinGeckoPriceProvider: () => CoinGeckoPriceProvider,
900
899
  CommunicationsModule: () => CommunicationsModule,
@@ -945,7 +944,6 @@ __export(index_exports, {
945
944
  bytesToHex: () => bytesToHex3,
946
945
  checkNetworkHealth: () => checkNetworkHealth,
947
946
  coinIdsMatch: () => coinIdsMatch,
948
- computeDirectAddressFromChainPubkey: () => computeDirectAddressFromChainPubkey,
949
947
  computeSwapId: () => computeSwapId,
950
948
  countCommittedTransactions: () => countCommittedTransactions,
951
949
  createAddress: () => createAddress,
@@ -1058,6 +1056,7 @@ __export(index_exports, {
1058
1056
  randomBytes: () => randomBytes2,
1059
1057
  randomHex: () => randomHex,
1060
1058
  randomUUID: () => randomUUID,
1059
+ recoverPubkeyFromSignature: () => recoverPubkeyFromSignature,
1061
1060
  ripemd160: () => ripemd160,
1062
1061
  sha256: () => sha2562,
1063
1062
  signMessage: () => signMessage,
@@ -1075,7 +1074,6 @@ __export(index_exports, {
1075
1074
  verifyManifestIntegrity: () => verifyManifestIntegrity,
1076
1075
  verifyNametagBinding: () => verifyNametagBinding,
1077
1076
  verifySignedMessage: () => verifySignedMessage,
1078
- verifySphereAuth: () => verifySphereAuth,
1079
1077
  verifySwapSignature: () => verifySwapSignature
1080
1078
  });
1081
1079
  module.exports = __toCommonJS(index_exports);
@@ -5111,6 +5109,27 @@ function verifySignedMessage(message, signature, expectedPubkey) {
5111
5109
  return false;
5112
5110
  }
5113
5111
  }
5112
+ function recoverPubkeyFromSignature(message, signature) {
5113
+ if (signature.length !== 130) {
5114
+ throw new SphereError(
5115
+ `Invalid signature length: expected 130 hex chars, got ${signature.length}`,
5116
+ "SIGNING_ERROR"
5117
+ );
5118
+ }
5119
+ const v = parseInt(signature.slice(0, 2), 16) - 31;
5120
+ const r = signature.slice(2, 66);
5121
+ const s = signature.slice(66, 130);
5122
+ if (v < 0 || v > 3) {
5123
+ throw new SphereError(
5124
+ `Invalid recovery byte: v=${v} out of range [0..3]`,
5125
+ "SIGNING_ERROR"
5126
+ );
5127
+ }
5128
+ const hashHex = hashSignMessage(message);
5129
+ const hashBytes = Buffer.from(hashHex, "hex");
5130
+ const recovered = ec.recoverPubKey(hashBytes, { r, s }, v);
5131
+ return recovered.encode("hex", true);
5132
+ }
5114
5133
 
5115
5134
  // l1/crypto.ts
5116
5135
  var import_crypto_js3 = __toESM(require("crypto-js"), 1);
@@ -27811,42 +27830,27 @@ async function parseAndDecryptWalletDat(data, password, onProgress) {
27811
27830
 
27812
27831
  // core/Sphere.ts
27813
27832
  var import_SigningService2 = require("@unicitylabs/state-transition-sdk/lib/sign/SigningService");
27814
- var import_nostr_js_sdk5 = require("@unicitylabs/nostr-js-sdk");
27815
-
27816
- // core/address-derivation.ts
27817
27833
  var import_TokenType5 = require("@unicitylabs/state-transition-sdk/lib/token/TokenType");
27818
27834
  var import_HashAlgorithm7 = require("@unicitylabs/state-transition-sdk/lib/hash/HashAlgorithm");
27819
27835
  var import_UnmaskedPredicateReference3 = require("@unicitylabs/state-transition-sdk/lib/predicate/embedded/UnmaskedPredicateReference");
27820
- var UNICITY_TOKEN_TYPE_HEX2 = "f8aa13834268d29355ff12183066f0cb902003629bbc5eb9ef0efbe397867509";
27821
- var COMPRESSED_PUBKEY_RE = /^(02|03)[0-9a-fA-F]{64}$/;
27822
- async function computeDirectAddressFromChainPubkey(chainPubkey) {
27823
- if (typeof chainPubkey !== "string" || !COMPRESSED_PUBKEY_RE.test(chainPubkey)) {
27824
- throw new Error(
27825
- `computeDirectAddressFromChainPubkey: chainPubkey must be 66-char hex with 02/03 prefix, got "${String(chainPubkey).slice(0, 12)}..."`
27826
- );
27827
- }
27828
- const tokenTypeBytes = Buffer.from(UNICITY_TOKEN_TYPE_HEX2, "hex");
27829
- const tokenType = new import_TokenType5.TokenType(tokenTypeBytes);
27830
- const publicKeyBytes = Buffer.from(chainPubkey, "hex");
27831
- const predicateRef = await import_UnmaskedPredicateReference3.UnmaskedPredicateReference.create(
27832
- tokenType,
27833
- "secp256k1",
27834
- publicKeyBytes,
27835
- import_HashAlgorithm7.HashAlgorithm.SHA256
27836
- );
27837
- return (await predicateRef.toAddress()).toString();
27838
- }
27839
-
27840
- // core/Sphere.ts
27836
+ var import_nostr_js_sdk5 = require("@unicitylabs/nostr-js-sdk");
27841
27837
  function isValidNametag2(nametag) {
27842
27838
  if ((0, import_nostr_js_sdk5.isPhoneNumber)(nametag)) return true;
27843
27839
  return /^[a-z0-9_-]{3,20}$/.test(nametag);
27844
27840
  }
27841
+ var UNICITY_TOKEN_TYPE_HEX2 = "f8aa13834268d29355ff12183066f0cb902003629bbc5eb9ef0efbe397867509";
27845
27842
  async function deriveL3PredicateAddress(privateKey) {
27846
27843
  const secret = Buffer.from(privateKey, "hex");
27847
27844
  const signingService = await import_SigningService2.SigningService.createFromSecret(secret);
27848
- const pubkeyHex = Buffer.from(signingService.publicKey).toString("hex");
27849
- return computeDirectAddressFromChainPubkey(pubkeyHex);
27845
+ const tokenTypeBytes = Buffer.from(UNICITY_TOKEN_TYPE_HEX2, "hex");
27846
+ const tokenType = new import_TokenType5.TokenType(tokenTypeBytes);
27847
+ const predicateRef = import_UnmaskedPredicateReference3.UnmaskedPredicateReference.create(
27848
+ tokenType,
27849
+ signingService.algorithm,
27850
+ signingService.publicKey,
27851
+ import_HashAlgorithm7.HashAlgorithm.SHA256
27852
+ );
27853
+ return (await (await predicateRef).toAddress()).toString();
27850
27854
  }
27851
27855
  var Sphere = class _Sphere {
27852
27856
  // Singleton
@@ -31857,38 +31861,8 @@ function createPriceProvider(config) {
31857
31861
  throw new SphereError(`Unsupported price platform: ${String(config.platform)}`, "INVALID_CONFIG");
31858
31862
  }
31859
31863
  }
31860
-
31861
- // core/auth.ts
31862
- var AuthVerificationError = class extends Error {
31863
- code;
31864
- constructor(message, code) {
31865
- super(message);
31866
- this.name = "AuthVerificationError";
31867
- this.code = code;
31868
- }
31869
- };
31870
- async function verifySphereAuth(input) {
31871
- const { challenge, signature, chainPubkey } = input;
31872
- let directAddress;
31873
- try {
31874
- directAddress = await computeDirectAddressFromChainPubkey(chainPubkey);
31875
- } catch (err) {
31876
- throw new AuthVerificationError(
31877
- `chainPubkey is malformed: ${err.message}`,
31878
- "PUBKEY_MALFORMED"
31879
- );
31880
- }
31881
- if (!verifySignedMessage(challenge, signature, chainPubkey)) {
31882
- throw new AuthVerificationError(
31883
- "Signature does not verify against chainPubkey",
31884
- "SIGNATURE_INVALID"
31885
- );
31886
- }
31887
- return { chainPubkey, directAddress };
31888
- }
31889
31864
  // Annotate the CommonJS export names for ESM import in node:
31890
31865
  0 && (module.exports = {
31891
- AuthVerificationError,
31892
31866
  COIN_TYPES,
31893
31867
  CoinGeckoPriceProvider,
31894
31868
  CommunicationsModule,
@@ -31939,7 +31913,6 @@ async function verifySphereAuth(input) {
31939
31913
  bytesToHex,
31940
31914
  checkNetworkHealth,
31941
31915
  coinIdsMatch,
31942
- computeDirectAddressFromChainPubkey,
31943
31916
  computeSwapId,
31944
31917
  countCommittedTransactions,
31945
31918
  createAddress,
@@ -32052,6 +32025,7 @@ async function verifySphereAuth(input) {
32052
32025
  randomBytes,
32053
32026
  randomHex,
32054
32027
  randomUUID,
32028
+ recoverPubkeyFromSignature,
32055
32029
  ripemd160,
32056
32030
  sha256,
32057
32031
  signMessage,
@@ -32069,7 +32043,6 @@ async function verifySphereAuth(input) {
32069
32043
  verifyManifestIntegrity,
32070
32044
  verifyNametagBinding,
32071
32045
  verifySignedMessage,
32072
- verifySphereAuth,
32073
32046
  verifySwapSignature
32074
32047
  });
32075
32048
  /*! Bundled license information: