{"version":3,"sources":["../src/utils.ts","../src/error.ts"],"sourcesContent":["import { v4 as uuidv4 } from 'uuid'\n\nimport { CosmiframeTimeoutError } from './error'\nimport {\n  CalledParentMethodResult,\n  MethodCallResultMessageNoId,\n  Origin,\n  OverrideHandler,\n  RequestMethodCallMessageNoId,\n} from './types'\n\n/**\n * Origin specified by the iframe to allow all origins. This is unsafe and\n * should not be done.\n */\nexport const UNSAFE_ALLOW_ANY_ORIGIN = 'UNSAFE_ALLOW_ANY_ORIGIN'\n\n/**\n * Send message call request to parent and listen for the result, only accepting\n * results from parents of allowed origins. Returns a promise that resolves with\n * the result on success or rejects with an error.\n */\nexport const callParentMethod = <T = any>(\n  message: RequestMethodCallMessageNoId,\n  origins: Origin[],\n  /**\n   * The timeout in milliseconds after which to reject the promise and stop\n   * listening if the parent has not responded. If undefined, no timeout.\n   *\n   * Defaults to no timeout.\n   */\n  timeout?: number\n): Promise<CalledParentMethodResult<T>> =>\n  new Promise<CalledParentMethodResult<T>>((resolve, reject) => {\n    let timeoutId: number | null = null\n    const id = uuidv4()\n\n    // Add one-time listener that waits for a response for the request we're\n    // about to send.\n    const listener = ({ origin, source, data }: MessageEvent) => {\n      // Verify we are receiving a response for the correct request from an\n      // allowed parent.\n      if (\n        !isOriginAllowed(origins, origin) ||\n        source !== window.parent ||\n        data.id !== id\n      ) {\n        return\n      }\n\n      // Remove listener once we receive a response for the correct request.\n      window.removeEventListener('message', listener)\n\n      // Remove timeout if set.\n      if (timeoutId !== null) {\n        clearTimeout(timeoutId)\n      }\n\n      if (data.type === 'success') {\n        resolve({\n          result: data.response,\n          origin,\n        })\n      } else {\n        reject(new Error(data.error))\n      }\n    }\n\n    window.addEventListener('message', listener)\n\n    try {\n      const data = {\n        ...message,\n        id,\n      }\n\n      // Send the message to our parent of any origin. This is safe because we\n      // will only accept responses back from parents of allowed origins.\n      window.parent.postMessage(data, '*')\n    } catch (err) {\n      // If fails to send, remove the listener and reject.\n      window.removeEventListener('message', listener)\n      reject(err)\n    }\n\n    // If timeout is set, add a timeout listener that will reject the promise\n    // if the parent has not responded.\n    if (timeout) {\n      timeoutId = setTimeout(() => {\n        window.removeEventListener('message', listener)\n        reject(\n          new CosmiframeTimeoutError(\n            `Timed out after ${timeout}ms waiting for parent to respond.`\n          )\n        )\n      }, timeout)\n    }\n  })\n\n/**\n * Convert override handler into a method call result message. If the override\n * handler is to call the method normally, returns undefined.\n */\nexport const processOverrideHandler = (\n  handler: OverrideHandler\n): MethodCallResultMessageNoId | undefined => {\n  if (!handler || handler.type === 'error') {\n    return {\n      type: 'error',\n      error:\n        (handler && handler.type === 'error' && handler.error) ||\n        'Handled by outer wallet.',\n    }\n  } else if (handler.type === 'success') {\n    return {\n      type: 'success',\n      response: handler.value,\n    }\n  }\n}\n\n/**\n * Returns whether or not the current app is being used in an iframe.\n */\nexport const isInIframe = () =>\n  typeof window !== 'undefined' && window.self !== window.parent\n\n/**\n * Returns whether or not the origin is allowed.\n */\nexport const isOriginAllowed = (allowedOrigins: Origin[], origin: string) =>\n  allowedOrigins.some(\n    (allowed) =>\n      // Allow all origins.\n      allowed === '*' ||\n      // Allow a specific origin.\n      (typeof allowed === 'string' && origin === allowed) ||\n      // Allow an origin that matches a regular expression.\n      (allowed instanceof RegExp && allowed.test(origin))\n  )\n","export class CosmiframeTimeoutError extends Error {\n  constructor(message: string) {\n    super(message)\n    this.name = 'CosmiframeTimeoutError'\n  }\n}\n"],"mappings":"+EAAA,OAASA,MAAMC,MAAc,OCAtB,IAAMC,EAAN,MAAMA,UAA+BC,KAAAA,CAC1CC,YAAYC,EAAiB,CAC3B,MAAMA,CAAAA,EACN,KAAKC,KAAO,wBACd,CACF,EAL4CH,EAAAA,EAAAA,0BAArC,IAAMD,EAANK,EDeA,IAAMC,EAA0B,0BAO1BC,EAAmBC,EAAA,CAC9BC,EACAC,EAOAC,IAEA,IAAIC,QAAqC,CAACC,EAASC,IAAAA,CACjD,IAAIC,EAA2B,KACzBC,EAAKC,EAAAA,EAILC,E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C,QAAUD,EAAQE,KAAK3B,CAAAA,CAAAA,EARlB","names":["v4","uuidv4","CosmiframeTimeoutError","Error","constructor","message","name","_CosmiframeTimeoutError","UNSAFE_ALLOW_ANY_ORIGIN","callParentMethod","__name","message","origins","timeout","Promise","resolve","reject","timeoutId","id","uuidv4","listener","origin","source","data","isOriginAllowed","window","parent","removeEventListener","clearTimeout","type","result","response","Error","error","addEventListener","postMessage","err","setTimeout","CosmiframeTimeoutError","processOverrideHandler","handler","value","isInIframe","self","allowedOrigins","some","allowed","RegExp","test"]}