typings and typechecking
This commit is contained in:
parent
43f9795dc7
commit
d89b128428
2 changed files with 31 additions and 5 deletions
|
|
@ -495,7 +495,7 @@ class Mediator {
|
||||||
field: 'deviceId',
|
field: 'deviceId',
|
||||||
message: 'Please specify a device ID'
|
message: 'Please specify a device ID'
|
||||||
}
|
}
|
||||||
logger.error(error)
|
logger.error(JSON.stringify(error))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -925,7 +925,7 @@ class Mediator {
|
||||||
API.Events.onChats(chats => {
|
API.Events.onChats(chats => {
|
||||||
if (Config.SHOW_LOG) {
|
if (Config.SHOW_LOG) {
|
||||||
logger.info('---chats---')
|
logger.info('---chats---')
|
||||||
logger.info(chats)
|
logger.info(JSON.stringify(chats))
|
||||||
logger.info('-----------------------')
|
logger.info('-----------------------')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -957,7 +957,7 @@ class Mediator {
|
||||||
API.Events.onDisplayName(displayName => {
|
API.Events.onDisplayName(displayName => {
|
||||||
if (Config.SHOW_LOG) {
|
if (Config.SHOW_LOG) {
|
||||||
logger.info('---displayName---')
|
logger.info('---displayName---')
|
||||||
logger.info(displayName)
|
logger.info(displayName || 'null or empty string')
|
||||||
logger.info('-----------------------')
|
logger.info('-----------------------')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -989,7 +989,7 @@ class Mediator {
|
||||||
API.Events.onCurrentHandshakeAddress(addr => {
|
API.Events.onCurrentHandshakeAddress(addr => {
|
||||||
if (Config.SHOW_LOG) {
|
if (Config.SHOW_LOG) {
|
||||||
logger.info('---addr---')
|
logger.info('---addr---')
|
||||||
logger.info(addr)
|
logger.info(addr || 'null or empty string')
|
||||||
logger.info('-----------------------')
|
logger.info('-----------------------')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,14 @@ const nonEncryptedEvents = [
|
||||||
]
|
]
|
||||||
|
|
||||||
const Encryption = {
|
const Encryption = {
|
||||||
|
/**
|
||||||
|
* @param {string} event
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
isNonEncrypted: event => nonEncryptedEvents.includes(event),
|
isNonEncrypted: event => nonEncryptedEvents.includes(event),
|
||||||
|
/**
|
||||||
|
* @param {{ deviceId: string , message: string }} arg0
|
||||||
|
*/
|
||||||
encryptKey: ({ deviceId, message }) => {
|
encryptKey: ({ deviceId, message }) => {
|
||||||
if (!authorizedDevices.has(deviceId)) {
|
if (!authorizedDevices.has(deviceId)) {
|
||||||
throw { field: 'deviceId', message: 'Unknown Device ID' }
|
throw { field: 'deviceId', message: 'Unknown Device ID' }
|
||||||
|
|
@ -33,6 +40,9 @@ const Encryption = {
|
||||||
)
|
)
|
||||||
return encryptedData.toString('base64')
|
return encryptedData.toString('base64')
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* @param {{ deviceId: string , message: string }} arg0
|
||||||
|
*/
|
||||||
decryptKey: ({ deviceId, message }) => {
|
decryptKey: ({ deviceId, message }) => {
|
||||||
if (!authorizedDevices.has(deviceId)) {
|
if (!authorizedDevices.has(deviceId)) {
|
||||||
throw { field: 'deviceId', message: 'Unknown Device ID' }
|
throw { field: 'deviceId', message: 'Unknown Device ID' }
|
||||||
|
|
@ -49,6 +59,9 @@ const Encryption = {
|
||||||
|
|
||||||
return encryptedData.toString()
|
return encryptedData.toString()
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* @param {{ deviceId: string , message: any , metadata?: any}} arg0
|
||||||
|
*/
|
||||||
encryptMessage: ({ deviceId, message, metadata }) => {
|
encryptMessage: ({ deviceId, message, metadata }) => {
|
||||||
const parsedMessage =
|
const parsedMessage =
|
||||||
typeof message === 'object' ? JSON.stringify(message) : message
|
typeof message === 'object' ? JSON.stringify(message) : message
|
||||||
|
|
@ -68,6 +81,9 @@ const Encryption = {
|
||||||
const encryptedData = encryptedBuffer.toString('base64')
|
const encryptedData = encryptedBuffer.toString('base64')
|
||||||
return { encryptedData, encryptedKey, iv: iv.toString('hex'), metadata }
|
return { encryptedData, encryptedKey, iv: iv.toString('hex'), metadata }
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* @param {{ message: string , key: string , iv: string }} arg0
|
||||||
|
*/
|
||||||
decryptMessage: ({ message, key, iv }) => {
|
decryptMessage: ({ message, key, iv }) => {
|
||||||
const data = Buffer.from(message, 'base64')
|
const data = Buffer.from(message, 'base64')
|
||||||
const cipher = Crypto.createDecipheriv(
|
const cipher = Crypto.createDecipheriv(
|
||||||
|
|
@ -84,6 +100,9 @@ const Encryption = {
|
||||||
|
|
||||||
return decryptedData.toString()
|
return decryptedData.toString()
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* @param {{ deviceId: string }} arg0
|
||||||
|
*/
|
||||||
isAuthorizedDevice: ({ deviceId }) => {
|
isAuthorizedDevice: ({ deviceId }) => {
|
||||||
if (authorizedDevices.has(deviceId)) {
|
if (authorizedDevices.has(deviceId)) {
|
||||||
return true
|
return true
|
||||||
|
|
@ -91,6 +110,9 @@ const Encryption = {
|
||||||
|
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* @param {{ deviceId: string , publicKey: string }} arg0
|
||||||
|
*/
|
||||||
authorizeDevice: ({ deviceId, publicKey }) =>
|
authorizeDevice: ({ deviceId, publicKey }) =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
authorizedDevices.set(deviceId, publicKey)
|
authorizedDevices.set(deviceId, publicKey)
|
||||||
|
|
@ -109,9 +131,10 @@ const Encryption = {
|
||||||
},
|
},
|
||||||
(err, publicKey, privateKey) => {
|
(err, publicKey, privateKey) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
// @ts-ignore
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
reject(err)
|
reject(err)
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const exportedKey = {
|
const exportedKey = {
|
||||||
|
|
@ -127,6 +150,9 @@ const Encryption = {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
|
/**
|
||||||
|
* @param {{ deviceId: string }} arg0
|
||||||
|
*/
|
||||||
unAuthorizeDevice: ({ deviceId }) => {
|
unAuthorizeDevice: ({ deviceId }) => {
|
||||||
authorizedDevices.delete(deviceId)
|
authorizedDevices.delete(deviceId)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue