From 68e1b7c2c95135a6d0960737127848e713c15f92 Mon Sep 17 00:00:00 2001 From: krigga <25533192+krigga@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:43:32 +0300 Subject: [PATCH] feat: Cell.fromHex --- src/boc/Cell.ts | 14 +++++++++++++- src/boc/cell/serialization.spec.ts | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/boc/Cell.ts b/src/boc/Cell.ts index 3b75e47..88e7ce5 100644 --- a/src/boc/Cell.ts +++ b/src/boc/Cell.ts @@ -34,7 +34,7 @@ export class Cell { } /** - * Helper class that deserializes a single cell from BOC in base64 + * Helper function that deserializes a single cell from BOC in base64 * @param src source string */ static fromBase64(src: string): Cell { @@ -45,6 +45,18 @@ export class Cell { return parsed[0]; } + /** + * Helper function that deserializes a single cell from BOC in hex + * @param src source string + */ + static fromHex(src: string): Cell { + let parsed = Cell.fromBoc(Buffer.from(src, 'hex')); + if (parsed.length !== 1) { + throw new Error("Deserialized more than one cell"); + } + return parsed[0]; + } + // Public properties readonly type: CellType; readonly bits: BitString; diff --git a/src/boc/cell/serialization.spec.ts b/src/boc/cell/serialization.spec.ts index 11e3361..d0e2f39 100644 --- a/src/boc/cell/serialization.spec.ts +++ b/src/boc/cell/serialization.spec.ts @@ -241,5 +241,10 @@ describe('boc', () => { expect(cell.toString()).toBe('x{000000E4}\n x{00000539}\n x{0000053A}'); expect(serialized).toBe('b5ee9c7281010301001400080e140208000000e4010200080000053900080000053a'); }); + + it('should deserialize cell from hex', () => { + let cell = Cell.fromHex('b5ee9c7241010201000d00010800000001010008000000027d4b3cf8'); + expect(cell.toString()).toBe('x{00000001}\n x{00000002}'); + }); });