-
Notifications
You must be signed in to change notification settings - Fork 56
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 #958 from EYBlockchain/westlad/kyc
KYC backend functionality
- Loading branch information
Showing
28 changed files
with
30,772 additions
and
18,705 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,21 @@ jobs: | |
npm ci | ||
npm run test | ||
unit-tests: | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: '16.17.0' | ||
- name: Unit Tests | ||
run: | | ||
npm ci | ||
cd common-files | ||
npm ci | ||
cd ../ | ||
npm run unit-test | ||
circuits-test: | ||
runs-on: ubuntu-20.04 | ||
env: | ||
|
@@ -136,6 +151,52 @@ jobs: | |
name: ganache-test-logs | ||
path: ./ganache-test.log | ||
|
||
kyc-test: | ||
env: | ||
WHITELISTING: enable | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: '16.17.0' | ||
|
||
- name: Start Containers | ||
run: | | ||
./setup-nightfall | ||
./start-nightfall -g -d &> kyc-test.log &disown | ||
- name: Wait for images to be ready | ||
uses: Wandalen/[email protected] | ||
with: | ||
command: | | ||
docker wait nightfall_3_deployer_1 | ||
attempt_limit: 100 | ||
attempt_delay: 20000 | ||
|
||
- name: Debug logs - after image builds | ||
if: always() | ||
run: cat kyc-test.log | ||
|
||
- name: Run integration test | ||
run: npm run test-kyc | ||
|
||
- name: Debug logs - after integration test run | ||
if: always() | ||
run: cat kyc-test.log | ||
|
||
- name: If integration test failed, shutdown the Containers | ||
if: failure() | ||
run: docker-compose -f docker-compose.yml -f docker-compose.ganache.yml down -v | ||
|
||
- name: If integration test failed, upload logs files as artifacts | ||
if: failure() | ||
uses: actions/upload-artifact@master | ||
with: | ||
name: kyc-test-logs | ||
path: ./ganackyche-test.log | ||
|
||
|
||
optimist-sync-test: | ||
runs-on: ubuntu-18.04 | ||
steps: | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Nightfall KYC adaptions | ||
|
||
Nightfall now incorporates the ability to manage a whitelist of accounts in support of KYC (Know Your Customer). When whitelisting is enabled, only accounts that are added to the whitelist are able to move funds from Layer 1 to Layer 2 and to withdraw Layer 1 funds from the Shield contract. | ||
|
||
Whitelisting can be controlled either externally to the blockchain or via a smart contract. Nightfall is agnostic about how KYC is applied. | ||
|
||
## Enabling Whitelisting | ||
|
||
To enable whitelisting, the deployer container should have its `WHITELISTING` environment variable set to `enable`. Setting the `WHITELISTING` variable to anything else will desable whitlisting. | ||
|
||
## Operating Whitelisting | ||
|
||
The KYC adaptions have recognise a new actor, the whitelist manager. A whitelist manager is able to whitelist users and to remove them from the whitelist. Each whitelist manager manager has a group ID associated with them, and users are added to the whitelist managers group ID when they are whitelisted. In practice, the group ID currently has little effect, other than acting as a grouping variable; all whitelisted users can interact, regardless of their group ID. | ||
|
||
Whitelist managers are created/removed by the contract owner (multisig). They can also operate as normal Nightfall users, thus they are able to whitelist themselves. | ||
|
||
All whitelisting functionality is managed by the contract `KYC.sol`, the functions therein are self-explanatory. | ||
|
||
Note that all users are, by default members of the null group (group ID = 0). Members of this group are NOT whitelisted when whitelisting is enabled. Memebership of any other group confirs whitlisted status. |
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
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
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,48 @@ | ||
/** | ||
Routes to perform whitelist manager KYC work | ||
*/ | ||
|
||
import express from 'express'; | ||
import logger from 'common-files/utils/logger.mjs'; | ||
import { addUserToWhitelist, removeUserFromWhitelist, isWhitelisted } from '../services/kyc.mjs'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/check', async (req, res, next) => { | ||
try { | ||
const { address } = req.query; | ||
logger.debug(`Details requested with address ${address}`); | ||
const whitelisted = await isWhitelisted(address); | ||
res.json({ isWhitelisted: whitelisted }); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
/** | ||
Add a use to a KYC whitelist (only works if user is a whitelist manager, otherwise just wastes gas) | ||
*/ | ||
router.post('/add', async (req, res, next) => { | ||
const { address } = req.body; | ||
try { | ||
const response = await addUserToWhitelist(address); | ||
res.json(response); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
/** | ||
Add a use to a KYC whitelist (only works if user is a relevant (to the group) whitelist manager, otherwise just wastes gas) | ||
*/ | ||
router.post('/remove', async (req, res, next) => { | ||
const { address } = req.body; | ||
try { | ||
const response = await removeUserFromWhitelist(address); | ||
res.json(response); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
export default router; |
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,23 @@ | ||
/** | ||
This module creates blockchain transactions to interact with the KYC smart contract | ||
*/ | ||
|
||
import constants from 'common-files/constants/index.mjs'; | ||
import { waitForContract } from 'common-files/utils/contract.mjs'; | ||
|
||
const { SHIELD_CONTRACT_NAME } = constants; | ||
|
||
export async function isWhitelisted(address) { | ||
const shieldContractInstance = await waitForContract(SHIELD_CONTRACT_NAME); | ||
return shieldContractInstance.methods.isWhitelisted(address).call(); | ||
} | ||
|
||
export async function addUserToWhitelist(address) { | ||
const shieldContractInstance = await waitForContract(SHIELD_CONTRACT_NAME); | ||
return shieldContractInstance.methods.addUserToWhitelist(address).encodeABI(); | ||
} | ||
|
||
export async function removeUserFromWhitelist(address) { | ||
const shieldContractInstance = await waitForContract(SHIELD_CONTRACT_NAME); | ||
return shieldContractInstance.methods.removeUserFromWhitelist(address).encodeABI(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: CC0-1.0 | ||
import './Ownable.sol'; | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
contract KYC is Ownable { | ||
|
||
bool public whitelisting; | ||
mapping(address => uint256) public users; | ||
mapping(address => uint256) public managers; | ||
|
||
// groupIds cannot be zero, although we don't specifcally chack for this because assigning a groupId of zero has | ||
// no effect other than wasting gas. | ||
|
||
function initialize() override virtual public onlyInitializing { | ||
whitelisting = false; | ||
Ownable.initialize(); | ||
} | ||
|
||
function addUserToWhitelist(address _user) external { | ||
// if a non-manager calls this, they will just assign someone to the zero group, which is the null value and has no effect | ||
users[_user] = managers[msg.sender]; | ||
} | ||
|
||
function removeUserFromWhitelist(address _user) external { | ||
require(users[_user] != 0, 'This user is not whitelisted, so cannot be delisted'); | ||
require (managers[msg.sender] == users[_user], 'You are not the manager of this group' ); | ||
delete users[_user]; | ||
} | ||
|
||
function createWhitelistManager(uint256 _groupId, address _manager) external onlyOwner { | ||
managers[_manager] = _groupId; | ||
} | ||
|
||
function removeWhitelistManager(address _manager) external onlyOwner { | ||
delete managers[_manager]; | ||
} | ||
|
||
function enableWhitelisting(bool _whitelisting) external onlyOwner { | ||
whitelisting = _whitelisting; | ||
} | ||
|
||
function isWhitelisted(address _user) public view returns (bool) { | ||
if (whitelisting == false ) return true; // whitelisting is turned off | ||
if (users[_user] != 0) return true; | ||
return false; | ||
} | ||
|
||
function isWhitelistManager(address _manager) public view returns (uint) { | ||
return managers[_manager]; | ||
} | ||
} |
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
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
Oops, something went wrong.