diff options
Diffstat (limited to 'src/lib/nip44.ts')
| -rw-r--r-- | src/lib/nip44.ts | 48 |
1 files changed, 12 insertions, 36 deletions
diff --git a/src/lib/nip44.ts b/src/lib/nip44.ts index e05fc685..f1f872b5 100644 --- a/src/lib/nip44.ts +++ b/src/lib/nip44.ts @@ -5,10 +5,7 @@ import { chacha20 } from "@noble/ciphers/chacha.js"; import { equalBytes } from "@noble/ciphers/utils.js"; import { secp256k1 } from "@noble/curves/secp256k1.js"; -import { - extract as hkdf_extract, - expand as hkdf_expand, -} from "@noble/hashes/hkdf.js"; +import { extract as hkdf_extract, expand as hkdf_expand } from "@noble/hashes/hkdf.js"; import { hmac } from "@noble/hashes/hmac.js"; import { sha256 } from "@noble/hashes/sha2.js"; import { concatBytes, randomBytes, utf8ToBytes } from "@noble/hashes/utils.js"; @@ -27,9 +24,7 @@ export const u = { }, getConversationKey(privkeyA: string, pubkeyB: string): Uint8Array { - const sharedX = secp256k1 - .getSharedSecret(privkeyA, "02" + pubkeyB) - .subarray(1, 33); + const sharedX = secp256k1.getSharedSecret(privkeyA, "02" + pubkeyB).subarray(1, 33); return hkdf_extract(sha256, sharedX, "nip44-v2"); }, @@ -45,8 +40,7 @@ export const u = { }, calcPaddedLen(len: number): number { - if (!Number.isSafeInteger(len) || len < 1) - throw new Error("expected positive integer"); + if (!Number.isSafeInteger(len) || len < 1) throw new Error("expected positive integer"); if (len <= 32) return 32; const nextPower = 1 << (Math.floor(Math.log2(len - 1)) + 1); const chunk = nextPower <= 256 ? 32 : nextPower / 8; @@ -54,14 +48,8 @@ export const u = { }, writeU16BE(num: number) { - if ( - !Number.isSafeInteger(num) || - num < u.minPlaintextSize || - num > u.maxPlaintextSize - ) - throw new Error( - "invalid plaintext size: must be between 1 and 65535 bytes", - ); + if (!Number.isSafeInteger(num) || num < u.minPlaintextSize || num > u.maxPlaintextSize) + throw new Error("invalid plaintext size: must be between 1 and 65535 bytes"); const arr = new Uint8Array(2); new DataView(arr.buffer).setUint16(0, num, false); return arr; @@ -89,8 +77,7 @@ export const u = { }, hmacAad(key: Uint8Array, message: Uint8Array, aad: Uint8Array) { - if (aad.length !== 32) - throw new Error("AAD associated data must be 32 bytes"); + if (aad.length !== 32) throw new Error("AAD associated data must be 32 bytes"); const combined = concatBytes(aad, message); return hmac(sha256, key, combined); }, @@ -102,11 +89,9 @@ export const u = { // raw payload: 99 (65+32+2) to 65603 (65+0xffff+2) // compressed payload (base64): 132b to 87472b decodePayload(payload: string) { - if (typeof payload !== "string") - throw new Error("payload must be a valid string"); + if (typeof payload !== "string") throw new Error("payload must be a valid string"); const plen = payload.length; - if (plen < 132 || plen > 87472) - throw new Error("invalid payload length: " + plen); + if (plen < 132 || plen > 87472) throw new Error("invalid payload length: " + plen); if (payload[0] === "#") throw new Error("unknown encryption version"); let data: Uint8Array; try { @@ -115,8 +100,7 @@ export const u = { throw new Error("invalid base64: " + (error as any).message); } const dlen = data.length; - if (dlen < 99 || dlen > 65603) - throw new Error("invalid data length: " + dlen); + if (dlen < 99 || dlen > 65603) throw new Error("invalid data length: " + dlen); const vers = data[0]; if (vers !== 2) throw new Error("unknown encryption version " + vers); return { @@ -132,24 +116,16 @@ export function encrypt( conversationKey: Uint8Array, nonce = randomBytes(32), ): string { - const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys( - conversationKey, - nonce, - ); + const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce); const padded = u.pad(plaintext); const ciphertext = chacha20(chacha_key, chacha_nonce, padded); const mac = u.hmacAad(hmac_key, ciphertext, nonce); - return base64.encode( - concatBytes(new Uint8Array([2]), nonce, ciphertext, mac), - ); + return base64.encode(concatBytes(new Uint8Array([2]), nonce, ciphertext, mac)); } export function decrypt(payload: string, conversationKey: Uint8Array): string { const { nonce, ciphertext, mac } = u.decodePayload(payload); - const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys( - conversationKey, - nonce, - ); + const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce); const calculatedMac = u.hmacAad(hmac_key, ciphertext, nonce); if (!equalBytes(calculatedMac, mac)) throw new Error("invalid MAC"); const padded = chacha20(chacha_key, chacha_nonce, ciphertext); |
