From 3647bbd68ac1b9defa9e344d90bc16f294433f55 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 17 Feb 2024 18:29:01 -0300 Subject: [PATCH] get rid of the last vestiges of webcrypto dependencies. --- nip47.test.ts | 5 ----- nip96.ts | 14 +++----------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/nip47.test.ts b/nip47.test.ts index 6c9f1b5..31197f3 100644 --- a/nip47.test.ts +++ b/nip47.test.ts @@ -1,14 +1,9 @@ -import crypto from 'node:crypto' import { describe, test, expect } from 'bun:test' import { hexToBytes } from '@noble/hashes/utils' import { makeNwcRequestEvent, parseConnectionString } from './nip47' import { decrypt } from './nip04.ts' import { NWCWalletRequest } from './kinds.ts' -// @ts-ignore -// eslint-disable-next-line no-undef -globalThis.crypto = crypto - describe('parseConnectionString', () => { test('returns pubkey, relay, and secret if connection string is valid', () => { const connectionString = diff --git a/nip96.ts b/nip96.ts index 734adcf..4d5f25e 100644 --- a/nip96.ts +++ b/nip96.ts @@ -1,5 +1,7 @@ +import { sha256 } from '@noble/hashes/sha256' import { EventTemplate } from './core' import { FileServerPreference } from './kinds' +import { bytesToHex } from '@noble/hashes/utils' /** * Represents the configuration for a server compliant with NIP-96. @@ -576,15 +578,5 @@ export function generateFSPEventTemplate(serverUrls: string[]): EventTemplate { * @returns A promise that resolves to the SHA-256 hash of the file. */ export async function calculateFileHash(file: Blob): Promise { - // Read the file as an ArrayBuffer - const buffer = await file.arrayBuffer() - - // Calculate the SHA-256 hash of the file - const hashBuffer = await crypto.subtle.digest('SHA-256', buffer) - - // Convert the hash to a hexadecimal string - const hashArray = Array.from(new Uint8Array(hashBuffer)) - const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('') - - return hashHex + return bytesToHex(sha256(new Uint8Array(await file.arrayBuffer()))) }