From 3cf32fcd9cd8bc76731eacbfaa2d95d818d94098 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Wed, 22 Mar 2023 15:06:52 +0800 Subject: [PATCH] refactor: support xdc address in api --- src/chains/ethereum/ethereum-base.ts | 3 ++- src/chains/ethereum/ethereum.validators.ts | 2 +- src/chains/xdc/xdc.ts | 3 ++- src/chains/xdc/xdc.validators.ts | 2 +- src/helpers.ts | 16 ++++++++++++++++ src/services/wallet/wallet.controllers.ts | 3 +++ src/services/wallet/wallet.validators.ts | 2 +- 7 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 src/helpers.ts diff --git a/src/chains/ethereum/ethereum-base.ts b/src/chains/ethereum/ethereum-base.ts index 68af366bd6..545f6ecd12 100644 --- a/src/chains/ethereum/ethereum-base.ts +++ b/src/chains/ethereum/ethereum-base.ts @@ -18,6 +18,7 @@ import fse from 'fs-extra'; import { ConfigManagerCertPassphrase } from '../../services/config-manager-cert-passphrase'; import { logger } from '../../services/logger'; import { ReferenceCountingCloseable } from '../../services/refcounting-closeable'; +import { convertXdcPublicKey } from '../../helpers'; // information about an Ethereum token export interface TokenInfo { @@ -191,7 +192,7 @@ export class EthereumBase { const path = `${walletPath}/${this.chainName}`; const encryptedPrivateKey: string = await fse.readFile( - `${path}/${address}.json`, + `${path}/${convertXdcPublicKey(address)}.json`, 'utf8' ); diff --git a/src/chains/ethereum/ethereum.validators.ts b/src/chains/ethereum/ethereum.validators.ts index 755314acfa..eef6580eb3 100644 --- a/src/chains/ethereum/ethereum.validators.ts +++ b/src/chains/ethereum/ethereum.validators.ts @@ -32,7 +32,7 @@ export const invalidNetworkError: string = 'The network param is not a string.'; // test if a string matches the shape of an Ethereum address export const isAddress = (str: string): boolean => { - return /^0x[a-fA-F0-9]{40}$/.test(str); + return /^(0x|xdc)[a-fA-F0-9]{40}$/.test(str); }; // given a request, look for a key called address that is an Ethereum wallet diff --git a/src/chains/xdc/xdc.ts b/src/chains/xdc/xdc.ts index 34156b09b1..541f235d10 100644 --- a/src/chains/xdc/xdc.ts +++ b/src/chains/xdc/xdc.ts @@ -8,6 +8,7 @@ import { XsswapConfig } from '../../connectors/xsswap/xsswap.config'; import { GlobianceConfig } from '../../connectors/globiance/globiance.config'; import { Ethereumish } from '../../services/common-interfaces'; import { ConfigManagerV2 } from '../../services/config-manager-v2'; +import { convertXdcPublicKey } from '../../helpers'; export class Xdc extends EthereumBase implements Ethereumish { private static _instances: { [name: string]: Xdc }; @@ -71,7 +72,7 @@ export class Xdc extends EthereumBase implements Ethereumish { } else if (reqSpender === 'globiance') { spender = GlobianceConfig.config.routerAddress(this._chain); } else { - spender = reqSpender; + spender = convertXdcPublicKey(reqSpender); } return spender; } diff --git a/src/chains/xdc/xdc.validators.ts b/src/chains/xdc/xdc.validators.ts index b4a875bec7..4374b8329e 100644 --- a/src/chains/xdc/xdc.validators.ts +++ b/src/chains/xdc/xdc.validators.ts @@ -14,7 +14,7 @@ import { } from '../ethereum/ethereum.validators'; export const invalidSpenderError: string = - 'The spender param is not a valid xdc address (0x followed by 40 hexidecimal characters).'; + 'The spender param is invalid (should be 0x|xdc followed by 40 hexidecimal characters).'; // given a request, look for a key called spender that is 'xsswap' or 'globiance' or an Ethereum address export const validateSpender: Validator = mkValidator( diff --git a/src/helpers.ts b/src/helpers.ts new file mode 100644 index 0000000000..02f5137ea3 --- /dev/null +++ b/src/helpers.ts @@ -0,0 +1,16 @@ +/** + * Returns the new address. + * + * This function convert xdc address prefix to 0x. + */ +export function convertXdcPublicKey(publicKey: string): string { + return publicKey.length === 43 && publicKey.slice(0, 3) === 'xdc' + ? '0x' + publicKey.slice(3) + : publicKey; +} + +export function convertXdcPrivateKey(privateKey: string): string { + return privateKey.length === 67 && privateKey.slice(0, 3) === 'xdc' + ? '0x' + privateKey.slice(3) + : privateKey; +} \ No newline at end of file diff --git a/src/services/wallet/wallet.controllers.ts b/src/services/wallet/wallet.controllers.ts index 73feeb802a..7ef1b6ab2f 100644 --- a/src/services/wallet/wallet.controllers.ts +++ b/src/services/wallet/wallet.controllers.ts @@ -29,6 +29,8 @@ import { import { EthereumBase } from '../../chains/ethereum/ethereum-base'; import { Near } from '../../chains/near/near'; +import { convertXdcPrivateKey } from '../../helpers'; + const walletPath = './conf/wallets'; export async function mkdirIfDoesNotExist(path: string): Promise { const exists = await fse.pathExists(path); @@ -86,6 +88,7 @@ export async function addWallet( try { if (connection instanceof EthereumBase) { + req.privateKey = convertXdcPrivateKey(req.privateKey); address = connection.getWalletFromPrivateKey(req.privateKey).address; encryptedPrivateKey = await connection.encrypt( req.privateKey, diff --git a/src/services/wallet/wallet.validators.ts b/src/services/wallet/wallet.validators.ts index a854de270e..a4cfa90920 100644 --- a/src/services/wallet/wallet.validators.ts +++ b/src/services/wallet/wallet.validators.ts @@ -18,7 +18,7 @@ export const invalidCosmosPrivateKeyError: string = // test if a string matches the shape of an Ethereum private key export const isEthPrivateKey = (str: string): boolean => { - return /^(0x)?[a-fA-F0-9]{64}$/.test(str); + return /^(0x|xdc)?[a-fA-F0-9]{64}$/.test(str); }; // test if a string matches the Near private key encoding format (i.e. :')