Skip to content

Commit

Permalink
refactor: support xdc address in api
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Mar 22, 2023
1 parent 2cb7867 commit 3cf32fc
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/chains/ethereum/ethereum-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'
);

Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum/ethereum.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/chains/xdc/xdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/chains/xdc/xdc.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions src/services/wallet/wallet.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const exists = await fse.pathExists(path);
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/services/wallet/wallet.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. <curve>:<encoded key>')
Expand Down

0 comments on commit 3cf32fc

Please sign in to comment.