Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Drop Buffer #217

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/JWE.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fromString } from 'uint8arrays'
import { base64ToBytes, bytesToBase64url, decodeBase64url, toSealed } from './util'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -128,7 +129,7 @@ export async function decryptJWE(jwe: JWE, decrypter: Decrypter): Promise<Uint8A
if (protHeader.enc !== decrypter.enc)
throw new Error(`not_supported: Decrypter does not supported: '${protHeader.enc}'`)
const sealed = toSealed(jwe.ciphertext, jwe.tag)
const aad = new Uint8Array(Buffer.from(jwe.aad ? `${jwe.protected}.${jwe.aad}` : jwe.protected))
const aad = fromString(jwe.aad ? `${jwe.protected}.${jwe.aad}` : jwe.protected)
let cleartext = null
if (protHeader.alg === 'dir' && decrypter.alg === 'dir') {
cleartext = await decrypter.decrypt(sealed, base64ToBytes(jwe.iv), aad)
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/JWT.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { VerificationMethod } from 'did-resolver'
import { TokenVerifier } from 'jsontokens'
import MockDate from 'mockdate'
import { fromString } from 'uint8arrays/from-string'
import { toString } from 'uint8arrays/to-string'
import {
createJWS,
createJWT,
Expand Down Expand Up @@ -669,7 +671,7 @@ describe('JWS', () => {
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)
Expand All @@ -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()
})
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseKey } from '../util'
import { fromString } from 'uint8arrays/from-string'

describe('parseKey', () => {
const privateKeyBase58 = '2sxRbZxrkTR1tmUH88aWcosMRf8zianLjV3vZcVewCDzgimGt5gLeHx1cm4bqfeEuVmDaCREgUNZbKHJAB8HHf9e'
Expand All @@ -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', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/xc20pEncryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -145,7 +146,7 @@ export function xc20pDirEncrypter(key: Uint8Array): Encrypter {
aad?: Uint8Array
): Promise<EncryptionResult> {
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,
Expand Down