Skip to content

Commit

Permalink
ETH address conversion + storing parachain in URL
Browse files Browse the repository at this point in the history
Fixes #459

I'm still thinking on how to properly test ethereum drips, will follow
up on that.
  • Loading branch information
mutantcornholio committed Nov 4, 2024
1 parent f47c0a2 commit cd6bf38
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 6 deletions.
9 changes: 9 additions & 0 deletions client/src/lib/components/NetworkInput.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script lang="ts">
import { goto } from "$app/navigation";
import { page } from "$app/stores";
import { testnet } from "$lib/utils/stores";
import { getChainName } from "../utils/networkData";
Expand Down Expand Up @@ -31,6 +33,13 @@
elem?.blur();
}
network = chain;
if (chain === -1) {
$page.url.searchParams.delete("parachain");
} else {
$page.url.searchParams.set("parachain", chain.toString());
}
goto(`?${$page.url.searchParams.toString()}`);
}
</script>

Expand Down
4 changes: 1 addition & 3 deletions client/src/lib/utils/stores.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { derived, writable } from "svelte/store";
import { writable } from "svelte/store";

import { type NetworkData, Paseo } from "./networkData";

// Defaults to Paseo, being updated in Faucet.svelte
export const testnet = writable<NetworkData>(Paseo);

export const testnetName = derived(testnet, ($net) => $net.networkName);

interface FaucetOperation {
success: boolean;
hash: string;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"dependencies": {
"@eng-automation/js": "^2.1.0",
"@noble/hashes": "^1.5.0",
"@polkadot-api/descriptors": "portal:.papi/descriptors",
"@polkadot-labs/hdkd": "^0.0.6",
"@polkadot-labs/hdkd-helpers": "^0.0.6",
Expand Down
9 changes: 7 additions & 2 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { convertAmountToBn, convertBnAmountToNumber, formatAmount } from "#src/d
import { isDripSuccessResponse } from "#src/guards";
import { logger } from "#src/logger";
import { getNetworkData } from "#src/papi/index";
import { isAccountPrivileged } from "#src/utils";
import { ethAddressToSS58, isAccountPrivileged } from "#src/utils";
import * as mSDK from "matrix-js-sdk";
import { AccountId } from "polkadot-api";

Expand Down Expand Up @@ -129,10 +129,15 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
}

const arg0_processed = arg0.trim().split(":");
const address = arg0_processed[0];
let address = arg0_processed[0];
const parachain_id = arg0_processed[1] ? arg0_processed[1] : "";
logger.debug(`Processed receiver to address ${address} and parachain id ${parachain_id}`);

if (address.startsWith("0x")) {
address = ethAddressToSS58(address, networkData.data.ethToSS58FillPrefix);
logger.debug(`Converted ETH address to ${address}`);
}

try {
AccountId().enc(address);
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions src/faucet.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,6 @@ describe("Faucet E2E", () => {
"Requester has reached their daily quota. Only request once per day",
);
});

test.todo("Faucet drips to ethereum address");
});
1 change: 1 addition & 0 deletions src/papi/chains/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const networkData: NetworkData = {
networkName: "Rococo",
rpcEndpoint: "ws://host.docker.internal:9933/",
matrixWhitelistPatterns: parityWhitelist,
ethToSS58FillPrefix: 0xee,
};

export const networkApi: NetworkApi = {
Expand Down
2 changes: 2 additions & 0 deletions src/papi/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface NetworkData {
dripAmount: string;
balanceCap: number;
matrixWhitelistPatterns: RegExp[];
// Should be 0xEE for all our currenly supported chains, but could be different for other chains
ethToSS58FillPrefix: number;
}

export function getNetworkData(networkName: string): { data: NetworkData; api: NetworkApi } {
Expand Down
1 change: 1 addition & 0 deletions src/papi/chains/paseo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const networkData: NetworkData = {
/^@purpletentacle:matrix\.org$/,
/^@carlosala:matrix\.org$/,
],
ethToSS58FillPrefix: 0xee,
};

export const networkApi: NetworkApi = {
Expand Down
1 change: 1 addition & 0 deletions src/papi/chains/versi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const networkData: NetworkData = {
networkName: "Versi",
rpcEndpoint: "wss://versi-rpc-node-0.parity-versi.parity.io/",
matrixWhitelistPatterns: parityWhitelist,
ethToSS58FillPrefix: 0xee,
};

const networkApi: NetworkApi = {
Expand Down
1 change: 1 addition & 0 deletions src/papi/chains/westend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const networkData: NetworkData = {
networkName: "Westend",
rpcEndpoint: "wss://westend-rpc.polkadot.io/",
matrixWhitelistPatterns: parityWhitelist,
ethToSS58FillPrefix: 0xee,
};

export const networkApi: NetworkApi = {
Expand Down
12 changes: 11 additions & 1 deletion src/server/routes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import polkadotActions from "#src/dripper/polkadot/PolkadotActions";
import { convertAmountToBn, formatAmount } from "#src/dripper/polkadot/utils";
import { logger } from "#src/logger";
import { getNetworkData } from "#src/papi/index";
import { ethAddressToSS58 } from "#src/utils";
import cors from "cors";
import express, { NextFunction, Request, Response } from "express";

Expand Down Expand Up @@ -54,10 +55,19 @@ const addressMiddleware = (
type PartialDrip<T extends FaucetRequestType | BotRequestType> = Partial<T> & Pick<T, "address">;

router.post<unknown, DripResponse, PartialDrip<FaucetRequestType>>("/drip/web", addressMiddleware, async (req, res) => {
const { address, parachain_id, recaptcha } = req.body;
const { parachain_id, recaptcha } = req.body;
if (!recaptcha) {
return missingParameterError(res, "recaptcha");
}

let { address } = req.body;

logger.debug(`Dripping to ${address}, parachain id ${parachain_id}`);
if (address.startsWith("0x")) {
address = ethAddressToSS58(address, networkData.data.ethToSS58FillPrefix);
logger.debug(`Converted ETH address to ${address}`);
}

try {
const dripResult = await dripRequestHandler.handleRequest({
external: true,
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { concatBytes, hexToBytes } from "@noble/hashes/utils";
import { config } from "#src/config";
import { getNetworkData } from "#src/papi/index";
import { AccountId } from "polkadot-api";

export function isAccountPrivileged(sender: string): boolean {
const networkName = config.Get("NETWORK");
const networkData = getNetworkData(networkName);

return networkData.data.matrixWhitelistPatterns.some((pattern) => pattern.test(sender));
}

// copypaste from https://github.com/paritytech/revive-remix/blob/2eca8b351f96734ffc94adc63b94f3587bd8d67d/libs/remix-ui/helper/src/lib/remix-ui-helper.ts#L93
export function ethAddressToSS58(address: string, prefix: number): string {
// Remove '0x' prefix
const ethAddress = address.slice(2);
const ethAddressBytes = hexToBytes(ethAddress);
// Pad the address to 32 bytes with `0xEE`
const paddedAddress = concatBytes(ethAddressBytes, new Uint8Array(32 - ethAddressBytes.length).fill(prefix));
return AccountId().dec(paddedAddress);
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5090,6 +5090,7 @@ __metadata:
"@eng-automation/js": "npm:^2.1.0"
"@eng-automation/js-style": "npm:^3.1.0"
"@eng-automation/testing": "npm:^1.5.1"
"@noble/hashes": "npm:^1.5.0"
"@polkadot-api/descriptors": "portal:.papi/descriptors"
"@polkadot-labs/hdkd": "npm:^0.0.6"
"@polkadot-labs/hdkd-helpers": "npm:^0.0.6"
Expand Down

0 comments on commit cd6bf38

Please sign in to comment.