1- import { bytesToHex , randomBytes } from '@noble/hashes/utils'
2- import { secp256k1 } from '@noble/curves/secp256k1'
1+ import { bytesToHex , hexToBytes , randomBytes } from '@noble/hashes/utils.js '
2+ import { secp256k1 } from '@noble/curves/secp256k1.js '
33import { cbc } from '@noble/ciphers/aes'
44import { base64 } from '@scure/base'
55
66import { utf8Decoder , utf8Encoder } from './utils.ts'
77
88export function encrypt ( secretKey : string | Uint8Array , pubkey : string , text : string ) : string {
9- const privkey : string = secretKey instanceof Uint8Array ? bytesToHex ( secretKey ) : secretKey
10- const key = secp256k1 . getSharedSecret ( privkey , '02' + pubkey )
9+ const privkey : Uint8Array = secretKey instanceof Uint8Array ? secretKey : hexToBytes ( secretKey )
10+ const key = secp256k1 . getSharedSecret ( privkey , hexToBytes ( '02' + pubkey ) )
1111 const normalizedKey = getNormalizedX ( key )
1212
1313 let iv = Uint8Array . from ( randomBytes ( 16 ) )
@@ -22,9 +22,9 @@ export function encrypt(secretKey: string | Uint8Array, pubkey: string, text: st
2222}
2323
2424export function decrypt ( secretKey : string | Uint8Array , pubkey : string , data : string ) : string {
25- const privkey : string = secretKey instanceof Uint8Array ? bytesToHex ( secretKey ) : secretKey
25+ const privkey : Uint8Array = secretKey instanceof Uint8Array ? secretKey : hexToBytes ( secretKey )
2626 let [ ctb64 , ivb64 ] = data . split ( '?iv=' )
27- let key = secp256k1 . getSharedSecret ( privkey , '02' + pubkey )
27+ let key = secp256k1 . getSharedSecret ( privkey , hexToBytes ( '02' + pubkey ) )
2828 let normalizedKey = getNormalizedX ( key )
2929
3030 let iv = base64 . decode ( ivb64 )
0 commit comments