Skip to content

Commit

Permalink
Switching to viem toHex/toBytes encoding util & prefixing hex with 0x
Browse files Browse the repository at this point in the history
  • Loading branch information
mattschoch committed Mar 4, 2024
1 parent 52f8c46 commit ba6295b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toBytes, toHex } from '@narval/policy-engine-shared'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { Test } from '@nestjs/testing'
import { mock } from 'jest-mock-extended'
Expand Down Expand Up @@ -30,7 +31,7 @@ describe('EncryptionService', () => {
Promise.resolve({
// unencryptedMasterKey: dfd9cc70f1ad02d19e0efa020d82f557022f59ca6bedbec1df38e8fd37ae3bb9
masterKey:
'0205785d67737fa3bae8eb249cf8d3baed5942f1677d8c98b4cdeef55560a3bcf510bd008d00030003617070000d61726d6f72792d656e67696e6500156177732d63727970746f2d7075626c69632d6b657900444177336764324b6e58646f512f2b76745347367031444442384d65766d61434b324c7861426e65476a315531537777526b376b4d366868752f707a446f48724c77773d3d0007707572706f7365000f646174612d656e6372797074696f6e000100146e617276616c2e61726d6f72792e656e67696e65002561726d6f72792e656e67696e652e6b656b000000800000000c8a92a7c9deb43316f6c29e8d0030132d63c7337c9888a06b638966e83056a0575958b42588b7aed999b9659e6d4bc5bed4664d91fae0b14d48917e00cdbb02000010000749ed0ed3616b7990f9e73f5a42eb46dc182002612e33dcb8e3c7d4759184c46ce3f0893a87ac15257d53097ac5d74affffffff00000001000000000000000000000001000000205d7209b51db8cf8264b9065add71a8514dc26baa6987d8a0a3acb1c4a2503b0f3b7c974a35ed234c1b94668736cd8bfa00673065023100a5d8d192e9802649dab86af6e00ab6d7472533e85dfe1006cb8bd9ef2472d15096fa42e742d18cb92530c762c3bd44d40230350299b42feaa1149c6ad1b25add24c30b3bf1c08263b96df0d43e2ad3e19802872e792040f1faf3d0a73bca6fb067ca',
'0x0205785d67737fa3bae8eb249cf8d3baed5942f1677d8c98b4cdeef55560a3bcf510bd008d00030003617070000d61726d6f72792d656e67696e6500156177732d63727970746f2d7075626c69632d6b657900444177336764324b6e58646f512f2b76745347367031444442384d65766d61434b324c7861426e65476a315531537777526b376b4d366868752f707a446f48724c77773d3d0007707572706f7365000f646174612d656e6372797074696f6e000100146e617276616c2e61726d6f72792e656e67696e65002561726d6f72792e656e67696e652e6b656b000000800000000c8a92a7c9deb43316f6c29e8d0030132d63c7337c9888a06b638966e83056a0575958b42588b7aed999b9659e6d4bc5bed4664d91fae0b14d48917e00cdbb02000010000749ed0ed3616b7990f9e73f5a42eb46dc182002612e33dcb8e3c7d4759184c46ce3f0893a87ac15257d53097ac5d74affffffff00000001000000000000000000000001000000205d7209b51db8cf8264b9065add71a8514dc26baa6987d8a0a3acb1c4a2503b0f3b7c974a35ed234c1b94668736cd8bfa00673065023100a5d8d192e9802649dab86af6e00ab6d7472533e85dfe1006cb8bd9ef2472d15096fa42e742d18cb92530c762c3bd44d40230350299b42feaa1149c6ad1b25add24c30b3bf1c08263b96df0d43e2ad3e19802872e792040f1faf3d0a73bca6fb067ca',
id: 'test-engine-id'
})
)
Expand Down Expand Up @@ -70,10 +71,10 @@ describe('EncryptionService', () => {
})

it('should encrypt then decrypt successfully, with a buffer from a hexstring', async () => {
const data = 'dfd9cc70f1ad02d19e0efa020d82f557022f59ca6bedbec1df38e8fd37ae3bb9'
const encrypted = await service.encrypt(Buffer.from(data, 'hex'))
const data = '0xdfd9cc70f1ad02d19e0efa020d82f557022f59ca6bedbec1df38e8fd37ae3bb9'
const encrypted = await service.encrypt(toBytes(data))
const decrypted = await service.decrypt(encrypted)

expect(decrypted.toString('hex')).toBe(data)
expect(toHex(decrypted)).toBe(data)
})
})
11 changes: 6 additions & 5 deletions apps/policy-engine/src/encryption/core/encryption.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RawAesWrappingSuiteIdentifier,
buildClient
} from '@aws-crypto/client-node'
import { toBytes, toHex } from '@narval/policy-engine-shared'
import { Inject, Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import crypto from 'crypto'
Expand Down Expand Up @@ -56,7 +57,7 @@ export class EncryptionService implements OnApplicationBootstrap {
encryptedMasterKey = await this.generateMasterKey(kek)
}

const decryptedMasterKey = await this.decryptMasterKey(kek, Buffer.from(encryptedMasterKey, 'hex'))
const decryptedMasterKey = await this.decryptMasterKey(kek, toBytes(encryptedMasterKey))
const isolatedMasterKey = Buffer.alloc(decryptedMasterKey.length)
decryptedMasterKey.copy(isolatedMasterKey, 0, 0, decryptedMasterKey.length)

Expand Down Expand Up @@ -110,7 +111,7 @@ export class EncryptionService implements OnApplicationBootstrap {
return result
}

private async decryptMasterKey(kek: Buffer, ciphertext: Buffer): Promise<Buffer> {
private async decryptMasterKey(kek: Buffer, ciphertext: Uint8Array): Promise<Buffer> {
const keyring = this.getKeyEncryptionKeyring(kek)
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)

Expand All @@ -124,7 +125,7 @@ export class EncryptionService implements OnApplicationBootstrap {
return plaintext
}

async encrypt(cleartext: string | Buffer): Promise<Buffer> {
async encrypt(cleartext: string | Buffer | Uint8Array): Promise<Buffer> {
const keyring = this.keyring
if (!keyring) throw new Error('Keyring not set')

Expand All @@ -135,7 +136,7 @@ export class EncryptionService implements OnApplicationBootstrap {
return result
}

async decrypt(ciphertext: Buffer): Promise<Buffer> {
async decrypt(ciphertext: Buffer | Uint8Array): Promise<Buffer> {
const keyring = this.keyring
if (!keyring) throw new Error('Keyring not set')

Expand All @@ -158,7 +159,7 @@ export class EncryptionService implements OnApplicationBootstrap {

// Encrypt it with the Key Encryption Key (KEK) that was derived from the MP
const encryptedMk = await this.encryptMaterKey(kek, mkBuffer)
const encryptedMkString = encryptedMk.toString('hex')
const encryptedMkString = toHex(encryptedMk)

// Save the Result.
const existingEngine = await this.encryptionRepository.getEngine(this.engineId)
Expand Down
1 change: 1 addition & 0 deletions packages/policy-engine-shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './lib/type/data-store.type'
export * from './lib/type/domain.type'
export * from './lib/type/entity.type'
export * from './lib/util/caip.util'
export * from './lib/util/encoding.util'
export * as EntityUtil from './lib/util/entity.util'
export * from './lib/util/enum.util'
export * from './lib/util/evm.util'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { toBytes, toHex } from 'viem/utils'

0 comments on commit ba6295b

Please sign in to comment.