Skip to content

Commit

Permalink
update utils
Browse files Browse the repository at this point in the history
  • Loading branch information
0xverin committed Oct 21, 2024
1 parent 219b2e9 commit 32618e6
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 385 deletions.
1 change: 1 addition & 0 deletions vc-di-tests/integration-tests/.env.local
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NODE_ENV = local
WORKER_ENDPOINT = ws://127.0.0.1:2000
PARACHAIN_ENDPOINT = ws://127.0.0.1:9944
LITENTRY_CLI_DIR=/opt/litentry/litentry-cli
1 change: 1 addition & 0 deletions vc-di-tests/integration-tests/assertion_contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Test Vc (direct request)', function () {
}

context = await initIntegrationTestContext(parachainEndpoint)
console.log('context', context)

teeShieldingKey = await getTeeShieldingKey(context)
aliceSubstrateIdentity =
Expand Down
7 changes: 4 additions & 3 deletions vc-di-tests/integration-tests/common/di-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export const createSignedTrustedGetter = async (
})
const payload = blake2AsU8a(getter.toU8a(), 256)

let signature = await createLitentryMultiSignature(parachainApi, {
const signature = await createLitentryMultiSignature(parachainApi, {
signer,
payload,
})
Expand Down Expand Up @@ -467,7 +467,6 @@ export const sendRequestFromTrustedCall = async (
context: IntegrationTestContext,
teeShieldingKey: KeyObject,
call: TrustedCallSigned,
isVcDirect = false,
onMessageReceived?: (res: WorkerRpcReturnValue) => void
) => {
// construct trusted operation
Expand All @@ -486,8 +485,10 @@ export const sendRequestFromTrustedCall = async (
hexToU8a(aesKey),
trustedOperation.toU8a()
)

const isRequestVc = call.call.isRequestVc || call.call.isRequestBatchVc
const request = createJsonRpcRequest(
isVcDirect ? 'author_requestVc' : 'author_submitAndWatchAesRequest',
isRequestVc ? 'author_requestVc' : 'author_submitAndWatchAesRequest',
[u8aToHex(requestParam)],
nextRequestId(context)
)
Expand Down
34 changes: 26 additions & 8 deletions vc-di-tests/integration-tests/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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 { createHash, randomBytes } from 'crypto'
import { createHash, randomBytes, type KeyObject } from 'crypto'
import { ECPairFactory, ECPairInterface } from 'ecpair'
import * as ecc from 'tiny-secp256k1'
import { ethers, Wallet } from 'ethers'
Expand All @@ -15,10 +15,10 @@ import {
PolkadotSigner,
BitcoinSigner,
SolanaSigner,
Signer,
} from './utils/crypto'
import { Wallets } from './common-types'
import type { ErrorDetail, StfError } from '@litentry/parachain-api'

export function blake2128Concat(data: HexString | Uint8Array): Uint8Array {
return u8aConcat(blake2AsU8a(data, 128), u8aToU8a(data))
}
Expand Down Expand Up @@ -77,6 +77,24 @@ export function genesisSolanaWallet(name: string): Keypair {
return keyPair
}

export const createWeb3Wallet = (
walletType: string,
walletName: string
): Signer => {
switch (walletType) {
case 'evm':
return new EthersSigner(randomEvmWallet())
case 'substrate':
return new PolkadotSigner(genesisSubstrateWallet(walletName))
case 'bitcoin':
return new BitcoinSigner(randomBitcoinWallet())
case 'solana':
return new SolanaSigner(genesisSolanaWallet(walletName))
default:
throw new Error(`Unsupported wallet type: ${walletType}`)
}
}

export const createWeb3Wallets = (): Wallets => {
const wallets: Wallets = {
evm: {},
Expand All @@ -86,12 +104,12 @@ export const createWeb3Wallets = (): Wallets => {
}
const walletNames = ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve']
for (const name of walletNames) {
wallets.evm[name] = new EthersSigner(randomEvmWallet())
wallets.substrate[name] = new PolkadotSigner(
genesisSubstrateWallet(name)
)
wallets.bitcoin[name] = new BitcoinSigner(randomBitcoinWallet())
wallets.solana[name] = new SolanaSigner(genesisSolanaWallet(name))
for (const walletType in wallets) {
;(wallets as any)[walletType][name] = createWeb3Wallet(
walletType,
name
)
}
}

return wallets
Expand Down
2 changes: 1 addition & 1 deletion vc-di-tests/integration-tests/common/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export function signAndSend(
}

// After removing the sudo module, we use `EnsureRootOrHalfCouncil` instead of `Sudo`,
// and there are only two council members in litmus-dev/rococo-dev/litentry-dev.
// and there are only two council members in rococo-dev/litentry-dev.
// So only `propose` is required, no vote.
//
// TODO: support to send the `vote extrinsic`, if the number of council members is greater than 2.
Expand Down
35 changes: 29 additions & 6 deletions vc-di-tests/integration-tests/common/utils/assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import { CorePrimitivesIdentity } from '@litentry/parachain-api'
import type { IntegrationTestContext } from '../common-types'
import { getIdGraphHash } from '../di-utils'
import type { HexString } from '@polkadot/util/types'
import { aesKey } from '../call'
import { nextRequestId } from '../helpers'
import { aesKey, sendRequest } from '../call'
import colors from 'colors'
import { WorkerRpcReturnValue, StfError } from '@litentry/parachain-api'
import { Bytes } from '@polkadot/types-codec'
import { decryptWithAes } from './crypto'
import { blake2AsHex } from '@polkadot/util-crypto'
import { base58Encode, blake2AsHex } from '@polkadot/util-crypto'
import { validateVcSchema } from '@litentry/vc-schema-validator'
import { PalletIdentityManagementTeeIdentityContext } from '@litentry/sidechain-api'
import { KeyObject } from 'crypto'
import * as base58 from 'micro-base58'
import { fail } from 'assert'

export function assertIdGraph(
actual: [
Expand Down Expand Up @@ -175,7 +174,7 @@ export async function assertVc(

assert.equal(
vcPayloadJson.issuer.mrenclave,
base58.encode(registeredEnclave.mrenclave),
base58Encode(registeredEnclave.mrenclave),
"Check VC mrenclave: it should equal enclave's mrenclave from parachains enclave registry"
)

Expand All @@ -187,9 +186,33 @@ export async function assertVc(

// step 7
// check runtime version is present
const [parachainSpecVersion, sidechainSpecVersion] = await Promise.all([
context.api.rpc.state.getRuntimeVersion(),
sendRequest(
context.tee,
{
jsonrpc: '2.0',
id: nextRequestId(context),
method: 'state_getRuntimeVersion',
params: [],
},
context.api
),
]).then(([parachainRuntime, sidechainReturnValue]) => {
const sidechainRuntime = context.api.createType(
'RuntimeVersion',
sidechainReturnValue.value
)

return [
parachainRuntime.specVersion.toNumber(),
sidechainRuntime.specVersion.toNumber(),
]
})

assert.deepEqual(
vcPayloadJson.issuer.runtimeVersion,
{ parachain: 9191, sidechain: 109 },
{ parachain: parachainSpecVersion, sidechain: sidechainSpecVersion },
'Check VC runtime version: it should equal the current defined versions'
)

Expand Down
18 changes: 10 additions & 8 deletions vc-di-tests/integration-tests/common/utils/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import {
WsProvider,
ApiPromise,
PalletTeebagEnclave,
} from '@litentry/parachain-api'
import { cryptoWaitReady } from '@polkadot/util-crypto'
import { hexToString } from '@polkadot/util'
import WebSocketAsPromised from 'websocket-as-promised'
Expand All @@ -17,6 +12,9 @@ import {
vc,
trusted_operations,
sidechain,
WsProvider,
ApiPromise,
PalletTeebagEnclave,
} from '@litentry/parachain-api'
import crypto from 'crypto'
import type { HexString } from '@polkadot/util/types'
Expand Down Expand Up @@ -44,7 +42,6 @@ export async function initIntegrationTestContext(
parachainEndpoint: string,
enclaveEndpoint?: string
): Promise<IntegrationTestContext> {
console.log('parachainEndpoint', parachainEndpoint)
const provider = new WsProvider(parachainEndpoint)
await cryptoWaitReady()

Expand All @@ -68,17 +65,21 @@ export async function initIntegrationTestContext(
? enclaveEndpoint
: await getenclaveEndpoint(api)

console.log('workerEndpoint', workerEndpoint)

const wsp = await initWorkerConnection(workerEndpoint)
console.log('workerEndpoint', workerEndpoint)
console.log('wsp', wsp)
const requestId = 1

const { sidechainMetaData, sidechainRegistry } = await getSidechainMetadata(
wsp,
api,
requestId
)
console.log('sidechainMetaData', sidechainMetaData)
console.log('sidechainRegistry', sidechainRegistry)
const { mrEnclave, teeShieldingKey } = await getEnclave(api)
console.log('mrEnclave', mrEnclave)
console.log('teeShieldingKey', teeShieldingKey)
return {
tee: wsp,
api,
Expand All @@ -91,6 +92,7 @@ export async function initIntegrationTestContext(
requestId,
}
}

async function getenclaveEndpoint(api: ApiPromise): Promise<string> {
const registry = await api.query.teebag.enclaveRegistry.entries()
const identityEnclaves = registry.reduce((enclaves, [, enclave]) => {
Expand Down
7 changes: 3 additions & 4 deletions vc-di-tests/integration-tests/common/utils/identity-helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { u8aToHex } from '@polkadot/util'
import { blake2AsHex } from '@polkadot/util-crypto'
import type { IntegrationTestContext } from '../common-types'
import { AesOutput } from '@litentry/parachain-api'
import { decryptWithAes, Signer } from './crypto'
import { ethers } from 'ethers'
import type { TypeRegistry } from '@polkadot/types'
import type { PalletIdentityManagementTeeIdentityContext } from '@litentry/sidechain-api'
import type {
LitentryValidationData,
CorePrimitivesIdentity,
AesOutput,
} from '@litentry/parachain-api'
import type { HexString } from '@polkadot/util/types'

Expand Down Expand Up @@ -194,11 +194,10 @@ export async function buildValidations(
signerIdentitity: CorePrimitivesIdentity,
linkIdentity: CorePrimitivesIdentity,
startingSidechainNonce: number,
network: 'ethereum' | 'substrate' | 'bitcoin' | 'solana',
network: 'evm' | 'substrate' | 'bitcoin' | 'solana',
signer?: Signer,
options?: { prettifiedMessage?: boolean }
): Promise<LitentryValidationData> {
const _options = { prettifiedMessage: false, ...options }
const validationNonce = startingSidechainNonce++

const msg = generateVerificationMessage(
Expand All @@ -207,7 +206,7 @@ export async function buildValidations(
linkIdentity,
validationNonce
)
if (network === 'ethereum') {
if (network === 'evm') {
const evmValidationData = {
Web3Validation: {
Evm: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export function describeLitentry(
//env url

const tmp = await initIntegrationTestContext(
process.env.WORKER_ENDPOINT!,
process.env.NODE_ENDPOINT!
process.env.PARACHAIN_ENDPOINT!
)
context.mrEnclave = tmp.mrEnclave
context.api = tmp.api
Expand Down
Loading

0 comments on commit 32618e6

Please sign in to comment.