From e14d866acab3d38a7fdceadc77dae40e02abe17e Mon Sep 17 00:00:00 2001 From: Sergey Ukustov Date: Thu, 27 Jan 2022 16:20:22 -0500 Subject: [PATCH] feat: Drop Buffer --- src/JWE.ts | 3 ++- src/__tests__/JWT.test.ts | 6 ++++-- src/__tests__/util.test.ts | 5 +++-- src/xc20pEncryption.ts | 3 ++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/JWE.ts b/src/JWE.ts index 8e293c59..86ba7520 100644 --- a/src/JWE.ts +++ b/src/JWE.ts @@ -1,3 +1,4 @@ +import { fromString } from 'uint8arrays' import { base64ToBytes, bytesToBase64url, decodeBase64url, toSealed } from './util' // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -128,7 +129,7 @@ export async function decryptJWE(jwe: JWE, decrypter: Decrypter): Promise { it('createJWS works with base64url payload', async () => { expect.assertions(2) // use the hex public key as an arbitrary payload - const encodedPayload = bytesToBase64url(Buffer.from(publicKey, 'hex')) + const encodedPayload = bytesToBase64url(fromString(publicKey, 'base16')) const jws = await createJWS(encodedPayload, signer) expect(jws).toMatchSnapshot() expect(jws.split('.')[1]).toEqual(encodedPayload) @@ -684,7 +686,7 @@ describe('JWS', () => { it('verifyJWS works with base64url payload', async () => { expect.assertions(1) - const encodedPayload = bytesToBase64url(Buffer.from(publicKey, 'hex')) + const encodedPayload = bytesToBase64url(fromString(publicKey, 'base16')) const jws = await createJWS(encodedPayload, signer) expect(() => verifyJWS(jws, { publicKeyHex: publicKey } as VerificationMethod)).not.toThrow() }) diff --git a/src/__tests__/util.test.ts b/src/__tests__/util.test.ts index 43c77ae7..ff542829 100644 --- a/src/__tests__/util.test.ts +++ b/src/__tests__/util.test.ts @@ -1,4 +1,5 @@ import { parseKey } from '../util' +import { fromString } from 'uint8arrays/from-string' describe('parseKey', () => { const privateKeyBase58 = '2sxRbZxrkTR1tmUH88aWcosMRf8zianLjV3vZcVewCDzgimGt5gLeHx1cm4bqfeEuVmDaCREgUNZbKHJAB8HHf9e' @@ -9,8 +10,8 @@ describe('parseKey', () => { const privateKeyBase64 = 'XfWLsGwi/rrGOClsD2KXA+K55KHZ1oKY3ewT0XV9c+BW0pm4zy/FqxLt72wLKKfVftBkmhiLbFuI508gh5LmLQ' const privateKeyBase64Url = 'XfWLsGwi_rrGOClsD2KXA-K55KHZ1oKY3ewT0XV9c-BW0pm4zy_FqxLt72wLKKfVftBkmhiLbFuI508gh5LmLQ' - const privateKeyBytes = Uint8Array.from( - Buffer.from('XfWLsGwi/rrGOClsD2KXA+K55KHZ1oKY3ewT0XV9c+BW0pm4zy/FqxLt72wLKKfVftBkmhiLbFuI508gh5LmLQ', 'base64') + const privateKeyBytes = fromString( + 'XfWLsGwi/rrGOClsD2KXA+K55KHZ1oKY3ewT0XV9c+BW0pm4zy/FqxLt72wLKKfVftBkmhiLbFuI508gh5LmLQ', 'base64' ) it('parses hex', () => { diff --git a/src/xc20pEncryption.ts b/src/xc20pEncryption.ts index 55dd3a2d..5b11824c 100644 --- a/src/xc20pEncryption.ts +++ b/src/xc20pEncryption.ts @@ -6,6 +6,7 @@ import { bytesToBase64url, base58ToBytes, encodeBase64url, toSealed, base64ToByt import { Recipient, EncryptionResult, Encrypter, Decrypter, ProtectedHeader } from './JWE' import type { VerificationMethod, Resolvable } from 'did-resolver' import { ECDH } from './ECDH' +import { fromString } from 'uint8arrays/from-string' /** * Extra parameters for JWE using authenticated encryption @@ -145,7 +146,7 @@ export function xc20pDirEncrypter(key: Uint8Array): Encrypter { aad?: Uint8Array ): Promise { const protHeader = encodeBase64url(JSON.stringify(Object.assign({ alg }, protectedHeader, { enc }))) - const encodedAad = new Uint8Array(Buffer.from(aad ? `${protHeader}.${bytesToBase64url(aad)}` : protHeader)) + const encodedAad = fromString(aad ? `${protHeader}.${bytesToBase64url(aad)}` : protHeader) return { ...xc20pEncrypt(cleartext, encodedAad), protectedHeader: protHeader,