public static byte[] toBytes(String str) { if (str == null) { return null; } try { return AddressTool.getAddress(str); } catch (Exception e) { throw new RuntimeException("address error", e); } }
public byte[] getPackingAddress() { if (null == packingAddress && this.blockSignature != null) { this.packingAddress = AddressTool.getAddress(blockSignature.getPublicKey()); } return packingAddress; }
public static Address fromHashs(String address) throws Exception { byte[] bytes = AddressTool.getAddress(address); return fromHashs(bytes); }
private List<byte[]> parseEventContract(List<String> events) { List<byte[]> result = new ArrayList<>(); if(events == null || events.isEmpty()) { return result; } for(String event : events) { try { Map<String, Object> eventMap = JSONUtils.json2map(event); String contractAddress = (String) eventMap.get(CONTRACT_EVENT_ADDRESS); result.add(AddressTool.getAddress(contractAddress)); } catch (Exception e) { Log.error(e); } } return result; }
@GET @Path("/balance/{address}") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "账户地址查询账户余额", notes = "result.data: balanceJson 返回对应的余额信息") @ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = Balance.class) }) public RpcClientResult getBalance(@ApiParam(name = "address", value = "账户地址", required = true) @PathParam("address") String address) { byte[] addressBytes = null; try { addressBytes = AddressTool.getAddress(address); } catch (Exception e) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult(); } if (addressBytes.length != Address.ADDRESS_LENGTH) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult(); } Result result = accountLedgerService.getBalance(addressBytes); return result.toRpcClientResult(); }
public MultiSigAccountDto(MultiSigAccount account) { this.address = account.getAddress().getBase58(); this.pubkeys = new ArrayList<>(); for (byte[] bytes : account.getPubKeyList()) { Map<String, String> map = new HashMap<>(); map.put("pubkey", Hex.encode(bytes)); map.put("address", AddressTool.getStringAddressByBytes(AddressTool.getAddress(bytes))); pubkeys.add(map); } this.m = account.getM(); this.alias = account.getAlias(); if (null == alias) { this.alias = ""; } }
/** * 获取脚本中的公钥生成地址 */ public static String getScriptAddress(List<ScriptChunk> chunks) { if (chunks.get(0).opcode == ScriptOpCodes.OP_0) { byte[] redeemByte = chunks.get(chunks.size() - 1).data; Script redeemScript = new Script(redeemByte); Address address = new Address(NulsContext.getInstance().getDefaultChainId(), NulsContext.P2SH_ADDRESS_TYPE, SerializeUtils.sha256hash160(redeemScript.getProgram())); return address.toString(); } else { return AddressTool.getStringAddressByBytes(AddressTool.getAddress(chunks.get(1).data)); } }
public static void initConfiguration() throws Exception { Block genesisBlock = GenesisBlock.getInstance(); NulsContext.getInstance().setGenesisBlock(genesisBlock); partakePacking = NulsConfig.MODULES_CONFIG.getCfgValue(CFG_CONSENSUS_SECTION, PROPERTY_PARTAKE_PACKING, false); Set<String> seedAddressSet = new HashSet<>(); String addresses = NulsConfig.MODULES_CONFIG.getCfgValue(CFG_CONSENSUS_SECTION, PROPERTY_SEED_NODES, ""); if (StringUtils.isBlank(addresses)) { return; } String[] array = addresses.split(SEED_NODES_DELIMITER); if (null == array) { return; } for (String address : array) { seedAddressSet.add(address); } for (String address : seedAddressSet) { seedNodeBytesList.add(AddressTool.getAddress(address)); seedNodeStringList.add(address); } int minUpgradeDelay = NulsConfig.MODULES_CONFIG.getCfgValue(CFG_CONSENSUS_SECTION, MIN_PROTOCOL_UPGRADE_DELAY, 1000); minProtocolUpgradeDelay = minUpgradeDelay; }
@GET @Path("/getTotalUTXO/{address}") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "获取总额和多少条utxo—做零钱换整功能", notes = "result.data: resultJson 返回转账结果") @ApiResponses(value = { @ApiResponse(code = 200, message = "success") }) public RpcClientResult getTotalUTXO(@ApiParam(name = "address", value = "获取总额和多少条utxo—做零钱换整功能", required = true) @PathParam("address") String address) { if (address == null) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult(); } if (!AddressTool.validAddress(address)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult(); } Result result = accountLedgerService.getAvailableTotalUTXO(AddressTool.getAddress(address)); if (!result.isSuccess()) { return Result.getFailed(AccountErrorCode.FAILED).toRpcClientResult(); } return result.toRpcClientResult(); }
@GET @Path("/redPunish/{address}") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "根据地址查询该账户是否被红牌惩罚过") @ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = Boolean.class) }) public RpcClientResult getRedPunishByAddress(@ApiParam(name = "address", value = "账户地址", required = true) @PathParam("address") String address) { if (!AddressTool.validAddress(address)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult(); } List<PunishLogPo> list = PocConsensusContext.getChainManager().getMasterChain().getChain().getRedPunishList(); boolean rs = false; for (PunishLogPo po : list) { if (Arrays.equals(AddressTool.getAddress(address), po.getAddress())) { rs = true; break; } } return Result.getSuccess().setData(rs).toRpcClientResult(); }
private List<Coin> getAllUtxoByAddress(String address) { List<Coin> coinList = new ArrayList<>(); byte[] addressBytes = AddressTool.getAddress(address); List<Entry<byte[], byte[]>> coinBytesList = contractUtxoStorageService.loadAllCoinList(); Coin coin; for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) { coin = new Coin(); try { coin.parse(coinEntryBytes.getValue(), 0); } catch (NulsException e) { Log.info("parse coin form db error"); continue; } if (Arrays.equals(coin.getAddress(), addressBytes)) { coin.setOwner(coinEntryBytes.getKey()); coinList.add(coin); } } Collections.sort(coinList, ContractCoinComparator.getInstance()); return coinList; }
private List<Coin> getAllUtxoByAddress(String address) { List<Coin> coinList = new ArrayList<>(); byte[] addressBytes = AddressTool.getAddress(address); List<Entry<byte[], byte[]>> coinBytesList = utxoLedgerUtxoStorageService.getAllUtxoEntryBytes(); Coin coin; for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) { coin = new Coin(); try { coin.parse(coinEntryBytes.getValue(), 0); } catch (NulsException e) { Log.info("parse coin form db error"); continue; } //if (Arrays.equals(coin.(), addressBytes)) if (Arrays.equals(coin.getAddress(), addressBytes)) { coin.setOwner(coinEntryBytes.getKey()); coinList.add(coin); } } Collections.sort(coinList, CoinComparator.getInstance()); return coinList; }
@POST @Path("/imputedprice") @Produces(MediaType.APPLICATION_JSON) @ApiOperation(value = "估算智能合约的price") @ApiResponses(value = { @ApiResponse(code = 200, message = "success") }) public RpcClientResult imputedPrice(@ApiParam(name = "imputedPriceForm", value = "估算智能合约的price", required = true) ImputedPrice imputedPrice) { try { String address = imputedPrice.getSender(); if (!AddressTool.validAddress(address)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult(); } Result<Account> accountResult = accountService.getAccount(address); if (accountResult.isFailed()) { return accountResult.toRpcClientResult(); } byte[] addressBytes = AddressTool.getAddress(address); long price = vmHelper.getLastedPriceForAccount(addressBytes); return Result.getSuccess().setData(price).toRpcClientResult(); } catch (Exception e) { return Result.getFailed().setMsg(e.getMessage()).toRpcClientResult(); } }
public Address(String address) { try { byte[] bytes = AddressTool.getAddress(address); Address addressTmp = Address.fromHashs(bytes); this.chainId = addressTmp.getChainId(); this.addressType = addressTmp.getAddressType(); this.hash160 = addressTmp.getHash160(); this.addressBytes = calcAddressbytes(); } catch (Exception e) { Log.error(e); } }
private Long getMaxAmount(Na fee, String address, Transaction tx) { long feeMax = TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES.multiply(TxMaxSizeValidator.MAX_TX_BYTES).getValue(); Long maxAmount = null; if (fee.getValue() > feeMax) { Result rs = accountLedgerService.getMaxAmountOfOnce(AddressTool.getAddress(address), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES); if (rs.isSuccess()) { maxAmount = ((Na) rs.getData()).getValue(); } } return maxAmount; }
@Override public boolean isTokenContractAddress(String contractAddress) { try { if (StringUtils.isBlank(contractAddress)) { return false; } if (!AddressTool.validAddress(contractAddress)) { return false; } byte[] contractAddressBytes = AddressTool.getAddress(contractAddress); Result<ContractAddressInfoPo> contractAddressInfoResult = contractAddressStorageService.getContractAddressInfo(contractAddressBytes); ContractAddressInfoPo po = contractAddressInfoResult.getData(); if(po == null) { return false; } return po.isNrc20(); } catch (Exception e) { Log.error("check if it is a token address error.", e); return false; } }
@Override public Result<List<ContractTokenTransferInfoPo>> getTokenTransferInfoList(String address) { try { Result accountResult = accountService.getAccount(address); if (accountResult.isFailed()) { return accountResult; } byte[] addressBytes = AddressTool.getAddress(address); List<ContractTokenTransferInfoPo> tokenTransferInfoListByAddress = contractTokenTransferStorageService.getTokenTransferInfoListByAddress(addressBytes); return Result.getSuccess().setData(tokenTransferInfoListByAddress); } catch (Exception e) { Log.error(e); return Result.getFailed(); } }
@Override public Result<List<ContractTokenTransferInfoPo>> getTokenTransferInfoList(String address, NulsDigestData hash) { try { if(hash == null) { return Result.getFailed(ContractErrorCode.NULL_PARAMETER); } Result accountResult = accountService.getAccount(address); if (accountResult.isFailed()) { return accountResult; } byte[] addressBytes = AddressTool.getAddress(address); List<ContractTokenTransferInfoPo> tokenTransferInfoListByAddress = contractTokenTransferStorageService.getTokenTransferInfoListByAddress(addressBytes, hash.serialize()); return Result.getSuccess().setData(tokenTransferInfoListByAddress); } catch (Exception e) { Log.error(e); return Result.getFailed(); } }
public static Coin convertCoin(InputDto utxo) { Coin coin = new Coin(); byte[] txHashBytes = Hex.decode(utxo.getFromHash()); coin.setOwner(ArraysTool.concatenate(txHashBytes, new VarInt(utxo.getFromIndex()).encode())); coin.setLockTime(utxo.getLockTime()); coin.setNa(Na.valueOf(utxo.getValue())); coin.setTempOwner(AddressTool.getAddress(utxo.getAddress())); return coin; }
@Override public Result<List<ContractTokenInfo>> getAllTokensByAccount(String address) { try { if (StringUtils.isBlank(address)) { return Result.getFailed(ContractErrorCode.NULL_PARAMETER); } if (!AddressTool.validAddress(address)) { return Result.getFailed(AccountErrorCode.ADDRESS_ERROR); } Result<List<ContractTokenInfo>> tokenListResult = contractBalanceManager.getAllTokensByAccount(address); List<ContractTokenInfo> list = tokenListResult.getData(); if(list != null && list.size() > 0) { byte[] prevStateRoot = ContractUtil.getStateRoot(NulsContext.getInstance().getBestBlock().getHeader()); ProgramExecutor track = programExecutor.begin(prevStateRoot); for(ContractTokenInfo tokenInfo : list) { tokenInfo.setStatus(track.status(AddressTool.getAddress(tokenInfo.getContractAddress())).ordinal()); } } return tokenListResult; } catch (Exception e) { Log.error("initial all tokens of the account error.", e); return Result.getFailed(ContractErrorCode.SYS_UNKOWN_EXCEPTION); } }