Skip to content

Commit

Permalink
demo prep
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Apr 12, 2024
1 parent 13e95a7 commit 3ce4241
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const DataStoreConfig = () => {
useDataStoreApi()

const [codeEditor, setCodeEditor] = useState<string>()
const [displayCodeEditor, setDisplayCodeEditor] = useState(false)
const [displayCodeEditor, setDisplayCodeEditor] = useState(true)
const [isDialogOpen, setIsDialogOpen] = useState(false)

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { Editor } from '@monaco-editor/react'
import { Decision, EvaluationResponse } from '@narval/policy-engine-shared'
import { hash } from '@narval/signature'
import axios from 'axios'
import axios, { AxiosError } from 'axios'
import { useMemo, useRef, useState } from 'react'
import NarButton from '../../_design-system/NarButton'
import useAccountSignature from '../../_hooks/useAccountSignature'
Expand All @@ -22,27 +22,30 @@ import example from './example.json'
const PlaygroundEditor = () => {
const { engineUrl, engineClientId, engineClientSecret, vaultUrl, vaultClientId } = useStore()
const { jwk, signAccountJwsd, signAccountJwt } = useAccountSignature()
const [data, setData] = useState<string | undefined>(JSON.stringify(example, null, 2))
const [codeEditor, setCodeEditor] = useState<string | undefined>(JSON.stringify(example, null, 2))
const [isProcessing, setIsProcessing] = useState<boolean>(false)
const [evaluationResponse, setEvaluationResponse] = useState<EvaluationResponse>()
const [signature, setSignature] = useState<string>()
const [error, setError] = useState<string>()

const editorRef = useRef<any>(null)
const monacoRef = useRef<any>(null)

const canBeSigned = useMemo(() => {
if (!codeEditor || !evaluationResponse) return false

try {
const transactionRequest = JSON.parse(data || '{}')
return transactionRequest?.authentication && evaluationResponse?.decision === Decision.PERMIT
const transactionRequest = JSON.parse(codeEditor)
return transactionRequest.authentication && evaluationResponse.decision === Decision.PERMIT
} catch (error) {
return false
}
}, [data, evaluationResponse])
}, [codeEditor, evaluationResponse])

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

const transactionRequest = JSON.parse(data)
const transactionRequest = JSON.parse(codeEditor)

const payload = {
iss: 'fe723044-35df-4e99-9739-122a48d4ab96',
Expand All @@ -56,12 +59,15 @@ const PlaygroundEditor = () => {
}

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

setIsProcessing(true)
setEvaluationResponse(undefined)
setSignature(undefined)
setError(undefined)

try {
const transactionRequest = JSON.parse(data)
const transactionRequest = JSON.parse(codeEditor)

const payload = {
iss: 'fe723044-35df-4e99-9739-122a48d4ab96',
Expand All @@ -82,7 +88,7 @@ const PlaygroundEditor = () => {
}
)

setData(
setCodeEditor(
JSON.stringify(
{
...transactionRequest,
Expand All @@ -93,8 +99,10 @@ const PlaygroundEditor = () => {
)
)
setEvaluationResponse(evaluation.data)
} catch (error) {
console.log(error)
} catch (err) {
const error = err as AxiosError
const errorData = error.response?.data as any
setError(errorData?.message || error.message)
}

setIsProcessing(false)
Expand All @@ -107,6 +115,10 @@ const PlaygroundEditor = () => {

if (!accessToken?.value || !request) return

setEvaluationResponse(undefined)
setSignature(undefined)
setError(undefined)

try {
const bodyPayload = { request }

Expand Down Expand Up @@ -135,8 +147,8 @@ const PlaygroundEditor = () => {
<Editor
height="70vh"
language="json"
value={data}
onChange={(value) => setData(value)}
value={codeEditor}
onChange={(value) => setCodeEditor(value)}
onMount={(editor, monaco) => {
editorRef.current = editor
monacoRef.current = monaco
Expand All @@ -157,8 +169,10 @@ const PlaygroundEditor = () => {
onClick={signRequest}
disabled={isProcessing || !canBeSigned}
/>
<NarButton label="Approve" leftIcon={<FontAwesomeIcon icon={faCheck} />} onClick={getApprovalSignature} />
{!isProcessing && evaluationResponse && (
{false && (
<NarButton label="Approve" leftIcon={<FontAwesomeIcon icon={faCheck} />} onClick={getApprovalSignature} />
)}
{!isProcessing && !error && evaluationResponse && (
<div className="flex items-center gap-2">
{evaluationResponse.decision === Decision.PERMIT && (
<FontAwesomeIcon icon={faCheckCircle} className="text-nv-green-500" />
Expand All @@ -173,12 +187,13 @@ const PlaygroundEditor = () => {
</div>
)}
</div>
{!isProcessing && evaluationResponse && (
{!isProcessing && error && <div className="text-nv-red-500 truncate">{error}</div>}
{!isProcessing && !error && evaluationResponse && (
<div className="border-2 border-white rounded-t-xl p-4 overflow-auto">
<pre>{JSON.stringify(evaluationResponse, null, 3)}</pre>
</div>
)}
{!isProcessing && signature && <div className="text-nv-white truncate">{signature}</div>}
{!isProcessing && !error && signature && <div className="text-nv-white truncate">{signature}</div>}
</div>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"request": {
"resourceId": "eip155:eoa:0x494042504a8148a6d00ab10ed26043f5579ce00f",
"action": "signTransaction",
"nonce": "6bb2b3e9-fa8a-4081-a837-575184ceb3a5",
"transactionRequest": {
"from": "0xed123cf8e3ba51c6c15da1eac74b2b5deea31448",
"to": "0x031d8c0ca142921c459bcb28104c0ff37928f9ed",
"value": "0xde0b6b3a7640000",
"chainId": 137,
"type": "2"
}
},
"feeds": [
{
"source": "armory/price-feed",
"sig": "eyJhbGciOiJFSVAxOTEiLCJraWQiOiIweDlENDMyYTA5Q0JmNTVGMjJBYTZhMkUyOTBhY0IxMkQ1N2QyOUIyRmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJmZTcyMzA0NC0zNWRmLTRlOTktOTczOS0xMjJhNDhkNGFiOTYiLCJyZXF1ZXN0SGFzaCI6IjB4ZjdmY2MyMzRhMTMzNWNmNGE0NDE0MzAwNWJhOWJhNWQyY2VlMTJhMmZmZDFlNDMyYTU4Yjc5YjllNzcyYzk5NiIsInN1YiI6ImVpcDE1NTplb2E6MHg0OTQwNDI1MDRhODE0OGE2ZDAwYWIxMGVkMjYwNDNmNTU3OWNlMDBmIn0.BxgEr9TILTIocaNpQs-59vKhBSePpS-q0D4VWfVpqIs0bRSBp8vBMHkKa7AxdtfMwRCBd86vldj-1Ebb5UtmMhs",
"data": {
"eip155:137/slip44:966": {
"fiat:usd": "0.99",
"fiat:eur": "1.10"
}
}
},
{
"source": "armory/historical-transfer-feed",
"sig": "eyJhbGciOiJFSVAxOTEiLCJraWQiOiIweDlENDMyYTA5Q0JmNTVGMjJBYTZhMkUyOTBhY0IxMkQ1N2QyOUIyRmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJmZTcyMzA0NC0zNWRmLTRlOTktOTczOS0xMjJhNDhkNGFiOTYiLCJyZXF1ZXN0SGFzaCI6IjB4ZjdmY2MyMzRhMTMzNWNmNGE0NDE0MzAwNWJhOWJhNWQyY2VlMTJhMmZmZDFlNDMyYTU4Yjc5YjllNzcyYzk5NiIsInN1YiI6ImVpcDE1NTplb2E6MHg0OTQwNDI1MDRhODE0OGE2ZDAwYWIxMGVkMjYwNDNmNTU3OWNlMDBmIn0.BxgEr9TILTIocaNpQs-59vKhBSePpS-q0D4VWfVpqIs0bRSBp8vBMHkKa7AxdtfMwRCBd86vldj-1Ebb5UtmMhs",
"data": [
{
"amount": "500000000000000000",
"from": "eip155:eoa:0x494042504a8148a6d00ab10ed26043f5579ce00f",
"token": "eip155:137/slip44:966",
"rates": { "fiat:usd": "0.90", "fiat:eur": "1.10" },
"chainId": 137,
"initiatedBy": "61e775a9-5f68-41ab-a775-5806845e6e72"
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Action,
Address,
CredentialEntity,
Feed,
HistoricalTransfer,
TransactionRequest,
UserRole
Expand All @@ -25,6 +26,7 @@ export type Input = {
resource?: { uid: string }
approvals?: CredentialEntity[]
transfers?: HistoricalTransfer[]
feeds?: Feed<unknown>[]
}

// TODO: (@wcalderipe, 18/03/24) Check with @samteb how can we replace these
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CredentialEntity,
Entities,
EvaluationRequest,
Feed,
Request,
SignMessageAction,
SignRawAction,
Expand All @@ -15,9 +16,14 @@ import { indexBy } from 'lodash/fp'
import { OpenPolicyAgentException } from '../exception/open-policy-agent.exception'
import { Data, Input, UserGroup, WalletGroup } from '../type/open-policy-agent.type'

type Mapping<R extends Request> = (request: R, principal: CredentialEntity, approvals?: CredentialEntity[]) => Input
type Mapping<R extends Request> = (
request: R,
principal: CredentialEntity,
approvals?: CredentialEntity[],
feeds?: Feed<unknown>[]
) => Input

const toSignTransaction: Mapping<SignTransactionAction> = (request, principal, approvals): Input => {
const toSignTransaction: Mapping<SignTransactionAction> = (request, principal, approvals, feeds): Input => {
const result = safeDecode({
input: {
type: InputType.TRANSACTION_REQUEST,
Expand All @@ -32,7 +38,8 @@ const toSignTransaction: Mapping<SignTransactionAction> = (request, principal, a
approvals,
intent: result.intent,
transactionRequest: request.transactionRequest,
resource: { uid: request.resourceId }
resource: { uid: request.resourceId },
feeds
}
}

Expand Down Expand Up @@ -105,8 +112,25 @@ export const toInput = (params: {
])
const mapper = mappers.get(action)

// For demo purpose, we need to modify timestamps of transaction
// and fake it that those happened in the last 24hours

// const feedsDemo = evaluation.feeds?.map((feed) => {
// if (feed.source === 'armory/historical-transfer-feed') {
// const { data, ...res } = feed
// const dataRes = (data as any[]).map((d: any) => ({
// ...d,
// timestamp: new Date().getTime() - 10 * 60 * 60 * 1000
// }))

// return { ...res, data: dataRes }
// }

// return feed
// }) as Feed<unknown>[]

if (mapper) {
return mapper(evaluation.request, params.principal, params.approvals)
return mapper(evaluation.request, params.principal, params.approvals, evaluation.feeds)
}

throw new OpenPolicyAgentException({
Expand Down
4 changes: 2 additions & 2 deletions packages/policy-engine-shared/src/lib/type/domain.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const Feed = <Data extends ZodTypeAny>(dataSchema: Data) =>
z.object({
source: z.string(),
sig: JwtString.nullable(),
data: dataSchema
data: dataSchema.optional()
})

/**
Expand All @@ -116,7 +116,7 @@ export type Feed<Data> = {
* any trusted data sources if they prefer.
*/
sig: JwtString | null
data: Data
data?: Data
}

export const EvaluationRequest = z
Expand Down

0 comments on commit 3ce4241

Please sign in to comment.