Skip to content

Commit

Permalink
Add support for sign messages
Browse files Browse the repository at this point in the history
  • Loading branch information
samteb committed Feb 27, 2024
1 parent bd13b97 commit 2421728
Show file tree
Hide file tree
Showing 8 changed files with 1,502 additions and 1,434 deletions.

This file was deleted.

Large diffs are not rendered by default.

65 changes: 61 additions & 4 deletions apps/policy-engine/src/opa/script/evaluate-legacy-policy.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,63 @@ export const run = async () => {
}
}

if (request.action === Action.SIGN_MESSAGE) {
const intentResult = safeDecode({
input: {
type: InputType.MESSAGE,
payload: request.message
}
})

if (intentResult?.success === false) {
console.log(`Could not decode intent: ${intentResult.error.message}`, JSON.stringify(request.message, null, 2))
continue
}

input = {
action: request.action,
intent: intentResult?.intent,
principal: {
uid: initiator_user_id,
userId: initiator_user_id,
alg: 'ES256K',
pubKey: ''
},
resource: request.resourceId ? { uid: request.resourceId } : undefined,
approvals: []
}
}

if (request.action === Action.SIGN_TYPED_DATA) {
const intentResult = safeDecode({
input: {
type: InputType.TYPED_DATA,
typedData: JSON.parse(request.typedData)
}
})

if (intentResult?.success === false) {
console.log(
`Could not decode intent: ${intentResult.error.message}`,
JSON.stringify(request.typedData, null, 2)
)
continue
}

input = {
action: request.action,
intent: intentResult?.intent,
principal: {
uid: initiator_user_id,
userId: initiator_user_id,
alg: 'ES256K',
pubKey: ''
},
resource: request.resourceId ? { uid: request.resourceId } : undefined,
approvals: []
}
}

const OPA_WASM_PATH = path.join(process.cwd(), './rego-build/policy.wasm')
const policyWasm = readFileSync(OPA_WASM_PATH)
const opaEngine = await loadPolicy(policyWasm, undefined, { 'time.now_ns': () => new Date().getTime() * 1000000 })
Expand All @@ -81,16 +138,16 @@ export const run = async () => {
const evalResult: { result: OpaResult }[] = await opaEngine.evaluate(input, 'main/evaluate')
const results = evalResult.map(({ result }) => result)

Check failure on line 139 in apps/policy-engine/src/opa/script/evaluate-legacy-policy.script.ts

View workflow job for this annotation

GitHub Actions / Build and test

'results' is assigned a value but never used

// if (request.action === Action.SIGN_TRANSACTION && status == 'denied' && results[0].permit) {
// if (status == 'denied' && results[0].permit) {
// // console.log({ id: results[0].reasons.map((reason) => reason.policyName), status, result: results[0].permit })
// if ((input.intent as any).type.includes('transfer')) {
// console.log({ intent: input.intent, initiator_user_id, status, result: results[0].permit })
// }
// }

if (request.action === Action.SIGN_TRANSACTION && status == 'completed' && !results[0].permit) {
console.log({ id: results[0].reasons.map((reason) => reason.policyName), status, result: results[0].permit })
}
// if (status == 'completed' && !results[0].permit) {
// console.log({ id: results[0].reasons.map((reason) => reason.policyName), status, result: results[0].permit })
// }
}
}

Expand Down
144 changes: 72 additions & 72 deletions apps/policy-engine/src/opa/script/requests/legacy-requests.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/policy-engine-shared/src/lib/type/action.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ export type SignMessageAction = BaseAction & {
message: string
}

export type SignTypedDataAction = BaseAction & {
action: typeof Action.SIGN_TYPED_DATA
resourceId: string
typedData: string
}

export type CreateOrganizationAction = BaseAction & {
action: typeof Action.CREATE_ORGANIZATION
organization: {
Expand Down
10 changes: 8 additions & 2 deletions packages/policy-engine-shared/src/lib/type/domain.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { AssetId } from '../util/caip.util'
import { CreateOrganizationAction, SignMessageAction, SignTransactionAction, Signature } from './action.type'
import {
CreateOrganizationAction,
SignMessageAction,
SignTransactionAction,
SignTypedDataAction,
Signature
} from './action.type'

export enum Decision {
PERMIT = 'Permit',
Expand Down Expand Up @@ -88,7 +94,7 @@ export type HistoricalTransfer = {
*/
export type Prices = Record<AssetId, Record<string, number>>

export type Request = SignTransactionAction | SignMessageAction | CreateOrganizationAction
export type Request = SignTransactionAction | SignMessageAction | SignTypedDataAction | CreateOrganizationAction

/**
* The feeds represent arbitrary data collected by the Armory and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const decode = ({ input, config = defaultConfig }: { input: DecodeInput; config?
return {
type: Intents.SIGN_RAW,
algorithm: input.raw.algorithm,
payload: input.raw.rawData
payload: input.raw.payload
}
default:
throw new DecoderError({ message: 'Invalid input type', status: 400 })
Expand Down
2 changes: 1 addition & 1 deletion packages/transaction-request-intent/src/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Eip712Domain = {
}

export type Raw = {
rawData: string
payload: string
algorithm: Alg
}

Expand Down

0 comments on commit 2421728

Please sign in to comment.