NFT verification with OPA

1. Via REST API

Add new policy

After the development of a policy using Rego, we need to add it to the NFT Kit.

API Doc

Swagger Doc | ReDoc

Curl call example

curl -X POST "https://nftkit.walt-test.cloud/v2/nftkit/nft/verifier/policies/create"  
 -H  "Content-Type: application/json" 
 -d "{\"name\":\"string\",\"description\":\"string\",\"input\":{\"additionalProp1\":{},\"additionalProp2\":{},\"additionalProp3\":{}},\"policy\":\"string\",\"policyQuery\":\"string\",\"policyEngine\":\"OPA\"}"

Body parameters:

{
  "name": "string",
  "description": "string",
  "input": {
    "additionalProp1": {},
    "additionalProp2": {},
    "additionalProp3": {}
  },
  "policy": "string",
  "policyQuery": "string",
  "policyEngine": "OPA"
}
  • name: [string] policy name.

  • description: [string] policy description.

  • input: [string] the input parameter of the open policy engine(OPA).

  • policy: [string] the policy Rego code that will be executed by OPA.

  • policyQuery: [string] the query parameter of the open policy engine(OPA).

  • policyEngine: [string] the engine who will run the policy.

Example:

curl -X POST "http://0.0.0.0:7000/v2/nftkit/nft/verifier/policies/create" 
 -H  "Content-Type: application/json" 
 -d "{  \"name\": \"policy 1\",  \"description\": \"policy 1\",  \"input\": {    \"Background\": \"Purple\",    \"Body\": \"Body 11\"  },  \"policy\": \"package app.nftimport future.keywords.ifdefault allow := falseallow if {\tvalid_nft_Background\tvalid_nft_Body}valid_nft_Background if input.Background= data.Backgroundvalid_nft_Body if input.Body= data.Body\",  \"policyQuery\": \"data.app.nft.allow\",  \"policyEngine\": \"OPA\"}"

Rego policy example:

package app.nft

import future.keywords.if

default allow := false


allow if {
	valid_nft_Background
	valid_nft_Body
}


valid_nft_Background if input.Background= data.Background

valid_nft_Body if input.Body= data.Body

Get policy list

Get the list of added policies.

API Doc

Swagger Doc | ReDoc

Curl call example

curl -X GET "http://0.0.0.0:7000/v2/nftkit/nft/verifier/policies"

Apply a policy to a NFT

Execute a policy against a NFT metadata.

API Doc

Swagger Doc | ReDoc

Curl call example

curl -X GET "http://0.0.0.0:7000/v2/nftkit/nft/verifier/chain/{{chain}}/contract/{{contract}}/token/{{tokenId}}/policy/{{policyName}}/verification"

Path parameters:

  • chain: [string] chain to work with. Either TEZOS, GHOSTNET, ETHEREUM, POLYGON, GOERLI, MUMBAI , TESTNET or MAINNET .

  • contract: [string] smart contract address

  • tokenId: [string] token id of the NFT

  • policyName: [string] the name of an already-added policy.

Example:

curl -X GET "http://0.0.0.0:7000/v2/nftkit/nft/verifier/chain/TEZOS/contract/KT1U6EHmNxJTkvaWJ4ThczG4FSDaHC21ssvi/token/1462880/policy/policy1/verification" 

2. Via code

Setup

  1. You need have OPA in your local machine. Running OPA instructions.

  2. You need to add a path of OPA in the environment variable. When using IntelliJ IDEA:

Code examples

You can run an OPA policy against an NFT metadata from an EVM compatible or Tezos chains.

  • EVM compatible:

val inputs = mutableMapOf("type" to "T1", "model" to "M1",
"reference" to "R1")//in the IDP Kit, inputs parameterexist in idp-Config.json
val policy = "/home/walid/Desktop/walt.id/opa/nft.rego"
/*in the IDP Kit, policy parameter exist in
idp-Config.json.
Path file that contains rego code*/
val query= "data.app.nft.allow" //in the IDP Kit, query parameter exist in idp-Config.json
val nftMetadata = NftService.getNftMetadata(Chain.MUMBAI,
        "0xf277BE034881eE38A9b270E5b6C5c6f333Af2517", BigInteger.valueOf(0))
val nftMetadataWrapper= NftMetadataWrapper(evmNftMetadata = nftMetadata)
val result= DynamicPolicy.doVerify(inputs, policy, query,
        nftMetadataWrapper) //true or false
  • Tezos

val inputs = mutableMapOf("type" to "T1", "model" to "M1",
"reference" to "R1")//in the IDP Kit, inputs parameterexist in idp-Config.json
val policy = "/home/walid/Desktop/walt.id/opa/nft.rego"
/*in the IDP Kit, policy parameter exist in
idp-Config.json.
Path file that contains rego code*/
val query= "data.app.nft.allow" //in the IDP Kit, query parameter exist in idp-Config.json
val nftMetadata = TezosNftService.getNftTezosMetadata(Chain.TEZOS,
    "KT1RCzrLhBpdKXkyasKGpDTCcdbBTipUk77x", "1")
val nftMetadataWrapper= NftMetadataWrapper(tezosNftMetadata = nftMetadata)
val result= DynamicPolicy.doVerify(inputs, policy, query,
        nftMetadataWrapper) //true or false

Rego Code Example:

package app.nft
import future.keywords.if
default allow := false
allow if {
valid_datanft_type
valid_datanft_model
valid_datanft_reference
}
valid_datanft_type if input.type= data.type
valid_datanft_model if input.model= data.model
valid_datanft_reference if input.reference= data.reference

Last updated