Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const finalizeBundle = (bundle: Int8Array): Int8Array => {
if (!isMultipleOfTransactionLength(bundle.length)) {
throw new Error(errors.ILLEGAL_TRANSACTION_BUFFER_LENGTH)
}
const sponge = new Kerl()
const bundleCopy = bundle.slice()
const bundleHash = new Int8Array(BUNDLE_LENGTH)
// This block recomputes bundle hash by incrementing `obsoleteTag` field of first transaction in the bundle.
// Normalized bundle should NOT contain value `13`.
while (true) {
// Absorb essence trits to squeeze bundle hash.
for (let offset = 0; offset < bundle.length; offset += TRANSACTION_LENGTH) {
sponge.absorb(transactionEssence(bundleCopy, offset), 0, TRANSACTION_ESSENCE_LENGTH)
}
// Set new bundle hash value.
sponge.squeeze(bundleHash, 0, BUNDLE_LENGTH)
// Stop mutation if essence results to secure bundle.
if (normalizedBundle(bundleHash).indexOf(MAX_TRYTE_VALUE /* 13 */) === -1) {
// Essence results to secure bundle.
break
}
// Essence results to insecure bundle. (Normalized bundle hash contains value `13`.)
bundleCopy.set(
increment(bundleCopy.subarray(OBSOLETE_TAG_OFFSET, OBSOLETE_TAG_OFFSET + OBSOLETE_TAG_LENGTH)),
OBSOLETE_TAG_OFFSET
)