Created
September 11, 2021 19:20
-
-
Save connor-davis/e241bb5f9b3ec565d56ec64398d81111 to your computer and use it in GitHub Desktop.
This generates a new chat using Gun in Lone Wolf!
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
let generateChat = async (chatWith) => { | |
sessionStorage.setItem( | |
'actualUser', | |
JSON.stringify({ | |
epriv: user.pair().epriv, | |
priv: user.pair().priv, | |
epub: user.pair().epub, | |
pub: user.pair().pub, | |
}) | |
); | |
let chat = await SEA.pair(); | |
let certificate = await SEA.certify( | |
[chatWith, user.is.pub], | |
[{ '*': 'messages' }], | |
chat, | |
null, | |
{} | |
); | |
let actualUser = JSON.parse(sessionStorage.getItem('actualUser')); | |
setTimeout(() => { | |
database.user().auth(chat, async () => { | |
let chatInfo = JSON.stringify({ | |
chat: database.user().pair().pub, | |
friend: chatWith, | |
}); | |
let chatInfoFriend = JSON.stringify({ | |
chat: database.user().pair().pub, | |
friend: actualUser.pub, | |
}); | |
database.user().get('certificates').put(certificate); | |
setTimeout(() => { | |
database.user().auth(actualUser, async ({ err }) => { | |
if (err) console.log(err); | |
else { | |
sessionStorage.setItem('actualUser', null); | |
database | |
.user() | |
.get('chats') | |
.set(chatInfo, async () => { | |
console.log('Chat added.'); | |
let friendChatWithCertificate = await database | |
.user(chatWith) | |
.get('chatWithCertificate') | |
.then(); | |
database | |
.user(chatWith) | |
.get('chats') | |
.set( | |
chatInfoFriend, | |
() => { | |
console.log('Chat added to friend.'); | |
}, | |
{ | |
opt: { cert: friendChatWithCertificate }, | |
} | |
); | |
}); | |
} | |
}); | |
}, 200); | |
}); | |
}, 200); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment