-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #372 from privacy-scaling-explorations/feat/balanc…
…e-credential feat(credentials): add the blockchain balance validator
- Loading branch information
Showing
5 changed files
with
151 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
libs/credentials/src/validators/blockchainBalance/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { BigNumber } from "ethers" | ||
import { validateCredentials } from "../.." | ||
import blockchainBalance from "./index" | ||
|
||
describe("BlockchainBalance", () => { | ||
const jsonRpcProviderMocked = { | ||
getBalance: jest.fn() | ||
} | ||
|
||
it("Should return true if an account has a balance greater than or equal to 10", async () => { | ||
jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from(12)) | ||
|
||
const result = await validateCredentials( | ||
{ | ||
id: blockchainBalance.id, | ||
criteria: { | ||
minBalance: "10" | ||
} | ||
}, | ||
{ | ||
address: "0x", | ||
jsonRpcProvider: jsonRpcProviderMocked | ||
} | ||
) | ||
|
||
expect(result).toBeTruthy() | ||
}) | ||
|
||
it("Should return true if an account has a balance greater than or equal to 10 using the block number", async () => { | ||
jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from(12)) | ||
|
||
const result = await validateCredentials( | ||
{ | ||
id: blockchainBalance.id, | ||
criteria: { | ||
minBalance: "10" | ||
} | ||
}, | ||
{ | ||
address: "0x", | ||
jsonRpcProvider: jsonRpcProviderMocked, | ||
blockNumber: 4749638 | ||
} | ||
) | ||
|
||
expect(result).toBeTruthy() | ||
}) | ||
|
||
it("Should throw an error if a criteria parameter is missing", async () => { | ||
const fun = () => | ||
validateCredentials( | ||
{ | ||
id: blockchainBalance.id, | ||
criteria: {} | ||
}, | ||
{ | ||
address: "0x", | ||
jsonRpcProvider: undefined | ||
} | ||
) | ||
|
||
await expect(fun).rejects.toThrow( | ||
"Parameter 'minBalance' has not been defined" | ||
) | ||
}) | ||
|
||
it("Should throw an error if a criteria parameter should not exist", async () => { | ||
const fun = () => | ||
validateCredentials( | ||
{ | ||
id: blockchainBalance.id, | ||
criteria: { | ||
minBalance: "100", | ||
minStars: 200 | ||
} | ||
}, | ||
{ | ||
address: "0x", | ||
jsonRpcProvider: undefined | ||
} | ||
) | ||
|
||
await expect(fun).rejects.toThrow( | ||
"Parameter 'minStars' should not be part of the criteria" | ||
) | ||
}) | ||
|
||
it("Should throw a type error if a criteria parameter has the wrong type", async () => { | ||
const fun = () => | ||
validateCredentials( | ||
{ | ||
id: blockchainBalance.id, | ||
criteria: { | ||
minBalance: 100 | ||
} | ||
}, | ||
{ | ||
address: "0x", | ||
jsonRpcProvider: undefined | ||
} | ||
) | ||
|
||
await expect(fun).rejects.toThrow( | ||
"Parameter 'minBalance' is not a string" | ||
) | ||
}) | ||
}) |
35 changes: 35 additions & 0 deletions
35
libs/credentials/src/validators/blockchainBalance/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { BigNumber } from "ethers" | ||
import { BlockchainContext, Validator } from "../.." | ||
|
||
export type Criteria = { | ||
minBalance: string | ||
} | ||
|
||
const validator: Validator = { | ||
id: "BLOCKCHAIN_BALANCE", | ||
|
||
criteriaABI: { | ||
minBalance: "string" | ||
}, | ||
|
||
/** | ||
* It checks if a user has a balance greater than or equal to 'minBalance'. | ||
* @param criteria The criteria used to check user's credentials. | ||
* @param context Context variables. | ||
* @returns True if the user meets the criteria. | ||
*/ | ||
async validate(criteria: Criteria, context) { | ||
if ("address" in context) { | ||
const balance = await ( | ||
context as BlockchainContext | ||
).jsonRpcProvider.getBalance( | ||
(context as BlockchainContext).address, | ||
(context as BlockchainContext).blockNumber | ||
) | ||
return balance >= BigNumber.from(criteria.minBalance) | ||
} | ||
throw new Error("No address value found") | ||
} | ||
} | ||
|
||
export default validator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters