Skip to content

Commit

Permalink
Add permit deadline criteria (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
samteb authored Jan 30, 2024
1 parent b897218 commit d28afd0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_contractDeploy {
"intent": {
"from": "eip155:eoa:0xddcf208f219a6e6af072f2cfdc615b2c1805f98e",
"type": "deployContract",
"bytecode": "",
"chainId": 137,
},
}

Expand All @@ -15,4 +15,6 @@ test_contractDeploy {

checkSourceAddress({"eip155:eoa:0xddcf208f219a6e6af072f2cfdc615b2c1805f98e"}) with input as contractDeployRequest
with data.entities as entities

checkIntentChainId({1, 137}) with input as contractDeployRequest with data.entities as entities
}
48 changes: 48 additions & 0 deletions apps/authz/src/opa/rego/__test__/criteria/intent/permit_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

test_permit {
permitRequest = {
"action": "signTransaction",
"intent": {
"type": "permit",
"from": "eip155:eoa:0xddcf208f219a6e6af072f2cfdc615b2c1805f98e",
"spender": "eip155:137:0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3",
"token": "eip155:137/erc20:0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
"amount": "1000000000000000000",
"deadline": "1634025600", # in ms
},
}

checkIntentType({"permit", "permit2"}) with input as permitRequest
with data.entities as entities

checkSourceAddress({"eip155:eoa:0xddcf208f219a6e6af072f2cfdc615b2c1805f98e"}) with input as permitRequest
with data.entities as entities

checkIntentSpenderAddress({"eip155:137:0xa45e21e9370ba031c5e1f47dedca74a7ce2ed7a3"}) with input as permitRequest
with data.entities as entities

checkIntentTokenAddress({"eip155:137/erc20:0x2791bca1f2de4661ed88a30c99a7a9449aa84174"}) with input as permitRequest
with data.entities as entities

checkIntentAmount({"currency": wildcard, "operator": "lte", "value": "1000000000000000000"}) with input as permitRequest
with data.entities as entities

checkPermitDeadline({"operator": "eq", "value": "1634025600"}) with input as permitRequest
with data.entities as entities

checkPermitDeadline({"operator": "neq", "value": "111111111"}) with input as permitRequest
with data.entities as entities

checkPermitDeadline({"operator": "lte", "value": "1634025600"}) with input as permitRequest
with data.entities as entities

checkPermitDeadline({"operator": "gte", "value": "1634025600"}) with input as permitRequest
with data.entities as entities

checkPermitDeadline({"operator": "lt", "value": "16340256000"}) with input as permitRequest
with data.entities as entities

checkPermitDeadline({"operator": "gt", "value": "163402560"}) with input as permitRequest
with data.entities as entities
}
12 changes: 6 additions & 6 deletions apps/authz/src/opa/rego/lib/criteria/intent/amount.rego
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ intentAmount(currency) = result {

checkIntentAmount(condition) {
condition.operator == "eq"
to_number(condition.value) == intentAmount(condition.currency)
intentAmount(condition.currency) == to_number(condition.value)
}

checkIntentAmount(condition) {
condition.operator == "neq"
to_number(condition.value) != intentAmount(condition.currency)
intentAmount(condition.currency) != to_number(condition.value)
}

checkIntentAmount(condition) {
condition.operator == "gt"
to_number(condition.value) < intentAmount(condition.currency)
intentAmount(condition.currency) > to_number(condition.value)
}

checkIntentAmount(condition) {
condition.operator == "lt"
to_number(condition.value) > intentAmount(condition.currency)
intentAmount(condition.currency) < to_number(condition.value)
}

checkIntentAmount(condition) {
condition.operator == "gte"
to_number(condition.value) <= intentAmount(condition.currency)
intentAmount(condition.currency) >= to_number(condition.value)
}

checkIntentAmount(condition) {
condition.operator == "lte"
to_number(condition.value) >= intentAmount(condition.currency)
intentAmount(condition.currency) <= to_number(condition.value)
}
36 changes: 36 additions & 0 deletions apps/authz/src/opa/rego/lib/criteria/intent/permit.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import future.keywords.in

# convert ms to ns
permitDeadlineMs = to_number(input.intent.deadline)

checkPermitDeadline(condition) {
condition.operator == "eq"
permitDeadlineMs == to_number(condition.value)
}

checkPermitDeadline(condition) {
condition.operator == "neq"
permitDeadlineMs != to_number(condition.value)
}

checkPermitDeadline(condition) {
condition.operator == "lte"
permitDeadlineMs <= to_number(condition.value)
}

checkPermitDeadline(condition) {
condition.operator == "gte"
permitDeadlineMs >= to_number(condition.value)
}

checkPermitDeadline(condition) {
condition.operator == "lt"
permitDeadlineMs < to_number(condition.value)
}

checkPermitDeadline(condition) {
condition.operator == "gt"
permitDeadlineMs > to_number(condition.value)
}

0 comments on commit d28afd0

Please sign in to comment.