Comparing version 0.49.0 to 0.49.1
@@ -70,6 +70,2 @@ "use strict"; | ||
substring(offset, length) { | ||
// Corner case of empty string | ||
if (length === 0 && offset === this._length) { | ||
return BitString.EMPTY; | ||
} | ||
// Check offset | ||
@@ -82,2 +78,6 @@ if (offset >= this._length) { | ||
} | ||
// Corner case of empty string | ||
if (length === 0) { | ||
return BitString.EMPTY; | ||
} | ||
if (offset + length > this._length) { | ||
@@ -84,0 +84,0 @@ throw new Error(`Offset ${offset} + Length ${length} > ${this._length} is out of bounds`); |
@@ -14,2 +14,2 @@ /** | ||
export declare function getBitsDescriptor(bits: BitString): number; | ||
export declare function getRepr(bits: BitString, refs: Cell[], level: number, type: CellType): Buffer; | ||
export declare function getRepr(originalBits: BitString, bits: BitString, refs: Cell[], level: number, type: CellType): Buffer; |
@@ -22,3 +22,3 @@ "use strict"; | ||
exports.getBitsDescriptor = getBitsDescriptor; | ||
function getRepr(bits, refs, level, type) { | ||
function getRepr(originalBits, bits, refs, level, type) { | ||
// Allocate | ||
@@ -30,3 +30,3 @@ const bitsLen = Math.ceil(bits.length / 8); | ||
repr[reprCursor++] = getRefsDescriptor(refs, level, type); | ||
repr[reprCursor++] = getBitsDescriptor(bits); | ||
repr[reprCursor++] = getBitsDescriptor(originalBits); | ||
// Write bits | ||
@@ -33,0 +33,0 @@ (0, paddedBits_1.bitsToPaddedBuffer)(bits).copy(repr, reprCursor); |
@@ -20,2 +20,13 @@ "use strict"; | ||
const crc32c_1 = require("../../utils/crc32c"); | ||
function getHashesCount(levelMask) { | ||
return getHashesCountFromMask(levelMask & 7); | ||
} | ||
function getHashesCountFromMask(mask) { | ||
let n = 0; | ||
for (let i = 0; i < 3; i++) { | ||
n += (mask & 1); | ||
mask = mask >> 1; | ||
} | ||
return n + 1; // 1 repr + up to 3 higher hashes | ||
} | ||
function readCell(reader, sizeBytes) { | ||
@@ -30,2 +41,9 @@ // D1 | ||
const paddingAdded = !!(d2 % 2); | ||
const levelMask = d1 >> 5; | ||
const hasHashes = (d1 & 16) != 0; | ||
const hash_bytes = 32; | ||
const hashesSize = hasHashes ? getHashesCount(levelMask) * hash_bytes : 0; | ||
const depthSize = hasHashes ? getHashesCount(levelMask) * 2 : 0; | ||
reader.skip(hashesSize * 8); | ||
reader.skip(depthSize * 8); | ||
// Bits | ||
@@ -32,0 +50,0 @@ let bits = BitString_1.BitString.EMPTY; |
@@ -196,2 +196,12 @@ "use strict"; | ||
}); | ||
it('should deserialize block (#21)', () => { | ||
let testCase = fs_1.default.readFileSync(__dirname + '/__testdata__/block.txt', 'utf8'); | ||
let cell = Cell_1.Cell.fromBase64(testCase); | ||
}); | ||
it('should hash tx with merkle body', () => { | ||
let testCase = JSON.parse(fs_1.default.readFileSync(__dirname + '/__testdata__/tx_with_merkle_body.json', 'utf8')); | ||
let boc = Buffer.from(testCase.boc, 'hex'); | ||
let cell = Cell_1.Cell.fromBoc(boc)[0]; | ||
expect(cell.hash().toString('hex')).toBe(testCase.hash); | ||
}); | ||
}); |
@@ -117,3 +117,3 @@ "use strict"; | ||
// | ||
let repr = (0, descriptor_1.getRepr)(currentBits, refs, levelI, type); | ||
let repr = (0, descriptor_1.getRepr)(bits, currentBits, refs, levelI, type); | ||
let hash = (0, ton_crypto_1.sha256_sync)(repr); | ||
@@ -120,0 +120,0 @@ // |
{ | ||
"name": "ton-core", | ||
"version": "0.49.0", | ||
"version": "0.49.1", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/ton-core/ton-core.git", |
@@ -84,7 +84,2 @@ /** | ||
// Corner case of empty string | ||
if (length === 0 && offset === this._length) { | ||
return BitString.EMPTY; | ||
} | ||
// Check offset | ||
@@ -97,2 +92,8 @@ if (offset >= this._length) { | ||
} | ||
// Corner case of empty string | ||
if (length === 0) { | ||
return BitString.EMPTY; | ||
} | ||
if (offset + length > this._length) { | ||
@@ -181,2 +182,2 @@ throw new Error(`Offset ${offset} + Length ${length} > ${this._length} is out of bounds`); | ||
[inspectSymbol] = () => this.toString() | ||
} | ||
} |
@@ -23,3 +23,3 @@ /** | ||
export function getRepr(bits: BitString, refs: Cell[], level: number, type: CellType) { | ||
export function getRepr(originalBits: BitString, bits: BitString, refs: Cell[], level: number, type: CellType) { | ||
@@ -33,3 +33,3 @@ // Allocate | ||
repr[reprCursor++] = getRefsDescriptor(refs, level, type); | ||
repr[reprCursor++] = getBitsDescriptor(bits); | ||
repr[reprCursor++] = getBitsDescriptor(originalBits); | ||
@@ -36,0 +36,0 @@ // Write bits |
@@ -212,3 +212,16 @@ /** | ||
}); | ||
it('should deserialize block (#21)', () => { | ||
let testCase = fs.readFileSync(__dirname + '/__testdata__/block.txt', 'utf8'); | ||
let cell = Cell.fromBase64(testCase); | ||
}); | ||
it('should hash tx with merkle body', () => { | ||
let testCase = JSON.parse(fs.readFileSync(__dirname + '/__testdata__/tx_with_merkle_body.json', 'utf8')); | ||
let boc = Buffer.from(testCase.boc, 'hex'); | ||
let cell = Cell.fromBoc(boc)[0]; | ||
expect(cell.hash().toString('hex')).toBe(testCase.hash); | ||
}); | ||
}); | ||
@@ -19,2 +19,15 @@ /** | ||
function getHashesCount(levelMask: number) { | ||
return getHashesCountFromMask(levelMask & 7) | ||
} | ||
function getHashesCountFromMask(mask: number) { | ||
let n = 0; | ||
for (let i = 0; i < 3; i++) { | ||
n += (mask & 1); | ||
mask = mask >> 1; | ||
} | ||
return n+1; // 1 repr + up to 3 higher hashes | ||
} | ||
function readCell(reader: BitReader, sizeBytes: number) { | ||
@@ -32,2 +45,12 @@ | ||
const levelMask = d1 >> 5; | ||
const hasHashes = (d1 & 16) != 0; | ||
const hash_bytes = 32; | ||
const hashesSize = hasHashes ? getHashesCount(levelMask) * hash_bytes : 0; | ||
const depthSize = hasHashes ? getHashesCount(levelMask) * 2 : 0; | ||
reader.skip(hashesSize * 8); | ||
reader.skip(depthSize * 8); | ||
// Bits | ||
@@ -34,0 +57,0 @@ let bits = BitString.EMPTY; |
@@ -132,3 +132,3 @@ /** | ||
let repr = getRepr(currentBits, refs, levelI, type); | ||
let repr = getRepr(bits, currentBits, refs, levelI, type); | ||
let hash = sha256_sync(repr); | ||
@@ -135,0 +135,0 @@ |
1169956
353
21198