Skip to content

Commit

Permalink
Add approvals example
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Apr 11, 2024
1 parent a7dd46a commit 13e95a7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 25 deletions.
15 changes: 4 additions & 11 deletions apps/devtool/src/app/_hooks/useDataStoreApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ import { Alg, Curves, KeyTypes, Payload, hash } from '@narval/signature'
import axios from 'axios'
import { useEffect, useState } from 'react'
import { v4 as uuid } from 'uuid'
import { useAccount } from 'wagmi'
import useAccountSignature from './useAccountSignature'

type DataStore = { entity: EntityStore; policy: PolicyStore }

const useDataStoreApi = () => {
const account = useAccount()
const { jwk, signAccountJwt } = useAccountSignature()

const [dataStore, setDataStore] = useState<DataStore>()
Expand All @@ -34,14 +32,9 @@ const useDataStoreApi = () => {
}
}, [dataStore])

useEffect(() => {
if (!account.isConnected) return

createCredential(account.address?.toLowerCase())
}, [account.address])

const getDataStore = async () => {
const { data } = await axios.get<DataStore>('/api/data-store')

setDataStore(data)

return data
Expand All @@ -61,14 +54,14 @@ const useDataStoreApi = () => {
const user: UserEntity = { id: uuid(), role: UserRole.ADMIN }

const publicKey: CredentialEntity = {
id: uuid(),
id: address,
userId: user.id,
key: {
kty: KeyTypes.EC,
crv: Curves.SECP256K1,
alg: Alg.ES256K,
kid: address.toLowerCase(),
addr: address.toLowerCase()
kid: address,
addr: address
}
}

Expand Down
53 changes: 47 additions & 6 deletions apps/devtool/src/app/api/data-store/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
"data": {
"addressBook": [],
"credentials": [
{
"id": "0x9D432a09CBf55F22Aa6a2E290acB12D57d29B2Fc",
"userId": "7a09904c-070e-4d00-8fa8-5f1dfafff2a5",
"key": {
"kty": "EC",
"crv": "secp256k1",
"alg": "ES256K",
"kid": "0x9D432a09CBf55F22Aa6a2E290acB12D57d29B2Fc",
"addr": "0x9D432a09CBf55F22Aa6a2E290acB12D57d29B2Fc"
}
},
{
"id": "0x0C151023EDedCC419caB0f49ABaCd2e87a4FF013",
"userId": "db5bf088-05f7-492c-a2a9-0b62696ac9c7",
"key": {
"kty": "EC",
"crv": "secp256k1",
"alg": "ES256K",
"kid": "0x0C151023EDedCC419caB0f49ABaCd2e87a4FF013",
"addr": "0x0C151023EDedCC419caB0f49ABaCd2e87a4FF013"
}
},
{
"id": "0x000c0d191308A336356BEe3813CC17F6868972C4",
"userId": "fe723044-35df-4e99-9739-122a48d4ab96",
"userId": "61e775a9-5f68-41ab-a775-5806845e6e72",
"key": {
"kty": "EC",
"crv": "secp256k1",
Expand All @@ -21,22 +43,30 @@
"userGroups": [],
"userWallets": [
{
"userId": "fe723044-35df-4e99-9739-122a48d4ab96",
"walletId": "eip155:eoa:0x56085a7d2e308ec8ce6fd707cdc5282431d025db"
"userId": "61e775a9-5f68-41ab-a775-5806845e6e72",
"walletId": "eip155:eoa:0x494042504a8148a6d00ab10ed26043f5579ce00f"
}
],
"users": [
{
"id": "fe723044-35df-4e99-9739-122a48d4ab96",
"id": "7a09904c-070e-4d00-8fa8-5f1dfafff2a5",
"role": "admin"
},
{
"id": "db5bf088-05f7-492c-a2a9-0b62696ac9c7",
"role": "admin"
},
{
"id": "61e775a9-5f68-41ab-a775-5806845e6e72",
"role": "admin"
}
],
"walletGroupMembers": [],
"walletGroups": [],
"wallets": [
{
"id": "eip155:eoa:0x56085a7d2e308ec8ce6fd707cdc5282431d025db",
"address": "0x56085a7d2e308ec8ce6fd707cdc5282431d025db",
"id": "eip155:eoa:0x494042504a8148a6d00ab10ed26043f5579ce00f",
"address": "0x494042504a8148a6d00ab10ed26043f5579ce00f",
"accountType": "eoa"
}
]
Expand All @@ -60,6 +90,17 @@
{
"criterion": "checkIntentType",
"args": ["transferErc721", "transferErc1155"]
},
{
"criterion": "checkApprovals",
"args": [
{
"approvalCount": 2,
"countPrincipal": false,
"approvalEntityType": "Narval::User",
"entityIds": ["7a09904c-070e-4d00-8fa8-5f1dfafff2a5", "db5bf088-05f7-492c-a2a9-0b62696ac9c7"]
}
]
}
],
"then": "permit"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use client'

import { faArrowsRotate, faCheckCircle, faFileSignature, faXmarkCircle } from '@fortawesome/pro-regular-svg-icons'
import {
faArrowsRotate,
faCheck,
faCheckCircle,
faFileSignature,
faTriangleExclamation,
faXmarkCircle
} from '@fortawesome/pro-regular-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { Editor } from '@monaco-editor/react'
import { Decision, EvaluationResponse } from '@narval/policy-engine-shared'
Expand Down Expand Up @@ -32,6 +39,22 @@ const PlaygroundEditor = () => {
}
}, [data, evaluationResponse])

const getApprovalSignature = async () => {
if (!data) return

const transactionRequest = JSON.parse(data)

const payload = {
iss: 'fe723044-35df-4e99-9739-122a48d4ab96',
sub: transactionRequest.request.resourceId,
requestHash: hash(transactionRequest.request)
}

const authentication = await signAccountJwt(payload)

console.log(authentication)
}

const sendEvaluation = async () => {
if (!data || !jwk) return

Expand Down Expand Up @@ -59,7 +82,16 @@ const PlaygroundEditor = () => {
}
)

setData(JSON.stringify({ ...transactionRequest, authentication }, null, 2))
setData(
JSON.stringify(
{
...transactionRequest,
...(evaluation.data?.decision === Decision.PERMIT && { authentication })
},
null,
2
)
)
setEvaluationResponse(evaluation.data)
} catch (error) {
console.log(error)
Expand Down Expand Up @@ -125,12 +157,18 @@ const PlaygroundEditor = () => {
onClick={signRequest}
disabled={isProcessing || !canBeSigned}
/>
<NarButton label="Approve" leftIcon={<FontAwesomeIcon icon={faCheck} />} onClick={getApprovalSignature} />
{!isProcessing && evaluationResponse && (
<div className="flex items-center gap-2">
<FontAwesomeIcon
icon={evaluationResponse.decision === Decision.PERMIT ? faCheckCircle : faXmarkCircle}
className={evaluationResponse.decision === Decision.PERMIT ? 'text-nv-green-500' : 'text-nv-red-500'}
/>
{evaluationResponse.decision === Decision.PERMIT && (
<FontAwesomeIcon icon={faCheckCircle} className="text-nv-green-500" />
)}
{evaluationResponse.decision === Decision.FORBID && (
<FontAwesomeIcon icon={faXmarkCircle} className="text-nv-red-500" />
)}
{evaluationResponse.decision === Decision.CONFIRM && (
<FontAwesomeIcon icon={faTriangleExclamation} className="text-nv-orange-500" />
)}
<div className="text-nv-white">{evaluationResponse.decision}</div>
</div>
)}
Expand Down
Empty file.
18 changes: 18 additions & 0 deletions apps/devtool/src/app/request-playground/_components/approvals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"request": {
"resourceId": "eip155:eoa:0x494042504a8148a6d00ab10ed26043f5579ce00f",
"action": "signTransaction",
"nonce": "6bb2b3e9-fa8a-4081-a837-575184ceb3a5",
"transactionRequest": {
"from": "0x24f0914062f66d487dc082802aac53cd10328d96",
"to": "0x43a595aa07275b4f72061106d80b21e02ff8a960",
"data": "0x42842e0e00000000000000000000000024f0914062f66d487dc082802aac53cd10328d960000000000000000000000008cc7929789e14ce2ac6d4e8f2294ee4c501283e30000000000000000000000000000000000000000000000000000000000000085",
"chainId": 137,
"type": "2"
}
},
"approvals": [
"eyJhbGciOiJFSVAxOTEiLCJraWQiOiIweDlENDMyYTA5Q0JmNTVGMjJBYTZhMkUyOTBhY0IxMkQ1N2QyOUIyRmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJmZTcyMzA0NC0zNWRmLTRlOTktOTczOS0xMjJhNDhkNGFiOTYiLCJyZXF1ZXN0SGFzaCI6IjB4ZjdmY2MyMzRhMTMzNWNmNGE0NDE0MzAwNWJhOWJhNWQyY2VlMTJhMmZmZDFlNDMyYTU4Yjc5YjllNzcyYzk5NiIsInN1YiI6ImVpcDE1NTplb2E6MHg0OTQwNDI1MDRhODE0OGE2ZDAwYWIxMGVkMjYwNDNmNTU3OWNlMDBmIn0.BxgEr9TILTIocaNpQs-59vKhBSePpS-q0D4VWfVpqIs0bRSBp8vBMHkKa7AxdtfMwRCBd86vldj-1Ebb5UtmMhs",
"eyJhbGciOiJFSVAxOTEiLCJraWQiOiIweDBDMTUxMDIzRURlZENDNDE5Y2FCMGY0OUFCYUNkMmU4N2E0RkYwMTMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJmZTcyMzA0NC0zNWRmLTRlOTktOTczOS0xMjJhNDhkNGFiOTYiLCJyZXF1ZXN0SGFzaCI6IjB4ZjdmY2MyMzRhMTMzNWNmNGE0NDE0MzAwNWJhOWJhNWQyY2VlMTJhMmZmZDFlNDMyYTU4Yjc5YjllNzcyYzk5NiIsInN1YiI6ImVpcDE1NTplb2E6MHg0OTQwNDI1MDRhODE0OGE2ZDAwYWIxMGVkMjYwNDNmNTU3OWNlMDBmIn0.Lnb8_P_4ilWhefkQXYVC563YYQBygpS2u3WZoFtfyFQzl9h35EzZgrgeXYPtFO05OuyRweyeH1960y6iEExV4xw"
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"request": {
"resourceId": "eip155:eoa:0x8101c44b3bf060f0a92e47516192faa8df719761",
"resourceId": "eip155:eoa:0x494042504a8148a6d00ab10ed26043f5579ce00f",
"action": "signTransaction",
"nonce": "6bb2b3e9-fa8a-4081-a837-575184ceb3a5",
"transactionRequest": {
Expand All @@ -11,5 +11,8 @@
"type": "2"
}
},
"approvals": []
"approvals": [
"eyJhbGciOiJFSVAxOTEiLCJraWQiOiIweDlENDMyYTA5Q0JmNTVGMjJBYTZhMkUyOTBhY0IxMkQ1N2QyOUIyRmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJmZTcyMzA0NC0zNWRmLTRlOTktOTczOS0xMjJhNDhkNGFiOTYiLCJyZXF1ZXN0SGFzaCI6IjB4ZjdmY2MyMzRhMTMzNWNmNGE0NDE0MzAwNWJhOWJhNWQyY2VlMTJhMmZmZDFlNDMyYTU4Yjc5YjllNzcyYzk5NiIsInN1YiI6ImVpcDE1NTplb2E6MHg0OTQwNDI1MDRhODE0OGE2ZDAwYWIxMGVkMjYwNDNmNTU3OWNlMDBmIn0.BxgEr9TILTIocaNpQs-59vKhBSePpS-q0D4VWfVpqIs0bRSBp8vBMHkKa7AxdtfMwRCBd86vldj-1Ebb5UtmMhs",
"eyJhbGciOiJFSVAxOTEiLCJraWQiOiIweDBDMTUxMDIzRURlZENDNDE5Y2FCMGY0OUFCYUNkMmU4N2E0RkYwMTMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJmZTcyMzA0NC0zNWRmLTRlOTktOTczOS0xMjJhNDhkNGFiOTYiLCJyZXF1ZXN0SGFzaCI6IjB4ZjdmY2MyMzRhMTMzNWNmNGE0NDE0MzAwNWJhOWJhNWQyY2VlMTJhMmZmZDFlNDMyYTU4Yjc5YjllNzcyYzk5NiIsInN1YiI6ImVpcDE1NTplb2E6MHg0OTQwNDI1MDRhODE0OGE2ZDAwYWIxMGVkMjYwNDNmNTU3OWNlMDBmIn0.Lnb8_P_4ilWhefkQXYVC563YYQBygpS2u3WZoFtfyFQzl9h35EzZgrgeXYPtFO05OuyRweyeH1960y6iEExV4xw"
]
}

0 comments on commit 13e95a7

Please sign in to comment.