Skip to content

Commit

Permalink
feat(data-proxy)!: add chain-id to signed registration payload
Browse files Browse the repository at this point in the history
This prevents replay attacks between different networks.

Closes: #37
  • Loading branch information
Thomasvdam committed Jan 6, 2025
1 parent 6713325 commit 6183b93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions workspace/data-proxy-sdk/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum Environment {
}

export interface DataProxyOptions {
chainId: string;
rpcUrl: string;

// URL to the explorer page
Expand All @@ -18,21 +19,25 @@ export interface DataProxyOptions {

export const defaultConfig: Record<Environment, DataProxyOptions> = {
mainnet: {
chainId: "seda-1",
rpcUrl: "https://rpc.seda.xyz",
explorerUrl: "https://explorer.seda.xyz",
privateKey: Buffer.from([]),
},
testnet: {
chainId: "seda-1-testnet",
rpcUrl: "https://rpc.testnet.seda.xyz",
explorerUrl: "https://testnet.explorer.seda.xyz",
privateKey: Buffer.from([]),
},
devnet: {
chainId: "seda-1-devnet",
rpcUrl: "https://rpc.devnet.seda.xyz",
explorerUrl: "https://devnet.explorer.seda.xyz",
privateKey: Buffer.from([]),
},
planet: {
chainId: "seda-1-planet",
rpcUrl: "https://rpc.planet.seda.xyz",
explorerUrl: "https://planet.explorer.seda.xyz",
privateKey: Buffer.from([]),
Expand Down
17 changes: 16 additions & 1 deletion workspace/data-proxy/src/cli/register.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from "@commander-js/extra-typings";
import { Keccak256 } from "@cosmjs/crypto";
import { fromBech32 } from "@cosmjs/encoding";
import { Environment } from "@seda-protocol/data-proxy-sdk";
import { defaultConfig } from "@seda-protocol/data-proxy-sdk/src/config";
import { trySync } from "@seda-protocol/utils";
Expand Down Expand Up @@ -49,7 +50,11 @@ export const registerCommand = new Command("register")
process.exit(1);
}

// TODO: Validate if seda address is valid
if (!isValidSedaAddress(payoutAddress)) {
console.error(`${payoutAddress} is not a valid SEDA address`);
process.exit(1);
}

const aSedaAmount = trySync(() => sedaToAseda(fee)).map(
(amount) => `${amount}aseda`,
);
Expand All @@ -64,6 +69,7 @@ export const registerCommand = new Command("register")

hasher.update(Buffer.from(payoutAddress));
hasher.update(Buffer.from(memo));
hasher.update(Buffer.from(network.value.chainId));
const hash = Buffer.from(hasher.digest());

const signatureRaw = ecdsaSign(hash, privateKey.value);
Expand All @@ -87,3 +93,12 @@ export const registerCommand = new Command("register")
console.info("");
console.info(`Submit your transaction on: \n${url.toString()}`);
});

function isValidSedaAddress(address: string): boolean {
try {
const { prefix } = fromBech32(address);
return prefix === "seda";
} catch (error) {
return false;
}
}

0 comments on commit 6183b93

Please sign in to comment.