Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
callmewhy committed Nov 23, 2024
1 parent 3533151 commit ae3f8bc
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions apps/example/src/hooks/useIframeWalletHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ const keypair = Keypair.generate()
export function useIframeWalletHandler() {
useEffect(() => {
const onMessage = async (event: MessageEvent<any>) => {
const iframeWindow = event.source as any
if (!event.source || !event.data?.type) {
return
}

if (iframeWindow && event.data?.target === 'iframe-wallet-adapter') {
const iframeWindow = event.source as any
if (event.data.target === 'iframe-wallet-adapter') {
console.info('useIframeWalletHandler - onMessage', event.data)
const { type, id, payload, target } = event.data as {
type: MessageType
Expand All @@ -23,31 +26,31 @@ export function useIframeWalletHandler() {

switch (type) {
case 'install': {
iframeWindow.postMessage({ target, id, type }, event.origin)
iframeWindow.postMessage({ target, id }, event.origin)
break
}
case 'connect': {
iframeWindow.postMessage({ target, id, type, payload: { publicKey: keypair.publicKey.toBase58() } }, '*')
iframeWindow.postMessage({ target, id, payload: { publicKey: keypair.publicKey.toBase58() } }, '*')
break
}
case 'disconnect': {
iframeWindow.postMessage({ target, id, type }, '*')
iframeWindow.postMessage({ target, id }, '*')
break
}
case 'signMessage': {
const signature = nacl.sign.detached(new Uint8Array(payload.message), keypair.secretKey)
iframeWindow.postMessage({ target, type, id, payload: { signature: Array.from(signature) } }, '*')
iframeWindow.postMessage({ target, id, payload: { signature: Array.from(signature) } }, '*')
break
}
case 'signTransaction': {
const tx = Transaction.from(new Uint8Array(payload.transaction))
tx.sign(keypair)
const signedTransaction = tx.serialize()
iframeWindow.postMessage({ target, id, type, payload: { signedTransaction } }, '*')
iframeWindow.postMessage({ target, id, payload: { signedTransaction } }, '*')
break
}
default: {
console.error('useIframeWalletHandler - onMessage - unknown type', type)
break
}
}
}
Expand Down

0 comments on commit ae3f8bc

Please sign in to comment.