Skip to content

Commit

Permalink
packages are imported inside monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Apr 22, 2024
1 parent 350bf63 commit 6886672
Show file tree
Hide file tree
Showing 28 changed files with 112 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ const PlaygroundEditor = () => {
<div className="pt-4">
<h3 className="pb-4">Examples</h3>
<div className="flex gap-5 ">
<NarButton
label="ERC-20"
onClick={async () => setCodeEditor(JSON.stringify(await erc20(), null, 2))}
/>
<NarButton label="ERC-20" onClick={async () => setCodeEditor(JSON.stringify(await erc20(), null, 2))} />
<NarButton
label="Spending limits"
onClick={async () => setCodeEditor(JSON.stringify(await spendingLimits(), null, 2))}
Expand Down
14 changes: 7 additions & 7 deletions demos/aa-client/project.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "aa",
"name": "aa-client",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "demos/aa-client/src",
"projectType": "application",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"executor": "@nx/webpack:webpack",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"main": "demos/aa-client/src/main.ts",
"target": "node",
"compiler": "tsc",
"outputPath": "dist/demos/aa-client",
"main": "demos/aa-client/src/main.ts",
"tsConfig": "demos/aa-client/tsconfig.app.json",
"isolatedConfig": true,
"webpackConfig": "demos/aa-client/webpack.config.js"
Expand All @@ -26,14 +26,14 @@
"executor": "@nx/js:node",
"defaultConfiguration": "development",
"options": {
"buildTarget": "aa:build"
"buildTarget": "aa-client:build"
},
"configurations": {
"development": {
"buildTarget": "aa:build:development"
"buildTarget": "aa-client:build:development"
},
"production": {
"buildTarget": "aa:build:production"
"buildTarget": "aa-client:build:production"
}
}
},
Expand All @@ -47,7 +47,7 @@
"test:type": {
"executor": "nx:run-commands",
"options": {
"command": "make aa/test/type"
"command": "make aa-client/test/type"
}
},
"test:unit": {
Expand Down
28 changes: 4 additions & 24 deletions demos/aa-client/src/NarvalSdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NarvalSdk, NarvalSdkConfig } from '@narval-xyz/sdk/src'
import { Config, NarvalSdk } from '@narval/sdk'

const ensureConfig = (): NarvalSdkConfig => {
const ensureConfig = (): Config => {
const ENGINE_URL = process.env.ENGINE_URL
if (!ENGINE_URL) {
throw new Error('ENGINE_URL is required')
Expand Down Expand Up @@ -33,28 +33,8 @@ const ensureConfig = (): NarvalSdkConfig => {
if (!VAULT_CLIENT_SECRET) {
throw new Error('VAULT_CLIENT_SECRET is required')
}
return {
engine: {
url: ENGINE_URL,
adminKey: ENGINE_ADMIN_KEY,
client: {
id: ENGINE_CLIENT_ID,
secret: ENGINE_CLIENT_SECRET
}
},
vault: {
url: VAULT_URL,
adminKey: VAULT_ADMIN_KEY,
client: {
id: VAULT_CLIENT_ID,
secret: VAULT_CLIENT_SECRET
}
},
dataStore: {
entityUrl: process.env.DATA_STORE_ENTITY_URL || 'http://localhost:3111/entities',
policyUrl: process.env.DATA_STORE_POLICY_URL || 'http://localhost:3111/policies'
}
}

return {} as Config
}

const default_sdk = new NarvalSdk(ensureConfig())
Expand Down
2 changes: 2 additions & 0 deletions demos/aa-client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ app.get('/', (req, res) => {
app.post('/signMessage', async (req, res) => {
try {
const { message, userId, walletId } = req.body
message as string
await walletProvider.signMessage(userId, walletId)

console.log('Signed message')
Expand Down Expand Up @@ -66,6 +67,7 @@ app.post('/policies', async (req, res) => {
app.post('/wallets/generate', async (req, res) => {
try {
const { eoa } = req.body
eoa as string
const newWallet = await walletProvider.generate4337Wallet()
res.json(newWallet)
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion demos/aa-client/src/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// src/models.ts

import { Jwk } from '@narval-xyz/signature'
import { Jwk } from '@narval/signature'
import { Polygon } from '@thirdweb-dev/chains'
import { SmartWallet } from '@thirdweb-dev/wallets'

Expand Down
8 changes: 4 additions & 4 deletions demos/aa-client/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EntityStore, PolicyStore } from '@narval-xyz/policy-engine-domain'
import { Entities, EntityStore, PolicyStore } from '@narval/policy-engine-shared'
import { User, Wallet } from './models'

export const users = new Map<string, User>()
export const users = new Map<string, User>([['matt', { id: 'matt', name: 'Matt', walletIds: [], credential: {} }]])
export const wallets = new Map<string, Wallet>()
export const policies = new Map<string, PolicyStore>()
export const entities = new Map<string, EntityStore>()
export const policies = new Map<string, PolicyStore>([['default', { data: [], signature: '' }]])
export const entities = new Map<string, EntityStore>([['default', { data: {} as Entities, signature: '' }]])
8 changes: 2 additions & 6 deletions demos/aa-client/src/thirdweb.client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Polygon } from '@thirdweb-dev/chains'
import { InAppWallet, SmartWallet } from '@thirdweb-dev/wallets'
import { SmartWallet } from '@thirdweb-dev/wallets'
import { LocalWalletNode } from '@thirdweb-dev/wallets/evm/wallets/local-wallet-node'

const createSmartAccount = async (eoa?: InAppWallet) => {
const createSmartAccount = async () => {
const factoryAddress = '0x61ffcc675cfbf8e0A35Cd6140bfe40Fe94DF845f'
const secretKey = process.env.THIRDWEB_SECRET as string

Expand All @@ -23,10 +23,6 @@ const createSmartAccount = async (eoa?: InAppWallet) => {

const smartWallet = new SmartWallet(config)

smartWallet.connect({
personalWallet: adminWallet
})

return smartWallet
}

Expand Down
53 changes: 28 additions & 25 deletions demos/aa-client/src/walletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
EntityStore,
Policy,
PolicyStore,
Request,
Then
} from '@narval-xyz/policy-engine-domain'
import { NarvalSdk, buildRequest } from '@narval-xyz/sdk/src'
import { Jwk, PublicKey, hash, signJwt } from '@narval-xyz/signature'
} from '@narval/policy-engine-shared'
import { NarvalSdk, buildRequest } from '@narval/sdk'
import { PublicKey } from '@narval/signature'
import { SmartWallet } from '@thirdweb-dev/wallets'
import { getUnixTime } from 'date-fns'
import { v4 } from 'uuid'
import { Hex } from 'viem'
import default_sdk from './NarvalSdk'
Expand Down Expand Up @@ -69,17 +69,6 @@ export default class WalletProvider {
return result.decision
}

async signData(data: Entities | Policy[], jwk: Jwk) {
const token = await signJwt(
{
data: hash(data),
iat: getUnixTime(new Date())
},
jwk
)
return token
}

async getPolicies(): Promise<PolicyStore> {
const policies = this.policyStore.values()
return policies.next().value
Expand Down Expand Up @@ -114,8 +103,21 @@ export default class WalletProvider {
then: Then.PERMIT
}
]
const policyStore = await this.narvalSdk.setPolicies(data)
this.policyStore.set(v4(), policyStore)

const request = {
action: 'savePolicies',
data
} as unknown as Request

const evaluationResponse = await this.narvalSdk.evaluate(request)

if (evaluationResponse.decision === Decision.PERMIT && evaluationResponse.accessToken) {
const policyStore = await this.narvalSdk.savePolicies(evaluationResponse.accessToken, data)
const policyStoreId = v4()

this.policyStore.set(policyStoreId, policyStore)
}
throw new Error('Unauthorized')
}

async updateEntities(userId: string): Promise<void> {
Expand Down Expand Up @@ -148,15 +150,16 @@ export default class WalletProvider {
const address = (await wallet.getAddress()) as Hex
entities.wallets.push({ id: Wallet.id, address, accountType: '4337' })
}
const request = {
action: 'saveEntities',
data: entities
} as unknown as Request

const jwk = this.narvalSdk.getEngineDefaultSigner() || user.credential
const signature = await this.signData(entities, jwk)
const entityStore = {
data: entities,
signature
const evaluationResponse = await this.narvalSdk.evaluate(request)

if (evaluationResponse.decision === Decision.PERMIT && evaluationResponse.accessToken) {
const store = await this.narvalSdk.saveEntities(evaluationResponse.accessToken, entities)
this.entityStore.set(v4(), store)
}
this.entityStore.set(v4(), entityStore)
}

async createTenant(userId: string, dataStoreUrl: string): Promise<void> {}
}
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"@mdx-js/react": "^3.0.0",
"@monaco-editor/react": "^4.6.0",
"@narval-xyz/policy-engine-domain": "^0.0.4",
"@narval-xyz/sdk": "^0.0.2",
"@narval-xyz/sdk": "^0.0.3",
"@narval-xyz/signature": "^0.1.7",
"@nestjs/axios": "^3.0.1",
"@nestjs/bull": "^10.1.0",
Expand Down
36 changes: 0 additions & 36 deletions packages/policy-engine-shared/package.json

This file was deleted.

20 changes: 1 addition & 19 deletions packages/policy-engine-shared/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,6 @@
"sourceRoot": "packages/policy-engine-shared/src",
"projectType": "library",
"targets": {
"buildLibs": {
"executor": "@nx/workspace:run-commands",
"options": {
"commands": ["nx run policy-engine-shared:build"]
}
},
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"rootDir": ".",
"outputPath": "{workspaceRoot}/packages/policy-engine-shared/dist",
"tsConfig": "{workspaceRoot}/packages/policy-engine-shared/tsconfig.lib.json",
"packageJson": "{workspaceRoot}/packages/policy-engine-shared/package.json",
"main": "{workspaceRoot}/packages/policy-engine-shared/src/index.ts"
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
Expand All @@ -43,6 +26,5 @@
}
}
},
"implicitDependencies": ["signature"],
"tags": []
"tags": ["package"]
}
2 changes: 1 addition & 1 deletion packages/policy-engine-shared/src/lib/dev.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SigningAlg,
privateKeyToJwk,
secp256k1PublicKeySchema
} from '@narval-xyz/signature'
} from '@narval/signature'
import { PrivateKeyAccount } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { Action } from './type/action.type'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { publicKeySchema } from '@narval-xyz/signature'
import { publicKeySchema } from '@narval/signature'
import { z } from 'zod'
import { AccountId, AssetId } from '../util/caip.util'
import { addressSchema } from './address.schema'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Alg } from '@narval-xyz/signature'
import { Alg } from '@narval/signature'
import { z } from 'zod'
import { Action } from '../type/action.type'
import { EntityType, FiatCurrency, IdentityOperators, ValueOperators } from '../type/domain.type'
Expand Down
8 changes: 5 additions & 3 deletions packages/policy-engine-shared/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"],
"rootDir": "../../packages"
"lib": ["es2020"],
"target": "es2020",
"moduleResolution": "node"
},
"include": ["src/**/*.ts", "../../apps/policy-engine/src/shared/decorators/validate-criterion.decorator.ts"],
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts"]
}
Loading

0 comments on commit 6886672

Please sign in to comment.