Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data providers tests for vip3 #2431

Merged
merged 41 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
dfb6095
revert data-provider-tests
0xverin Jan 15, 2024
d924341
fix CorePrimitivesIdentity imports
0xverin Jan 18, 2024
f09fef4
Restore the data-provider test.
0xverin Jan 24, 2024
cc54692
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 24, 2024
5603473
remove vipp3 test file
0xverin Jan 24, 2024
f884970
debug vcPayloadJson
0xverin Jan 24, 2024
23e886c
refactor credential definitions
0xverin Jan 25, 2024
934de08
format
0xverin Jan 25, 2024
568f336
add event listening
0xverin Jan 25, 2024
c6504e2
complete vip3 tests
0xverin Jan 25, 2024
6738c79
fix vip3MembershipCardSliver
0xverin Jan 25, 2024
d2ec757
add to ci.yml
0xverin Jan 26, 2024
c654917
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 26, 2024
e51c132
fix ci
0xverin Jan 26, 2024
248fb0a
extract and reuse function
0xverin Jan 26, 2024
ed722f2
test cli
0xverin Jan 26, 2024
41b613a
format
0xverin Jan 26, 2024
e5dd23b
remove client config
0xverin Jan 26, 2024
79feee1
format
0xverin Jan 26, 2024
4a945ee
random substrate wallet
0xverin Jan 26, 2024
f76265a
test client
0xverin Jan 26, 2024
24a4787
fix cli issue
0xverin Jan 26, 2024
9d37fc2
fix cli path
0xverin Jan 26, 2024
3b0d09c
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 26, 2024
53843ae
env example
0xverin Jan 26, 2024
10292b5
debug local
0xverin Jan 26, 2024
fb88115
format
0xverin Jan 26, 2024
cd91504
fix cli path
0xverin Jan 26, 2024
1ffd66d
test mock address
0xverin Jan 26, 2024
70f825c
withdraw account test.
0xverin Jan 26, 2024
0411ac4
modefy into json file
0xverin Jan 29, 2024
a9e8e19
delete ts file
0xverin Jan 29, 2024
0376100
format
0xverin Jan 29, 2024
772fd21
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 29, 2024
dfac634
improve logic
0xverin Jan 29, 2024
5da02c2
fix comments
0xverin Jan 30, 2024
f45272f
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 30, 2024
9e37d11
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 30, 2024
6b7eb64
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 30, 2024
639d04e
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 30, 2024
3e374c4
Merge branch 'dev' into vip3-vc-tests
0xverin Jan 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ jobs:
- test_name: lit-parentchain-nonce
- test_name: lit-ii-batch-test
- test_name: lit-test-stress-script
- test_name: lit-data-providers-test
steps:
- uses: actions/checkout@v4

Expand Down
24 changes: 24 additions & 0 deletions tee-worker/docker/lit-data-providers-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
services:
lit-data-providers-test:
image: litentry/litentry-cli:latest
container_name: litentry-data-providers-test
volumes:
- ../ts-tests:/ts-tests
- ../client-api:/client-api
- ../cli:/usr/local/worker-cli
build:
context: ..
dockerfile: build.Dockerfile
target: deployed-client
depends_on:
litentry-node:
condition: service_healthy
litentry-worker-1:
condition: service_healthy
networks:
- litentry-test-network
entrypoint: "bash -c '/usr/local/worker-cli/lit_ts_integration_test.sh test-data-providers 2>&1' "
restart: "no"
networks:
litentry-test-network:
driver: bridge
4 changes: 3 additions & 1 deletion tee-worker/ts-tests/integration-tests/.env.local.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
NODE_ENV = local
WORKER_ENDPOINT = wss://localhost:2000
NODE_ENDPOINT = ws://localhost:9944
NODE_ENDPOINT = ws://localhost:9944
BINARY_DIR=../../bin
LITENTRY_CLI_DIR=../../bin/litentry-cli
1 change: 1 addition & 0 deletions tee-worker/ts-tests/integration-tests/.env.staging
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ NODE_ENV = staging
WORKER_ENDPOINT = wss://litentry-worker-1:2011
NODE_ENDPOINT = "ws://litentry-node:9912"
BINARY_DIR=/usr/local/bin
LITENTRY_CLI_DIR=/usr/local/bin/litentry-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CorePrimitivesAssertion, CorePrimitivesNetworkWeb3Network } from 'parachain-api';
import type { Codec } from '@polkadot/types-codec/types';
import type { U8aLike } from '@polkadot/util/types';
type DataProvider = {
id: string;
name: string;
url: string;
};

type AssertionGenericPayload = string | Array<string | number | Codec | U8aLike> | Record<string, unknown>;

import vip3Json from './vip3-credential-test.json' assert { type: 'json' };
export const vip3CredentialJson = vip3Json as unknown as CredentialDefinition[];

export interface CredentialDefinition {
id: string;
name: string;
description: string;
assertion: {
id: CorePrimitivesAssertion['type'];
payload: AssertionGenericPayload;
};
dataProvider: DataProvider;
network: CorePrimitivesNetworkWeb3Network['type'];
mockDid: string;
mockWeb3Network: string;
expectedCredentialValue: boolean;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id": "vip3-membership-card-gold",
"name": "VIP3 Membership Card Gold",
"description": "VIP3 Membership Card Gold",
"assertion": {
"id": "Vip3MembershipCard",
"payload": "Gold"
},
"dataProvider": "vip3",
"network": "ethereum",
"mockDid": "litentry:evm:0x651614cA9097C5ba189Ef85e7851Ef9cff592B2c",
"mockWeb3Network": "bsc,ethereum",
"expectedCredentialValue": true
},
{
"id": "vip3-membership-card-silver",
"name": "VIP3 Membership Card Silver",
"description": "VIP3 Membership Card Silver",
"assertion": {
"id": "Vip3MembershipCard",
"payload": "Silver"
},
"dataProvider": "vip3",
"network": "ethereum",
"mockDid": "litentry:evm:0x10CdF7F7A32E2F24c853AE6567b75D862Ee2B46f",
"mockWeb3Network": "bsc,ethereum",
"expectedCredentialValue": true
}
]
6 changes: 6 additions & 0 deletions tee-worker/ts-tests/integration-tests/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { KeyringPair } from '@polkadot/keyring/types';
import type { HexString } from '@polkadot/util/types';
import './config';
import { IntegrationTestContext, JsonRpcRequest } from './common-types';
import { randomBytes } from 'crypto';

// format and setup
const keyring = new Keyring({ type: 'sr25519' });
Expand Down Expand Up @@ -71,3 +72,8 @@ export function nextRequestId(context: IntegrationTestContext): number {
context.requestId = nextId;
return nextId;
}

export function randomSubstrateWallet(): KeyringPair {
const keyring = new Keyring({ type: 'sr25519' });
return keyring.addFromSeed(randomBytes(32));
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export async function assertVc(context: IntegrationTestContext, subject: CorePri
// step 4
// extrac proof and vc without proof json
const vcPayloadJson = JSON.parse(decryptVcPayload);

const { proof, ...vcWithoutProof } = vcPayloadJson;

// step 5
Expand Down
134 changes: 134 additions & 0 deletions tee-worker/ts-tests/integration-tests/data-providers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { randomBytes, KeyObject } from 'crypto';
import { step } from 'mocha-steps';
import { assert } from 'chai';
import { buildIdentityFromKeypair, decryptWithAes, initIntegrationTestContext, PolkadotSigner } from './common/utils';
import { randomSubstrateWallet } from './common/helpers';
import { assertIsInSidechainBlock } from './common/utils/assertion';
import {
getSidechainNonce,
getTeeShieldingKey,
sendRequestFromTrustedCall,
createSignedTrustedCallRequestVc,
} from './common/di-utils'; // @fixme move to a better place
import type { IntegrationTestContext } from './common/common-types';
import { CorePrimitivesIdentity, RequestVCResult } from 'parachain-api';
import { aesKey } from './common/call';
import { $ as zx } from 'zx';
import { subscribeToEventsWithExtHash } from './common/transactions';
import { KeyringPair } from '@polkadot/keyring/types';
import { u8aToHex } from '@polkadot/util';
import { vip3CredentialJson, CredentialDefinition } from './common/credential-json';
describe('Test Vc (direct invocation)', function () {
let context: IntegrationTestContext = undefined as any;
let teeShieldingKey: KeyObject = undefined as any;
const substrateIdentities: CorePrimitivesIdentity[] = [];

const clientDir = process.env.LITENTRY_CLI_DIR;
const reqExtHash = '0x0000000000000000000000000000000000000000000000000000000000000000';
const keyringPairs: KeyringPair[] = [];
let argvId = '';
const credentialsJson: CredentialDefinition[] = [...vip3CredentialJson];

this.timeout(6000000);
before(async () => {
context = await initIntegrationTestContext(process.env.WORKER_ENDPOINT!, process.env.NODE_ENDPOINT!);
teeShieldingKey = await getTeeShieldingKey(context);
});

// usage example:
// `pnpm run test-data-providers:local --id=vip3-membership-card-gold` for single test
// `pnpm run test-data-providers:local` for all tests
const argv = process.argv.indexOf('--id');
argvId = process.argv[argv + 1];
const {
protocol: workerProtocal,
hostname: workerHostname,
port: workerPort,
} = new URL(process.env.WORKER_ENDPOINT!);
const { protocol: nodeProtocal, hostname: nodeHostname, port: nodePort } = new URL(process.env.NODE_ENDPOINT!);

async function linkIdentityViaCli(id: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to utilise this function for other VC scenarios, like NFT holder or BNB domain?

const credentialDefinitions = credentialsJson.find((item) => item.id === id) as CredentialDefinition;

const keyringPair = randomSubstrateWallet();
keyringPairs.push(keyringPair);
const formatAddress = u8aToHex(keyringPair.publicKey);

const substrateIdentity = await buildIdentityFromKeypair(new PolkadotSigner(keyringPair), context);
substrateIdentities.push(substrateIdentity);
const eventsPromise = subscribeToEventsWithExtHash(reqExtHash, context);
try {
// CLIENT = "$CLIENT_BIN -p $NPORT -P $WORKER1PORT -u $NODEURL -U $WORKER1URL"
const commandPromise = zx`${clientDir} -p ${nodePort} -P ${workerPort} -u ${
nodeProtocal + nodeHostname
} -U ${workerProtocal + workerHostname}\
trusted -d link-identity did:litentry:substrate:${formatAddress}\
did:${credentialDefinitions.mockDid}\
${credentialDefinitions.mockWeb3Network}`;

await commandPromise;
} catch (error: any) {
console.log(`Exit code: ${error.exitCode}`);
console.log(`Error: ${error.stderr}`);
throw error;
}

const events = (await eventsPromise).map(({ event }) => event);
assert.equal(events.length, 1);
}

async function requestVc(id: string, index: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to utilise this function for other scenarios, like NFT holder or BNB domain?

Copy link
Contributor Author

@0xverin 0xverin Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're referring to other cases, right? Currently, this pr is for VIP3, but I am testing the other cases. To avoid ambiguity, I am redefining credentials array here.

const credentialDefinitions = credentialsJson.find((item) => item.id === id) as CredentialDefinition;

const assertion = {
[credentialDefinitions.assertion.id]: credentialDefinitions.assertion.payload,
};

let currentNonce = (await getSidechainNonce(context, teeShieldingKey, substrateIdentities[index])).toNumber();
const getNextNonce = () => currentNonce++;
const nonce = getNextNonce();
console.log(nonce, substrateIdentities[index].toHuman(), u8aToHex(keyringPairs[index].publicKey));

const requestIdentifier = `0x${randomBytes(32).toString('hex')}`;
const requestVcCall = await createSignedTrustedCallRequestVc(
context.api,
context.mrEnclave,
context.api.createType('Index', nonce),
new PolkadotSigner(keyringPairs[index]),
substrateIdentities[index],
context.api.createType('Assertion', assertion).toHex(),
context.api.createType('Option<RequestAesKey>', aesKey).toHex(),
requestIdentifier
);
const res = await sendRequestFromTrustedCall(context, teeShieldingKey, requestVcCall);
await assertIsInSidechainBlock(`${Object.keys(assertion)[0]} requestVcCall`, res);

const vcResults = context.api.createType('RequestVCResult', res.value) as unknown as RequestVCResult;
const decryptVcPayload = decryptWithAes(aesKey, vcResults.vc_payload, 'utf-8').replace('0x', '');
const vcPayloadJson = JSON.parse(decryptVcPayload);

assert.equal(vcPayloadJson.credentialSubject.values[0], credentialDefinitions.expectedCredentialValue);
}

if (argvId && credentialsJson.find((item) => item.id === argvId)) {
const credentialDefinitions = credentialsJson.find((item) => item.id === argvId) as CredentialDefinition;

step(
`linking identity::${credentialDefinitions.mockDid} via cli and request vc::${credentialDefinitions.mockDid}`,
async function () {
await linkIdentityViaCli(argvId);
await requestVc(argvId, 0);
}
);
} else {
credentialsJson.forEach(({ id }, index) => {
step(
`linking identity::${credentialsJson[index].mockDid} via cli and request vc::${credentialsJson[index].id}`,
async function () {
await linkIdentityViaCli(id);
await requestVc(id, index);
}
);
});
}
});
7 changes: 5 additions & 2 deletions tee-worker/ts-tests/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"test-ii-vc:local": "pnpm exec eslint . && pnpm exec cross-env NODE_ENV=local mocha --exit --sort -r ts-node/register --loader=ts-node/esm 'ii_vc.test.ts'",
"test-ii-vc:staging": "pnpm exec eslint . && pnpm exec cross-env NODE_ENV=staging mocha --exit --sort -r ts-node/register --loader=ts-node/esm 'ii_vc.test.ts'",
"test-ii-batch:local": "pnpm exec eslint . && pnpm exec cross-env NODE_ENV=local mocha --exit --sort -r ts-node/register --loader=ts-node/esm 'ii_batch.test.ts'",
"test-ii-batch:staging": "pnpm exec eslint . && pnpm exec cross-env NODE_ENV=staging mocha --exit --sort -r ts-node/register --loader=ts-node/esm 'ii_batch.test.ts'"
"test-ii-batch:staging": "pnpm exec eslint . && pnpm exec cross-env NODE_ENV=staging mocha --exit --sort -r ts-node/register --loader=ts-node/esm 'ii_batch.test.ts'",
"test-data-providers:local": "pnpm exec eslint . && pnpm exec cross-env NODE_ENV=local mocha --exit --sort -r ts-node/register --loader=ts-node/esm 'data-providers.test.ts'",
"test-data-providers:staging": "pnpm exec eslint . && pnpm exec cross-env NODE_ENV=staging mocha --exit --sort -r ts-node/register --loader=ts-node/esm 'data-providers.test.ts'"
0xverin marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@noble/ed25519": "^1.7.3",
Expand Down Expand Up @@ -49,7 +51,8 @@
"scale-ts": "^0.2.11",
"sidechain-api": "file:../../client-api/sidechain-api",
"websocket-as-promised": "^2.0.1",
"ws": "^8.8.1"
"ws": "^8.8.1",
"zx": "^7.2.3"
},
"devDependencies": {
"@ethersproject/providers": "^5.7.2",
Expand Down
Loading