-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement wallet bridge separately from wallet user
- Loading branch information
1 parent
7c670d9
commit 41c1278
Showing
6 changed files
with
151 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/cosmic-swingset/lib/ag-solo/html/wallet-bridge.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Agoric Wallet Bridge</title> | ||
</head> | ||
<body> | ||
<p>This bridge is only for dApps to reach your wallet. It contains no user-servicable parts.</p> | ||
|
||
<script type="text/javascript"> | ||
const walletPublicURL = new URL('/wallet-bridge', window.origin.replace(/^http/, 'ws')).href; | ||
const ws = new WebSocket(walletPublicURL); | ||
const wsQueue = []; | ||
const dappQueue = []; | ||
let origin; | ||
ws.addEventListener('message', ev => { | ||
const obj = JSON.parse(ev.data) | ||
// console.log('to dapp', origin, obj); | ||
if (origin === undefined) { | ||
dappQueue.push(obj); | ||
return; | ||
} | ||
if (window.parent !== window) { | ||
window.parent.postMessage(obj, origin); | ||
} | ||
}); | ||
|
||
ws.addEventListener('open', () => { | ||
if (wsQueue.length) { | ||
console.log('sending', wsQueue.length, 'queued messages from', origin); | ||
} | ||
while (wsQueue.length) { | ||
ws.send(wsQueue.shift()); | ||
} | ||
}); | ||
|
||
|
||
window.addEventListener('message', ev => { | ||
if (origin === undefined) { | ||
// First-come, first-serve. | ||
origin = ev.origin; | ||
while (dappQueue.length) { | ||
const dappObj = dappQueue.shift(); | ||
if (window.parent !== window) { | ||
window.parent.postMessage(dappObj); | ||
} | ||
} | ||
} | ||
// console.log('from dapp', origin, ev.data); | ||
const obj = { ...ev.data, dappOrigin: origin }; | ||
if (ws.readyState !== ws.OPEN) { | ||
wsQueue.push(obj); | ||
} else { | ||
ws.send(JSON.stringify(obj)); | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.