Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/client/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,15 @@ module.exports = function (client, options) {
function getAcknowledgements () {
let acc = 0
const acknowledgements = []
const lastSeenMessages = []

for (let i = 0; i < client._lastSeenMessages.capacity; i++) {
const idx = (client._lastSeenMessages.offset + i) % 20
const message = client._lastSeenMessages[idx]
if (message) {
acc |= 1 << i
acknowledgements.push(message.signature)
lastSeenMessages.push(message)
message.pending = false
}
}
Expand All @@ -400,7 +402,8 @@ module.exports = function (client, options) {

return {
acknowledgements,
acknowledged: bitset
acknowledged: bitset,
checksum: computeChatChecksum(lastSeenMessages)
}
}

Expand All @@ -411,15 +414,15 @@ module.exports = function (client, options) {
if (message.startsWith('/')) {
const command = message.slice(1)
if (mcData.supportFeature('useChatSessions')) { // 1.19.3+
const { acknowledged, acknowledgements } = getAcknowledgements()
const { acknowledged, acknowledgements, checksum } = getAcknowledgements()
const canSign = client.profileKeys && client._session
const chatPacket = {
command,
timestamp: options.timestamp,
salt: options.salt,
argumentSignatures: canSign ? signaturesForCommand(command, options.timestamp, options.salt, options.preview, acknowledgements) : [],
messageCount: client._lastSeenMessages.pending,
checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+
checksum, // 1.21.5+
acknowledged
}
client.write((mcData.supportFeature('seperateSignedChatCommandPacket') && canSign) ? 'chat_command_signed' : 'chat_command', chatPacket)
Expand All @@ -444,14 +447,14 @@ module.exports = function (client, options) {
}

if (mcData.supportFeature('useChatSessions')) {
const { acknowledgements, acknowledged } = getAcknowledgements()
const { acknowledgements, acknowledged, checksum } = getAcknowledgements()
client.write('chat_message', {
message,
timestamp: options.timestamp,
salt: options.salt,
signature: (client.profileKeys && client._session) ? client.signMessage(message, options.timestamp, options.salt, undefined, acknowledgements) : undefined,
offset: client._lastSeenMessages.pending,
checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+
checksum, // 1.21.5+
acknowledged
})
client._lastSeenMessages.pending = 0
Expand Down
57 changes: 57 additions & 0 deletions test/chatAcknowledgementTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-env mocha */
Comment thread
Pix3lPirat3 marked this conversation as resolved.

const assert = require('assert')
const EventEmitter = require('events')
const injectChatPlugin = require('../src/client/chat')
const { computeChatChecksum } = require('../src/datatypes/checksums')
const { supportedVersions } = require('../src/version')

const checksumVersions = supportedVersions.filter(version => require('minecraft-data')(version).version['>=']('1.21.5'))

for (const version of checksumVersions) {
describe(`chat acknowledgements ${version}v`, () => {
for (const [outbound, packetName] of [
['outbound message', 'chat_message'],
['/outbound command', 'chat_command']
]) {
it(`checksums wrapped last-seen messages in acknowledgement order for ${packetName}`, () => {
const client = new EventEmitter()
client.version = version
client.uuid = '00000000-0000-0000-0000-000000000001'

const writes = []
client.write = (name, data) => writes.push({ name, data })

injectChatPlugin(client, {})

const signatures = []
for (let i = 1; i <= 21; i++) {
const signature = Buffer.from([i, i + 1])
signatures.push(signature)
client.emit('player_chat', {
signature,
senderUuid: '00000000-0000-0000-0000-000000000002',
plainMessage: `message ${i}`,
index: i,
previousMessages: [],
salt: 1n,
timestamp: 1n,
unsignedChatContent: null,
type: 0,
networkName: null,
networkTargetName: null
})
}

client._signedChat(outbound, { timestamp: 1n, salt: 1n })

const expected = computeChatChecksum(
signatures.slice(-20).map(signature => ({ signature }))
)
assert.strictEqual(writes.length, 1)
assert.strictEqual(writes[0].name, packetName)
assert.strictEqual(writes[0].data.checksum, expected)
})
}
})
}
Loading