Skip to content

Commit

Permalink
P-590 Refactor web3 wallets generation logic (#2579)
Browse files Browse the repository at this point in the history
* todo list

* refactor create wallets

* genesis substrate wallet

* remove getSubstrateSigner

* Modify id_substrate_identity test

* modify di_bitcoin_identity test

* modify di_evm_identity tests

* modify di&dr vc tests

* fmt

* fmr and clean up

* fix ci failures
  • Loading branch information
0xverin committed Mar 16, 2024
1 parent 276564a commit 9536b9c
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 555 deletions.
19 changes: 9 additions & 10 deletions tee-worker/ts-tests/integration-tests/common/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ import { Wallet } from 'ethers';
import type { KeyringPair } from '@polkadot/keyring/types';
import type { HexString } from '@polkadot/util/types';
import { ECPairInterface } from 'ecpair';
import { Signer } from './utils/crypto';
// If there are types already defined in the client-api, please avoid redefining these types.
// Instead, make every effort to use the types that have been generated within the client-api.
interface EthersWalletItem {
[key: string]: Wallet;
}
interface SubstrateWalletItem {
[key: string]: KeyringPair;

interface WalletType {
[walletName: string]: Signer;
}
interface BitcoinWalletItem {
[key: string]: ECPairInterface;
export interface Wallets {
evm: WalletType;
substrate: WalletType;
bitcoin: WalletType;
}
export type IntegrationTestContext = {
tee: WebSocketAsPromised;
api: ApiPromise;
teeShieldingKey: KeyObject;
mrEnclave: HexString;
ethersWallet: EthersWalletItem;
substrateWallet: SubstrateWalletItem;
bitcoinWallet: BitcoinWalletItem;
web3Wallets: Wallets;
sidechainMetaData: Metadata;
sidechainRegistry: TypeRegistry;
chainIdentifier: number;
Expand Down
67 changes: 28 additions & 39 deletions tee-worker/ts-tests/integration-tests/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,9 @@ import { IntegrationTestContext, JsonRpcRequest } from './common-types';
import { randomBytes } from 'crypto';
import { ECPairFactory, ECPairInterface } from 'ecpair';
import * as ecc from 'tiny-secp256k1';
// format and setup
const keyring = new Keyring({ type: 'sr25519' });
export function getSubstrateSigner(): {
alice: KeyringPair;
bob: KeyringPair;
charlie: KeyringPair;
eve: KeyringPair;
} {
const alice = keyring.addFromUri('//Alice', { name: 'Alice' });
const bob = keyring.addFromUri('//Bob', { name: 'Bob' });
const charlie = keyring.addFromUri('//Charlie', { name: 'Charlie' });
const eve = keyring.addFromUri('//Eve', { name: 'Eve' });
const signers = {
alice,
bob,
charlie,
eve,
};
return signers;
}
export function getEvmSigner(): {
alice: string;
bob: string;
charlie: string;
dave: string;
eve: string;
} {
const secp256k1PrivateKeyLength = 32;
const names = ['alice', 'bob', 'charlie', 'dave', 'eve'];
const keys = new Array<string>();
for (const name of names) {
const result = Buffer.alloc(secp256k1PrivateKeyLength);
result.fill(name, secp256k1PrivateKeyLength - Buffer.from(name, 'utf8').length);

keys.push(result.toString('hex'));
}
return { alice: keys[0], bob: keys[1], charlie: keys[2], dave: keys[3], eve: keys[4] };
}

import { ethers, Wallet } from 'ethers';
import { EthersSigner, PolkadotSigner, BitcoinSigner } from './utils/crypto';
import { Wallets } from './common-types';
export function blake2128Concat(data: HexString | Uint8Array): Uint8Array {
return u8aConcat(blake2AsU8a(data, 128), u8aToU8a(data));
}
Expand Down Expand Up @@ -74,6 +38,9 @@ export function nextRequestId(context: IntegrationTestContext): number {
return nextId;
}

export function randomEvmWallet(): Wallet {
return ethers.Wallet.createRandom();
}
export function randomSubstrateWallet(): KeyringPair {
const keyring = new Keyring({ type: 'sr25519' });
return keyring.addFromSeed(randomBytes(32));
Expand All @@ -84,3 +51,25 @@ export function randomBitcoinWallet(): ECPairInterface {
const keyPair = ecPair.makeRandom();
return keyPair;
}

export function genesisSubstrateWallet(name: string): KeyringPair {
const keyring = new Keyring({ type: 'sr25519' });
const keyPair = keyring.addFromUri(`//${name}`, { name });
return keyPair;
}

export const createWeb3Wallets = (): Wallets => {
const wallets: Wallets = {
evm: {},
substrate: {},
bitcoin: {},
};
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());
}

return wallets;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Event } from '@polkadot/types/interfaces';
import { hexToU8a, u8aToHex } from '@polkadot/util';
import { assert } from 'chai';
import * as ed from '@noble/ed25519';
Expand All @@ -11,7 +10,7 @@ import { aesKey } from '../call';
import colors from 'colors';
import { WorkerRpcReturnValue, StfError } from 'parachain-api';
import { Bytes } from '@polkadot/types-codec';
import { Signer, decryptWithAes } from './crypto';
import { decryptWithAes } from './crypto';
import { blake2AsHex } from '@polkadot/util-crypto';
import { validateVcSchema } from '@litentry/vc-schema-validator';
import { PalletIdentityManagementTeeIdentityContext } from 'sidechain-api';
Expand Down
44 changes: 4 additions & 40 deletions tee-worker/ts-tests/integration-tests/common/utils/context.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { WsProvider, ApiPromise } from 'parachain-api';
import { Keyring } from '@polkadot/api';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { hexToString } from '@polkadot/util';
import { ethers } from 'ethers';
import WebSocketAsPromised from 'websocket-as-promised';
import WebSocket from 'ws';
import Options from 'websocket-as-promised/types/options';
import { KeyObject } from 'crypto';
import { getSidechainMetadata } from '../call';
import { getEvmSigner, getSubstrateSigner } from '../helpers';
import type { IntegrationTestContext, Web3Wallets } from '../common-types';
import { createWeb3Wallets } from '../helpers';
import type { IntegrationTestContext } from '../common-types';
import { identity, vc, trusted_operations, sidechain } from 'parachain-api';
import crypto from 'crypto';
import type { HexString } from '@polkadot/util/types';
import { randomBitcoinWallet } from '../helpers';
// maximum block number that we wait in listening events before we timeout
export const defaultListenTimeoutInBlockNumber = 15;

Expand All @@ -37,20 +34,7 @@ export async function initIntegrationTestContext(
const provider = new WsProvider(substrateEndpoint);
await cryptoWaitReady();

const ethersWallet = {
alice: new ethers.Wallet(getEvmSigner().alice),
bob: new ethers.Wallet(getEvmSigner().bob),
charlie: new ethers.Wallet(getEvmSigner().charlie),
dave: new ethers.Wallet(getEvmSigner().dave),
eve: new ethers.Wallet(getEvmSigner().eve),
};

const substrateWallet = getSubstrateSigner();

const bitcoinWallet = {
alice: randomBitcoinWallet(),
bob: randomBitcoinWallet(),
};
const web3Wallets = createWeb3Wallets();

const types = { ...identity.types, ...vc.types, ...trusted_operations.types, ...sidechain.types };

Expand All @@ -71,9 +55,7 @@ export async function initIntegrationTestContext(
api,
teeShieldingKey,
mrEnclave,
ethersWallet,
substrateWallet,
bitcoinWallet,
web3Wallets,
sidechainMetaData,
sidechainRegistry,
chainIdentifier,
Expand Down Expand Up @@ -108,21 +90,3 @@ export async function getEnclave(api: ApiPromise): Promise<{
teeShieldingKey,
};
}

export async function generateWeb3Wallets(count: number): Promise<Web3Wallets[]> {
const seed = 'litentry seed';
const addresses: Web3Wallets[] = [];
const keyring = new Keyring({ type: 'sr25519' });

for (let i = 0; i < count; i++) {
const substratePair = keyring.addFromUri(`${seed}//${i}`);
const evmWallet = ethers.Wallet.createRandom();
const bitcoinPair = randomBitcoinWallet();
addresses.push({
substrateWallet: substratePair,
evmWallet: evmWallet,
bitcoinWallet: bitcoinPair,
});
}
return addresses;
}

0 comments on commit 9536b9c

Please sign in to comment.