MCPcopy Create free account
hub / github.com/PageLeay/celestial-runtime / _getBytes

Function _getBytes

src.ts/utils/data.ts:28–45  ·  view source on GitHub ↗
(value: BytesLike, name?: string, copy?: boolean)

Source from the content-addressed store, hash-verified

26export type BytesLike = DataHexString | Uint8Array;
27
28function _getBytes(value: BytesLike, name?: string, copy?: boolean): Uint8Array {
29 if (value instanceof Uint8Array) {
30 if (copy) { return new Uint8Array(value); }
31 return value;
32 }
33
34 if (typeof(value) === "string" && value.match(/^0x(?:[0-9a-f][0-9a-f])*$/i)) {
35 const result = new Uint8Array((value.length - 2) / 2);
36 let offset = 2;
37 for (let i = 0; i < result.length; i++) {
38 result[i] = parseInt(value.substring(offset, offset + 2), 16);
39 offset += 2;
40 }
41 return result;
42 }
43
44 assertArgument(false, "invalid BytesLike value", name || "value", value);
45}
46
47/**
48 * Get a typed Uint8Array for %%value%%. If already a Uint8Array

Callers 2

getBytesFunction · 0.70
getBytesCopyFunction · 0.70

Calls 1

assertArgumentFunction · 0.90

Tested by

no test coverage detected