@medialane/sdk 0.43.0 → 0.44.0

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
@@ -6825,6 +6825,95 @@ function parseAdminHeaders(get) {
6825
6825
  }
6826
6826
  }
6827
6827
 
6828
+ // src/siws/client.ts
6829
+ var STORAGE_PREFIX = "ml_siws_";
6830
+ function decodeBase64url(str) {
6831
+ const base64 = str.replace(/-/g, "+").replace(/_/g, "/");
6832
+ const padding = "=".repeat((4 - base64.length % 4) % 4);
6833
+ return atob(base64 + padding);
6834
+ }
6835
+ function readTokenExpiry(token) {
6836
+ try {
6837
+ if (!token.startsWith("siws_")) return null;
6838
+ const inner = token.slice(5);
6839
+ const dot = inner.lastIndexOf(".");
6840
+ if (dot === -1) return null;
6841
+ const data = JSON.parse(decodeBase64url(inner.slice(0, dot)));
6842
+ return typeof data.exp === "number" ? data.exp : null;
6843
+ } catch {
6844
+ return null;
6845
+ }
6846
+ }
6847
+ function getSiwsStorageKey(address) {
6848
+ return `${STORAGE_PREFIX}${address.toLowerCase()}`;
6849
+ }
6850
+ function isSiwsTokenValid(token) {
6851
+ if (!token?.startsWith("siws_")) return false;
6852
+ const expiry = readTokenExpiry(token);
6853
+ return Boolean(expiry && expiry > Math.floor(Date.now() / 1e3));
6854
+ }
6855
+ function getStoredSiwsToken(address) {
6856
+ if (typeof window === "undefined") return null;
6857
+ const key = getSiwsStorageKey(address);
6858
+ const token = localStorage.getItem(key);
6859
+ if (isSiwsTokenValid(token)) return token;
6860
+ localStorage.removeItem(key);
6861
+ return null;
6862
+ }
6863
+ function storeSiwsToken(address, token) {
6864
+ if (typeof window === "undefined") return;
6865
+ localStorage.setItem(getSiwsStorageKey(address), token);
6866
+ }
6867
+ function normalizeSiwsSignature(signature) {
6868
+ if (Array.isArray(signature)) {
6869
+ return signature.map(String);
6870
+ }
6871
+ if (signature && typeof signature === "object") {
6872
+ const { r, s } = signature;
6873
+ if (r !== void 0 && s !== void 0) {
6874
+ return [String(r), String(s)];
6875
+ }
6876
+ }
6877
+ return [String(signature)];
6878
+ }
6879
+ async function requestSiwsToken({
6880
+ backendUrl,
6881
+ walletAddress,
6882
+ signer
6883
+ }) {
6884
+ const base = backendUrl.replace(/\/$/, "");
6885
+ const nonceRes = await fetch(`${base}/v1/auth/siws/nonce`, {
6886
+ method: "POST",
6887
+ headers: { "Content-Type": "application/json" },
6888
+ body: JSON.stringify({ walletAddress })
6889
+ });
6890
+ if (!nonceRes.ok) {
6891
+ throw new Error("Failed to prepare wallet sign-in");
6892
+ }
6893
+ const { nonce, typedData } = await nonceRes.json();
6894
+ const signature = normalizeSiwsSignature(await signer.signMessage(typedData));
6895
+ const verifyRes = await fetch(`${base}/v1/auth/siws/verify`, {
6896
+ method: "POST",
6897
+ headers: { "Content-Type": "application/json" },
6898
+ body: JSON.stringify({ walletAddress, nonce, signature })
6899
+ });
6900
+ if (!verifyRes.ok) {
6901
+ let backendMessage;
6902
+ try {
6903
+ const body = await verifyRes.json();
6904
+ if (body?.message) backendMessage = body.message;
6905
+ } catch {
6906
+ }
6907
+ throw new Error(backendMessage ?? "Wallet sign-in failed");
6908
+ }
6909
+ const { token } = await verifyRes.json();
6910
+ if (!isSiwsTokenValid(token)) {
6911
+ throw new Error("Wallet sign-in returned an invalid token");
6912
+ }
6913
+ storeSiwsToken(walletAddress, token);
6914
+ return token;
6915
+ }
6916
+
6828
6917
  // src/types/api.ts
6829
6918
  var OPEN_LICENSES = ["CC0", "CC BY", "CC BY-SA", "CC BY-NC"];
6830
6919
 
@@ -7156,22 +7245,28 @@ exports.getCreatorCoinPrice = getCreatorCoinPrice;
7156
7245
  exports.getListableTokens = getListableTokens;
7157
7246
  exports.getService = getService;
7158
7247
  exports.getServicesByCapability = getServicesByCapability;
7248
+ exports.getSiwsStorageKey = getSiwsStorageKey;
7249
+ exports.getStoredSiwsToken = getStoredSiwsToken;
7159
7250
  exports.getTokenByAddress = getTokenByAddress;
7160
7251
  exports.getTokenBySymbol = getTokenBySymbol;
7161
7252
  exports.isServiceId = isServiceId;
7253
+ exports.isSiwsTokenValid = isSiwsTokenValid;
7162
7254
  exports.isTransientRpcError = isTransientRpcError;
7163
7255
  exports.listServices = listServices;
7164
7256
  exports.normalizeAddress = normalizeAddress;
7165
7257
  exports.normalizeHash = normalizeHash;
7258
+ exports.normalizeSiwsSignature = normalizeSiwsSignature;
7166
7259
  exports.parseAdminHeaders = parseAdminHeaders;
7167
7260
  exports.parseAmount = parseAmount;
7168
7261
  exports.parseCreatorCoinCreated = parseCreatorCoinCreated;
7169
7262
  exports.randomNonce = randomNonce;
7263
+ exports.requestSiwsToken = requestSiwsToken;
7170
7264
  exports.resolveConfig = resolveConfig;
7171
7265
  exports.resolveFeeConfig = resolveFeeConfig;
7172
7266
  exports.sessionKeyHashOf = sessionKeyHashOf;
7173
7267
  exports.shortenAddress = shortenAddress;
7174
7268
  exports.signAdminRequest = signAdminRequest;
7269
+ exports.storeSiwsToken = storeSiwsToken;
7175
7270
  exports.stringifyBigInts = stringifyBigInts;
7176
7271
  exports.teamCoinsRaw = teamCoinsRaw;
7177
7272
  exports.u256ToBigInt = u256ToBigInt;