Skip to content

Commit

Permalink
rename hash-request to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Mar 1, 2024
1 parent 250519d commit 445633a
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Feed, HistoricalTransfer, Signature } from '@narval/policy-engine-shared'
import { Alg, hashRequest } from '@narval/signature'
import { Alg, hash } from '@narval/signature'
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { mapValues, omit } from 'lodash/fp'
Expand Down Expand Up @@ -30,7 +30,7 @@ export class HistoricalTransferFeedService implements DataFeed<HistoricalTransfe
async sign(data: HistoricalTransfer[]): Promise<Signature> {
const account = privateKeyToAccount(this.getPrivateKey())
const sig = await account.signMessage({
message: hashRequest(data)
message: hash(data)
})

return {
Expand Down
4 changes: 2 additions & 2 deletions apps/armory/src/data-feed/core/service/price-feed.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action, AssetId, Feed, Signature } from '@narval/policy-engine-shared'
import { Alg, hashRequest } from '@narval/signature'
import { Alg, hash } from '@narval/signature'
import { InputType, Intents, safeDecode } from '@narval/transaction-request-intent'
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
Expand Down Expand Up @@ -29,7 +29,7 @@ export class PriceFeedService implements DataFeed<Prices> {
async sign(data: Prices): Promise<Signature> {
const account = privateKeyToAccount(this.getPrivateKey())
const sig = await account.signMessage({
message: hashRequest(data)
message: hash(data)
})

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Decision, EvaluationResponse, Feed, Prices } from '@narval/policy-engine-shared'
import { Alg, hashRequest } from '@narval/signature'
import { Alg, hash } from '@narval/signature'
import { Test } from '@nestjs/testing'
import { MockProxy, mock } from 'jest-mock-extended'
import { PrivateKeyAccount, generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
Expand Down Expand Up @@ -99,8 +99,8 @@ describe(ClusterService.name, () => {
authzRequest: AuthorizationRequest,
partial?: Partial<EvaluationResponse>
): Promise<EvaluationResponse> => {
const hash = hashRequest(authzRequest.request)
const signature = await nodeAccount.signMessage({ message: hash })
const requestHash = hash(authzRequest.request)
const signature = await nodeAccount.signMessage({ message: requestHash })

return {
decision: Decision.PERMIT,
Expand Down Expand Up @@ -168,7 +168,7 @@ describe(ClusterService.name, () => {
it('throws when node attestation is invalid', async () => {
const permit = await generateEvaluationResponse(nodeAccount, authzRequest)
const signature = await nodeAccount.signMessage({
message: hashRequest({ notTheOriginalRequest: true })
message: hash({ notTheOriginalRequest: true })
})

authzApplicationClientMock.evaluation.mockResolvedValue({
Expand Down
6 changes: 3 additions & 3 deletions apps/armory/src/orchestration/core/service/cluster.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Decision, EvaluationRequest, EvaluationResponse } from '@narval/policy-engine-shared'
import { hashRequest } from '@narval/signature'
import { hash } from '@narval/signature'
import { Injectable, Logger } from '@nestjs/common'
import { zip } from 'lodash/fp'
import { ClusterNotFoundException } from '../../core/exception/cluster-not-found.exception'
Expand Down Expand Up @@ -120,10 +120,10 @@ export class ClusterService {
}

private async recoverPubKey(response: EvaluationResponse) {
const hash = hashRequest(response.request) as `0x${string}`
const requestHash = hash(response.request) as `0x${string}`

return recoverMessageAddress({
message: hash,
message: requestHash,
signature: response.attestation?.sig as `0x${string}`
})
}
Expand Down
4 changes: 2 additions & 2 deletions apps/policy-engine/src/app/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Request,
Signature
} from '@narval/policy-engine-shared'
import { Alg, hashRequest } from '@narval/signature'
import { Alg, hash } from '@narval/signature'
import { safeDecode } from '@narval/transaction-request-intent'
import {
BadRequestException,
Expand Down Expand Up @@ -159,7 +159,7 @@ export class AppService {
}: EvaluationRequest): Promise<EvaluationResponse> {
// Pre-Process
// verify the signatures of the Principal and any Approvals
const verificationMessage = hashRequest(request)
const verificationMessage = hash(request)

const principalCredential = await this.#verifySignature(authentication, verificationMessage)
if (!principalCredential) throw new Error(`Could not find principal`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action, EvaluationRequest, FIXTURE, Request, TransactionRequest } from '@narval/policy-engine-shared'
import { Alg, hashRequest } from '@narval/signature'
import { Alg, hash } from '@narval/signature'
import { toHex } from 'viem'

export const ONE_ETH = BigInt('1000000000000000000')
Expand All @@ -22,7 +22,7 @@ export const generateInboundRequest = async (): Promise<EvaluationRequest> => {
resourceId: FIXTURE.WALLET.Engineering.id
}

const message = hashRequest(request)
const message = hash(request)

const aliceSignature = await FIXTURE.ACCOUNT.Alice.signMessage({ message })
const bobSignature = await FIXTURE.ACCOUNT.Bob.signMessage({ message })
Expand Down
2 changes: 1 addition & 1 deletion packages/signature/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { decode } from './lib/decode'
export { hashRequest } from './lib/hash-request'
export { hash } from './lib/hash-request'
export { sign } from './lib/sign'
export * from './lib/types'
export { verify } from './lib/verify'
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { hashRequest } from '../../hash-request'
import { hash } from '../../hash-request'

describe('hashRequest', () => {
it('hashes the given object', () => {
expect(
hashRequest({
hash({
a: 'a',
b: 1,
c: false
Expand All @@ -12,7 +12,7 @@ describe('hashRequest', () => {
})

it('hashes the given array', () => {
expect(hashRequest(['a', 1, false])).toEqual('cdd23dea0598c5ffc66b6a53f9dc7448a87b47454f209caa310e21da91754173')
expect(hash(['a', 1, false])).toEqual('cdd23dea0598c5ffc66b6a53f9dc7448a87b47454f209caa310e21da91754173')
})

it('hashes two objects deterministically', () => {
Expand All @@ -35,6 +35,6 @@ describe('hashRequest', () => {
a: 'a'
}

expect(hashRequest(a)).toEqual(hashRequest(b))
expect(hash(a)).toEqual(hash(b))
})
})
4 changes: 2 additions & 2 deletions packages/signature/src/lib/__test__/unit/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hashRequest } from '../../hash-request'
import { hash } from '../../hash-request'
import { Alg } from '../../types'

export const ALGORITHM = Alg.ES256
Expand Down Expand Up @@ -27,7 +27,7 @@ MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgUDs3iCP93sknEZ+c
DcdsS+UdaUgKK0XVajrBWZbQYWehRANCAAT4HK9PrjSP2z5sFVzU+NOU6Jlr2jHK
56woxq1wNvSAh4JZtYO4SLJWaSB8YUD3caXqgH3gSIHYQWm+EY2h4nAc
-----END PRIVATE KEY-----`
export const HASH = hashRequest(REQUEST)
export const HASH = hash(REQUEST)
export const SIGNED_TOKEN =
'eyJhbGciOiJFUzI1NiIsImtpZCI6InRlc3Qta2lkIn0.eyJyZXF1ZXN0SGFzaCI6IjY4NjMxYmIyMmIxNzFkMjk2YTUyMmJiNmMzMjQ4MDU1NTk3YmY2M2VhYzJiYTk1ZjFmZDAyYTQ4YWUxZWRmOGMiLCJpYXQiOjE3MzM4NzUyMDAsImV4cCI6MTczMzk2MTYwMH0.0D_W3jtdBCAIht39FvPTtC6o9TywEQ_i1TL4BTDQIdndL1X2eoFawoczhWqhEQeP3MUs3XnLeBhtfbf25U3EsQ'
export const HEADER_PART = 'eyJhbGciOiJFUzI1NiIsImtpZCI6InRlc3Qta2lkIn0'
Expand Down
2 changes: 1 addition & 1 deletion packages/signature/src/lib/hash-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const sort = (value: unknown): unknown => {
* @param value an object
* @returns object's hash
*/
export const hashRequest = (value: unknown): string => {
export const hash = (value: unknown): string => {
return createHash('sha256')
.update(stringify(sort(value)))
.digest('hex')
Expand Down
6 changes: 3 additions & 3 deletions packages/signature/src/lib/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SignJWT, base64url, importPKCS8 } from 'jose'
import { isHex, toBytes } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { JwtError } from './error'
import { hashRequest } from './hash-request'
import { hash } from './hash-request'
import { Alg, SignatureInput } from './types'

const DEF_EXP_TIME = '2h'
Expand All @@ -20,7 +20,7 @@ const eoaKeys = async (signingInput: SignatureInput): Promise<string> => {
const expNumeric = exp ? Math.floor(exp.getTime() / 1000) : now + 2 * 60 * 60
const header = { alg: algorithm, kid }
const payload = {
requestHash: hashRequest(request),
requestHash: hash(request),
iat: iatNumeric,
exp: expNumeric
}
Expand Down Expand Up @@ -49,7 +49,7 @@ export async function sign(signingInput: SignatureInput): Promise<string> {
return eoaKeys(signingInput)
}
const privateKey = await importPKCS8(pk, algorithm)
const requestHash = hashRequest(request)
const requestHash = hash(request)

const jwt = await new SignJWT({ requestHash })
.setProtectedHeader({ alg: algorithm, kid })
Expand Down

0 comments on commit 445633a

Please sign in to comment.