Skip to content

Commit

Permalink
Merge pull request #42 from krigga/from-hex
Browse files Browse the repository at this point in the history
Add Cell.fromHex
  • Loading branch information
dvlkv authored Sep 13, 2024
2 parents 8d6a6a0 + 68e1b7c commit 9c1b8f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/boc/Cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/boc/cell/serialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
});
});

0 comments on commit 9c1b8f7

Please sign in to comment.