+const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] }
+
+/* TODO: duplicate in both client.js and host.js */
+function getPublicKey() {
+ return new Promise( (resolve, reject) => {
+ /* Check local storage for public key */
+ if (!window.localStorage.getItem('public-key')) {
+ console.log('public key is undefined')
+ /* If doesn't exist, generate public and private key pair, store in
+ local storage */
+ crypto.subtle.generateKey(
+ { name:'RSA-OAEP',
+ modulusLength: 2048,
+ publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
+ hash: {name: "SHA-256"}
+ },
+ true,
+ ['encrypt', 'decrypt']
+ ).then((keyPair) => {
+ /* TODO: Do we need to store the private key as well? */
+ crypto.subtle.exportKey('jwk', keyPair.publicKey)
+ .then((exportedKey) => {
+ window.localStorage.setItem('publicKey', exportedKey)
+ console.log('public key is' + window.localStorage.getItem('publicKey'))
+ resolve(exportedKey)
+ })
+
+ })
+ }
+ else {
+ resolve(window.localStorage.getItem('publicKey'))
+ }
+ })
+
+}
+
+function postServer(url, data) {
+ const request = new XMLHttpRequest()
+ request.open('POST', url, true)
+ request.setRequestHeader('Content-Type', 'application/json' )
+ request.setRequestHeader('X-Strapp-Type', 'ice-candidate-submission')
+ request.send(data)
+}
+
+/* TODO: All this does is wrap a function in a promise */
+function pollServer(url, clientPubKey, func) {
+ return new Promise((resolve, reject) => {
+ func(url, clientPubKey, resolve, reject )
+ })
+}