Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/services/DappsStakingService2.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { cryptoWaitReady, decodeAddress, signatureVerify } from '@polkadot/util-crypto';
import { u8aToHex } from '@polkadot/util';
import {
cryptoWaitReady,
decodeAddress,
evmToAddress,
isEthereumAddress,
signatureVerify,
} from '@polkadot/util-crypto';
import { stringToU8a, u8aToHex } from '@polkadot/util';
import { inject, injectable } from 'inversify';
import { IApiFactory } from '../client/ApiFactory';
import { ContainerTypes } from '../containertypes';
import { DappItem, NewDappItem } from '../models/Dapp';
import { NetworkType } from '../networks';
import { DappsStakingService, IDappsStakingService } from './DappsStakingService';
import { IFirebaseService } from './FirebaseService';
import { ethers } from 'ethers';
import { ASTAR_SS58_FORMAT } from './TxQueryService';

@injectable()
/**
Expand Down Expand Up @@ -56,10 +64,19 @@ export class DappsStakingService2 extends DappsStakingService implements IDappsS
network: NetworkType,
): Promise<boolean> {
const api = this._apiFactory.getApiInstance(network);
const isEvmSigner = isEthereumAddress(senderAddress);
const ss58SenderAddress = isEvmSigner ? evmToAddress(senderAddress, ASTAR_SS58_FORMAT) : senderAddress;
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding an inline comment explaining why the sender address is converted to SS58 format for EVM signers and how this conversion aligns with the expected payload format.

Copilot uses AI. Check for mistakes.

// Build signed payload and check signature
const signedMessage = await api.getRegisterDappPayload(dappAddress, senderAddress);
const isValidSignature = await this.isValidSignature(signedMessage, signature, senderAddress);
const messageToVerify = await api.getRegisterDappPayload(dappAddress, ss58SenderAddress);
let isValidSignature = false;

if (isEvmSigner) {
const signerAddress = ethers.verifyMessage(messageToVerify, signature);
isValidSignature = signerAddress.toLowerCase() === senderAddress.toLowerCase();
Comment on lines +75 to +76
Copy link

Copilot AI May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider wrapping ethers.verifyMessage in a try-catch block to gracefully handle potential exceptions during signature verification for EVM signers.

Suggested change
const signerAddress = ethers.verifyMessage(messageToVerify, signature);
isValidSignature = signerAddress.toLowerCase() === senderAddress.toLowerCase();
try {
const signerAddress = ethers.verifyMessage(messageToVerify, signature);
isValidSignature = signerAddress.toLowerCase() === senderAddress.toLowerCase();
} catch (error) {
console.error('Error verifying EVM signature:', error);
isValidSignature = false;
}

Copilot uses AI. Check for mistakes.
} else {
isValidSignature = await this.isValidSignature(messageToVerify, signature, senderAddress);
}

if (isValidSignature) {
// Check if dapp and sender are already registered to a node.
Expand All @@ -69,7 +86,7 @@ export class DappsStakingService2 extends DappsStakingService implements IDappsS
if (registeredDapp) {
return (
registeredDapp.state.toString() === 'Registered' &&
registeredDapp.developer.toString() === senderAddress
registeredDapp.developer.toString() === ss58SenderAddress
);
} else {
throw new Error(`The dapp ${dappAddress} is not registered with developer account ${senderAddress}`);
Expand Down