Skip to content

Commit

Permalink
Merge pull request #1044 from IntersectMBO/tests/drep-directory
Browse files Browse the repository at this point in the history
Tests/drep directory
  • Loading branch information
NabinKawan authored May 19, 2024
2 parents 899edbe + a877731 commit 9646306
Show file tree
Hide file tree
Showing 42 changed files with 648 additions and 427 deletions.
3 changes: 0 additions & 3 deletions tests/govtool-frontend/playwright/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ DOCS_URL=https://docs.sanchogov.tools
# 0 for testnet, 1 for mainnet
NETWORK_ID=0

# Create mock wallets if true
ONE_TIME_WALLET_SETUP=false

# Faucet
FAUCET_API_URL=https://faucet.sanchonet.world.dev.cardano.org
FAUCET_API_KEY=
Expand Down
3 changes: 3 additions & 0 deletions tests/govtool-frontend/playwright/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "es5"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const environments = {
apiUrl: `${process.env.HOST_URL}/api` || "http://localhost:8080/api",
docsUrl: process.env.DOCS_URL || "https://docs.sanchogov.tools",
networkId: parseInt(process.env.NETWORK_ID) || 0,
oneTimeWalletSetup: process.env.ONE_TIME_WALLET_SETUP === "true" || false,
faucet: {
apiUrl:
process.env.FAUCET_API_URL ||
Expand Down
30 changes: 15 additions & 15 deletions tests/govtool-frontend/playwright/lib/constants/staticWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ export const dRep01Wallet: StaticWallet = {
dRepId: "drep1g654cyehkfenyycl8sdemrnk38ka9avnulnfhawu7rp8skl824l",
};

// export const dRep02Wallet: StaticWallet = {
// payment: {
// private: "71120ea01dc0c367da113a7ee7b3744a46f793edb4f30a06b46d800324b2c999",
// public: "66724455eaacb6dea6686ba09bc159d5deef3d82ebf9c6a60d61748b59e32627",
// pkh: "363547ffb44d337f8055515e75e8af516e557b3270bfa4d9198e7195",
// },
// stake: {
// private: "4dfc89a9d680b237146dde69282c709e93ba91ac0b028e980bc40ec573c77f0f",
// public: "009c10056aff887d66135886d1fb9f046190bdf1d90a3f9cff954386f7cf37fb",
// pkh: "4d52d1d178157ab4c5ab6f8cb109ff91f750b367830463ef8344007e",
// },
// address:
// "addr_test1qqmr23llk3xnxluq24g4ua0g4agku4tmxfctlfxerx88r92d2tgaz7q4026vt2m03jcsnlu37agtxeurq337lq6yqplqftpnqu",
// dRepId: "drep1f4fdr5tcz4atf3dtd7xtzz0lj8m4pvm8svzx8murgsq8u6dkmf4",
// };
export const dRep02Wallet: StaticWallet = {
payment: {
private: "71120ea01dc0c367da113a7ee7b3744a46f793edb4f30a06b46d800324b2c999",
public: "66724455eaacb6dea6686ba09bc159d5deef3d82ebf9c6a60d61748b59e32627",
pkh: "363547ffb44d337f8055515e75e8af516e557b3270bfa4d9198e7195",
},
stake: {
private: "4dfc89a9d680b237146dde69282c709e93ba91ac0b028e980bc40ec573c77f0f",
public: "009c10056aff887d66135886d1fb9f046190bdf1d90a3f9cff954386f7cf37fb",
pkh: "4d52d1d178157ab4c5ab6f8cb109ff91f750b367830463ef8344007e",
},
address:
"addr_test1qqmr23llk3xnxluq24g4ua0g4agku4tmxfctlfxerx88r92d2tgaz7q4026vt2m03jcsnlu37agtxeurq337lq6yqplqftpnqu",
dRepId: "drep1f4fdr5tcz4atf3dtd7xtzz0lj8m4pvm8svzx8murgsq8u6dkmf4",
};

export const adaHolder01Wallet: StaticWallet = {
payment: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function createTempDRepAuth(page: Page, wallet: ShelleyWallet) {

export async function createTempAdaHolderAuth(
page: Page,
wallet: ShelleyWallet,
wallet: ShelleyWallet
) {
await importWallet(page, wallet.json());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Page } from "@playwright/test";

export default async function createWallet(
page: Page,
config?: CardanoTestWalletConfig,
config?: CardanoTestWalletConfig
) {
const wallet = (await ShelleyWallet.generate()).json();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StaticWallet } from "@types";

export async function importWallet(
page: Page,
wallet: StaticWallet | CardanoTestWallet,
wallet: StaticWallet | CardanoTestWallet
) {
await page.addInitScript((wallet) => {
// @ts-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import path = require("path");

export default async function loadDemosExtension(
page: Page,
enableStakeSigning = false,
enableStakeSigning = false
) {
const demosBundleScriptPath = path.resolve(
__dirname,
"../../node_modules/@cardanoapi/cardano-test-wallet/script.js",
"../../node_modules/@cardanoapi/cardano-test-wallet/script.js"
);
let walletConfig: CardanoTestWalletConfig = {
enableStakeSigning,
Expand Down
28 changes: 14 additions & 14 deletions tests/govtool-frontend/playwright/lib/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Ed25519Key {
}
public static async fromPrivateKeyHex(privKey) {
return await Ed25519Key.fromPrivateKey(
Uint8Array.from(Buffer.from(privKey, "hex")),
Uint8Array.from(Buffer.from(privKey, "hex"))
);
}

Expand Down Expand Up @@ -59,20 +59,20 @@ export class Ed25519Key {
public static fromJson(json: any): Ed25519Key {
if (!json || typeof json !== "object") {
throw new Error(
"Invalid JSON format for Ed25519Key: Input must be a non-null object.",
"Invalid JSON format for Ed25519Key: Input must be a non-null object."
);
}

if (!json.private || !json.public || !json.pkh) {
throw new Error(
"Invalid JSON format for Ed25519Key: Missing required fields (private, public, or pkh).",
"Invalid JSON format for Ed25519Key: Missing required fields (private, public, or pkh)."
);
}

return new Ed25519Key(
Uint8Array.from(Buffer.from(json.private, "hex")),
Uint8Array.from(Buffer.from(json.public, "hex")),
Uint8Array.from(Buffer.from(json.pkh, "hex")),
Uint8Array.from(Buffer.from(json.pkh, "hex"))
);
}
}
Expand All @@ -92,7 +92,7 @@ export class ShelleyWallet {
public static async generate() {
const wallet = new ShelleyWallet(
await Ed25519Key.generate(),
await Ed25519Key.generate(),
await Ed25519Key.generate()
);
return wallet;
}
Expand All @@ -102,7 +102,7 @@ export class ShelleyWallet {
return bech32.encode(
prefix,
bech32.toWords(Buffer.from(this.addressRawBytes(networkId))),
200,
200
);
}

Expand All @@ -127,7 +127,7 @@ export class ShelleyWallet {
return bech32.encode(
prefix,
bech32.toWords(Buffer.from(this.rewardAddressRawBytes(networkId))),
200,
200
);
}
public json() {
Expand All @@ -150,18 +150,18 @@ export class ShelleyWallet {

if (!paymentKey || typeof paymentKey !== "object") {
throw new Error(
"ShelleyWallet.fromJson : Invalid payment key: It must be an object.",
"ShelleyWallet.fromJson : Invalid payment key: It must be an object."
);
}

if (!stakeKey || typeof stakeKey !== "object") {
throw new Error(
"ShelleyWallet.fromJson : Invalid stake key: It must be an object.",
"ShelleyWallet.fromJson : Invalid stake key: It must be an object."
);
}
return new ShelleyWallet(
Ed25519Key.fromJson(paymentKey),
Ed25519Key.fromJson(stakeKey),
Ed25519Key.fromJson(stakeKey)
);
}

Expand Down Expand Up @@ -198,7 +198,7 @@ export class ShelleyWalletAddress implements Address {
private constructor(
network: number | "mainnet" | "testnet",
pkh: Uint8Array,
skh: Uint8Array,
skh: Uint8Array
) {
this.network =
network == "mainnet" ? 1 : network == "testnet" ? 0 : network;
Expand All @@ -215,7 +215,7 @@ export class ShelleyWalletAddress implements Address {
"ShelleyAddress.fromRawBytes: Invalid byte array length. expected: " +
ADDR_LENGTH +
" got: " +
bytea.length,
bytea.length
);
}
bytebuffer = Buffer.from(bytea);
Expand All @@ -227,15 +227,15 @@ export class ShelleyWalletAddress implements Address {
return new ShelleyWalletAddress(
bytebuffer.at(0),
paymentKeyHash,
stakeKeyHash,
stakeKeyHash
);
}
toBech32(): string {
const prefix = this.network == 0 ? "addr_test" : "addr";
return bech32.encode(
prefix,
bech32.toWords(Buffer.from(this.toRawBytes())),
200,
200
);
}
toRawBytes(): Uint8Array {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ShelleyWallet } from "./crypto";

export default async function generateShellyWallets(
numWallets: number = 100,
numWallets: number = 100
): Promise<ShelleyWallet[]> {
const wallets: ShelleyWallet[] = [];

Expand Down
2 changes: 1 addition & 1 deletion tests/govtool-frontend/playwright/lib/helpers/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface BrowserConfig {

export async function createNewPageWithWallet(
browser: Browser,
{ storageState, wallet, enableStakeSigning }: BrowserConfig,
{ storageState, wallet, enableStakeSigning }: BrowserConfig
): Promise<Page> {
const context = await browser.newContext({
storageState: storageState,
Expand Down
23 changes: 0 additions & 23 deletions tests/govtool-frontend/playwright/lib/helpers/setupWallets.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { bech32 } from "bech32";
import { blake2bHex } from "blakejs";
import convertBufferToHex from "./convertBufferToHex";
import { ShelleyWallet } from "./crypto";

export default function extractDRepFromWallet(wallet: ShelleyWallet) {
const stakePubKey = convertBufferToHex(wallet.stakeKey.public);

export default function extractDRepsFromStakePubKey(stakePubKey: string) {
const dRepKeyBytes = Buffer.from(stakePubKey, "hex");
const dRepId = blake2bHex(dRepKeyBytes, undefined, 28);
const words = bech32.toWords(Buffer.from(dRepId, "hex"));
const dRepIdBech32 = bech32.encode("drep", words);
return { dRepId, dRepIdBech32 };
return dRepIdBech32;
}
41 changes: 33 additions & 8 deletions tests/govtool-frontend/playwright/lib/helpers/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { Page, expect } from "@playwright/test";
import kuberService from "@services/kuberService";
import { LockInterceptor, LockInterceptorInfo } from "lib/lockInterceptor";
import { Logger } from "../../../cypress/lib/logger/logger";
import convertBufferToHex from "./convertBufferToHex";
import { ShelleyWallet } from "./crypto";

/**
* Polls the transaction status until it's resolved or times out.
* address is used to release lock of that address
*/
export async function pollTransaction(
txHash: string,
lockInfo?: LockInterceptorInfo,
lockInfo?: LockInterceptorInfo
) {
try {
Logger.info(`Waiting for tx completion: ${txHash}`);
Expand All @@ -23,7 +25,7 @@ export async function pollTransaction(
},
{
timeout: environments.txTimeOut,
},
}
)
.toBeGreaterThan(0);

Expand All @@ -34,7 +36,7 @@ export async function pollTransaction(
await LockInterceptor.releaseLockForAddress(
lockInfo.address,
lockInfo.lockId,
`Task completed for:${lockInfo.lockId}`,
`Task completed for:${lockInfo.lockId}`
);
} catch (err) {
if (lockInfo) {
Expand All @@ -43,7 +45,7 @@ export async function pollTransaction(
await LockInterceptor.releaseLockForAddress(
lockInfo.address,
lockInfo.lockId,
`Task failure: \n${JSON.stringify(errorMessage)}`,
`Task failure: \n${JSON.stringify(errorMessage)}`
);
}

Expand All @@ -53,7 +55,7 @@ export async function pollTransaction(

export async function waitForTxConfirmation(
page: Page,
triggerCallback?: () => Promise<void>,
triggerCallback?: () => Promise<void>
) {
let transactionHash: string | undefined;
const transactionStatusPromise = page.waitForRequest((request) => {
Expand All @@ -64,9 +66,9 @@ export async function waitForTxConfirmation(
await expect(
page
.getByTestId("alert-warning")
.getByText("Transaction in progress", { exact: false }),
.getByText("Transaction in progress", { exact: false })
).toBeVisible({
timeout: 10000,
timeout: 10_000,
});
const url = (await transactionStatusPromise).url();
const regex = /\/transaction\/status\/([^\/]+)$/;
Expand All @@ -77,6 +79,29 @@ export async function waitForTxConfirmation(

if (transactionHash) {
await pollTransaction(transactionHash);
await page.reload();
await expect(
page.getByText("In Progress", { exact: true }).first() //FIXME: Only one element needs to be displayed
).not.toBeVisible({ timeout: 20_000 });
}
}

export async function registerStakeForWallet(wallet: ShelleyWallet) {
const { txId, lockInfo } = await kuberService.registerStake(
convertBufferToHex(wallet.stakeKey.private),
convertBufferToHex(wallet.stakeKey.pkh),
convertBufferToHex(wallet.paymentKey.private),
wallet.addressBech32(environments.networkId)
);
await pollTransaction(txId, lockInfo);
}

export async function transferAdaForWallet(
wallet: ShelleyWallet,
amount?: number
) {
const { txId, lockInfo } = await kuberService.transferADA(
[wallet.addressBech32(environments.networkId)],
amount
);
await pollTransaction(txId, lockInfo);
}
Loading

0 comments on commit 9646306

Please sign in to comment.