Skip to content

Commit

Permalink
identity-linking using di call
Browse files Browse the repository at this point in the history
  • Loading branch information
0xverin committed Jul 4, 2024
1 parent fc8a2a2 commit bfca5e3
Showing 1 changed file with 58 additions and 34 deletions.
92 changes: 58 additions & 34 deletions tee-worker/ts-tests/integration-tests/assertion_contracts.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import { randomBytes, KeyObject } from 'crypto';
import { step } from 'mocha-steps';
import { initIntegrationTestContext } from './common/utils';
import { buildValidations, initIntegrationTestContext } from './common/utils';
import { assertIsInSidechainBlock, assertVc } from './common/utils/assertion';
import {
getSidechainNonce,
getTeeShieldingKey,
sendRequestFromTrustedCall,
createSignedTrustedCallRequestVc,
createSignedTrustedCallLinkIdentity,
} from './common/di-utils'; // @fixme move to a better place
import type { IntegrationTestContext } from './common/common-types';
import { aesKey } from './common/call';
import type { CorePrimitivesIdentity } from 'parachain-api';
import type { CorePrimitivesIdentity, LitentryValidationData, Web3Network } from 'parachain-api';
import fs from 'fs';
import path from 'path';
import { assert } from 'chai';
import { genesisSubstrateWallet } from './common/helpers';
import { KeyringPair } from '@polkadot/keyring/types';
import { subscribeToEvents, subscribeToEventsWithExtHash } from './common/transactions';
import { subscribeToEvents } from './common/transactions';
import { encryptWithTeeShieldingKey } from './common/utils/crypto';
import { ethers } from 'ethers';
import { sleep } from './common/utils';
import { $ as zx } from 'zx';
import { Bytes, Vec } from '@polkadot/types-codec';

describe('Test Vc (direct request)', function () {
let context: IntegrationTestContext = undefined as any;
Expand All @@ -29,8 +31,12 @@ describe('Test Vc (direct request)', function () {

let alice: KeyringPair = undefined as any;
let contractBytecode = undefined as any;
const clientDir = process.env.LITENTRY_CLI_DIR;

const linkIdentityRequestParams: {
nonce: number;
identity: CorePrimitivesIdentity;
validation: LitentryValidationData;
networks: Bytes | Vec<Web3Network>;
}[] = [];
this.timeout(6000000);

before(async () => {
Expand All @@ -57,7 +63,7 @@ describe('Test Vc (direct request)', function () {

const secret = '0x' + encryptedSecrets.toString('hex');

const assertionId = '0x0000000000000000000000000000000000000000';
const assertionId = '0x0000000000000000000000000000000000000003';
const createAssertionEventsPromise = subscribeToEvents('evmAssertions', 'AssertionCreated', context.api);

const proposal = context.api.tx.evmAssertions.createAssertion(assertionId, contractBytecode, [secret]);
Expand All @@ -67,34 +73,52 @@ describe('Test Vc (direct request)', function () {
assert.equal(event.length, 1);
});

step('linking identities (alice) via cli', async function () {
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!);
const reqExtHash = '0x0000000000000000000000000000000000000000000000000000000000000000';
const eventsPromise = subscribeToEventsWithExtHash(reqExtHash, context);

// alice linking a evm identity
try {
const commandPromise = zx`${clientDir} -p ${nodePort} -P ${workerPort} -u ${
nodeProtocal + nodeHostname
} -U ${workerProtocal + workerHostname}\
trusted -d link-identity did:litentry:substrate:0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d\
did:litentry:evm:0x4B04b9166f471a72e067d68560a141a1d02332Ef\
bsc`;

await commandPromise;
} catch (error: any) {
console.log(`Exit code: ${error.exitCode}`);
console.log(`Error: ${error.stderr}`);
throw error;
}
step('linking identities (alice)', async function () {
let currentNonce = (await getSidechainNonce(context, aliceSubstrateIdentity)).toNumber();
const getNextNonce = () => currentNonce++;
const evmNonce = getNextNonce();

const events = (await eventsPromise).map(({ event }) => event);
assert.equal(events.length, 1);
const evmIdentity = await context.web3Wallets.evm.Alice.getIdentity(context);
const evmValidation = await buildValidations(
context,
aliceSubstrateIdentity,
evmIdentity,
evmNonce,
'ethereum',
context.web3Wallets.evm.Alice
);
const evmNetworks = context.api.createType('Vec<Web3Network>', ['Ethereum', 'Bsc']);
linkIdentityRequestParams.push({
nonce: evmNonce,
identity: evmIdentity,
validation: evmValidation,
networks: evmNetworks,
});

let counter = 0;
for (const { nonce, identity, validation, networks } of linkIdentityRequestParams) {
counter++;
const requestIdentifier = `0x${randomBytes(32).toString('hex')}`;
const linkIdentityCall = await createSignedTrustedCallLinkIdentity(
context.api,
context.mrEnclave,
context.api.createType('Index', nonce),
context.web3Wallets.substrate.Alice,
aliceSubstrateIdentity,
identity.toHex(),
validation.toHex(),
networks.toHex(),
context.api.createType('Option<RequestAesKey>', aesKey).toHex(),
requestIdentifier,
{
withWrappedBytes: false,
withPrefix: counter % 2 === 0, // alternate per entry
}
);

const res = await sendRequestFromTrustedCall(context, teeShieldingKey, linkIdentityCall);
await assertIsInSidechainBlock('linkIdentityCall', res);
}
});

step('requesting VC for deployed contract', async function () {
Expand All @@ -106,7 +130,7 @@ describe('Test Vc (direct request)', function () {
const encodedData = abiCoder.encode(['string'], ['bnb']);

const assertion = {
dynamic: [Uint8Array.from(Buffer.from('0000000000000000000000000000000000000000', 'hex')), encodedData],
dynamic: [Uint8Array.from(Buffer.from('0000000000000000000000000000000000000003', 'hex')), encodedData],
};

const requestVcCall = await createSignedTrustedCallRequestVc(
Expand Down

0 comments on commit bfca5e3

Please sign in to comment.