{"version":3,"file":"wallet-post-message-channel.mjs","names":["#request","#isSendCalled"],"sources":["../../src/web-wallet-channel/wallet-post-message-channel.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { parse } from 'valibot';\nimport type { RequestType } from './requests.js';\nimport { Request } from './requests.js';\nimport type { ResponsePayloadType, ResponseType } from './responses.js';\nimport { verifyJwtSession } from '../jwt-session/index.js';\n\nexport class WalletPostMessageChannel {\n\t#request: RequestType;\n\t#isSendCalled: boolean = false;\n\n\tconstructor(request: RequestType) {\n\t\tif (typeof window === 'undefined' || !window.opener) {\n\t\t\tthrow new Error(\n\t\t\t\t'This functionality requires a window opened through `window.open`. `window.opener` is not available.',\n\t\t\t);\n\t\t}\n\n\t\tthis.#request = request;\n\t}\n\n\tstatic fromPayload(payload: RequestType) {\n\t\tconst request = parse(Request, payload);\n\n\t\treturn new WalletPostMessageChannel(request);\n\t}\n\n\tstatic fromUrlHash(hash: string = window.location.hash.slice(1)) {\n\t\tconst decoded = atob(decodeURIComponent(hash));\n\t\tconst request = parse(Request, JSON.parse(decoded));\n\n\t\treturn new WalletPostMessageChannel(request);\n\t}\n\n\tgetRequestData() {\n\t\treturn this.#request;\n\t}\n\n\tasync verifyJwtSession(secretKey: Parameters<typeof verifyJwtSession>[1]) {\n\t\tif (!('session' in this.#request.payload)) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst session = await verifyJwtSession(this.#request.payload.session, secretKey);\n\n\t\tif (session.aud !== new URL(this.#request.appUrl).origin) {\n\t\t\tthrow new Error('App and session origin mismatch');\n\t\t}\n\n\t\tconst requestAddress = this.#request.payload.address;\n\t\tconst addressInSession = session.payload.accounts.find(\n\t\t\t(account) => account.address === requestAddress,\n\t\t);\n\n\t\tif (!addressInSession) {\n\t\t\tthrow new Error('Requested account not found in session');\n\t\t}\n\n\t\treturn session;\n\t}\n\n\tsendMessage(payload: ResponsePayloadType) {\n\t\tif (this.#isSendCalled) {\n\t\t\tthrow new Error('sendMessage() can only be called once');\n\t\t}\n\n\t\tthis.#isSendCalled = true;\n\n\t\twindow.opener.postMessage(\n\t\t\t{\n\t\t\t\tid: this.#request.requestId,\n\t\t\t\tsource: 'web-wallet-channel',\n\t\t\t\tpayload,\n\t\t\t\tversion: this.#request.version,\n\t\t\t} satisfies ResponseType,\n\t\t\tthis.#request.appUrl,\n\t\t);\n\t}\n\n\tclose(payload?: ResponsePayloadType) {\n\t\tif (payload) {\n\t\t\tthis.sendMessage(payload);\n\t\t}\n\t\twindow.close();\n\t}\n}\n"],"mappings":";;;;;AASA,IAAa,2BAAb,MAAa,yBAAyB;CACrC;CACA,gBAAyB;CAEzB,YAAY,SAAsB;AACjC,MAAI,OAAO,WAAW,eAAe,CAAC,OAAO,OAC5C,OAAM,IAAI,MACT,uGACA;AAGF,QAAKA,UAAW;;CAGjB,OAAO,YAAY,SAAsB;AAGxC,SAAO,IAAI,yBAFK,MAAM,SAAS,QAAQ,CAEK;;CAG7C,OAAO,YAAY,OAAe,OAAO,SAAS,KAAK,MAAM,EAAE,EAAE;EAChE,MAAM,UAAU,KAAK,mBAAmB,KAAK,CAAC;AAG9C,SAAO,IAAI,yBAFK,MAAM,SAAS,KAAK,MAAM,QAAQ,CAAC,CAEP;;CAG7C,iBAAiB;AAChB,SAAO,MAAKA;;CAGb,MAAM,iBAAiB,WAAmD;AACzE,MAAI,EAAE,aAAa,MAAKA,QAAS,SAChC,QAAO;EAGR,MAAM,UAAU,MAAM,iBAAiB,MAAKA,QAAS,QAAQ,SAAS,UAAU;AAEhF,MAAI,QAAQ,QAAQ,IAAI,IAAI,MAAKA,QAAS,OAAO,CAAC,OACjD,OAAM,IAAI,MAAM,kCAAkC;EAGnD,MAAM,iBAAiB,MAAKA,QAAS,QAAQ;AAK7C,MAAI,CAJqB,QAAQ,QAAQ,SAAS,MAChD,YAAY,QAAQ,YAAY,eACjC,CAGA,OAAM,IAAI,MAAM,yCAAyC;AAG1D,SAAO;;CAGR,Y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