From 965dc35cefb35d6d36e06f9f17df2dbf064ab037 Mon Sep 17 00:00:00 2001 From: sotatek-huyle3 Date: Mon, 23 Oct 2023 09:19:27 +0700 Subject: [PATCH 1/2] fix: #339 handle when asset name is invalid hex string --- .../bloxbean/cardano/client/transaction/spec/Asset.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/transaction-spec/src/main/java/com/bloxbean/cardano/client/transaction/spec/Asset.java b/transaction-spec/src/main/java/com/bloxbean/cardano/client/transaction/spec/Asset.java index 3fb86076..5c42e1dd 100644 --- a/transaction-spec/src/main/java/com/bloxbean/cardano/client/transaction/spec/Asset.java +++ b/transaction-spec/src/main/java/com/bloxbean/cardano/client/transaction/spec/Asset.java @@ -25,7 +25,12 @@ public byte[] getNameAsBytes() { if (name != null && !name.isEmpty()) { //Check if caller has provided a hex string as asset name if (name.startsWith("0x")) { - assetNameBytes = HexUtil.decodeHexString(name.substring(2)); + try { + assetNameBytes = HexUtil.decodeHexString(name.substring(2)); + } catch (IllegalArgumentException e) { + // name is not actually a hex string + assetNameBytes = name.getBytes(StandardCharsets.UTF_8); + } } else { assetNameBytes = name.getBytes(StandardCharsets.UTF_8); } From 5a1fc23a5c7b02290b1008c7c25356ed42c643ba Mon Sep 17 00:00:00 2001 From: sotatek-huyle3 Date: Mon, 23 Oct 2023 10:51:49 +0700 Subject: [PATCH 2/2] chore: add test for invalid hex asset name case --- .../cardano/client/transaction/spec/AssetSpecTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/transaction-spec/src/test/java/com/bloxbean/cardano/client/transaction/spec/AssetSpecTest.java b/transaction-spec/src/test/java/com/bloxbean/cardano/client/transaction/spec/AssetSpecTest.java index 98f5484b..374b6dba 100644 --- a/transaction-spec/src/test/java/com/bloxbean/cardano/client/transaction/spec/AssetSpecTest.java +++ b/transaction-spec/src/test/java/com/bloxbean/cardano/client/transaction/spec/AssetSpecTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import java.math.BigInteger; +import java.nio.charset.StandardCharsets; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -75,4 +76,12 @@ void notEqualsWhenDifferentValue() { assertThat(asset1).isNotEqualTo(asset2); } + + @Test + void invalidHexName() { + Asset asset = Asset.builder().name("0xtest").value(BigInteger.valueOf(700L)).build(); + + byte[] expectedBytes = "0xtest".getBytes(StandardCharsets.UTF_8); + assertThat(asset.getNameAsBytes()).isEqualTo(expectedBytes); + } }