Skip to content

Commit d495765

Browse files
committed
chore: initialise TAGGED_HASH_PREFIXES on-demand
1 parent a05587f commit d495765

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/crypto.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ const TAGS = [
4949
'KeyAgg coefficient',
5050
];
5151
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
52-
const TAGGED_HASH_PREFIXES = Object.fromEntries(
53-
TAGS.map(tag => {
54-
const tagHash = sha256(Buffer.from(tag));
55-
return [tag, Buffer.concat([tagHash, tagHash])];
56-
}),
57-
);
52+
let TAGGED_HASH_PREFIXES = undefined;
5853
function taggedHash(prefix, data) {
54+
if (!TAGGED_HASH_PREFIXES) {
55+
TAGGED_HASH_PREFIXES = Object.fromEntries(
56+
TAGS.map(tag => {
57+
const tagHash = sha256(Buffer.from(tag));
58+
return [tag, Buffer.concat([tagHash, tagHash])];
59+
}),
60+
);
61+
}
5962
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));
6063
}
6164
exports.taggedHash = taggedHash;

ts_src/crypto.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ const TAGS = [
4242
] as const;
4343
export type TaggedHashPrefix = typeof TAGS[number];
4444
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
45-
const TAGGED_HASH_PREFIXES = Object.fromEntries(
46-
TAGS.map(tag => {
47-
const tagHash = sha256(Buffer.from(tag));
48-
return [tag, Buffer.concat([tagHash, tagHash])];
49-
}),
50-
) as { [k in TaggedHashPrefix]: Buffer };
45+
let TAGGED_HASH_PREFIXES = undefined as
46+
| { [k in TaggedHashPrefix]: Buffer }
47+
| undefined;
5148

5249
export function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer {
50+
if (!TAGGED_HASH_PREFIXES) {
51+
TAGGED_HASH_PREFIXES = Object.fromEntries(
52+
TAGS.map(tag => {
53+
const tagHash = sha256(Buffer.from(tag));
54+
return [tag, Buffer.concat([tagHash, tagHash])];
55+
}),
56+
) as { [k in TaggedHashPrefix]: Buffer };
57+
}
5358
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));
5459
}

0 commit comments

Comments
 (0)