public BlockHeader(JsonObject jsonObject) { this.chain = Hex.decode(jsonObject.get("chain").getAsString()); this.version = Hex.decode(jsonObject.get("version").getAsString()); this.type = Hex.decode(jsonObject.get("type").getAsString()); this.prevBlockHash = Hex.decode(jsonObject.get("prevBlockHash").getAsString()); this.index = HexUtil.hexStringToLong(jsonObject.get("index").getAsString()); this.timestamp = HexUtil.hexStringToLong(jsonObject.get("timestamp").getAsString()); this.merkleRoot = Hex.decode(jsonObject.get("merkleRoot").getAsString()); this.bodyLength = HexUtil.hexStringToLong(jsonObject.get("bodyLength").getAsString()); }
public static String toHexString(long number) { byte[] bytes = ByteUtil.longToBytes(number); return toHexString(bytes); }
public static long hexStringToLong(String data) { byte[] bytes = hexStringToBytes(data); return ByteUtil.byteArrayToLong(bytes); }
@Test public void isValidAddress() { assert HexUtil.isValidAddress(HexUtil.addressStringToBytes(VALID_ADDR)); }
@Test public void getAddressShortString() { assert HexUtil.getAddressShortString(HexUtil.addressStringToBytes(VALID_ADDR)) .equals("6c386a..."); }
/** * @param addr length should be 20 * @return short string represent 1f21c... */ public static String getAddressShortString(byte[] addr) { if (!isValidAddress(addr)) { throw new Error("not an address"); } String addrShort = Hex.toHexString(addr, 0, 3); return addrShort + "..."; }
@Test public void getHashListShort() { List<byte[]> hashList = java.util.Arrays.asList("a".getBytes(), "b".getBytes()); assert HexUtil.getHashListShort(hashList).equals(" 61...62"); } }
/** * Decodes a hex string to address bytes and checks validity * * @param hex - a hex string of the address, e.g., 6c386a4b26f73c802f34673f7248bb118f97424a * @return - decode and validated address byte[] */ public static byte[] addressStringToBytes(String hex) { final byte[] addr; try { addr = Hex.decode(hex); } catch (DecoderException addressIsNotValid) { return null; } if (isValidAddress(addr)) { return addr; } return null; }
public TransactionHeader(JsonObject jsonObject) { this.chain = Hex.decode(jsonObject.get("chain").getAsString()); this.version = Hex.decode(jsonObject.get("version").getAsString()); this.type = Hex.decode(jsonObject.get("type").getAsString()); this.timestamp = HexUtil.hexStringToLong(jsonObject.get("timestamp").getAsString()); this.bodyHash = Hex.decode(jsonObject.get("bodyHash").getAsString()); this.bodyLength = HexUtil.hexStringToLong(jsonObject.get("bodyLength").getAsString()); }
@Test public void testToHexString() { assertEquals("", HexUtil.toHexString(null)); }
byte[] actuals = HexUtil.hexStringToBytes(str); byte[] expected = new byte[] {0, 0}; assertArrayEquals(expected, actuals); byte[] actuals = HexUtil.hexStringToBytes(str); byte[] expected = new byte[] {0, 0}; assertArrayEquals(expected, actuals); byte[] actuals = HexUtil.hexStringToBytes(str); byte[] expected = new byte[] {69, -90}; assertArrayEquals(expected, actuals); byte[] actuals = HexUtil.hexStringToBytes(str); byte[] expected = Hex.decode(str); assertArrayEquals(expected, actuals); byte[] actuals = HexUtil.hexStringToBytes(str); byte[] expected = new byte[] {}; assertArrayEquals(expected, actuals); byte[] actuals = HexUtil.hexStringToBytes(str); byte[] expected = new byte[] {0}; assertArrayEquals(expected, actuals); byte[] actuals = HexUtil.hexStringToBytes(str); byte[] expected = new byte[] {0}; assertArrayEquals(expected, actuals); byte[] actuals = HexUtil.hexStringToBytes(str);
protected Branch(JsonObject json) { this.json = json; this.branchId = BranchId.of(json); this.name = json.get("name").getAsString(); this.symbol = json.get("symbol").getAsString(); this.property = json.get("property").getAsString(); String timestampHex = json.get("timestamp").getAsString(); this.timestamp = HexUtil.hexStringToLong(timestampHex); this.rawForSign = rawHashForSign(); this.signature = Sha3Hash.createByHashed(Hex.decode(json.get("signature").getAsString())); this.owner = Address.of(json.get("owner").getAsString()); this.contractId = ContractId.of(json.get("contractId").getAsString()); this.description = json.get("description").getAsString(); }
private static JsonObject getJsonObjectFromFile(String dir, String fileName) { String seedPath = String.format("classpath:/%s/%s", dir, fileName); Resource resource = new DefaultResourceLoader().getResource(seedPath); try (InputStream is = resource.getInputStream()) { Reader json = new InputStreamReader(is, StandardCharsets.UTF_8); JsonObject jsonObject = JsonUtil.parseJsonObject(json); if (!jsonObject.has("timestamp")) { long timestamp = TimeUtils.time(); jsonObject.addProperty("timestamp", HexUtil.toHexString(timestamp)); } return jsonObject; } catch (Exception e) { throw new NonExistObjectException(seedPath); } }
long bodyLength = HexUtil.hexStringToLong(header.get("bodyLength").getAsString()); long timestamp = HexUtil.hexStringToLong(header.get("timestamp").getAsString()); if (timestamp < System.currentTimeMillis() - (COMMAND_ACTIVE_TIME)) { log.error("Timestamp is not valid.");
long bodyLength = HexUtil.hexStringToLong(header.get("bodyLength").getAsString()); long timestamp = HexUtil.hexStringToLong(header.get("timestamp").getAsString()); if (timestamp < System.currentTimeMillis() - (COMMAND_ACTIVE_TIME)) { log.error("Timestamp is not valid.");
@Test public void defaultTest() { String name = "STEM"; String symbol = "STEM"; String property = "ecosystem"; String description = "The Basis of the YGGDRASH Ecosystem. " + "It is also an aggregate and a blockchain containing information " + "of all Branch Chains."; String contractId = "d399cd6d34288d04ba9e68ddfda9f5fe99dd778e"; String timestamp = "00000166c837f0c9"; JsonObject genesis = new JsonObject(); JsonObject json = ContractTestUtils.createBranchJson(name, symbol, property, description, contractId, timestamp, genesis); Branch branch = Branch.of(json); assertThat(branch.getName()).isEqualTo(name); assertThat(branch.getSymbol()).isEqualTo(symbol); assertThat(branch.getProperty()).isEqualTo(property); assertThat(branch.getDescription()).isEqualTo(description); assertThat(branch.getContractId().toString()).isEqualTo(contractId); assertThat(branch.getTimestamp()) .isEqualTo(HexUtil.hexStringToLong(timestamp)); assertThat(branch.getOwner().toString()).isEqualTo(TestConstants.wallet().getHexAddress()); assertThat(branch.verify()).isTrue(); }