@pooflabs/core 0.0.11 → 0.0.13

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.mjs CHANGED
@@ -3085,8 +3085,9 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
3085
3085
  const config = await getConfig();
3086
3086
  async function executeRequest() {
3087
3087
  const authHeader = await createAuthHeader(config.isServer);
3088
- const headers = Object.assign({ 'Content-Type': 'application/json', "X-Public-App-Id": config.appId, "X-App-Id": config.appId }, authHeader);
3089
- if (typeof window !== 'undefined' && window.CUSTOM_TAROBASE_APP_ID_HEADER) {
3088
+ const headers = Object.assign({ "Content-Type": "application/json", "X-Public-App-Id": config.appId, "X-App-Id": config.appId }, authHeader);
3089
+ if (typeof window !== "undefined" &&
3090
+ window.CUSTOM_TAROBASE_APP_ID_HEADER) {
3090
3091
  const customAppId = window.CUSTOM_TAROBASE_APP_ID_HEADER;
3091
3092
  if (customAppId) {
3092
3093
  headers["X-App-Id"] = customAppId;
@@ -3096,12 +3097,18 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
3096
3097
  if (_overrides === null || _overrides === void 0 ? void 0 : _overrides.headers) {
3097
3098
  Object.assign(headers, _overrides.headers);
3098
3099
  }
3100
+ // Signal to backend that client supports offchain transaction signing
3101
+ if (typeof window !== "undefined" &&
3102
+ window.TAROBASE_SUPPORTS_OFFCHAIN_SIGNING) {
3103
+ console.log("X-Supports-Offchain-Signing", "true");
3104
+ headers["X-Supports-Offchain-Signing"] = "true";
3105
+ }
3099
3106
  const requestConfig = {
3100
3107
  method,
3101
- url: `${config.apiUrl}${urlPath.startsWith('/') ? urlPath : `/${urlPath}`}`,
3108
+ url: `${config.apiUrl}${urlPath.startsWith("/") ? urlPath : `/${urlPath}`}`,
3102
3109
  headers,
3103
3110
  };
3104
- if (method !== 'GET' && method !== 'get') {
3111
+ if (method !== "GET" && method !== "get") {
3105
3112
  requestConfig.data = data ? JSON.stringify(data) : {};
3106
3113
  }
3107
3114
  const response = await axios(requestConfig);
@@ -3118,7 +3125,9 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
3118
3125
  throw new Error("No refresh token found");
3119
3126
  }
3120
3127
  const refreshData = await refreshSession(refreshToken);
3121
- if (refreshData && refreshData.idToken && refreshData.accessToken) {
3128
+ if (refreshData &&
3129
+ refreshData.idToken &&
3130
+ refreshData.accessToken) {
3122
3131
  updateIdTokenAndAccessToken(refreshData.idToken, refreshData.accessToken);
3123
3132
  }
3124
3133
  return await executeRequest();
@@ -3386,9 +3395,15 @@ async function setMany(many, options) {
3386
3395
  clearCacheByPrefix(path);
3387
3396
  });
3388
3397
  if (setResponse.status === 202) {
3389
- // This means that the document needs to be set on-chain, the API did nothing and gave us the
3390
- // required info to run this transaction.
3391
- const transactions = setResponse.data.transactions;
3398
+ // This means that the document needs to be set on-chain (or signed for offchain),
3399
+ // the API did nothing and gave us the required info to run this transaction.
3400
+ const { transactions, offchainTransaction } = setResponse.data;
3401
+ // Handle offchain transaction flow (for 'offchain' protocol apps)
3402
+ if (offchainTransaction) {
3403
+ const transactionResult = await handleOffchainTransaction(offchainTransaction, authProvider, options);
3404
+ return Object.assign(Object.assign({}, documents.map(d => d.document)), { transactionId: transactionResult.signature, signedTransaction: transactionResult.signedTransaction });
3405
+ }
3406
+ // Handle Solana on-chain transaction flow
3392
3407
  let lastTxSignature = undefined;
3393
3408
  let signedTransaction = undefined;
3394
3409
  for (let i = 0; i < transactions.length; i++) {
@@ -3469,6 +3484,40 @@ async function setMany(many, options) {
3469
3484
  const transactionResult = await authProvider.runTransaction(undefined, solTransaction, options);
3470
3485
  return transactionResult;
3471
3486
  }
3487
+ async function handleOffchainTransaction(tx, authProvider, options) {
3488
+ const config = await getConfig();
3489
+ // 1. Sign the transaction message using the auth provider
3490
+ const signature = await authProvider.signMessage(tx.message);
3491
+ // 2. Create signed transaction
3492
+ const signedTx = {
3493
+ transaction: tx,
3494
+ signature
3495
+ };
3496
+ // 3. If shouldSubmitTx is false, return signed but not submitted
3497
+ if ((options === null || options === void 0 ? void 0 : options.shouldSubmitTx) === false) {
3498
+ return {
3499
+ signature: '',
3500
+ signedTransaction: bufferExports.Buffer.from(JSON.stringify(signedTx)).toString('base64')
3501
+ };
3502
+ }
3503
+ // 4. Submit to RPC endpoint
3504
+ const rpcUrl = `${config.apiUrl}/app/${config.appId}/rpc`;
3505
+ const rpcResponse = await fetch(rpcUrl, {
3506
+ method: 'POST',
3507
+ headers: { 'Content-Type': 'application/json' },
3508
+ body: JSON.stringify({
3509
+ jsonrpc: '2.0',
3510
+ id: 1,
3511
+ method: 'sendTransaction',
3512
+ params: [bufferExports.Buffer.from(JSON.stringify(signedTx)).toString('base64')]
3513
+ })
3514
+ });
3515
+ const result = await rpcResponse.json();
3516
+ if (result.error) {
3517
+ throw new Error(result.error.message);
3518
+ }
3519
+ return { signature: result.result };
3520
+ }
3472
3521
  }
3473
3522
  // Helper to clear cache entries by prefix
3474
3523
  function clearCacheByPrefix(prefix) {