@Test public void setAlias() { List<Account> accounts = accountService.createAccount(1, "nuls123456").getData(); Account account = accounts.get(0); Result result = aliasService.setAlias(account.getAddress().toString(), "nuls123456", "Charlie555"); assertTrue(result.isSuccess()); }
public Result<String> setAlias(String addr, String aliasName, String password) { if (!AddressTool.validAddress(addr)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR); } Account account = accountService.getAccount(addr).getData(); if (null == account) { return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST); } if (account.isEncrypted() && account.isLocked()) { if (!account.validatePassword(password)) { return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG); } } if (!account.isOk()) { return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT); } if (StringUtils.isNotBlank(account.getAlias())) { return Result.getFailed(AccountErrorCode.ACCOUNT_ALREADY_SET_ALIAS); } if (!StringUtils.validAlias(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_FORMAT_WRONG); } if (!isAliasUsable(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_EXIST); } byte[] addressBytes = account.getAddress().getAddressBytes(); try { AliasTransaction tx = new AliasTransaction(); tx.setTime(TimeService.currentTimeMillis()); Alias alias = new Alias(addressBytes, aliasName); tx.setTxData(alias); CoinDataResult coinDataResult = accountLedgerService.getCoinData(addressBytes, AccountConstant.ALIAS_NA, tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (!coinDataResult.isEnough()) { return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE); } CoinData coinData = new CoinData(); coinData.setFrom(coinDataResult.getCoinList()); Coin change = coinDataResult.getChange(); if (null != change) { List<Coin> toList = new ArrayList<>(); toList.add(change); coinData.setTo(toList); } Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, AccountConstant.ALIAS_NA, 0); coinData.addTo(coin); tx.setCoinData(coinData); tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash())); List<ECKey> signEckeys = new ArrayList<>(); List<ECKey> scriptEckeys = new ArrayList<>(); ; ECKey eckey = account.getEcKey(password); if ((coinDataResult.getSignType() & 0x01) == 0x01) { signEckeys.add(eckey); } if ((coinDataResult.getSignType() & 0x02) == 0x02) { scriptEckeys.add(eckey); } SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys); Result saveResult = accountLedgerService.verifyAndSaveUnconfirmedTransaction(tx); if (saveResult.isFailed()) { if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) { Result rs = accountLedgerService.getMaxAmountOfOnce(account.getAddress().getAddressBytes(), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (rs.isSuccess()) { Na maxAmount = (Na) rs.getData(); rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND); rs.setMsg(rs.getMsg() + maxAmount.toDouble()); } return rs; } return saveResult; } this.transactionService.newTx(tx); Result sendResult = this.transactionService.broadcastTx(tx); if (sendResult.isFailed()) { accountLedgerService.deleteTransaction(tx); return sendResult; } String hash = tx.getHash().getDigestHex(); return Result.getSuccess().setData(hash); } catch (Exception e) { Log.error(e); return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION); } }
AliasService { public Result<String> setAlias(String addr, String aliasName, String password) { if (!AddressTool.validAddress(addr)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR); } Account account = accountService.getAccount(addr).getData(); if (null == account) { return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST); } if (account.isEncrypted() && account.isLocked()) { if (!account.validatePassword(password)) { return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG); } } if (!account.isOk()) { return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT); } if (StringUtils.isNotBlank(account.getAlias())) { return Result.getFailed(AccountErrorCode.ACCOUNT_ALREADY_SET_ALIAS); } if (!StringUtils.validAlias(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_FORMAT_WRONG); } if (!isAliasUsable(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_EXIST); } byte[] addressBytes = account.getAddress().getAddressBytes(); try { AliasTransaction tx = new AliasTransaction(); tx.setTime(TimeService.currentTimeMillis()); Alias alias = new Alias(addressBytes, aliasName); tx.setTxData(alias); CoinDataResult coinDataResult = accountLedgerService.getCoinData(addressBytes, AccountConstant.ALIAS_NA, tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (!coinDataResult.isEnough()) { return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE); } CoinData coinData = new CoinData(); coinData.setFrom(coinDataResult.getCoinList()); Coin change = coinDataResult.getChange(); if (null != change) { List<Coin> toList = new ArrayList<>(); toList.add(change); coinData.setTo(toList); } Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, AccountConstant.ALIAS_NA, 0); coinData.addTo(coin); tx.setCoinData(coinData); tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash())); List<ECKey> signEckeys = new ArrayList<>(); List<ECKey> scriptEckeys = new ArrayList<>(); ; ECKey eckey = account.getEcKey(password); if ((coinDataResult.getSignType() & 0x01) == 0x01) { signEckeys.add(eckey); } if ((coinDataResult.getSignType() & 0x02) == 0x02) { scriptEckeys.add(eckey); } SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys); Result saveResult = accountLedgerService.verifyAndSaveUnconfirmedTransaction(tx); if (saveResult.isFailed()) { if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) { Result rs = accountLedgerService.getMaxAmountOfOnce(account.getAddress().getAddressBytes(), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (rs.isSuccess()) { Na maxAmount = (Na) rs.getData(); rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND); rs.setMsg(rs.getMsg() + maxAmount.toDouble()); } return rs; } return saveResult; } this.transactionService.newTx(tx); Result sendResult = this.transactionService.broadcastTx(tx); if (sendResult.isFailed()) { accountLedgerService.deleteTransaction(tx); return sendResult; } String hash = tx.getHash().getDigestHex(); return Result.getSuccess().setData(hash); } catch (Exception e) { Log.error(e); return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION); } } }
AliasService { public Result<String> setAlias(String addr, String aliasName, String password) { if (!AddressTool.validAddress(addr)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR); } Account account = accountService.getAccount(addr).getData(); if (null == account) { return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST); } if (account.isEncrypted() && account.isLocked()) { if (!account.validatePassword(password)) { return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG); } } if (!account.isOk()) { return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT); } if (StringUtils.isNotBlank(account.getAlias())) { return Result.getFailed(AccountErrorCode.ACCOUNT_ALREADY_SET_ALIAS); } if (!StringUtils.validAlias(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_FORMAT_WRONG); } if (!isAliasUsable(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_EXIST); } byte[] addressBytes = account.getAddress().getAddressBytes(); try { AliasTransaction tx = new AliasTransaction(); tx.setTime(TimeService.currentTimeMillis()); Alias alias = new Alias(addressBytes, aliasName); tx.setTxData(alias); CoinDataResult coinDataResult = accountLedgerService.getCoinData(addressBytes, AccountConstant.ALIAS_NA, tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (!coinDataResult.isEnough()) { return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE); } CoinData coinData = new CoinData(); coinData.setFrom(coinDataResult.getCoinList()); Coin change = coinDataResult.getChange(); if (null != change) { List<Coin> toList = new ArrayList<>(); toList.add(change); coinData.setTo(toList); } Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, AccountConstant.ALIAS_NA, 0); coinData.addTo(coin); tx.setCoinData(coinData); tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash())); List<ECKey> signEckeys = new ArrayList<>(); List<ECKey> scriptEckeys = new ArrayList<>(); ; ECKey eckey = account.getEcKey(password); if ((coinDataResult.getSignType() & 0x01) == 0x01) { signEckeys.add(eckey); } if ((coinDataResult.getSignType() & 0x02) == 0x02) { scriptEckeys.add(eckey); } SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys); Result saveResult = accountLedgerService.verifyAndSaveUnconfirmedTransaction(tx); if (saveResult.isFailed()) { if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) { Result rs = accountLedgerService.getMaxAmountOfOnce(account.getAddress().getAddressBytes(), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (rs.isSuccess()) { Na maxAmount = (Na) rs.getData(); rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND); rs.setMsg(rs.getMsg() + maxAmount.toDouble()); } return rs; } return saveResult; } this.transactionService.newTx(tx); Result sendResult = this.transactionService.broadcastTx(tx); if (sendResult.isFailed()) { accountLedgerService.deleteTransaction(tx); return sendResult; } String hash = tx.getHash().getDigestHex(); return Result.getSuccess().setData(hash); } catch (Exception e) { Log.error(e); return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION); } }  }
AliasService { public Result<String> setAlias(String addr, String aliasName, String password) { if (!AddressTool.validAddress(addr)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR); } Account account = accountService.getAccount(addr).getData(); if (null == account) { return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST); } if (account.isEncrypted() && account.isLocked()) { if (!account.validatePassword(password)) { return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG); } } if (!account.isOk()) { return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT); } if (StringUtils.isNotBlank(account.getAlias())) { return Result.getFailed(AccountErrorCode.ACCOUNT_ALREADY_SET_ALIAS); } if (!StringUtils.validAlias(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_FORMAT_WRONG); } if (!isAliasUsable(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_EXIST); } byte[] addressBytes = account.getAddress().getAddressBytes(); try { AliasTransaction tx = new AliasTransaction(); tx.setTime(TimeService.currentTimeMillis()); Alias alias = new Alias(addressBytes, aliasName); tx.setTxData(alias); CoinDataResult coinDataResult = accountLedgerService.getCoinData(addressBytes, AccountConstant.ALIAS_NA, tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (!coinDataResult.isEnough()) { return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE); } CoinData coinData = new CoinData(); coinData.setFrom(coinDataResult.getCoinList()); Coin change = coinDataResult.getChange(); if (null != change) { List<Coin> toList = new ArrayList<>(); toList.add(change); coinData.setTo(toList); } Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, AccountConstant.ALIAS_NA, 0); coinData.addTo(coin); tx.setCoinData(coinData); tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash())); List<ECKey> signEckeys = new ArrayList<>(); List<ECKey> scriptEckeys = new ArrayList<>(); ; ECKey eckey = account.getEcKey(password); if ((coinDataResult.getSignType() & 0x01) == 0x01) { signEckeys.add(eckey); } if ((coinDataResult.getSignType() & 0x02) == 0x02) { scriptEckeys.add(eckey); } SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys); Result saveResult = accountLedgerService.verifyAndSaveUnconfirmedTransaction(tx); if (saveResult.isFailed()) { if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) { Result rs = accountLedgerService.getMaxAmountOfOnce(account.getAddress().getAddressBytes(), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (rs.isSuccess()) { Na maxAmount = (Na) rs.getData(); rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND); rs.setMsg(rs.getMsg() + maxAmount.toDouble()); } return rs; } return saveResult; } this.transactionService.newTx(tx); Result sendResult = this.transactionService.broadcastTx(tx); if (sendResult.isFailed()) { accountLedgerService.deleteTransaction(tx); return sendResult; } String hash = tx.getHash().getDigestHex(); return Result.getSuccess().setData(hash); } catch (Exception e) { Log.error(e); return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION); } }  Result<String> setAlias(String addr, String aliasName, String password); Result saveAlias(AliasPo aliaspo); Alias getAlias(String alias); boolean isAliasUsable(String alias); Result rollbackAlias(AliasPo aliasPo); Result<Na> getAliasFee(String address, String aliasName); Result<Na> getMultiAliasFee(String address, String aliasName); Result<String> setMutilAlias(String addr, String signAddr, String aliasName, String password, List<String> pubKeys, int m, String txdata); Result<String> setMutilAlias(String addr, String signAddr, String aliasName, String password); Result signMultiAliasTransaction(String signAddr, String password, String txdata); boolean isMutilAliasUsable(byte[] address, String aliasName); Result<String> txMutilProcessing(Transaction tx, List<P2PHKSignature> p2PHKSignatures, List<Script> scripts, TransactionSignature transactionSignature, byte[] fromAddr); List<AliasPo> getAllAlias(); }
AliasService { public Result<String> setAlias(String addr, String aliasName, String password) { if (!AddressTool.validAddress(addr)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR); } Account account = accountService.getAccount(addr).getData(); if (null == account) { return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST); } if (account.isEncrypted() && account.isLocked()) { if (!account.validatePassword(password)) { return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG); } } if (!account.isOk()) { return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT); } if (StringUtils.isNotBlank(account.getAlias())) { return Result.getFailed(AccountErrorCode.ACCOUNT_ALREADY_SET_ALIAS); } if (!StringUtils.validAlias(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_FORMAT_WRONG); } if (!isAliasUsable(aliasName)) { return Result.getFailed(AccountErrorCode.ALIAS_EXIST); } byte[] addressBytes = account.getAddress().getAddressBytes(); try { AliasTransaction tx = new AliasTransaction(); tx.setTime(TimeService.currentTimeMillis()); Alias alias = new Alias(addressBytes, aliasName); tx.setTxData(alias); CoinDataResult coinDataResult = accountLedgerService.getCoinData(addressBytes, AccountConstant.ALIAS_NA, tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (!coinDataResult.isEnough()) { return Result.getFailed(AccountErrorCode.INSUFFICIENT_BALANCE); } CoinData coinData = new CoinData(); coinData.setFrom(coinDataResult.getCoinList()); Coin change = coinDataResult.getChange(); if (null != change) { List<Coin> toList = new ArrayList<>(); toList.add(change); coinData.setTo(toList); } Coin coin = new Coin(NulsConstant.BLACK_HOLE_ADDRESS, AccountConstant.ALIAS_NA, 0); coinData.addTo(coin); tx.setCoinData(coinData); tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash())); List<ECKey> signEckeys = new ArrayList<>(); List<ECKey> scriptEckeys = new ArrayList<>(); ; ECKey eckey = account.getEcKey(password); if ((coinDataResult.getSignType() & 0x01) == 0x01) { signEckeys.add(eckey); } if ((coinDataResult.getSignType() & 0x02) == 0x02) { scriptEckeys.add(eckey); } SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys); Result saveResult = accountLedgerService.verifyAndSaveUnconfirmedTransaction(tx); if (saveResult.isFailed()) { if (KernelErrorCode.DATA_SIZE_ERROR.getCode().equals(saveResult.getErrorCode().getCode())) { Result rs = accountLedgerService.getMaxAmountOfOnce(account.getAddress().getAddressBytes(), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (rs.isSuccess()) { Na maxAmount = (Na) rs.getData(); rs = Result.getFailed(KernelErrorCode.DATA_SIZE_ERROR_EXTEND); rs.setMsg(rs.getMsg() + maxAmount.toDouble()); } return rs; } return saveResult; } this.transactionService.newTx(tx); Result sendResult = this.transactionService.broadcastTx(tx); if (sendResult.isFailed()) { accountLedgerService.deleteTransaction(tx); return sendResult; } String hash = tx.getHash().getDigestHex(); return Result.getSuccess().setData(hash); } catch (Exception e) { Log.error(e); return Result.getFailed(KernelErrorCode.SYS_UNKOWN_EXCEPTION); } }  Result<String> setAlias(String addr, String aliasName, String password); Result saveAlias(AliasPo aliaspo); Alias getAlias(String alias); boolean isAliasUsable(String alias); Result rollbackAlias(AliasPo aliasPo); Result<Na> getAliasFee(String address, String aliasName); Result<Na> getMultiAliasFee(String address, String aliasName); Result<String> setMutilAlias(String addr, String signAddr, String aliasName, String password, List<String> pubKeys, int m, String txdata); Result<String> setMutilAlias(String addr, String signAddr, String aliasName, String password); Result signMultiAliasTransaction(String signAddr, String password, String txdata); boolean isMutilAliasUsable(byte[] address, String aliasName); Result<String> txMutilProcessing(Transaction tx, List<P2PHKSignature> p2PHKSignatures, List<Script> scripts, TransactionSignature transactionSignature, byte[] fromAddr); List<AliasPo> getAllAlias();  }