@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.js CHANGED
@@ -3105,8 +3105,9 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
3105
3105
  const config = await getConfig();
3106
3106
  async function executeRequest() {
3107
3107
  const authHeader = await createAuthHeader(config.isServer);
3108
- const headers = Object.assign({ 'Content-Type': 'application/json', "X-Public-App-Id": config.appId, "X-App-Id": config.appId }, authHeader);
3109
- if (typeof window !== 'undefined' && window.CUSTOM_TAROBASE_APP_ID_HEADER) {
3108
+ const headers = Object.assign({ "Content-Type": "application/json", "X-Public-App-Id": config.appId, "X-App-Id": config.appId }, authHeader);
3109
+ if (typeof window !== "undefined" &&
3110
+ window.CUSTOM_TAROBASE_APP_ID_HEADER) {
3110
3111
  const customAppId = window.CUSTOM_TAROBASE_APP_ID_HEADER;
3111
3112
  if (customAppId) {
3112
3113
  headers["X-App-Id"] = customAppId;
@@ -3116,12 +3117,18 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
3116
3117
  if (_overrides === null || _overrides === void 0 ? void 0 : _overrides.headers) {
3117
3118
  Object.assign(headers, _overrides.headers);
3118
3119
  }
3120
+ // Signal to backend that client supports offchain transaction signing
3121
+ if (typeof window !== "undefined" &&
3122
+ window.TAROBASE_SUPPORTS_OFFCHAIN_SIGNING) {
3123
+ console.log("X-Supports-Offchain-Signing", "true");
3124
+ headers["X-Supports-Offchain-Signing"] = "true";
3125
+ }
3119
3126
  const requestConfig = {
3120
3127
  method,
3121
- url: `${config.apiUrl}${urlPath.startsWith('/') ? urlPath : `/${urlPath}`}`,
3128
+ url: `${config.apiUrl}${urlPath.startsWith("/") ? urlPath : `/${urlPath}`}`,
3122
3129
  headers,
3123
3130
  };
3124
- if (method !== 'GET' && method !== 'get') {
3131
+ if (method !== "GET" && method !== "get") {
3125
3132
  requestConfig.data = data ? JSON.stringify(data) : {};
3126
3133
  }
3127
3134
  const response = await axios(requestConfig);
@@ -3138,7 +3145,9 @@ async function makeApiRequest(method, urlPath, data, _overrides) {
3138
3145
  throw new Error("No refresh token found");
3139
3146
  }
3140
3147
  const refreshData = await refreshSession(refreshToken);
3141
- if (refreshData && refreshData.idToken && refreshData.accessToken) {
3148
+ if (refreshData &&
3149
+ refreshData.idToken &&
3150
+ refreshData.accessToken) {
3142
3151
  updateIdTokenAndAccessToken(refreshData.idToken, refreshData.accessToken);
3143
3152
  }
3144
3153
  return await executeRequest();
@@ -3406,9 +3415,15 @@ async function setMany(many, options) {
3406
3415
  clearCacheByPrefix(path);
3407
3416
  });
3408
3417
  if (setResponse.status === 202) {
3409
- // This means that the document needs to be set on-chain, the API did nothing and gave us the
3410
- // required info to run this transaction.
3411
- const transactions = setResponse.data.transactions;
3418
+ // This means that the document needs to be set on-chain (or signed for offchain),
3419
+ // the API did nothing and gave us the required info to run this transaction.
3420
+ const { transactions, offchainTransaction } = setResponse.data;
3421
+ // Handle offchain transaction flow (for 'offchain' protocol apps)
3422
+ if (offchainTransaction) {
3423
+ const transactionResult = await handleOffchainTransaction(offchainTransaction, authProvider, options);
3424
+ return Object.assign(Object.assign({}, documents.map(d => d.document)), { transactionId: transactionResult.signature, signedTransaction: transactionResult.signedTransaction });
3425
+ }
3426
+ // Handle Solana on-chain transaction flow
3412
3427
  let lastTxSignature = undefined;
3413
3428
  let signedTransaction = undefined;
3414
3429
  for (let i = 0; i < transactions.length; i++) {
@@ -3489,6 +3504,40 @@ async function setMany(many, options) {
3489
3504
  const transactionResult = await authProvider.runTransaction(undefined, solTransaction, options);
3490
3505
  return transactionResult;
3491
3506
  }
3507
+ async function handleOffchainTransaction(tx, authProvider, options) {
3508
+ const config = await getConfig();
3509
+ // 1. Sign the transaction message using the auth provider
3510
+ const signature = await authProvider.signMessage(tx.message);
3511
+ // 2. Create signed transaction
3512
+ const signedTx = {
3513
+ transaction: tx,
3514
+ signature
3515
+ };
3516
+ // 3. If shouldSubmitTx is false, return signed but not submitted
3517
+ if ((options === null || options === void 0 ? void 0 : options.shouldSubmitTx) === false) {
3518
+ return {
3519
+ signature: '',
3520
+ signedTransaction: bufferExports.Buffer.from(JSON.stringify(signedTx)).toString('base64')
3521
+ };
3522
+ }
3523
+ // 4. Submit to RPC endpoint
3524
+ const rpcUrl = `${config.apiUrl}/app/${config.appId}/rpc`;
3525
+ const rpcResponse = await fetch(rpcUrl, {
3526
+ method: 'POST',
3527
+ headers: { 'Content-Type': 'application/json' },
3528
+ body: JSON.stringify({
3529
+ jsonrpc: '2.0',
3530
+ id: 1,
3531
+ method: 'sendTransaction',
3532
+ params: [bufferExports.Buffer.from(JSON.stringify(signedTx)).toString('base64')]
3533
+ })
3534
+ });
3535
+ const result = await rpcResponse.json();
3536
+ if (result.error) {
3537
+ throw new Error(result.error.message);
3538
+ }
3539
+ return { signature: result.result };
3540
+ }
3492
3541
  }
3493
3542
  // Helper to clear cache entries by prefix
3494
3543
  function clearCacheByPrefix(prefix) {