Skip to content

Commit 9b003aa

Browse files
committed
fix: add toBase64() util
1 parent 900f52c commit 9b003aa

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/beacon-node/src/util/sszBytes.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function getSeenAttDataKeyElectra(electraAttestationBytes: Uint8Array): A
8989
return null;
9090
}
9191

92-
return Buffer.from(electraAttestationBytes.subarray(startIndex, startIndex + seenKeyLength)).toString("base64");
92+
return toBase64(electraAttestationBytes.subarray(startIndex, startIndex + seenKeyLength));
9393
}
9494

9595
/**
@@ -102,9 +102,7 @@ export function getSeenAttDataKeyPhase0(data: Uint8Array): AttDataBase64 | null
102102
}
103103

104104
// base64 is a bit efficient than hex
105-
return Buffer.from(data.subarray(VARIABLE_FIELD_OFFSET, VARIABLE_FIELD_OFFSET + ATTESTATION_DATA_SIZE)).toString(
106-
"base64"
107-
);
105+
return toBase64(data.subarray(VARIABLE_FIELD_OFFSET, VARIABLE_FIELD_OFFSET + ATTESTATION_DATA_SIZE));
108106
}
109107

110108
/**
@@ -229,7 +227,7 @@ export function getSeenAttDataKeyFromSignedAggregateAndProofElectra(data: Uint8A
229227
}
230228

231229
// base64 is a bit efficient than hex
232-
return Buffer.from(data.subarray(startIndex, endIndex)).toString("base64");
230+
return toBase64(data.subarray(startIndex, endIndex));
233231
}
234232

235233
/**
@@ -242,12 +240,12 @@ export function getSeenAttDataKeyFromSignedAggregateAndProofPhase0(data: Uint8Ar
242240
}
243241

244242
// base64 is a bit efficient than hex
245-
return Buffer.from(
243+
return toBase64(
246244
data.subarray(
247245
SIGNED_AGGREGATE_AND_PROOF_SLOT_OFFSET,
248246
SIGNED_AGGREGATE_AND_PROOF_SLOT_OFFSET + ATTESTATION_DATA_SIZE
249247
)
250-
).toString("base64");
248+
);
251249
}
252250

253251
/**
@@ -301,3 +299,7 @@ function getSlotFromOffset(data: Uint8Array, offset: number): Slot {
301299
// Read only the first 4 bytes of Slot, max value is 4,294,967,295 will be reached 1634 years after genesis
302300
return dv.getUint32(offset, true);
303301
}
302+
303+
function toBase64(data: Uint8Array): string {
304+
return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString("base64");
305+
}

0 commit comments

Comments
 (0)