Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function fromHex(str: string): Uint8Array {
if (typeof str !== 'string')
throw new TypeError('"str" is not a string');
return sodium.from_hex(str);
}
private async authenticatePeer(peer: SimplePeer.Instance, peerId?: string) {
const peerPublicKey = peerId ? sodium.from_hex(peerId) : undefined
const userPublicKey = await mutualHandshake(peer, localUser().id, peerPublicKey)
if (!userPublicKey) {
const addr = `${(peer as any).remoteAddress}:${(peer as any).remotePort}`
peer.destroy()
throw new NetworkError(
NetworkErrorCode.PeerAuthenticationFailure,
`Failed to authenticate with peer [${addr}]`
)
}
const userId = sodium.to_hex(userPublicKey)
const netId = new NetUniqueId(userPublicKey)
const conn = new RTCPeerConn(netId, peer)
console.debug(`Authenticated peer ${userId}`, conn)
await sodium.ready
if (ephemeral) {
return keyPair()
}
let localKeyPair: KeyPair | undefined
{
const publicKey = localStorage.getItem(PUBKEY_PROP)
const privateKey = localStorage.getItem(SECKEY_PROP)
if (publicKey && privateKey) {
localKeyPair = {
publicKey: sodium.from_hex(publicKey),
privateKey: sodium.from_hex(privateKey),
keyType: 'curve25519'
}
}
}
if (!localKeyPair) {
localKeyPair = keyPair()
try {
localStorage.setItem(PUBKEY_PROP, sodium.to_hex(localKeyPair.publicKey))
localStorage.setItem(SECKEY_PROP, sodium.to_hex(localKeyPair.privateKey))
} catch (e) {}
}
return localKeyPair
}
decrypt(key, message)
{
try
{
message = _sodium.from_hex(message);
return _sodium.to_string(_sodium.crypto_secretbox_open_easy(message.slice(_sodium.crypto_secretbox_NONCEBYTES), message.slice(0, _sodium.crypto_secretbox_NONCEBYTES), _sodium.from_hex(key)));
}
catch(e)
{
this._adapter.log.warn(JSON.stringify(e.message));
return false;
}
}