diff --git a/src/main/java/io/xdag/rpc/modules/xdag/XdagModuleChainBase.java b/src/main/java/io/xdag/rpc/modules/xdag/XdagModuleChainBase.java index 5c437d5f..c6b29265 100644 --- a/src/main/java/io/xdag/rpc/modules/xdag/XdagModuleChainBase.java +++ b/src/main/java/io/xdag/rpc/modules/xdag/XdagModuleChainBase.java @@ -38,6 +38,7 @@ import static io.xdag.utils.BasicUtils.amount2xdag; import static io.xdag.utils.BasicUtils.hash2Address; import static io.xdag.utils.BasicUtils.pubAddress2Hash; +import static io.xdag.utils.PubkeyAddressUtils.checkAddress; import static io.xdag.utils.PubkeyAddressUtils.toBase58; import static io.xdag.utils.XdagTime.xdagTimestampToMs; @@ -153,17 +154,21 @@ public String getMaxXferBalance() { public BlockResultDTO getBlockDTOByHash(String hash) { Bytes32 blockHash; - if (StringUtils.length(hash) == 32) { - blockHash = address2Hash(hash); + if (checkAddress(hash)) { + return transferAccountToBlockResultDTO(hash); } else { - blockHash = BasicUtils.getHash(hash); - } - Block block = blockchain.getBlockByHash(blockHash, true); - if (block == null) { - block = blockchain.getBlockByHash(blockHash, false); - return transferBlockInfoToBlockResultDTO(block); + if (StringUtils.length(hash) == 32) { + blockHash = address2Hash(hash); + } else { + blockHash = BasicUtils.getHash(hash); + } + Block block = blockchain.getBlockByHash(blockHash, true); + if (block == null) { + block = blockchain.getBlockByHash(blockHash, false); + return transferBlockInfoToBlockResultDTO(block); + } + return transferBlockToBlockResultDTO(block); } - return transferBlockToBlockResultDTO(block); } private BlockResultDTO transferBlockToBriefBlockResultDTO(Block block) { @@ -209,6 +214,28 @@ private BlockResultDTO transferBlockInfoToBlockResultDTO(Block block) { return BlockResultDTOBuilder.build(); } + private BlockResultDTO transferAccountToBlockResultDTO(String address) { + UInt64 balance = kernel.getAddressStore().getBalanceByAddress(Hash2byte(pubAddress2Hash(address).mutableCopy())); + + BlockResultDTO.BlockResultDTOBuilder BlockResultDTOBuilder = BlockResultDTO.builder(); + BlockResultDTOBuilder.address(address) + .hash(null) + .balance(String.format("%.9f", amount2xdag(balance))) + .type("Wallet") + .blockTime(xdagTimestampToMs(kernel.getConfig().getSnapshotSpec().getSnapshotTime())) + .timeStamp(kernel.getConfig().getSnapshotSpec().getSnapshotTime()) +// .flags(Integer.toHexString(block.getInfo().getFlags())) +// .diff(toQuantityJsonHex(block.getInfo().getDifficulty())) +// .remark(block.getInfo().getRemark() == null ? "" : new String(block.getInfo().getRemark(), +// StandardCharsets.UTF_8).trim()) + .state("Accepted") +// .type(getType(block)) +// .refs(getLinks(block)) +// .height(block.getInfo().getHeight()) + .transactions(getTxHistory(address)); + return BlockResultDTOBuilder.build(); + } + private BlockResultDTO transferBlockToBlockResultDTO(Block block) { if (null == block) { return null;