Skip to content

Commit

Permalink
Merge pull request #171 from narval-xyz/feature/vault
Browse files Browse the repository at this point in the history
Vault setup
  • Loading branch information
mattschoch authored Mar 15, 2024
2 parents 02e7946 + 6719ed2 commit b34dea2
Show file tree
Hide file tree
Showing 87 changed files with 2,956 additions and 110 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include ./apps/armory/Makefile
include ./apps/devtool/Makefile
include ./apps/policy-engine/Makefile
include ./apps/vault/Makefile
include ./packages/policy-engine-shared/Makefile
include ./packages/transaction-request-intent/Makefile
include ./packages/signature/Makefile
Expand Down
2 changes: 1 addition & 1 deletion apps/armory/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const withSwagger = (app: INestApplication): INestApplication => {
* @returns The modified INestApplication instance.
*/
const withGlobalPipes = (app: INestApplication): INestApplication => {
app.useGlobalPipes(new ValidationPipe())
app.useGlobalPipes(new ValidationPipe({ transform: true }))

return app
}
Expand Down
117 changes: 9 additions & 108 deletions apps/policy-engine/src/engine/evaluation-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,112 +1,13 @@
import { AccessList, AccountId, Action, Address, BaseActionDto, FiatCurrency, Hex } from '@narval/policy-engine-shared'
import {
AccountId,
Action,
FiatCurrency,
SignMessageRequestDataDto,
SignTransactionRequestDataDto
} from '@narval/policy-engine-shared'
import { ApiExtraModels, ApiProperty, getSchemaPath } from '@nestjs/swagger'
import { Transform, Type } from 'class-transformer'
import { IsDefined, IsEthereumAddress, IsIn, IsOptional, IsString, ValidateNested } from 'class-validator'

export class TransactionRequestDto {
@IsString()
@IsDefined()
@IsEthereumAddress()
@Transform(({ value }) => value.toLowerCase())
@ApiProperty({
required: true,
format: 'EthereumAddress'
})
from: Address

@IsString()
@IsEthereumAddress()
@Transform(({ value }) => value.toLowerCase())
@ApiProperty({
format: 'EthereumAddress'
})
to?: Address | null

@IsString()
@ApiProperty({
type: 'string',
format: 'Hexadecimal'
})
data?: Hex

@IsOptional()
@Transform(({ value }) => BigInt(value))
@ApiProperty({
format: 'bigint',
required: false,
type: 'string'
})
gas?: bigint
@IsOptional()
@Transform(({ value }) => BigInt(value))
@ApiProperty({
format: 'bigint',
required: false,
type: 'string'
})
maxFeePerGas?: bigint
@IsOptional()
@Transform(({ value }) => BigInt(value))
@ApiProperty({
format: 'bigint',
required: false,
type: 'string'
})
maxPriorityFeePerGas?: bigint

@ApiProperty()
nonce?: number

value?: Hex

chainId: number

accessList?: AccessList

type?: '2'
}

export class SignTransactionRequestDataDto extends BaseActionDto {
@IsIn(Object.values(Action))
@IsDefined()
@ApiProperty({
enum: Object.values(Action),
default: Action.SIGN_TRANSACTION
})
action: typeof Action.SIGN_TRANSACTION

@IsString()
@IsDefined()
@ApiProperty()
resourceId: string

@ValidateNested()
@IsDefined()
@ApiProperty({
type: TransactionRequestDto
})
transactionRequest: TransactionRequestDto
}

export class SignMessageRequestDataDto extends BaseActionDto {
@IsIn(Object.values(Action))
@IsDefined()
@ApiProperty({
enum: Object.values(Action),
default: Action.SIGN_MESSAGE
})
action: typeof Action.SIGN_MESSAGE

@IsString()
@IsDefined()
@ApiProperty()
resourceId: string

@IsString()
@IsDefined()
@ApiProperty()
message: string // TODO: Is this string hex or raw?
}
import { Type } from 'class-transformer'
import { IsDefined, IsOptional, ValidateNested } from 'class-validator'

export class HistoricalTransferDto {
amount: string
Expand Down
2 changes: 1 addition & 1 deletion apps/policy-engine/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const withSwagger = (app: INestApplication): INestApplication => {
* @returns The modified INestApplication instance.
*/
const withGlobalPipes = (app: INestApplication): INestApplication => {
app.useGlobalPipes(new ValidationPipe())
app.useGlobalPipes(new ValidationPipe({ transform: true }))

return app
}
Expand Down
13 changes: 13 additions & 0 deletions apps/vault/.env.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NODE_ENV=development

PORT=3011

APP_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/vault?schema=public"

APP_UID="local-dev-vault-instance-1"

MASTER_PASSWORD="unsafe-local-dev-master-password"

KEYRING_TYPE="raw"

# MASTER_AWS_KMS_ARN="arn:aws:kms:us-east-2:728783560968:key/f6aa3ddb-47c3-4f31-977d-b93205bb23d1"
13 changes: 13 additions & 0 deletions apps/vault/.env.test.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NODE_ENV=test

PORT=3011

APP_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/vault-test?schema=public"

APP_UID="local-dev-vault-instance-1"

MASTER_PASSWORD="unsafe-local-test-master-password"

KEYRING_TYPE="raw"

# MASTER_AWS_KMS_ARN="arn:aws:kms:us-east-2:728783560968:key/f6aa3ddb-47c3-4f31-977d-b93205bb23d1"
18 changes: 18 additions & 0 deletions apps/vault/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
6 changes: 6 additions & 0 deletions apps/vault/.lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
'*.{ts,tsx}': (filenames) => [
`eslint --no-error-on-unmatched-pattern ${filenames.join(' ')}; echo "ESLint completed with exit code $?"`,
`prettier --write ${filenames.join(' ')}`
]
}
129 changes: 129 additions & 0 deletions apps/vault/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
VAULT_PROJECT_NAME := vault
VAULT_PROJECT_DIR := ./apps/vault
VAULT_DATABASE_SCHEMA := ${VAULT_PROJECT_DIR}/src/shared/module/persistence/schema/schema.prisma

# === Start ===

vault/start/dev:
npx nx serve ${VAULT_PROJECT_NAME}

# === Setup ===

vault/setup:
make vault/copy-default-env
make vault/db/setup
make vault/test/db/setup
make vault/cli CMD=provision

vault/copy-default-env:
cp ${VAULT_PROJECT_DIR}/.env.default ${VAULT_PROJECT_DIR}/.env
cp ${VAULT_PROJECT_DIR}/.env.test.default ${VAULT_PROJECT_DIR}/.env.test

# === Build ===

vault/build/script:
npx tsc --project ${VAULT_PROJECT_DIR}/tsconfig.app.json
npx tsc-alias --project ${VAULT_PROJECT_DIR}/tsconfig.app.json

# == Code format ==

vault/format:
npx nx format:write --projects ${VAULT_PROJECT_NAME}

vault/lint:
npx nx lint ${VAULT_PROJECT_NAME} -- --fix

vault/format/check:
npx nx format:check --projects ${VAULT_PROJECT_NAME}

vault/lint/check:
npx nx lint ${VAULT_PROJECT_NAME}

# === Database ===

vault/db/generate-types:
npx prisma generate \
--schema ${VAULT_DATABASE_SCHEMA}

vault/db/migrate:
npx dotenv -e ${VAULT_PROJECT_DIR}/.env -- \
prisma migrate dev \
--schema ${VAULT_DATABASE_SCHEMA}

vault/db/setup:
@echo ""
@echo "${TERM_GREEN}🛠️ Setting up Vault development database${TERM_NO_COLOR}"
@echo ""
npx dotenv -e ${VAULT_PROJECT_DIR}/.env -- \
prisma migrate reset \
--schema ${VAULT_DATABASE_SCHEMA} \
--force
make vault/db/seed

@echo ""
@echo "${TERM_GREEN}🛠️ Setting up Vault test database${TERM_NO_COLOR}"
@echo ""
make vault/test/db/setup

vault/db/create-migration:
npx dotenv -e ${VAULT_PROJECT_DIR}/.env -- \
prisma migrate dev \
--schema ${VAULT_DATABASE_SCHEMA} \
--name ${NAME}

# To maintain seed data within their respective modules and then import them
# into the main seed.ts file for execution, it's necessary to compile the
# project and resolve its path aliases before running the vanilla JavaScript
# seed entry point.
vault/db/seed:
npx dotenv -e ${VAULT_PROJECT_DIR}/.env -- \
ts-node -r tsconfig-paths/register --project ${VAULT_PROJECT_DIR}/tsconfig.app.json ${VAULT_PROJECT_DIR}/src/shared/module/persistence/seed.ts


# === Testing ===

vault/test/db/setup:
npx dotenv -e ${VAULT_PROJECT_DIR}/.env.test --override -- \
prisma migrate reset \
--schema ${VAULT_DATABASE_SCHEMA} \
--skip-seed \
--force


vault/test/type:
make vault/db/generate-types
npx tsc \
--project ${VAULT_PROJECT_DIR}/tsconfig.app.json \
--noEmit

vault/test/unit:
npx nx test:unit ${VAULT_PROJECT_NAME} -- ${ARGS}

vault/test/unit/watch:
make vault/test/unit ARGS=--watch

vault/test/integration:
npx nx test:integration ${VAULT_PROJECT_NAME} -- ${ARGS}

vault/test/integration/watch:
make vault/test/integration ARGS=--watch

vault/test/e2e:
npx nx test:e2e ${VAULT_PROJECT_NAME} -- ${ARGS}

vault/test/e2e/watch:
make vault/test/e2e ARGS=--watch

vault/test:
make vault/test/unit
make vault/test/integration
make vault/test/e2e

# === CLI ===

vault/cli:
npx dotenv -e ${VAULT_PROJECT_DIR}/.env -- \
ts-node -r tsconfig-paths/register \
--project ${VAULT_PROJECT_DIR}/tsconfig.app.json \
${VAULT_PROJECT_DIR}/src/cli.ts ${CMD}

42 changes: 42 additions & 0 deletions apps/vault/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Vault

TBD

## Requirements

## Getting started

```bash
make vault/setup
```

## Running

```bash
make vault/start/dev
```

## Testing

```bash
make vault/test/type
make vault/test/unit
make vault/test/integration
make vault/test/e2e
```

## Formatting

```bash
make vault/format
make vault/lint

make vault/format/check
make vault/lint/check
```

## CLI

```bash
make vault/cli CMD=help
```
20 changes: 20 additions & 0 deletions apps/vault/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from 'jest'

const config: Config = {
displayName: 'vault',
moduleFileExtensions: ['ts', 'js', 'html'],
preset: '../../jest.preset.js',
setupFiles: ['<rootDir>/jest.setup.ts'],
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json'
}
]
},
workerThreads: true // EXPERIMENTAL; lets BigInt serialization work
}

export default config
Loading

0 comments on commit b34dea2

Please sign in to comment.