Skip to content

Commit

Permalink
fix: align generatePartialSHA interface with input-generators usage
Browse files Browse the repository at this point in the history
  • Loading branch information
1 parent 13ddc28 commit 8785d2f
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/helpers/src/sha-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ export function padUint8ArrayWithZeros(array: Uint8Array, length: number): Uint8

export function generatePartialSHA({
body,
precomputeText,
shaCutoffIndex,
bodyLength,
selectorString,
maxRemainingBodyLength,
}: {
body: Uint8Array;
precomputeText: Uint8Array;
shaCutoffIndex: number;
bodyLength: number;
selectorString?: string;
maxRemainingBodyLength: number;
}): {
bodyRemaining: Uint8Array;
precomputedSha: Uint8Array;
bodyRemainingLength: number;
} {
if (!body || !(body instanceof Uint8Array)) {
throw new Error('Invalid input: body must be a Uint8Array');
}

const precomputeText = selectorString ? Buffer.from(selectorString) : body;
const shaCutoffIndex = selectorString ? body.indexOf(precomputeText[0]) : bodyLength;

if (shaCutoffIndex < 0) {
throw new Error('Negative sha cutoff index');
}
Expand All @@ -52,20 +60,21 @@ export function generatePartialSHA({

const buffer = new ArrayBuffer(maxRemainingBodyLength);
const bodyRemaining = new Uint8Array(buffer);

const slicedBody = new Uint8Array(body.buffer, shaCutoffIndex);
bodyRemaining.set(slicedBody);

if (bodyRemaining.length < maxRemainingBodyLength) {
return {
bodyRemaining: padUint8ArrayWithZeros(bodyRemaining, maxRemainingBodyLength),
precomputedSha: partialSha(precomputeText, shaCutoffIndex)
precomputedSha: partialSha(precomputeText, shaCutoffIndex),
bodyRemainingLength: bodyRemaining.length
};
}

return {
bodyRemaining,
precomputedSha: partialSha(precomputeText, shaCutoffIndex)
precomputedSha: partialSha(precomputeText, shaCutoffIndex),
bodyRemainingLength: bodyRemaining.length
};
}

Expand Down

0 comments on commit 8785d2f

Please sign in to comment.