Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async importAccount({ privateKey, name }) {
// 埋点
logger.info(`Importing account '${ name }' from popup`);
const account = new Account(
privateKey.match(/^T/) && TronWeb.isAddress(privateKey) ? ACCOUNT_TYPE.LEDGER : ACCOUNT_TYPE.PRIVATE_KEY,
privateKey
);
const {
address
} = account;
account.name = name;
if (Object.keys(this.accounts).length === 0) {
this.setCache();
}
this.accounts[address] = account;
StorageService.saveAccount(account);
this.emit('setAccounts', this.getAccounts());
this.selectAccount(address);
_importPrivateKey(privateKey) {
try {
if (privateKey.match(/^T/) && TronWeb.isAddress(privateKey)) {
this.privateKey = null;
this.address = privateKey;
} else {
this.privateKey = privateKey;
this.address = TronWeb.address.fromPrivateKey(privateKey);
}
} catch (ex) { // eslint-disable-line
throw new Error('INVALID_PRIVATE_KEY');
}
}
onChange(address) {
address = address.trim();
const { smart } = this.props.tokens;
const error = Object.keys(smart).includes(address) ? 'ERRORS.TOKEN_ADDED' : false;
let isValid = !error && address.length === 34;
try {
isValid = isValid && TronWeb.isAddress(address);
} catch {}
this.setState({
address,
isValid,
error
});
}
async onRecipientChange(address) {
const { selectedAddress } = this.state;
const { chains } = this.props;
const recipient = {
value: address,
valid: VALIDATION_STATE.NONE
};
if(!address.length)
return this.setState({recipient:{value: '', valid: false, error: ''}});
if(!TronWeb.isAddress(address)) {
recipient.valid = false;
recipient.error = 'EXCEPTION.SEND.ADDRESS_FORMAT_ERROR';
} else {
const account = await PopupAPI.getAccountInfo(address);
if(!account[chains.selected === '_'? 'mainchain' : 'sidechain' ].address) {
recipient.isActivated = false;
recipient.valid = true;
recipient.error = 'EXCEPTION.SEND.ADDRESS_UNACTIVATED_ERROR';
} else if(address === selectedAddress) {
recipient.isActivated = true;
recipient.valid = false;
recipient.error = 'EXCEPTION.SEND.ADDRESS_SAME_ERROR';
} else {
recipient.isActivated = true;
recipient.valid = true;
recipient.error = '';
<input type="text"> {
const value = e.target.value;
this.state.address.value = value;
this.state.address.valid = TronWeb.isAddress(value);
this.setState({ address: this.state.address });
}} placeholder={formatMessage({ id: 'MENU.ADD_TRC20_TOKEN.INPUT_PLACE_HOLDER' })} />
onRecipientChange(address) {
address = address.trim();
const recipient = {
value: address,
valid: VALIDATION_STATE.NONE
};
if(!address.length)
return this.setState({ recipient });
if(!TronWeb.isAddress(address))
recipient.valid = VALIDATION_STATE.INVALID;
else recipient.valid = VALIDATION_STATE.VALID;
this.setState({
recipient
});
}