space-data-module-sdk 0.4.1 → 0.5.4

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.
@@ -116,3 +116,46 @@ export async function aesGcmDecrypt(key, ciphertext, tag, iv, aad = null) {
116
116
  aad ? toUint8Array(aad) : undefined,
117
117
  );
118
118
  }
119
+
120
+ function normalizeCtrIv(nonceStart) {
121
+ const nonce = toUint8Array(nonceStart);
122
+ if (nonce.length !== 12) {
123
+ throw new Error("AES-256-CTR expects a 12-byte NONCE_START value.");
124
+ }
125
+ const iv = new Uint8Array(16);
126
+ iv.set(nonce, 0);
127
+ return iv;
128
+ }
129
+
130
+ async function getAesCtrApi() {
131
+ const wallet = await getWasmWallet();
132
+ const aesCtr = wallet?.aesCtr;
133
+ if (
134
+ !aesCtr ||
135
+ typeof aesCtr.encrypt !== "function" ||
136
+ typeof aesCtr.decrypt !== "function"
137
+ ) {
138
+ throw new Error(
139
+ "hd-wallet-wasm aesCtr API is unavailable; cannot process ENC payloads.",
140
+ );
141
+ }
142
+ return aesCtr;
143
+ }
144
+
145
+ export async function aesCtrEncrypt(key, plaintext, nonceStart) {
146
+ const aesCtr = await getAesCtrApi();
147
+ return aesCtr.encrypt(
148
+ toUint8Array(key),
149
+ toUint8Array(plaintext),
150
+ normalizeCtrIv(nonceStart),
151
+ );
152
+ }
153
+
154
+ export async function aesCtrDecrypt(key, ciphertext, nonceStart) {
155
+ const aesCtr = await getAesCtrApi();
156
+ return aesCtr.decrypt(
157
+ toUint8Array(key),
158
+ toUint8Array(ciphertext),
159
+ normalizeCtrIv(nonceStart),
160
+ );
161
+ }