| 47 | }() |
| 48 | |
| 49 | function updateAndValidateSession (uuid, message, currentSignature, index, previousMessages, salt, timestamp) { |
| 50 | const player = client._players[uuid] |
| 51 | |
| 52 | if (player && player.hasChainIntegrity) { |
| 53 | if (!player.lastSignature || player.lastSignature.equals(currentSignature) || index > player.sessionIndex) { |
| 54 | player.lastSignature = currentSignature |
| 55 | player.sessionIndex = index |
| 56 | } else { |
| 57 | player.hasChainIntegrity = false |
| 58 | } |
| 59 | |
| 60 | if (player.hasChainIntegrity) { |
| 61 | const length = Buffer.byteLength(message, 'utf8') |
| 62 | const validBuffers = previousMessages |
| 63 | .filter(msg => msg.signature || client._signatureCache[msg.id]) // Filter out invalid messages |
| 64 | .map(msg => msg.signature || client._signatureCache[msg.id]) |
| 65 | .filter(buf => Buffer.isBuffer(buf)) |
| 66 | |
| 67 | const acknowledgements = validBuffers.length > 0 |
| 68 | ? ['i32', validBuffers.length, 'buffer', Buffer.concat(validBuffers)] |
| 69 | : ['i32', 0] |
| 70 | const signable = concat('i32', 1, 'UUID', uuid, 'UUID', player.sessionUuid, 'i32', index, 'i64', salt, 'i64', timestamp / 1000n, 'i32', length, 'pstring', message, ...acknowledgements) |
| 71 | |
| 72 | player.hasChainIntegrity = crypto.verify('RSA-SHA256', signable, player.publicKey, currentSignature) |
| 73 | } |
| 74 | |
| 75 | return player.hasChainIntegrity |
| 76 | } |
| 77 | |
| 78 | return false |
| 79 | } |
| 80 | |
| 81 | function updateAndValidateChat (uuid, previousSignature, currentSignature, payload) { |
| 82 | // Get the player information |