Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cell.fromHex #42

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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}');
});
});

Loading