added test for fetching chunk

This commit is contained in:
2025-08-07 18:47:19 +03:00
parent ad40301ffc
commit 6433ca5a58
4 changed files with 64 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import {
handleActiveChat,
handleAddressGroupInvitesMessage,
handleArbitraryDataFileList,
handleArbitraryDataFileMessage,
handleArbitraryLatestTransaction,
handleBlockDataMessage,
handleGroupBansMessage,
@@ -34,6 +35,7 @@ import {
createGetAddressGroupInvitesPayload,
createGetAddressNamesPayload,
createGetArbitraryDataFileListPayload,
createGetArbitraryDataFilePayload,
createGetArbitraryLatestTransactionPayload,
createGetBansPayload,
createGetGroupInvitesPayload,
@@ -178,8 +180,20 @@ export async function getArbitraryResource(
null
)
);
const dataFileList = handleArbitraryDataFileList(res2);
if (dataFileList.hashes?.length > 0) {
const res3: Buffer = await client.sendRequest(
MessageType.GET_ARBITRARY_DATA_FILE,
createGetArbitraryDataFilePayload(
dataFileList.signature,
dataFileList.hashes[0]
)
);
console.log('res22', handleArbitraryDataFileList(res2));
console.log('res333', handleArbitraryDataFileMessage(res3));
}
console.log('res22');
}
if (Array.isArray(data) && data.length > 0) {
return data[0];

View File

@@ -1151,3 +1151,34 @@ export function handleArbitraryDataFileList(buffer) {
isRelayPossible,
};
}
export function handleArbitraryDataFileMessage(buffer) {
let offset = 0;
if (buffer.length < 68) {
throw new Error(
`Invalid message length: got ${buffer.length}, expected at least 68`
);
}
const signature = buffer.subarray(offset, offset + 64);
offset += 64;
const dataLength = buffer.readInt32BE(offset);
offset += 4;
const remaining = buffer.length - offset;
if (remaining < dataLength) {
throw new Error(
`Data length mismatch: expected ${dataLength} bytes, but only ${remaining} bytes available`
);
}
const data = buffer.subarray(offset, offset + dataLength);
return {
signature,
dataLength,
data,
};
}

View File

@@ -10,6 +10,8 @@ export enum MessageType {
GET_SUPPLY = 74,
LAST_BLOCK_HEIGHT = 75,
GET_LAST_BLOCK_HEIGHT = 76,
ARBITRARY_DATA_FILE = 110,
GET_ARBITRARY_DATA_FILE = 111,
ARBITRARY_DATA_FILE_LIST = 120,
GET_ARBITRARY_DATA_FILE_LIST = 121,
ARBITRARY_LATEST_TRANSACTION = 122,

View File

@@ -610,3 +610,19 @@ export function createGetArbitraryDataFileListPayload(
return Buffer.concat(buffers);
}
export function createGetArbitraryDataFilePayload(signature, hash) {
if (signature.length !== 64) {
throw new Error(
`Invalid signature length: expected 64 bytes, got ${signature.length}`
);
}
if (hash.length !== 32) {
throw new Error(
`Invalid hash length: expected 32 bytes, got ${hash.length}`
);
}
return Buffer.concat([signature, hash]);
}