@oasisprotocol/privana-sdk 0.5.9 → 0.5.11
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/{chunk-KCXYYUUG.cjs → chunk-4RHSKIPZ.cjs} +374 -374
- package/dist/chunk-4RHSKIPZ.cjs.map +1 -0
- package/dist/{chunk-RDYNQUMV.js → chunk-KUHXTCVW.js} +374 -374
- package/dist/chunk-KUHXTCVW.js.map +1 -0
- package/dist/index.cjs +348 -290
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +88 -31
- package/dist/index.js.map +1 -1
- package/dist/on-ramp.cjs +5 -5
- package/dist/on-ramp.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-KCXYYUUG.cjs.map +0 -1
- package/dist/chunk-RDYNQUMV.js.map +0 -1
|
@@ -1368,7 +1368,7 @@ async function ctrlHydrateViaRefresh(ctrl, record, address) {
|
|
|
1368
1368
|
}
|
|
1369
1369
|
}
|
|
1370
1370
|
|
|
1371
|
-
// src/sdk/
|
|
1371
|
+
// src/sdk/utils/private-read-token-store.ts
|
|
1372
1372
|
var cache = /* @__PURE__ */ new Map();
|
|
1373
1373
|
function createScopeKey(apiUrl, chainId, address) {
|
|
1374
1374
|
return `${apiUrl.replace(/\/$/, "")}:${chainId}:${address.toLowerCase()}`;
|
|
@@ -1993,395 +1993,129 @@ function useSafePrivanaContext() {
|
|
|
1993
1993
|
return react.useContext(PrivanaContext);
|
|
1994
1994
|
}
|
|
1995
1995
|
|
|
1996
|
-
// src/sdk/
|
|
1997
|
-
function
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
}
|
|
2010
|
-
function canUseBrowserStorage() {
|
|
2011
|
-
const probeKey = "privana:storage-probe";
|
|
2012
|
-
for (const storage of storageCandidates()) {
|
|
1996
|
+
// src/sdk/utils/deposit-finality.ts
|
|
1997
|
+
async function checkDepositWithFinalityRetry({
|
|
1998
|
+
checkDeposit,
|
|
1999
|
+
isStale,
|
|
2000
|
+
onRetry,
|
|
2001
|
+
timeoutMs,
|
|
2002
|
+
retryIntervalMs,
|
|
2003
|
+
startedAt = Date.now(),
|
|
2004
|
+
now = Date.now,
|
|
2005
|
+
sleep = defaultSleep
|
|
2006
|
+
}) {
|
|
2007
|
+
while (true) {
|
|
2008
|
+
if (isStale()) return { kind: "stale" };
|
|
2013
2009
|
try {
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2010
|
+
const response = await checkDeposit();
|
|
2011
|
+
if (!isInsufficientFinalityMessage(response.detail) || response.status !== "error") {
|
|
2012
|
+
return { kind: "response", response };
|
|
2013
|
+
}
|
|
2014
|
+
if (response.detail) onRetry(response.detail);
|
|
2015
|
+
} catch (error) {
|
|
2016
|
+
if (isStale()) return { kind: "stale" };
|
|
2017
|
+
if (!isInsufficientFinalityError(error)) throw error;
|
|
2018
|
+
onRetry(
|
|
2019
|
+
error instanceof AccountingApiError && error.detail ? error.detail : error instanceof Error ? error.message : String(error)
|
|
2020
|
+
);
|
|
2018
2021
|
}
|
|
2022
|
+
if (now() - startedAt > timeoutMs) return { kind: "timeout" };
|
|
2023
|
+
await sleep(retryIntervalMs);
|
|
2019
2024
|
}
|
|
2020
|
-
return false;
|
|
2021
2025
|
}
|
|
2022
|
-
function
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
try {
|
|
2026
|
-
storage.setItem(key, value);
|
|
2027
|
-
stored = true;
|
|
2028
|
-
} catch {
|
|
2029
|
-
}
|
|
2026
|
+
function isInsufficientFinalityError(error) {
|
|
2027
|
+
if (error instanceof AccountingApiError) {
|
|
2028
|
+
return isInsufficientFinalityMessage(error.detail) || isInsufficientFinalityMessage(error.message);
|
|
2030
2029
|
}
|
|
2031
|
-
return
|
|
2030
|
+
return error instanceof Error && isInsufficientFinalityMessage(error.message);
|
|
2032
2031
|
}
|
|
2033
|
-
function
|
|
2034
|
-
|
|
2035
|
-
try {
|
|
2036
|
-
const value = storage.getItem(key);
|
|
2037
|
-
if (value !== null) return value;
|
|
2038
|
-
} catch {
|
|
2039
|
-
}
|
|
2040
|
-
}
|
|
2041
|
-
return null;
|
|
2032
|
+
function isInsufficientFinalityMessage(message) {
|
|
2033
|
+
return message?.includes("Insufficient finality") ?? false;
|
|
2042
2034
|
}
|
|
2043
|
-
function
|
|
2044
|
-
|
|
2045
|
-
try {
|
|
2046
|
-
storage.removeItem(key);
|
|
2047
|
-
} catch {
|
|
2048
|
-
}
|
|
2049
|
-
}
|
|
2035
|
+
function defaultSleep(milliseconds) {
|
|
2036
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
2050
2037
|
}
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2038
|
+
var INITIAL_AUTH_BACKOFF_MS = 5e3;
|
|
2039
|
+
var MAX_AUTH_BACKOFF_MS = 6e4;
|
|
2040
|
+
var privateReadFailureCache = /* @__PURE__ */ new Map();
|
|
2041
|
+
var privateReadInflight = /* @__PURE__ */ new Map();
|
|
2042
|
+
async function executeHostedAuthPrivateReadRequest({
|
|
2043
|
+
client,
|
|
2044
|
+
hostedAuthSession,
|
|
2045
|
+
refreshHostedAuthSession,
|
|
2046
|
+
request
|
|
2047
|
+
}) {
|
|
2048
|
+
const ensureHostedAuth = async (forceRefresh) => {
|
|
2049
|
+
if (!hostedAuthSession) {
|
|
2050
|
+
throw new HostedAuthRequiredError();
|
|
2051
|
+
}
|
|
2052
|
+
if (!forceRefresh && isHostedAuthSessionActive(hostedAuthSession)) {
|
|
2053
|
+
client.clearPrivateReadToken();
|
|
2054
|
+
client.setBearerToken(hostedAuthSession.accessToken);
|
|
2055
|
+
return hostedAuthSession.accessToken;
|
|
2056
|
+
}
|
|
2057
|
+
const refreshed = await refreshHostedAuthSession();
|
|
2058
|
+
client.clearPrivateReadToken();
|
|
2059
|
+
client.setBearerToken(refreshed.accessToken);
|
|
2060
|
+
return refreshed.accessToken;
|
|
2061
|
+
};
|
|
2062
|
+
await ensureHostedAuth(false);
|
|
2055
2063
|
try {
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2064
|
+
return await request(client);
|
|
2065
|
+
} catch (error) {
|
|
2066
|
+
if (!(error instanceof AccountingApiError) || error.statusCode !== 401) {
|
|
2067
|
+
throw error;
|
|
2068
|
+
}
|
|
2069
|
+
await ensureHostedAuth(true);
|
|
2070
|
+
return request(client);
|
|
2061
2071
|
}
|
|
2062
2072
|
}
|
|
2063
|
-
function
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
storage.setItem(key, value);
|
|
2068
|
-
} catch {
|
|
2069
|
-
return false;
|
|
2070
|
-
}
|
|
2071
|
-
try {
|
|
2072
|
-
storageCandidate("sessionStorage")?.removeItem(key);
|
|
2073
|
-
} catch {
|
|
2074
|
-
}
|
|
2075
|
-
return true;
|
|
2073
|
+
function clearPrivateReadScope(scopeKey, client) {
|
|
2074
|
+
deleteCachedPrivateReadToken(scopeKey);
|
|
2075
|
+
privateReadFailureCache.delete(scopeKey);
|
|
2076
|
+
client.clearPrivateReadToken();
|
|
2076
2077
|
}
|
|
2077
|
-
function
|
|
2078
|
+
async function executeSiwePrivateReadRequest({
|
|
2079
|
+
client,
|
|
2080
|
+
scopeKey,
|
|
2081
|
+
getToken,
|
|
2082
|
+
request
|
|
2083
|
+
}) {
|
|
2084
|
+
const run = (token2) => request(client.withPrivateReadToken(token2));
|
|
2085
|
+
const token = await getToken(false);
|
|
2078
2086
|
try {
|
|
2079
|
-
return
|
|
2080
|
-
} catch {
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
}
|
|
2084
|
-
function removeSharedBrowserStorageItem(key) {
|
|
2085
|
-
for (const storage of storageCandidates()) {
|
|
2086
|
-
try {
|
|
2087
|
-
storage.removeItem(key);
|
|
2088
|
-
} catch {
|
|
2087
|
+
return await run(token);
|
|
2088
|
+
} catch (error) {
|
|
2089
|
+
if (!(error instanceof AccountingApiError) || error.statusCode !== 401) {
|
|
2090
|
+
throw error;
|
|
2089
2091
|
}
|
|
2092
|
+
clearPrivateReadScope(scopeKey, client);
|
|
2093
|
+
return run(await getToken(true));
|
|
2090
2094
|
}
|
|
2091
2095
|
}
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
return amount * (BUFFER_SCALE - shave) / BUFFER_SCALE;
|
|
2103
|
-
}
|
|
2104
|
-
function clampLockAmount(amount, maxAmount) {
|
|
2105
|
-
return maxAmount !== void 0 && maxAmount < amount ? maxAmount : amount;
|
|
2106
|
-
}
|
|
2107
|
-
function requireServiceAddress(serviceAddress) {
|
|
2108
|
-
if (!serviceAddress) {
|
|
2109
|
-
throw new Error("Service address not configured");
|
|
2110
|
-
}
|
|
2111
|
-
return serviceAddress;
|
|
2112
|
-
}
|
|
2113
|
-
function requireDepositLockOwner(walletAddress, beneficiary) {
|
|
2114
|
-
if (!walletAddress) throw new Error("No wallet connected");
|
|
2115
|
-
if (!beneficiary) throw new Error("No authenticated deposit account");
|
|
2116
|
-
if (walletAddress.toLowerCase() !== beneficiary.toLowerCase()) {
|
|
2117
|
-
throw new Error("Connected wallet does not match the authenticated deposit account");
|
|
2118
|
-
}
|
|
2119
|
-
return beneficiary;
|
|
2120
|
-
}
|
|
2121
|
-
function walletClientAccountAddress(walletClient) {
|
|
2122
|
-
const account = walletClient.account;
|
|
2123
|
-
if (!account) return void 0;
|
|
2124
|
-
return typeof account === "string" ? account : account.address;
|
|
2096
|
+
function recordPrivateReadFailure(scopeKey) {
|
|
2097
|
+
const previous = privateReadFailureCache.get(scopeKey);
|
|
2098
|
+
const backoffMs = Math.min(
|
|
2099
|
+
previous ? previous.backoffMs * 2 : INITIAL_AUTH_BACKOFF_MS,
|
|
2100
|
+
MAX_AUTH_BACKOFF_MS
|
|
2101
|
+
);
|
|
2102
|
+
privateReadFailureCache.set(scopeKey, {
|
|
2103
|
+
backoffMs,
|
|
2104
|
+
retryAt: Date.now() + backoffMs
|
|
2105
|
+
});
|
|
2125
2106
|
}
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
tokenId,
|
|
2133
|
-
amount,
|
|
2134
|
-
lockDuration = DEFAULT_LOCK_DURATION_SECONDS
|
|
2135
|
-
}) {
|
|
2136
|
-
if (amount <= 0n) {
|
|
2137
|
-
throw new Error("Lock amount must be positive");
|
|
2138
|
-
}
|
|
2139
|
-
const signerAddress = walletClientAccountAddress(walletClient);
|
|
2140
|
-
if (!signerAddress || signerAddress.toLowerCase() !== userAddress.toLowerCase()) {
|
|
2141
|
-
throw new Error("Connected wallet does not match the authenticated deposit account");
|
|
2107
|
+
function ensureFailureBackoff(scopeKey) {
|
|
2108
|
+
const failure = privateReadFailureCache.get(scopeKey);
|
|
2109
|
+
if (!failure) return;
|
|
2110
|
+
if (failure.retryAt <= Date.now()) {
|
|
2111
|
+
privateReadFailureCache.delete(scopeKey);
|
|
2112
|
+
return;
|
|
2142
2113
|
}
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
verifyingContract: networkConfig.accountingContract,
|
|
2149
|
-
message: {
|
|
2150
|
-
serviceAddress,
|
|
2151
|
-
tokenId,
|
|
2152
|
-
amount,
|
|
2153
|
-
expiry,
|
|
2154
|
-
nonce: BigInt(nonce)
|
|
2155
|
-
}
|
|
2156
|
-
});
|
|
2157
|
-
return {
|
|
2158
|
-
service_address: serviceAddress,
|
|
2159
|
-
token_id: tokenId,
|
|
2160
|
-
amount: amount.toString(),
|
|
2161
|
-
expiry: expiry.toString(),
|
|
2162
|
-
nonce: String(nonce),
|
|
2163
|
-
signature
|
|
2164
|
-
};
|
|
2165
|
-
}
|
|
2166
|
-
var EXPIRY_SLACK_SECONDS = 60;
|
|
2167
|
-
function isSignedLockUsable(payload) {
|
|
2168
|
-
const expiry = Number(payload.expiry);
|
|
2169
|
-
if (!Number.isFinite(expiry)) return false;
|
|
2170
|
-
return expiry > Math.floor(Date.now() / 1e3) + EXPIRY_SLACK_SECONDS;
|
|
2171
|
-
}
|
|
2172
|
-
var PostDepositLockError = class _PostDepositLockError extends Error {
|
|
2173
|
-
constructor(message, reason, signedAmount, creditedAmount, options) {
|
|
2174
|
-
super(message, options);
|
|
2175
|
-
this.reason = reason;
|
|
2176
|
-
this.signedAmount = signedAmount;
|
|
2177
|
-
this.creditedAmount = creditedAmount;
|
|
2178
|
-
this.name = "PostDepositLockError";
|
|
2179
|
-
this.submissionMayHaveSucceeded = options?.submissionMayHaveSucceeded ?? false;
|
|
2180
|
-
Object.setPrototypeOf(this, _PostDepositLockError.prototype);
|
|
2181
|
-
}
|
|
2182
|
-
};
|
|
2183
|
-
async function submitPendingLock({
|
|
2184
|
-
client,
|
|
2185
|
-
payload,
|
|
2186
|
-
creditedAmount,
|
|
2187
|
-
beforeSubmit
|
|
2188
|
-
}) {
|
|
2189
|
-
let signedAmount;
|
|
2190
|
-
try {
|
|
2191
|
-
signedAmount = BigInt(payload.amount);
|
|
2192
|
-
} catch (err) {
|
|
2193
|
-
throw new PostDepositLockError(
|
|
2194
|
-
"Stored signed lock payload is malformed",
|
|
2195
|
-
"submission-failed",
|
|
2196
|
-
void 0,
|
|
2197
|
-
creditedAmount,
|
|
2198
|
-
{ cause: err }
|
|
2199
|
-
);
|
|
2200
|
-
}
|
|
2201
|
-
if (!isSignedLockUsable(payload)) {
|
|
2202
|
-
throw new PostDepositLockError(
|
|
2203
|
-
"Signed lock expired before the deposit was credited",
|
|
2204
|
-
"expired",
|
|
2205
|
-
signedAmount,
|
|
2206
|
-
creditedAmount
|
|
2207
|
-
);
|
|
2208
|
-
}
|
|
2209
|
-
if (creditedAmount !== void 0 && creditedAmount < signedAmount) {
|
|
2210
|
-
throw new PostDepositLockError(
|
|
2211
|
-
"Credited amount is below the signed lock amount",
|
|
2212
|
-
"credited-below-signed",
|
|
2213
|
-
signedAmount,
|
|
2214
|
-
creditedAmount
|
|
2215
|
-
);
|
|
2216
|
-
}
|
|
2217
|
-
try {
|
|
2218
|
-
beforeSubmit?.();
|
|
2219
|
-
return await client.lockFunds(payload);
|
|
2220
|
-
} catch (err) {
|
|
2221
|
-
throw new PostDepositLockError(
|
|
2222
|
-
err instanceof Error ? err.message : "Lock submission failed",
|
|
2223
|
-
"submission-failed",
|
|
2224
|
-
signedAmount,
|
|
2225
|
-
creditedAmount,
|
|
2226
|
-
{ cause: err }
|
|
2227
|
-
);
|
|
2228
|
-
}
|
|
2229
|
-
}
|
|
2230
|
-
function pendingLockKey(userAddress, correlationId) {
|
|
2231
|
-
return `privana:pending-lock:${userAddress.toLowerCase()}:${correlationId}`;
|
|
2232
|
-
}
|
|
2233
|
-
function savePendingLock(userAddress, correlationId, payload) {
|
|
2234
|
-
const record = { payload, savedAt: Date.now() };
|
|
2235
|
-
const stored = setBrowserStorageItem(
|
|
2236
|
-
pendingLockKey(userAddress, correlationId),
|
|
2237
|
-
JSON.stringify(record)
|
|
2238
|
-
);
|
|
2239
|
-
if (!stored) {
|
|
2240
|
-
throw new Error("Unable to persist signed lock for recovery");
|
|
2241
|
-
}
|
|
2242
|
-
}
|
|
2243
|
-
function loadPendingLock(userAddress, correlationId) {
|
|
2244
|
-
const key = pendingLockKey(userAddress, correlationId);
|
|
2245
|
-
try {
|
|
2246
|
-
const raw = getBrowserStorageItem(key);
|
|
2247
|
-
if (!raw) return void 0;
|
|
2248
|
-
const record = JSON.parse(raw);
|
|
2249
|
-
if (!record?.payload?.signature) {
|
|
2250
|
-
removeBrowserStorageItem(key);
|
|
2251
|
-
return void 0;
|
|
2252
|
-
}
|
|
2253
|
-
return record.payload;
|
|
2254
|
-
} catch {
|
|
2255
|
-
return void 0;
|
|
2256
|
-
}
|
|
2257
|
-
}
|
|
2258
|
-
function clearPendingLock(userAddress, correlationId) {
|
|
2259
|
-
removeBrowserStorageItem(pendingLockKey(userAddress, correlationId));
|
|
2260
|
-
}
|
|
2261
|
-
|
|
2262
|
-
// src/sdk/hooks/deposit-finality.ts
|
|
2263
|
-
async function checkDepositWithFinalityRetry({
|
|
2264
|
-
checkDeposit,
|
|
2265
|
-
isStale,
|
|
2266
|
-
onRetry,
|
|
2267
|
-
timeoutMs,
|
|
2268
|
-
retryIntervalMs,
|
|
2269
|
-
startedAt = Date.now(),
|
|
2270
|
-
now = Date.now,
|
|
2271
|
-
sleep = defaultSleep
|
|
2272
|
-
}) {
|
|
2273
|
-
while (true) {
|
|
2274
|
-
if (isStale()) return { kind: "stale" };
|
|
2275
|
-
try {
|
|
2276
|
-
const response = await checkDeposit();
|
|
2277
|
-
if (!isInsufficientFinalityMessage(response.detail) || response.status !== "error") {
|
|
2278
|
-
return { kind: "response", response };
|
|
2279
|
-
}
|
|
2280
|
-
if (response.detail) onRetry(response.detail);
|
|
2281
|
-
} catch (error) {
|
|
2282
|
-
if (isStale()) return { kind: "stale" };
|
|
2283
|
-
if (!isInsufficientFinalityError(error)) throw error;
|
|
2284
|
-
onRetry(
|
|
2285
|
-
error instanceof AccountingApiError && error.detail ? error.detail : error instanceof Error ? error.message : String(error)
|
|
2286
|
-
);
|
|
2287
|
-
}
|
|
2288
|
-
if (now() - startedAt > timeoutMs) return { kind: "timeout" };
|
|
2289
|
-
await sleep(retryIntervalMs);
|
|
2290
|
-
}
|
|
2291
|
-
}
|
|
2292
|
-
function isInsufficientFinalityError(error) {
|
|
2293
|
-
if (error instanceof AccountingApiError) {
|
|
2294
|
-
return isInsufficientFinalityMessage(error.detail) || isInsufficientFinalityMessage(error.message);
|
|
2295
|
-
}
|
|
2296
|
-
return error instanceof Error && isInsufficientFinalityMessage(error.message);
|
|
2297
|
-
}
|
|
2298
|
-
function isInsufficientFinalityMessage(message) {
|
|
2299
|
-
return message?.includes("Insufficient finality") ?? false;
|
|
2300
|
-
}
|
|
2301
|
-
function defaultSleep(milliseconds) {
|
|
2302
|
-
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
2303
|
-
}
|
|
2304
|
-
var INITIAL_AUTH_BACKOFF_MS = 5e3;
|
|
2305
|
-
var MAX_AUTH_BACKOFF_MS = 6e4;
|
|
2306
|
-
var privateReadFailureCache = /* @__PURE__ */ new Map();
|
|
2307
|
-
var privateReadInflight = /* @__PURE__ */ new Map();
|
|
2308
|
-
async function executeHostedAuthPrivateReadRequest({
|
|
2309
|
-
client,
|
|
2310
|
-
hostedAuthSession,
|
|
2311
|
-
refreshHostedAuthSession,
|
|
2312
|
-
request
|
|
2313
|
-
}) {
|
|
2314
|
-
const ensureHostedAuth = async (forceRefresh) => {
|
|
2315
|
-
if (!hostedAuthSession) {
|
|
2316
|
-
throw new HostedAuthRequiredError();
|
|
2317
|
-
}
|
|
2318
|
-
if (!forceRefresh && isHostedAuthSessionActive(hostedAuthSession)) {
|
|
2319
|
-
client.clearPrivateReadToken();
|
|
2320
|
-
client.setBearerToken(hostedAuthSession.accessToken);
|
|
2321
|
-
return hostedAuthSession.accessToken;
|
|
2322
|
-
}
|
|
2323
|
-
const refreshed = await refreshHostedAuthSession();
|
|
2324
|
-
client.clearPrivateReadToken();
|
|
2325
|
-
client.setBearerToken(refreshed.accessToken);
|
|
2326
|
-
return refreshed.accessToken;
|
|
2327
|
-
};
|
|
2328
|
-
await ensureHostedAuth(false);
|
|
2329
|
-
try {
|
|
2330
|
-
return await request(client);
|
|
2331
|
-
} catch (error) {
|
|
2332
|
-
if (!(error instanceof AccountingApiError) || error.statusCode !== 401) {
|
|
2333
|
-
throw error;
|
|
2334
|
-
}
|
|
2335
|
-
await ensureHostedAuth(true);
|
|
2336
|
-
return request(client);
|
|
2337
|
-
}
|
|
2338
|
-
}
|
|
2339
|
-
function clearPrivateReadScope(scopeKey, client) {
|
|
2340
|
-
deleteCachedPrivateReadToken(scopeKey);
|
|
2341
|
-
privateReadFailureCache.delete(scopeKey);
|
|
2342
|
-
client.clearPrivateReadToken();
|
|
2343
|
-
}
|
|
2344
|
-
async function executeSiwePrivateReadRequest({
|
|
2345
|
-
client,
|
|
2346
|
-
scopeKey,
|
|
2347
|
-
getToken,
|
|
2348
|
-
request
|
|
2349
|
-
}) {
|
|
2350
|
-
const run = (token2) => request(client.withPrivateReadToken(token2));
|
|
2351
|
-
const token = await getToken(false);
|
|
2352
|
-
try {
|
|
2353
|
-
return await run(token);
|
|
2354
|
-
} catch (error) {
|
|
2355
|
-
if (!(error instanceof AccountingApiError) || error.statusCode !== 401) {
|
|
2356
|
-
throw error;
|
|
2357
|
-
}
|
|
2358
|
-
clearPrivateReadScope(scopeKey, client);
|
|
2359
|
-
return run(await getToken(true));
|
|
2360
|
-
}
|
|
2361
|
-
}
|
|
2362
|
-
function recordPrivateReadFailure(scopeKey) {
|
|
2363
|
-
const previous = privateReadFailureCache.get(scopeKey);
|
|
2364
|
-
const backoffMs = Math.min(
|
|
2365
|
-
previous ? previous.backoffMs * 2 : INITIAL_AUTH_BACKOFF_MS,
|
|
2366
|
-
MAX_AUTH_BACKOFF_MS
|
|
2367
|
-
);
|
|
2368
|
-
privateReadFailureCache.set(scopeKey, {
|
|
2369
|
-
backoffMs,
|
|
2370
|
-
retryAt: Date.now() + backoffMs
|
|
2371
|
-
});
|
|
2372
|
-
}
|
|
2373
|
-
function ensureFailureBackoff(scopeKey) {
|
|
2374
|
-
const failure = privateReadFailureCache.get(scopeKey);
|
|
2375
|
-
if (!failure) return;
|
|
2376
|
-
if (failure.retryAt <= Date.now()) {
|
|
2377
|
-
privateReadFailureCache.delete(scopeKey);
|
|
2378
|
-
return;
|
|
2379
|
-
}
|
|
2380
|
-
throw new Error(
|
|
2381
|
-
`Private-read authentication is temporarily paused after a recent failure. Retry in ${Math.ceil(
|
|
2382
|
-
(failure.retryAt - Date.now()) / 1e3
|
|
2383
|
-
)}s.`
|
|
2384
|
-
);
|
|
2114
|
+
throw new Error(
|
|
2115
|
+
`Private-read authentication is temporarily paused after a recent failure. Retry in ${Math.ceil(
|
|
2116
|
+
(failure.retryAt - Date.now()) / 1e3
|
|
2117
|
+
)}s.`
|
|
2118
|
+
);
|
|
2385
2119
|
}
|
|
2386
2120
|
function usePrivateReadRequest() {
|
|
2387
2121
|
const wagmiContext = react.useContext(wagmi.WagmiContext);
|
|
@@ -2705,6 +2439,272 @@ function creditedAmountFromResponse(response, requestedAmount) {
|
|
|
2705
2439
|
function isDefinitiveCandidateFailure(error) {
|
|
2706
2440
|
return error instanceof AccountingApiError && error.statusCode === 400;
|
|
2707
2441
|
}
|
|
2442
|
+
|
|
2443
|
+
// src/sdk/utils/browser-storage.ts
|
|
2444
|
+
function storageCandidate(name) {
|
|
2445
|
+
try {
|
|
2446
|
+
if (typeof window === "undefined") return void 0;
|
|
2447
|
+
return window[name] ?? void 0;
|
|
2448
|
+
} catch {
|
|
2449
|
+
return void 0;
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
function storageCandidates() {
|
|
2453
|
+
return [storageCandidate("localStorage"), storageCandidate("sessionStorage")].filter(
|
|
2454
|
+
(storage) => storage !== void 0
|
|
2455
|
+
);
|
|
2456
|
+
}
|
|
2457
|
+
function canUseBrowserStorage() {
|
|
2458
|
+
const probeKey = "privana:storage-probe";
|
|
2459
|
+
for (const storage of storageCandidates()) {
|
|
2460
|
+
try {
|
|
2461
|
+
storage.setItem(probeKey, "1");
|
|
2462
|
+
storage.removeItem(probeKey);
|
|
2463
|
+
return true;
|
|
2464
|
+
} catch {
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
return false;
|
|
2468
|
+
}
|
|
2469
|
+
function setBrowserStorageItem(key, value) {
|
|
2470
|
+
let stored = false;
|
|
2471
|
+
for (const storage of storageCandidates()) {
|
|
2472
|
+
try {
|
|
2473
|
+
storage.setItem(key, value);
|
|
2474
|
+
stored = true;
|
|
2475
|
+
} catch {
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2478
|
+
return stored;
|
|
2479
|
+
}
|
|
2480
|
+
function getBrowserStorageItem(key) {
|
|
2481
|
+
for (const storage of storageCandidates()) {
|
|
2482
|
+
try {
|
|
2483
|
+
const value = storage.getItem(key);
|
|
2484
|
+
if (value !== null) return value;
|
|
2485
|
+
} catch {
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
return null;
|
|
2489
|
+
}
|
|
2490
|
+
function removeBrowserStorageItem(key) {
|
|
2491
|
+
for (const storage of storageCandidates()) {
|
|
2492
|
+
try {
|
|
2493
|
+
storage.removeItem(key);
|
|
2494
|
+
} catch {
|
|
2495
|
+
}
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
function canUseSharedBrowserStorage() {
|
|
2499
|
+
const storage = storageCandidate("localStorage");
|
|
2500
|
+
if (!storage) return false;
|
|
2501
|
+
const probeKey = "privana:shared-storage-probe";
|
|
2502
|
+
try {
|
|
2503
|
+
storage.setItem(probeKey, "1");
|
|
2504
|
+
storage.removeItem(probeKey);
|
|
2505
|
+
return true;
|
|
2506
|
+
} catch {
|
|
2507
|
+
return false;
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
function setSharedBrowserStorageItem(key, value) {
|
|
2511
|
+
const storage = storageCandidate("localStorage");
|
|
2512
|
+
if (!storage) return false;
|
|
2513
|
+
try {
|
|
2514
|
+
storage.setItem(key, value);
|
|
2515
|
+
} catch {
|
|
2516
|
+
return false;
|
|
2517
|
+
}
|
|
2518
|
+
try {
|
|
2519
|
+
storageCandidate("sessionStorage")?.removeItem(key);
|
|
2520
|
+
} catch {
|
|
2521
|
+
}
|
|
2522
|
+
return true;
|
|
2523
|
+
}
|
|
2524
|
+
function getSharedBrowserStorageItem(key) {
|
|
2525
|
+
try {
|
|
2526
|
+
return storageCandidate("localStorage")?.getItem(key) ?? null;
|
|
2527
|
+
} catch {
|
|
2528
|
+
return null;
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
function removeSharedBrowserStorageItem(key) {
|
|
2532
|
+
for (const storage of storageCandidates()) {
|
|
2533
|
+
try {
|
|
2534
|
+
storage.removeItem(key);
|
|
2535
|
+
} catch {
|
|
2536
|
+
}
|
|
2537
|
+
}
|
|
2538
|
+
}
|
|
2539
|
+
|
|
2540
|
+
// src/sdk/utils/pending-lock.ts
|
|
2541
|
+
var DEFAULT_LOCK_DURATION_SECONDS = 259200;
|
|
2542
|
+
var DEFAULT_ONRAMP_LOCK_BUFFER = 0.02;
|
|
2543
|
+
var BUFFER_SCALE = 1000000n;
|
|
2544
|
+
function applyLockBuffer(amount, buffer = DEFAULT_ONRAMP_LOCK_BUFFER) {
|
|
2545
|
+
if (!Number.isFinite(buffer) || buffer < 0 || buffer >= 1) {
|
|
2546
|
+
throw new Error(`Lock buffer must be in [0, 1), got ${buffer}`);
|
|
2547
|
+
}
|
|
2548
|
+
const shave = BigInt(Math.max(0, Math.ceil(buffer * Number(BUFFER_SCALE) - 1e-6)));
|
|
2549
|
+
return amount * (BUFFER_SCALE - shave) / BUFFER_SCALE;
|
|
2550
|
+
}
|
|
2551
|
+
function clampLockAmount(amount, maxAmount) {
|
|
2552
|
+
return maxAmount !== void 0 && maxAmount < amount ? maxAmount : amount;
|
|
2553
|
+
}
|
|
2554
|
+
function requireServiceAddress(serviceAddress) {
|
|
2555
|
+
if (!serviceAddress) {
|
|
2556
|
+
throw new Error("Service address not configured");
|
|
2557
|
+
}
|
|
2558
|
+
return serviceAddress;
|
|
2559
|
+
}
|
|
2560
|
+
function requireDepositLockOwner(walletAddress, beneficiary) {
|
|
2561
|
+
if (!walletAddress) throw new Error("No wallet connected");
|
|
2562
|
+
if (!beneficiary) throw new Error("No authenticated deposit account");
|
|
2563
|
+
if (walletAddress.toLowerCase() !== beneficiary.toLowerCase()) {
|
|
2564
|
+
throw new Error("Connected wallet does not match the authenticated deposit account");
|
|
2565
|
+
}
|
|
2566
|
+
return beneficiary;
|
|
2567
|
+
}
|
|
2568
|
+
function walletClientAccountAddress(walletClient) {
|
|
2569
|
+
const account = walletClient.account;
|
|
2570
|
+
if (!account) return void 0;
|
|
2571
|
+
return typeof account === "string" ? account : account.address;
|
|
2572
|
+
}
|
|
2573
|
+
async function createSignedLockRequest({
|
|
2574
|
+
client,
|
|
2575
|
+
walletClient,
|
|
2576
|
+
userAddress,
|
|
2577
|
+
networkConfig,
|
|
2578
|
+
serviceAddress,
|
|
2579
|
+
tokenId,
|
|
2580
|
+
amount,
|
|
2581
|
+
lockDuration = DEFAULT_LOCK_DURATION_SECONDS
|
|
2582
|
+
}) {
|
|
2583
|
+
if (amount <= 0n) {
|
|
2584
|
+
throw new Error("Lock amount must be positive");
|
|
2585
|
+
}
|
|
2586
|
+
const signerAddress = walletClientAccountAddress(walletClient);
|
|
2587
|
+
if (!signerAddress || signerAddress.toLowerCase() !== userAddress.toLowerCase()) {
|
|
2588
|
+
throw new Error("Connected wallet does not match the authenticated deposit account");
|
|
2589
|
+
}
|
|
2590
|
+
const expiry = BigInt(Math.floor(Date.now() / 1e3) + lockDuration);
|
|
2591
|
+
const { nonce } = await client.getLockNonce(userAddress);
|
|
2592
|
+
const signature = await signLockMessage({
|
|
2593
|
+
walletClient,
|
|
2594
|
+
chainId: networkConfig.chainId,
|
|
2595
|
+
verifyingContract: networkConfig.accountingContract,
|
|
2596
|
+
message: {
|
|
2597
|
+
serviceAddress,
|
|
2598
|
+
tokenId,
|
|
2599
|
+
amount,
|
|
2600
|
+
expiry,
|
|
2601
|
+
nonce: BigInt(nonce)
|
|
2602
|
+
}
|
|
2603
|
+
});
|
|
2604
|
+
return {
|
|
2605
|
+
service_address: serviceAddress,
|
|
2606
|
+
token_id: tokenId,
|
|
2607
|
+
amount: amount.toString(),
|
|
2608
|
+
expiry: expiry.toString(),
|
|
2609
|
+
nonce: String(nonce),
|
|
2610
|
+
signature
|
|
2611
|
+
};
|
|
2612
|
+
}
|
|
2613
|
+
var EXPIRY_SLACK_SECONDS = 60;
|
|
2614
|
+
function isSignedLockUsable(payload) {
|
|
2615
|
+
const expiry = Number(payload.expiry);
|
|
2616
|
+
if (!Number.isFinite(expiry)) return false;
|
|
2617
|
+
return expiry > Math.floor(Date.now() / 1e3) + EXPIRY_SLACK_SECONDS;
|
|
2618
|
+
}
|
|
2619
|
+
var PostDepositLockError = class _PostDepositLockError extends Error {
|
|
2620
|
+
constructor(message, reason, signedAmount, creditedAmount, options) {
|
|
2621
|
+
super(message, options);
|
|
2622
|
+
this.reason = reason;
|
|
2623
|
+
this.signedAmount = signedAmount;
|
|
2624
|
+
this.creditedAmount = creditedAmount;
|
|
2625
|
+
this.name = "PostDepositLockError";
|
|
2626
|
+
this.submissionMayHaveSucceeded = options?.submissionMayHaveSucceeded ?? false;
|
|
2627
|
+
Object.setPrototypeOf(this, _PostDepositLockError.prototype);
|
|
2628
|
+
}
|
|
2629
|
+
};
|
|
2630
|
+
async function submitPendingLock({
|
|
2631
|
+
client,
|
|
2632
|
+
payload,
|
|
2633
|
+
creditedAmount,
|
|
2634
|
+
beforeSubmit
|
|
2635
|
+
}) {
|
|
2636
|
+
let signedAmount;
|
|
2637
|
+
try {
|
|
2638
|
+
signedAmount = BigInt(payload.amount);
|
|
2639
|
+
} catch (err) {
|
|
2640
|
+
throw new PostDepositLockError(
|
|
2641
|
+
"Stored signed lock payload is malformed",
|
|
2642
|
+
"submission-failed",
|
|
2643
|
+
void 0,
|
|
2644
|
+
creditedAmount,
|
|
2645
|
+
{ cause: err }
|
|
2646
|
+
);
|
|
2647
|
+
}
|
|
2648
|
+
if (!isSignedLockUsable(payload)) {
|
|
2649
|
+
throw new PostDepositLockError(
|
|
2650
|
+
"Signed lock expired before the deposit was credited",
|
|
2651
|
+
"expired",
|
|
2652
|
+
signedAmount,
|
|
2653
|
+
creditedAmount
|
|
2654
|
+
);
|
|
2655
|
+
}
|
|
2656
|
+
if (creditedAmount !== void 0 && creditedAmount < signedAmount) {
|
|
2657
|
+
throw new PostDepositLockError(
|
|
2658
|
+
"Credited amount is below the signed lock amount",
|
|
2659
|
+
"credited-below-signed",
|
|
2660
|
+
signedAmount,
|
|
2661
|
+
creditedAmount
|
|
2662
|
+
);
|
|
2663
|
+
}
|
|
2664
|
+
try {
|
|
2665
|
+
beforeSubmit?.();
|
|
2666
|
+
return await client.lockFunds(payload);
|
|
2667
|
+
} catch (err) {
|
|
2668
|
+
throw new PostDepositLockError(
|
|
2669
|
+
err instanceof Error ? err.message : "Lock submission failed",
|
|
2670
|
+
"submission-failed",
|
|
2671
|
+
signedAmount,
|
|
2672
|
+
creditedAmount,
|
|
2673
|
+
{ cause: err }
|
|
2674
|
+
);
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
function pendingLockKey(userAddress, correlationId) {
|
|
2678
|
+
return `privana:pending-lock:${userAddress.toLowerCase()}:${correlationId}`;
|
|
2679
|
+
}
|
|
2680
|
+
function savePendingLock(userAddress, correlationId, payload) {
|
|
2681
|
+
const record = { payload, savedAt: Date.now() };
|
|
2682
|
+
const stored = setBrowserStorageItem(
|
|
2683
|
+
pendingLockKey(userAddress, correlationId),
|
|
2684
|
+
JSON.stringify(record)
|
|
2685
|
+
);
|
|
2686
|
+
if (!stored) {
|
|
2687
|
+
throw new Error("Unable to persist signed lock for recovery");
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
function loadPendingLock(userAddress, correlationId) {
|
|
2691
|
+
const key = pendingLockKey(userAddress, correlationId);
|
|
2692
|
+
try {
|
|
2693
|
+
const raw = getBrowserStorageItem(key);
|
|
2694
|
+
if (!raw) return void 0;
|
|
2695
|
+
const record = JSON.parse(raw);
|
|
2696
|
+
if (!record?.payload?.signature) {
|
|
2697
|
+
removeBrowserStorageItem(key);
|
|
2698
|
+
return void 0;
|
|
2699
|
+
}
|
|
2700
|
+
return record.payload;
|
|
2701
|
+
} catch {
|
|
2702
|
+
return void 0;
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
function clearPendingLock(userAddress, correlationId) {
|
|
2706
|
+
removeBrowserStorageItem(pendingLockKey(userAddress, correlationId));
|
|
2707
|
+
}
|
|
2708
2708
|
var BYTES32_PATTERN = /^0x[0-9a-fA-F]{64}$/;
|
|
2709
2709
|
var PROVIDER_ASSET_CODE_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
2710
2710
|
var TRANSAK_MINIMUM_TARGET_PERCENT = 105n;
|
|
@@ -6163,5 +6163,5 @@ exports.useSafePrivanaContext = useSafePrivanaContext;
|
|
|
6163
6163
|
exports.useSiweAuth = useSiweAuth;
|
|
6164
6164
|
exports.useTransakOnRamp = useTransakOnRamp;
|
|
6165
6165
|
exports.waitForTransactionReceipt = waitForTransactionReceipt;
|
|
6166
|
-
//# sourceMappingURL=chunk-
|
|
6167
|
-
//# sourceMappingURL=chunk-
|
|
6166
|
+
//# sourceMappingURL=chunk-4RHSKIPZ.cjs.map
|
|
6167
|
+
//# sourceMappingURL=chunk-4RHSKIPZ.cjs.map
|