Skip to content

Commit

Permalink
Merge pull request #340 from Sotatek-HuyLe3a/fix_339
Browse files Browse the repository at this point in the history
fix: #339 handle when asset name is invalid hex string
  • Loading branch information
satran004 authored Oct 26, 2023
2 parents 995dd55 + 5a1fc23 commit 00f69a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 00f69a0

Please sign in to comment.