Skip to content

Commit 5def009

Browse files
authored
feat: improve encoding & add test coverage (#31)
1 parent 8ef8284 commit 5def009

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

types/messages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Address, beginCell, Builder, Cell, Slice, TupleReader } from '@ton/core';
2-
import { DepositLog, GatewayOp, GatewayState } from './types';
3-
import { bufferToHexString, evmAddressToSlice, sliceToHexString } from './utils';
1+
import { Address, beginCell, Builder, Cell, Slice } from '@ton/core';
2+
import { GatewayOp } from './types';
3+
import { evmAddressToSlice } from './utils';
44

55
// op code, query id (0)
66
function newIntent(op: GatewayOp): Builder {

types/utils.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { hexStringToCell, sliceToHexString } from './utils';
2+
3+
const text =
4+
'Lorem Ipsum is simply dummy text of the printing and typesetting industry.' +
5+
" Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," +
6+
' when an unknown printer took a galley of type and scrambled it to make a type specimen book.';
7+
8+
describe('utils', () => {
9+
it('should convert a text string to a hex string', () => {
10+
// ARRANGE
11+
// Given a text string in a hex format
12+
const from = textToHexString(text);
13+
14+
// ACT
15+
// Convert it into a BOC
16+
const cell = hexStringToCell(from);
17+
18+
// ASSERT
19+
// Convert cell back to hex string
20+
const to = sliceToHexString(cell.beginParse());
21+
22+
expect(to).toBe(from);
23+
24+
// And convert it back to a text string
25+
const text2 = hexStringToText(from);
26+
expect(text2).toBe(text);
27+
});
28+
});
29+
30+
function textToHexString(text: string): string {
31+
const buffer = Buffer.from(text, 'utf-8');
32+
33+
return `0x${buffer.toString('hex')}`;
34+
}
35+
36+
// "0x..." -> text"
37+
function hexStringToText(hex: string): string {
38+
return Buffer.from(hex.substring(2), 'hex').toString('utf-8');
39+
}

types/utils.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ export function bufferToHexString(b: Buffer): string {
3535
}
3636

3737
/**
38-
* Converts a Slice to a hex string
38+
* Converts a Slice to a hex string (0x...)
3939
* @param s - Slice
40-
* @param bytes - Number of bytes to convert
4140
* @returns string (`0x...`)
4241
*/
43-
export function sliceToHexString(s: Slice, bytes: number): string {
44-
return bufferToHexString(s.loadBuffer(bytes));
42+
export function sliceToHexString(s: Slice): string {
43+
return bufferToHexString(readBuffer(s));
4544
}
4645

4746
/**
@@ -83,3 +82,32 @@ function writeBuffer(src: Buffer, builder: Builder) {
8382
builder = builder.storeBuffer(src);
8483
}
8584
}
85+
86+
// https://github.com/ton-org/ton-core/blob/4eaced536d0a89f9374d9772884c7b52bddb68ba/src/boc/utils/strings.ts#L13
87+
export function readBuffer(slice: Slice) {
88+
// Check consistency
89+
if (slice.remainingBits % 8 !== 0) {
90+
throw new Error(`Invalid string length: ${slice.remainingBits}`);
91+
}
92+
93+
if (slice.remainingRefs !== 0 && slice.remainingRefs !== 1) {
94+
throw new Error(`invalid number of refs: ${slice.remainingRefs}`);
95+
}
96+
97+
// Read string
98+
let res: Buffer;
99+
100+
if (slice.remainingBits === 0) {
101+
res = Buffer.alloc(0);
102+
} else {
103+
res = slice.loadBuffer(slice.remainingBits / 8);
104+
}
105+
106+
// Read tail
107+
if (slice.remainingRefs === 1) {
108+
const tail = slice.loadRef().beginParse();
109+
res = Buffer.concat([res, readBuffer(tail)]);
110+
}
111+
112+
return res;
113+
}

0 commit comments

Comments
 (0)