import { IAssetService } from 'core/service/transaction' ; import { IAssetDelegate , Transaction , TransactionModel } from 'shared/model/transaction' ; import { Account } from 'shared/model/account' ; import DelegateRepo from 'core/repository/delegate' ; import { ResponseEntity } from 'shared/model/response' ; import config from 'shared/config' ; class TransactionDelegateService implements IAssetService < IAssetDelegate > { create ( trs ) { return { username : trs . asset . username . toLowerCase ( ) . trim ( ) , url : trs . asset . url , } ; } getBytes ( trs ) { return Buffer . from ( trs . asset . username , 'utf8' ) ; } validate ( trs ) { const errors = [ ] ; if ( ! trs . asset || ! trs . asset . username ) { errors . push ( 'Invalid transaction asset' ) ; } if ( trs . asset . username !== trs . asset . username . toLowerCase ( ) ) { errors . push ( 'Username must be lowercase' ) ; } const isAddress = /^[0-9]{1,25}$/ig ; const allowSymbols = /^[a-z0-9!@$&_.]+$/g ; const username = String ( trs . asset . username ) . toLowerCase ( ) . trim ( ) ; if ( username === '' ) { errors . push ( 'Empty username' ) ; } if ( username . length > config . CONSTANTS . MAX_DELEGATE_USERNAME_LENGTH ) { errors . push ( 'Username is too long. Maximum is 20 characters' ) ; } if ( isAddress . test ( username ) ) { errors . push ( 'Username can not be a potential address' ) ; } if ( ! allowSymbols . test ( username ) ) { errors . push ( 'Username can only contain alphanumeric characters with the exception of !@$&_.' ) ; } return new ResponseEntity < void > ( { errors } ) ; } verifyUnconfirmed ( trs , sender ) { const errors = [ ] ; if ( sender . delegate ) { errors . push ( 'Account is already a delegate' ) ; } const username = String ( trs . asset . username ) . toLowerCase ( ) . trim ( ) ; const existingDelegate = DelegateRepo . isUserNameExists ( username ) ; if ( existingDelegate ) { errors . push ( 'Username already exists' ) ; } return new ResponseEntity < void > ( { errors } ) ; } calculateFee ( trs , sender ) { return config . CONSTANTS . FEES . DELEGATE ; } applyUnconfirmed ( trs , sender ) { sender . delegate = DelegateRepo . add ( sender , { username : trs . asset . username , url : trs . asset . url } ) ; } undoUnconfirmed ( trs , sender , senderOnly ) { if ( ! senderOnly ) { DelegateRepo . delete ( sender ) ; } sender . delegate = null ; } } export default new TransactionDelegateService ( ) ;