Skip to content

Commit

Permalink
random substrate wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
0xverin committed Jan 26, 2024
1 parent 79feee1 commit 4a945ee
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as dataProviders from '../config/data-providers';
import { AccountNetworks, Networks } from '../config/networks';
import { CredentialDefinition } from '../config/types';

const mockAddress: HexString = '0x651614cA9097C5ba189Ef85e7851Ef9cff592B2c';
const mockAddress: HexString = '0x10CdF7F7A32E2F24c853AE6567b75D862Ee2B46f';
export const vip3MembershipCardSilver: CredentialDefinition = {
id: 'vip3-membership-card-silver',
name: 'VIP3 Membership Card Sliver',
Expand Down
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));
}
123 changes: 70 additions & 53 deletions tee-worker/ts-tests/integration-tests/data-providers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { randomBytes, KeyObject } from 'crypto';
import { step } from 'mocha-steps';
import { assert } from 'chai';
import { buildIdentityFromKeypair, decryptWithAes, initIntegrationTestContext, PolkadotSigner } from './common/utils';
import {
buildIdentityFromKeypair,
decryptWithAes,
initIntegrationTestContext,
PolkadotSigner,
sleep,
} from './common/utils';
import { randomSubstrateWallet } from './common/helpers';
import { assertIsInSidechainBlock } from './common/utils/assertion';
import {
getSidechainNonce,
Expand All @@ -15,38 +22,44 @@ import { aesKey } from './common/call';
import { $ as zx } from 'zx';
import { credentialDefinitionMap } from './common/credential-definitions';
import { subscribeToEventsWithExtHash } from './common/transactions';
import { KeyringPair } from '@polkadot/keyring/types';
import { u8aToHex } from '@polkadot/util';

describe('Test Vc (direct invocation)', function () {
let context: IntegrationTestContext = undefined as any;
let teeShieldingKey: KeyObject = undefined as any;
let aliceSubstrateIdentity: CorePrimitivesIdentity = undefined as any;

// CLIENT="${CLIENT_BIN} -p ${LITENTRY_RPC_PORT} -P ${WORKER_1_PORT} -u ${LITENTRY_RPC_URL} -U ${WORKER_1_URL}"
const substrateIdentities: CorePrimitivesIdentity[] = [];

const client = process.env.BINARY_DIR + '/litentry-cli';

const aliceAddressFormat = '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d';
const reqExtHash = '0x0000000000000000000000000000000000000000000000000000000000000000';
const keyringPairs: KeyringPair[] = [];
let argvId = '';
this.timeout(6000000);
before(async () => {
context = await initIntegrationTestContext(process.env.WORKER_ENDPOINT!, process.env.NODE_ENDPOINT!);
teeShieldingKey = await getTeeShieldingKey(context);
aliceSubstrateIdentity = await buildIdentityFromKeypair(
new PolkadotSigner(context.substrateWallet.alice),
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
// `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];

async function linkIdentityViaCli(id: string) {
const keyringPair = randomSubstrateWallet();
keyringPairs.push(keyringPair);
const formatAddress = u8aToHex(keyringPair.publicKey);

const substrateIdentity = (await buildIdentityFromKeypair(
new PolkadotSigner(keyringPair),
context
)) as CorePrimitivesIdentity;
substrateIdentities.push(substrateIdentity);
const eventsPromise = subscribeToEventsWithExtHash(reqExtHash, context);
try {
await zx`${client} trusted -d link-identity did:litentry:substrate:${aliceAddressFormat}\
await zx`${client} trusted -d link-identity did:litentry:substrate:${formatAddress}\
did:${credentialDefinitionMap[id].mockDid}\
${credentialDefinitionMap[id].mockWeb3Network}`;
} catch (error: any) {
Expand All @@ -59,51 +72,55 @@ describe('Test Vc (direct invocation)', function () {
assert.equal(events.length, 1);
}

// eslint-disable-next-line no-prototype-builtins
if (argvId && credentialDefinitionMap.hasOwnProperty(argvId)) {
step(`linking identity-${credentialDefinitionMap[argvId].mockDid} via cli`, async function () {
await linkIdentityViaCli(argvId);
});
} else {
Object.keys(credentialDefinitionMap).forEach((id) => {
step(`linking identity-${credentialDefinitionMap[id].mockAddress} via cli`, async function () {
await linkIdentityViaCli(id);
});
});
}
async function requestVc(id: string, index: number) {
const assertion = {
[credentialDefinitionMap[id].assertion.id]: credentialDefinitionMap[id].assertion.payload,
};

Object.keys(credentialDefinitionMap).forEach((id) => {
step(`request vc for ${credentialDefinitionMap[id]}`, async function () {
const assertion = {
[credentialDefinitionMap[id].assertion.id]: credentialDefinitionMap[id].assertion.payload,
};
console.log(assertion);
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));

let currentNonce = (await getSidechainNonce(context, teeShieldingKey, aliceSubstrateIdentity)).toNumber();
const getNextNonce = () => currentNonce++;
const nonce = getNextNonce();
const requestIdentifier = `0x${randomBytes(32).toString('hex')}`;
const requestVcCall = await createSignedTrustedCallRequestVc(
context.api,
context.mrEnclave,
context.api.createType('Index', nonce),
new PolkadotSigner(context.substrateWallet.alice),
aliceSubstrateIdentity,
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 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);
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],
credentialDefinitionMap[id].expectedCredentialValue
assert.equal(vcPayloadJson.credentialSubject.values[0], credentialDefinitionMap[id].expectedCredentialValue);
}

// eslint-disable-next-line no-prototype-builtins
if (argvId && credentialDefinitionMap.hasOwnProperty(argvId)) {
step(
`linking identity::${credentialDefinitionMap[argvId].mockDid} via cli and request vc::${credentialDefinitionMap[argvId].id}`,
async function () {
await linkIdentityViaCli(argvId);
await requestVc(argvId, 0);
}
);
} else {
Object.keys(credentialDefinitionMap).forEach((id, index) => {
step(
`linking identity::${credentialDefinitionMap[id].mockDid} via cli and request vc::${credentialDefinitionMap[id].id}`,
async function () {
await linkIdentityViaCli(id);
await requestVc(id, index);
}
);
});
});
}
});

0 comments on commit 4a945ee

Please sign in to comment.