From 3c698902d48c91d3630d5f87dda2f2eeecf45ecb Mon Sep 17 00:00:00 2001 From: BlobMaster41 <96896824+BlobMaster41@users.noreply.github.com> Date: Fri, 5 Jul 2024 02:05:48 -0400 Subject: [PATCH] Added read address array --- package.json | 2 +- src/buffer/BinaryReader.ts | 11 +++++++++++ src/buffer/BinaryWriter.ts | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 7fd3406..cd5ef6f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "@btc-vision/bsi-binary", - "version": "1.0.26", + "version": "1.0.27", "author": "BlobMaster41", "description": "OPNet official buffer serialization and deserialization library", "main": "build/index.js", diff --git a/src/buffer/BinaryReader.ts b/src/buffer/BinaryReader.ts index a3338ca..5f0cdd2 100644 --- a/src/buffer/BinaryReader.ts +++ b/src/buffer/BinaryReader.ts @@ -49,6 +49,17 @@ export class BinaryReader { return events; } + public readAddressArray(): Address[] { + const length = this.readU16(); + const result: Address[] = new Array
(length); + + for (let i = 0; i < length; i++) { + result[i] = this.readAddress(); + } + + return result; + } + public readEvent(): NetEvent { const eventType = this.readStringWithLength(); const eventDataSelector = this.readU64(); diff --git a/src/buffer/BinaryWriter.ts b/src/buffer/BinaryWriter.ts index 188c222..1ec3f79 100644 --- a/src/buffer/BinaryWriter.ts +++ b/src/buffer/BinaryWriter.ts @@ -290,6 +290,16 @@ export class BinaryWriter { this.writeBytes(value); } + public writeAddressArray(value: Address[]): void { + if(value.length > 65535) throw new Error('Array size is too large'); + + this.writeU16(value.length); + + for (let i = 0; i < value.length; i++) { + this.writeAddress(value[i]); + } + } + private fromAddress(value: Address): Uint8Array { if (value.length > ADDRESS_BYTE_LENGTH) { throw new Error('Address is too long');