Skip to content

Commit

Permalink
Merge pull request #576 from bandada-infra/feat/libs-credentials
Browse files Browse the repository at this point in the history
Update EAS attestations criteria and context
  • Loading branch information
vplasencia authored Oct 10, 2024
2 parents 3f13a2c + 9ed3b83 commit 298c50d
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 21 deletions.
6 changes: 5 additions & 1 deletion libs/credentials/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ Validates the EAS attestations of a user.
- **id**: The id for the EAS attestations validation.
- **criteria**:
- **minAttestations**: The minimum number of attestations required.
- **network**: The id of the supported attestation network.
- **attester** (_optional_): The attester of the attestation.
- **schemaId** (_optional_): The schema id of the attestation.
- **revocable** (_optional_): The revocable option of the attestation.
Expand All @@ -181,17 +182,21 @@ Validates the EAS attestations of a user.
- **address**: The user address to validate.

```typescript
import { easCredentialSupportedNetworks } from "@bandada/utils"
import {
validateCredentials,
easAttestations,
EASNetworks
} from "@bandada/credentials"

const network = easCredentialSupportedNetworks.find("sepolia")

validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: network.id,
schemaId: "0x",
attester: "0x1",
revocable: false,
Expand All @@ -200,7 +205,6 @@ validateCredentials(
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x2"
}
)
Expand Down
9 changes: 6 additions & 3 deletions libs/credentials/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ export enum EASNetworks {
ETHEREUM = "ethereum",
ETHEREUM_SEPOLIA = "sepolia",
ARBITRUM = "arbitrum",
ARIBITRUM_NOVA = "arbitrum-nova",
BASE = "base",
BASE_GOERLI = "base-goerli",
BASE_SEPOLIA = "base-sepolia",
LINEA = "linea",
OPTIMISM = "optimism",
OPTIMISM_GOERLI = "optimism-goerli"
OPTIMISM_SEPOLIA = "optimism-sepolia-bedrock",
SCROLL = "scroll",
POLYGON = "polygon",
CELO = "celo"
}

export type Web2Context = {
Expand All @@ -27,7 +31,6 @@ export type BlockchainContext = {
}

export type EASContext = {
network: EASNetworks
address: BigNumberish
}

Expand Down
50 changes: 38 additions & 12 deletions libs/credentials/src/validators/easAttestations/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EASNetworks, validateCredentials } from "../.."
import { validateCredentials } from "../.."
import easAttestations from "./index"

jest.mock("../..", () => ({
Expand All @@ -15,12 +15,12 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 3,
network: "sepolia",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c"
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -36,6 +36,7 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "sepolia",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -45,7 +46,6 @@ describe("EASAttestations", () => {
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -61,6 +61,7 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "sepolia",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d4",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -70,7 +71,6 @@ describe("EASAttestations", () => {
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d4"
}
)
Expand All @@ -86,6 +86,7 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "sepolia",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5d",
Expand All @@ -95,7 +96,6 @@ describe("EASAttestations", () => {
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -111,6 +111,7 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "sepolia",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -120,7 +121,6 @@ describe("EASAttestations", () => {
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -136,6 +136,7 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "sepolia",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -145,7 +146,6 @@ describe("EASAttestations", () => {
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -161,6 +161,7 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "sepolia",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -170,7 +171,6 @@ describe("EASAttestations", () => {
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -186,12 +186,12 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 3,
network: "sepolia",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c"
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x"
}
)
Expand All @@ -211,7 +211,6 @@ describe("EASAttestations", () => {
criteria: {}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -234,13 +233,13 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "sepolia",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
test: 123
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -261,12 +260,12 @@ describe("EASAttestations", () => {
id: easAttestations.id,
criteria: {
minAttestations: "1",
network: "sepolia",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c"
}
},
{
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)
Expand All @@ -275,4 +274,31 @@ describe("EASAttestations", () => {
"Parameter 'minAttestations' is not a number"
)
})

it("Should throw invalid network error if the network is invalid", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => {
throw new Error("Invalid network")
})

const fun = validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
network: "test",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
revocable: true,
revoked: false,
isOffchain: false
}
},
{
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

await expect(fun).rejects.toThrow("Invalid network")
})
})
22 changes: 18 additions & 4 deletions libs/credentials/src/validators/easAttestations/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Context, Validator } from "../.."
import { Context, EASNetworks, Validator } from "../.."
import provider from "../../providers/eas"

export type Criteria = {
minAttestations: number
network: string
schemaId?: string
attester?: string
revocable?: boolean
Expand All @@ -18,6 +19,10 @@ const validator: Validator = {
type: "number",
optional: false
},
network: {
type: "string",
optional: false
},
attester: {
type: "string",
optional: true
Expand Down Expand Up @@ -47,16 +52,25 @@ const validator: Validator = {
* @returns True if the user meets the criteria.
*/
async validate(criteria: Criteria, context: Context) {
if ("network" in context) {
if (
"address" in context &&
criteria.minAttestations &&
criteria.network
) {
const {
minAttestations,
network,
attester,
schemaId,
revocable,
revoked,
isOffchain
} = criteria

if (!Object.values(EASNetworks).includes(network as EASNetworks)) {
throw new Error("Invalid network")
}

let whereConditions = `recipient: { equals: "${context.address}" }`

if (attester !== undefined && attester !== null) {
Expand Down Expand Up @@ -91,14 +105,14 @@ const validator: Validator = {
}`

const attestations = await provider.queryGraph(
context.network,
network as EASNetworks,
query
)

return attestations.data.attestations.length >= minAttestations
}

throw new Error("No recipient value found")
throw new Error("No address value found")
}
}

Expand Down
24 changes: 24 additions & 0 deletions libs/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,27 @@ import { getContractAddresses } from "@bandada/utils"

const addresses = getContractAddresses("sepolia")
```

\# **blockchainCredentialSupportedNetworks**

Returns the list of supported blockchain networks.

```ts
import { blockchainCredentialSupportedNetworks } from "@bandada/utils"

const networks = blockchainCredentialSupportedNetworks
const id = blockchainCredentialSupportedNetworks.find("sepolia")
const name = blockchainCredentialSupportedNetworks.find("Sepolia")
```

\# **easCredentialSupportedNetworks**

Returns the list of supported EAS attestations networks. List of supported networks can be found [here](https://docs.attest.org/docs/developer-tools/api).

```ts
import { easCredentialSupportedNetworks } from "@bandada/utils"

const networks = easCredentialSupportedNetworks
const id = easCredentialSupportedNetworks.find("sepolia")
const name = easCredentialSupportedNetworks.find("Ethereum (Sepolia)")
```
Loading

0 comments on commit 298c50d

Please sign in to comment.