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.
persistence.on('data', ({ key, value }) => {
const trits = Int8Array.from(value)
const [prefix, id] = key.toString().split(':')
if (prefix === '0') {
if (isMultipleOfTransactionLength(trits.length)) {
bundles.write(trits)
const bundle = []
for (let offset = 0; offset < trits.length; offset += TRANSACTION_LENGTH) {
bundle.push(
asTransactionObject(tritsToTrytes(trits.slice(offset, offset + TRANSACTION_LENGTH)))
)
}
withdrawalsList.push(bundle)
}
if (trits.length === CDA_LENGTH) {
deposits.write(trits)
const cda = deserializeCDAInput(trits)
depositsList.push(cda)
addresses.push(tritsToTrytes(cda.address))
batch: (ops: ReadonlyArray>) => {
for (const { type, value } of ops) {
if (type === PersistenceBatchTypes.writeBundle || type === PersistenceBatchTypes.deleteBundle) {
if (!isMultipleOfTransactionLength(value.length)) {
throw new RangeError(errors.ILLEGAL_BUNDLE_LENGTH)
}
} else if (
type === PersistenceBatchTypes.writeCDA ||
type === PersistenceBatchTypes.deleteCDA
) {
if (value.length !== CDA_LENGTH) {
throw new RangeError(errors.ILLEGAL_CDA_LENGTH)
}
} else {
throw new Error(errors.ILLEGAL_BATCH)
}
}
return ready()
.then(() =>
}) => ({ value }: { value: Buffer }) => {
const trits = bytesToTrits(value)
if (trits.length !== 0 && isMultipleOfTransactionLength(trits.length)) {
bundles.write(trits)
}
if (trits.length === CDA_LENGTH) {
deposits.write(trits)
}
}
throw new RangeError(errors.ILLEGAL_TRANSACTION_BUFFER_LENGTH)
}
if (!Number.isInteger(offset)) {
throw new TypeError(errors.ILLEGAL_TRANSACTION_OFFSET)
}
if (!isMultipleOfTransactionLength(offset)) {
throw new RangeError(errors.ILLEGAL_TRANSACTION_OFFSET)
}
if (!Number.isInteger(length)) {
throw new TypeError(errors.ILLEGAL_BUNDLE_LENGTH)
}
if (!isMultipleOfTransactionLength(length)) {
throw new RangeError(errors.ILLEGAL_BUNDLE_LENGTH)
}
let sum = 0
for (let bundleOffset = 0; bundleOffset < length; bundleOffset += TRANSACTION_LENGTH) {
sum += tritsToValue(transactionValue(buffer, offset + bundleOffset))
}
return sum
}
export const valueSum = (buffer: Int8Array, offset: number, length: number): number => {
if (!isMultipleOfTransactionLength(buffer.length)) {
throw new RangeError(errors.ILLEGAL_TRANSACTION_BUFFER_LENGTH)
}
if (!Number.isInteger(offset)) {
throw new TypeError(errors.ILLEGAL_TRANSACTION_OFFSET)
}
if (!isMultipleOfTransactionLength(offset)) {
throw new RangeError(errors.ILLEGAL_TRANSACTION_OFFSET)
}
if (!Number.isInteger(length)) {
throw new TypeError(errors.ILLEGAL_BUNDLE_LENGTH)
}
if (!isMultipleOfTransactionLength(length)) {
throw new RangeError(errors.ILLEGAL_BUNDLE_LENGTH)
}
let sum = 0
for (let bundleOffset = 0; bundleOffset < length; bundleOffset += TRANSACTION_LENGTH) {
sum += tritsToValue(transactionValue(buffer, offset + bundleOffset))
}