Merge pull request #21 from shocknet/seed-backup-event

Seed backup event
This commit is contained in:
Daniel Lugo 2020-01-24 21:34:41 -04:00 committed by GitHub
commit b0b8eb521c
3 changed files with 63 additions and 2 deletions

View file

@ -309,6 +309,7 @@ class Mediator {
socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests) socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests)
socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests) socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests)
socket.on(Event.ON_BIO, this.onBio) socket.on(Event.ON_BIO, this.onBio)
socket.on(Event.ON_SEED_BACKUP, this.onSeedBackup)
socket.on(IS_GUN_AUTH, this.isGunAuth) socket.on(IS_GUN_AUTH, this.isGunAuth)
} }
@ -943,6 +944,36 @@ class Mediator {
}) })
} }
} }
/**
* @param {Readonly<{ token: string }>} body
*/
onSeedBackup = async body => {
try {
const { token } = body
await throwOnInvalidToken(token)
await API.Events.onSeedBackup(
seedBackup => {
this.socket.emit(Event.ON_SEED_BACKUP, {
ok: true,
msg: seedBackup,
origBody: body
})
},
user,
mySEA
)
} catch (err) {
console.log(err)
this.socket.emit(Event.ON_SEED_BACKUP, {
ok: false,
msg: err.message,
origBody: body
})
}
}
} }
/** /**

View file

@ -1230,6 +1230,34 @@ const onBio = (cb, user) => {
}) })
} }
/** @type {string|null} */
let currentSeedBackup = null
/**
* @param {(seedBackup: string|null) => void} cb
* @param {UserGUNNode} user
* @param {ISEA} SEA
* @throws {Error} If user hasn't been auth.
* @returns {Promise<void>}
*/
const onSeedBackup = async (cb, user, SEA) => {
if (!user.is) {
throw new Error(ErrorCode.NOT_AUTH)
}
const mySecret = await SEA.secret(user._.sea.epub, user._.sea)
const callb = debounce(cb, DEBOUNCE_WAIT_TIME)
callb(currentSeedBackup)
user.get(Key.SEED_BACKUP).on(async seedBackup => {
if (typeof seedBackup === 'string') {
currentSeedBackup = await SEA.decrypt(seedBackup, mySecret)
callb(currentSeedBackup)
}
})
}
module.exports = { module.exports = {
__onSentRequestToUser, __onSentRequestToUser,
__onUserToIncoming, __onUserToIncoming,
@ -1242,5 +1270,6 @@ module.exports = {
onChats, onChats,
onSimplerReceivedRequests, onSimplerReceivedRequests,
onSimplerSentRequests, onSimplerSentRequests,
onBio onBio,
onSeedBackup
} }

View file

@ -6,7 +6,8 @@ const Events = {
ON_HANDSHAKE_ADDRESS: "ON_HANDSHAKE_ADDRESS", ON_HANDSHAKE_ADDRESS: "ON_HANDSHAKE_ADDRESS",
ON_RECEIVED_REQUESTS: "ON_RECEIVED_REQUESTS", ON_RECEIVED_REQUESTS: "ON_RECEIVED_REQUESTS",
ON_SENT_REQUESTS: "ON_SENT_REQUESTS", ON_SENT_REQUESTS: "ON_SENT_REQUESTS",
ON_BIO: "ON_BIO" ON_BIO: "ON_BIO",
ON_SEED_BACKUP: "ON_SEED_BACKUP",
}; };
module.exports = Events; module.exports = Events;