@tangle-network/agent-interface 5.10.25 → 5.10.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,6 +12,7 @@ export declare function sha256Bytes(value: Uint8Array): Sha256Digest;
|
|
|
12
12
|
/** RFC 8785 serialization used by every candidate-producing package. */
|
|
13
13
|
export declare function canonicalCandidateJson(value: unknown): string;
|
|
14
14
|
export declare function canonicalCandidateBytes(value: unknown): Uint8Array;
|
|
15
|
+
/** The digest of {@link canonicalCandidateBytes}, hashed piece by piece: memory stays near the value's own size, not several times its encoding. */
|
|
15
16
|
export declare function canonicalCandidateDigest(value: unknown): Sha256Digest;
|
|
16
17
|
export declare function omitTopLevelDigest<T extends {
|
|
17
18
|
digest: Sha256Digest;
|
|
@@ -36,40 +36,65 @@ export function sha256Utf8(value) {
|
|
|
36
36
|
return sha256Bytes(new TextEncoder().encode(value));
|
|
37
37
|
}
|
|
38
38
|
export function sha256Bytes(value) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
return digestHex(sha256(value));
|
|
40
|
+
}
|
|
41
|
+
function digestHex(bytes) {
|
|
42
|
+
return `sha256:${Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("")}`;
|
|
42
43
|
}
|
|
43
44
|
/** RFC 8785 serialization used by every candidate-producing package. */
|
|
44
45
|
export function canonicalCandidateJson(value) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return serializeCanonicalCandidate(value);
|
|
46
|
+
const parts = [];
|
|
47
|
+
writeCanonicalCandidate(value, (part) => parts.push(part));
|
|
48
|
+
return parts.join("");
|
|
49
49
|
}
|
|
50
50
|
export function canonicalCandidateBytes(value) {
|
|
51
51
|
return new TextEncoder().encode(canonicalCandidateJson(value));
|
|
52
52
|
}
|
|
53
|
+
/** The digest of {@link canonicalCandidateBytes}, hashed piece by piece: memory stays near the value's own size, not several times its encoding. */
|
|
53
54
|
export function canonicalCandidateDigest(value) {
|
|
54
|
-
|
|
55
|
+
const hash = sha256.create();
|
|
56
|
+
const encoder = new TextEncoder();
|
|
57
|
+
let pending = "";
|
|
58
|
+
writeCanonicalCandidate(value, (part) => {
|
|
59
|
+
pending += part;
|
|
60
|
+
if (pending.length >= 65_536) {
|
|
61
|
+
hash.update(encoder.encode(pending));
|
|
62
|
+
pending = "";
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return digestHex(hash.update(encoder.encode(pending)).digest());
|
|
55
66
|
}
|
|
56
67
|
export function omitTopLevelDigest(value) {
|
|
57
68
|
const { digest: _digest, ...material } = value;
|
|
58
69
|
return material;
|
|
59
70
|
}
|
|
60
|
-
function
|
|
61
|
-
if (value
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
if (typeof value === "string")
|
|
65
|
-
return JSON.stringify(value);
|
|
66
|
-
if (Array.isArray(value)) {
|
|
67
|
-
return `[${value.map(serializeCanonicalCandidate).join(",")}]`;
|
|
71
|
+
function writeCanonicalCandidate(value, write) {
|
|
72
|
+
if (!isCanonicalJsonValue(value)) {
|
|
73
|
+
throw new Error("candidate document must be finite, acyclic RFC 8785 JSON");
|
|
68
74
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
const walk = (item) => {
|
|
76
|
+
if (item === null || typeof item !== "object") {
|
|
77
|
+
write(JSON.stringify(item));
|
|
78
|
+
}
|
|
79
|
+
else if (Array.isArray(item)) {
|
|
80
|
+
write("[");
|
|
81
|
+
item.forEach((member, index) => {
|
|
82
|
+
if (index > 0)
|
|
83
|
+
write(",");
|
|
84
|
+
walk(member);
|
|
85
|
+
});
|
|
86
|
+
write("]");
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
write("{");
|
|
90
|
+
Object.keys(item).sort().forEach((key, index) => {
|
|
91
|
+
write(`${index > 0 ? "," : ""}${JSON.stringify(key)}:`);
|
|
92
|
+
walk(item[key]);
|
|
93
|
+
});
|
|
94
|
+
write("}");
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
walk(value);
|
|
73
98
|
}
|
|
74
99
|
export function isWellFormedUnicode(value) {
|
|
75
100
|
for (let index = 0; index < value.length; index++) {
|