From 9fbcc0b5cfb47bb28029f3bed44129aec4820245 Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:24:54 +0100 Subject: [PATCH 1/6] issues/349 - Refactor account from String to Key --- ...tractSerializedKeyTaggedHexDeserializer.java | 2 +- .../com/casper/sdk/model/entity/Account.java | 13 ++++++++++--- .../sdk/model/entity/AddressableEntity.java | 1 - src/main/java/com/casper/sdk/model/key/Key.java | 12 +++--------- .../com/casper/sdk/model/key/NamedKeyKey.java | 2 +- .../casper/sdk/service/CasperServiceTests.java | 17 +++++++++++------ 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/casper/sdk/jackson/deserializer/AbstractSerializedKeyTaggedHexDeserializer.java b/src/main/java/com/casper/sdk/jackson/deserializer/AbstractSerializedKeyTaggedHexDeserializer.java index 1f0eef55..7bf17833 100644 --- a/src/main/java/com/casper/sdk/jackson/deserializer/AbstractSerializedKeyTaggedHexDeserializer.java +++ b/src/main/java/com/casper/sdk/jackson/deserializer/AbstractSerializedKeyTaggedHexDeserializer.java @@ -38,7 +38,7 @@ public T deserialize(final JsonParser p, final DeserializationContext ctxt) thro if (strKey.contains("-")) { try { //noinspection unchecked - return (T) Key.fromKeyString(strKey); + return (T) Key.create(strKey); } catch (NoSuchKeyTagException e) { throw new CasperClientException("No such key: " + strKey, e); } diff --git a/src/main/java/com/casper/sdk/model/entity/Account.java b/src/main/java/com/casper/sdk/model/entity/Account.java index 329320ef..c020188e 100644 --- a/src/main/java/com/casper/sdk/model/entity/Account.java +++ b/src/main/java/com/casper/sdk/model/entity/Account.java @@ -1,6 +1,9 @@ package com.casper.sdk.model.entity; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.casper.sdk.exception.NoSuchKeyTagException; +import com.casper.sdk.model.key.Key; +import com.fasterxml.jackson.annotation.JsonTypeName; +import com.fasterxml.jackson.annotation.JsonValue; import lombok.*; /** @@ -13,9 +16,13 @@ @Builder @NoArgsConstructor @AllArgsConstructor +@JsonTypeName("Account") public class Account implements EntityAddressKind { - @JsonProperty("Account") - private String account; + @JsonValue + private Key account; + public Account(final String accountSt) throws NoSuchKeyTagException { + this.account = Key.create(accountSt); + } } diff --git a/src/main/java/com/casper/sdk/model/entity/AddressableEntity.java b/src/main/java/com/casper/sdk/model/entity/AddressableEntity.java index 38b9df0c..8386971b 100644 --- a/src/main/java/com/casper/sdk/model/entity/AddressableEntity.java +++ b/src/main/java/com/casper/sdk/model/entity/AddressableEntity.java @@ -30,5 +30,4 @@ public class AddressableEntity implements StateEntity { /** The entry points of the addressable entity. */ @JsonProperty("entry_points") private List entryPoints; - } diff --git a/src/main/java/com/casper/sdk/model/key/Key.java b/src/main/java/com/casper/sdk/model/key/Key.java index d04b33f1..9c733177 100644 --- a/src/main/java/com/casper/sdk/model/key/Key.java +++ b/src/main/java/com/casper/sdk/model/key/Key.java @@ -26,6 +26,7 @@ @EqualsAndHashCode(callSuper = true) public class Key extends AbstractSerializedKeyTaggedHex { + public static Key deserialize(final DeserializerBuffer deser) throws NoSuchKeyTagException { try { final KeyTag keyTag = KeyTag.getByTag(deser.readU8()); @@ -40,7 +41,8 @@ public static Key deserialize(final DeserializerBuffer deser) throws NoSuchKeyTa } } - public static Key fromKeyString(final String strKey) throws NoSuchKeyTagException { + @JsonCreator + public static Key create(final String strKey) throws NoSuchKeyTagException { final KeyTag keyTag = KeyTag.getByKeyName(strKey); try { final Key key = keyTag.getKeyClass().getDeclaredConstructor().newInstance(); @@ -67,13 +69,6 @@ public String generateAccountHash(final boolean includePrefix) throws IOExceptio return (includePrefix ? "account-hash-" : "") + ByteUtils.encodeHexString(Blake2b.digest(byteArrayOutputStream.toByteArray(), 32)); } - @JsonCreator - public void createKey(final String key) throws NoSuchKeyTagException { - Key obj = Key.fromTaggedHexString(key); - this.setTag(obj.getTag()); - this.setKey(obj.getKey()); - } - @Override public String toString() { return getTag().getKeyName() + ByteUtils.encodeHexString(this.getKey()); @@ -87,5 +82,4 @@ protected void fromStringCustom(final String strKey) { final String[] split = strKey.split("-"); this.setKey(ByteUtils.parseHexString(split[split.length - 1])); } - } diff --git a/src/main/java/com/casper/sdk/model/key/NamedKeyKey.java b/src/main/java/com/casper/sdk/model/key/NamedKeyKey.java index 596dd81b..1aa7c396 100644 --- a/src/main/java/com/casper/sdk/model/key/NamedKeyKey.java +++ b/src/main/java/com/casper/sdk/model/key/NamedKeyKey.java @@ -32,7 +32,7 @@ protected void fromStringCustom(final String strKey) { final String[] split = strKey.split("-"); try { final String baseAddrStr = split[2] + "-" + split[3] + "-" + split[4]; - baseAddr = (AddressableEntityKey) Key.fromKeyString(baseAddrStr); + baseAddr = (AddressableEntityKey) Key.create(baseAddrStr); stringBytes = Hex.decode(split[5]); refreshKey(); } catch (NoSuchKeyTagException e) { diff --git a/src/test/java/com/casper/sdk/service/CasperServiceTests.java b/src/test/java/com/casper/sdk/service/CasperServiceTests.java index cf0d7705..af27145f 100644 --- a/src/test/java/com/casper/sdk/service/CasperServiceTests.java +++ b/src/test/java/com/casper/sdk/service/CasperServiceTests.java @@ -2,6 +2,7 @@ import com.casper.sdk.exception.CasperClientException; import com.casper.sdk.exception.DynamicInstanceException; +import com.casper.sdk.exception.NoSuchKeyTagException; import com.casper.sdk.helper.TransactionHelper; import com.casper.sdk.identifier.block.HashBlockIdentifier; import com.casper.sdk.identifier.block.HeightBlockIdentifier; @@ -41,6 +42,7 @@ import com.casper.sdk.model.era.EraInfoData; import com.casper.sdk.model.globalstate.GlobalStateData; import com.casper.sdk.model.key.AlgorithmTag; +import com.casper.sdk.model.key.Key; import com.casper.sdk.model.key.PublicKey; import com.casper.sdk.model.peer.PeerData; import com.casper.sdk.model.reward.GetRewardResult; @@ -650,7 +652,7 @@ void infoGetTransactionByDeployHash() throws NoSuchAlgorithmException, IOExcepti @Test - void infoGetContractTransactionByHash() throws NoSuchAlgorithmException, IOException, DynamicInstanceException { + void infoGetContractTransactionByHash() throws NoSuchAlgorithmException { mockNode.withRcpResponseDispatcher().withMethod("info_get_transaction").withBody("$.params.transaction_hash.Deploy", "9a35bd593202c587ec21fabb1caa44e5e40284a5228c50d35f78585b3e90279b").thenDispatch(getClass().getResource("/transaction-samples/info_get_contract_transaction.json")); @@ -736,9 +738,11 @@ void queryBalanceDetails() throws IOException, DynamicInstanceException { } @Test - void stateGetEntityAccount() throws NoSuchAlgorithmException { + void stateGetEntityAccount() throws NoSuchAlgorithmException, NoSuchKeyTagException { - mockNode.withRcpResponseDispatcher().withMethod("state_get_entity").withBody("$.params.entity_identifier.PublicKey", "0138329930033bca4773a6623574ad7870ee39c554f153f15609e200e50049a7de").thenDispatch(getClass().getResource("/entity/getstateentity-account-result.json")); + mockNode.withRcpResponseDispatcher().withMethod("state_get_entity") + .withBody("$.params.entity_identifier.PublicKey", "0138329930033bca4773a6623574ad7870ee39c554f153f15609e200e50049a7de") + .thenDispatch(getClass().getResource("/entity/getstateentity-account-result.json")); final StateEntityResult stateEntityResult = casperServiceMock.getStateEntity(new PublicKeyIdentifier(PublicKey.fromTaggedHexString("0138329930033bca4773a6623574ad7870ee39c554f153f15609e200e50049a7de")), null); @@ -754,7 +758,8 @@ void stateGetEntityAccount() throws NoSuchAlgorithmException { assertThat(entity.getEntity().getPackageHash(), is("package-22138fb22c624016fd73cd2abd3285ccd27cb80e20c29f3d94d34e95ee182030")); Account account = (Account) entity.getEntity().getEntityAddressKind(); - assertThat(account.getAccount(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); + + assertThat(account.getAccount(), is(Key.create("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10"))); assertThat(entity.getEntity().getMainPurse().getJsonURef(), is("uref-3dfdbde34845bd2e731cf39fba450745eba645b75d5feb3dcf953fd06d69bf14-007")); assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); @@ -899,7 +904,7 @@ void stateGetEntityLegacyAccount() { } @Test - void stateGetEntityAccountWithHashBlockIdentifier() { + void stateGetEntityAccountWithHashBlockIdentifier() throws NoSuchKeyTagException { mockNode.withRcpResponseDispatcher().withMethod("state_get_entity").withBody("$.params.entity_identifier.AccountHash", "account-hash-f1075fce3b8cd4eab748b8705ca02444a5e35c0248662649013d8a5cb2b1a87c").withBody("$.params.block_identifier.Hash", "b0b28b6e89522a2c9476d477208f603cb9911dae77dad61da66346d56764ee8b").thenDispatch(getClass().getResource("/entity/getstateentity-account-result.json")); @@ -918,7 +923,7 @@ void stateGetEntityAccountWithHashBlockIdentifier() { assertThat(entity.getEntity().getPackageHash(), is("package-22138fb22c624016fd73cd2abd3285ccd27cb80e20c29f3d94d34e95ee182030")); Account account = (Account) entity.getEntity().getEntityAddressKind(); - assertThat(account.getAccount(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); + assertThat(account.getAccount(), is(Key.create("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10"))); assertThat(entity.getEntity().getMainPurse().getJsonURef(), is("uref-3dfdbde34845bd2e731cf39fba450745eba645b75d5feb3dcf953fd06d69bf14-007")); assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); From dafa8a7150b11a2ca1648e9a619b8e5959e489d3 Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:02:14 +0100 Subject: [PATCH 2/6] issues/349 - Refactor Operation key from String to Key class --- .../sdk/jackson/serializer/KeySerializer.java | 20 +++++++++++++++++++ .../casper/sdk/model/deploy/Operation.java | 3 ++- .../casper/sdk/model/entity/EntityAddr.java | 9 ++++----- .../java/com/casper/sdk/model/key/Key.java | 4 +++- 4 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/casper/sdk/jackson/serializer/KeySerializer.java diff --git a/src/main/java/com/casper/sdk/jackson/serializer/KeySerializer.java b/src/main/java/com/casper/sdk/jackson/serializer/KeySerializer.java new file mode 100644 index 00000000..992582b0 --- /dev/null +++ b/src/main/java/com/casper/sdk/jackson/serializer/KeySerializer.java @@ -0,0 +1,20 @@ +package com.casper.sdk.jackson.serializer; + +import com.casper.sdk.model.key.Key; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.IOException; + +/** + * Desrializer for {@link Key} types. + * + * @author ian@meywood.com + */ +public class KeySerializer extends JsonSerializer { + @Override + public void serialize(final Key key, final JsonGenerator gen, final SerializerProvider serializers) throws IOException { + gen.writeString(key.toString()); + } +} diff --git a/src/main/java/com/casper/sdk/model/deploy/Operation.java b/src/main/java/com/casper/sdk/model/deploy/Operation.java index 74243c2f..40dcfc02 100644 --- a/src/main/java/com/casper/sdk/model/deploy/Operation.java +++ b/src/main/java/com/casper/sdk/model/deploy/Operation.java @@ -1,5 +1,6 @@ package com.casper.sdk.model.deploy; +import com.casper.sdk.model.key.Key; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -23,7 +24,7 @@ public class Operation { /** * The formatted string of the `Key` */ - private String key; + private Key key; /** * @see OpKind diff --git a/src/main/java/com/casper/sdk/model/entity/EntityAddr.java b/src/main/java/com/casper/sdk/model/entity/EntityAddr.java index a52b7232..f139b147 100644 --- a/src/main/java/com/casper/sdk/model/entity/EntityAddr.java +++ b/src/main/java/com/casper/sdk/model/entity/EntityAddr.java @@ -1,7 +1,6 @@ package com.casper.sdk.model.entity; import com.casper.sdk.exception.NoSuchKeyTagException; -import com.casper.sdk.model.key.KeyTag; import com.casper.sdk.model.key.Tag; import lombok.AllArgsConstructor; import lombok.Getter; @@ -34,10 +33,10 @@ public static EntityAddr getByTag(byte tag) throws NoSuchKeyTagException { public static EntityAddr getByKeyName(final String keyName) throws NoSuchKeyTagException { // Search in reverse order to get the most specific key eg 'bid-addr-' and 'bid-' - for (final EntityAddr entityAddr: values()) { - if (entityAddr.getKeyName().equals(keyName)) { - return entityAddr; - } + for (final EntityAddr entityAddr : values()) { + if (entityAddr.getKeyName().equals(keyName)) { + return entityAddr; + } } throw new NoSuchKeyTagException("No such key name: " + keyName); } diff --git a/src/main/java/com/casper/sdk/model/key/Key.java b/src/main/java/com/casper/sdk/model/key/Key.java index 9c733177..98f9b8dc 100644 --- a/src/main/java/com/casper/sdk/model/key/Key.java +++ b/src/main/java/com/casper/sdk/model/key/Key.java @@ -2,8 +2,10 @@ import com.casper.sdk.exception.NoSuchKeyTagException; import com.casper.sdk.jackson.deserializer.KeyDeserializer; +import com.casper.sdk.jackson.serializer.KeySerializer; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.syntifi.crypto.key.hash.Blake2b; import dev.oak3.sbs4j.DeserializerBuffer; import dev.oak3.sbs4j.util.ByteUtils; @@ -22,11 +24,11 @@ * @since 0.0.1 */ @JsonDeserialize(using = KeyDeserializer.class) +@JsonSerialize(using = KeySerializer.class) @NoArgsConstructor @EqualsAndHashCode(callSuper = true) public class Key extends AbstractSerializedKeyTaggedHex { - public static Key deserialize(final DeserializerBuffer deser) throws NoSuchKeyTagException { try { final KeyTag keyTag = KeyTag.getByTag(deser.readU8()); From 19cc77ec142bc452ac5943c9dce9a90ba5cae08e Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Mon, 23 Sep 2024 13:22:31 +0100 Subject: [PATCH 3/6] issues/349 - Added key serializer --- .../com/casper/sdk/model/key/KeyTest.java | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/test/java/com/casper/sdk/model/key/KeyTest.java b/src/test/java/com/casper/sdk/model/key/KeyTest.java index bd872786..7fd234cd 100644 --- a/src/test/java/com/casper/sdk/model/key/KeyTest.java +++ b/src/test/java/com/casper/sdk/model/key/KeyTest.java @@ -41,7 +41,7 @@ void hashKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.HASH)); assertThat(key.getKey(), is(Hex.decode("0202020202020202020202020202020202020202020202020202020202020202"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("010202020202020202020202020202020202020202020202020202020202020202")); } @@ -53,7 +53,7 @@ void urefKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(URefKey.class))); assertThat(key.getTag(), is(KeyTag.UREF)); assertThat(key.getKey(), is(Hex.decode("030303030303030303030303030303030303030303030303030303030303030301"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("02030303030303030303030303030303030303030303030303030303030303030301")); assertThat(((URefKey) key).getURef(), is(URef.fromString("uref-0303030303030303030303030303030303030303030303030303030303030303-001"))); @@ -71,7 +71,7 @@ void transferKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.TRANSFER)); assertThat(key.getKey(), is(Hex.decode("0404040404040404040404040404040404040404040404040404040404040404"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("030404040404040404040404040404040404040404040404040404040404040404")); } @@ -83,7 +83,7 @@ void deployKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.DEPLOY_INFO)); assertThat(key.getKey(), is(Hex.decode("0505050505050505050505050505050505050505050505050505050505050505"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("040505050505050505050505050505050505050505050505050505050505050505")); } @@ -95,7 +95,7 @@ void eraInfoKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(EraInfoKey.class))); assertThat(key.getTag(), is(KeyTag.ERA_INFO)); assertThat(key.getKey(), is(Hex.decode("3930000000000000"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("053930000000000000")); } @@ -107,7 +107,7 @@ void balanceKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.BALANCE)); assertThat(key.getKey(), is(Hex.decode("0707070707070707070707070707070707070707070707070707070707070707"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("060707070707070707070707070707070707070707070707070707070707070707")); } @@ -119,7 +119,7 @@ void bidKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.BID)); assertThat(key.getKey(), is(Hex.decode("0808080808080808080808080808080808080808080808080808080808080808"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("070808080808080808080808080808080808080808080808080808080808080808")); } @@ -131,7 +131,7 @@ void withdrawKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.WITHDRAW)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("080909090909090909090909090909090909090909090909090909090909090909")); } @@ -143,7 +143,7 @@ void dictionaryKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.DICTIONARY)); assertThat(key.getKey(), is(Hex.decode("1010101010101010101010101010101010101010101010101010101010101010"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("091010101010101010101010101010101010101010101010101010101010101010")); } @@ -155,7 +155,7 @@ void systemContractRegistryKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.SYSTEM_ENTITY_REGISTRY)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0a0909090909090909090909090909090909090909090909090909090909090909")); } @@ -167,7 +167,7 @@ void eraSummaryKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.ERA_SUMMARY)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0b0909090909090909090909090909090909090909090909090909090909090909")); } @@ -179,7 +179,7 @@ void unbondKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.UNBOND)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0c0909090909090909090909090909090909090909090909090909090909090909")); } @@ -191,7 +191,7 @@ void chainspecRegistryKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.CHAINSPEC_REGISTRY)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0d0909090909090909090909090909090909090909090909090909090909090909")); } @@ -203,7 +203,7 @@ void checksumRegistryKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.CHECKSUM_REGISTRY)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0e0909090909090909090909090909090909090909090909090909090909090909")); } @@ -215,7 +215,7 @@ void contractPackageKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.HASH)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is("hash-0909090909090909090909090909090909090909090909090909090909090909")); + assertThat(toJson(key), is("hash-0909090909090909090909090909090909090909090909090909090909090909")); assertThat(key.getAlgoTaggedHex(), is("010909090909090909090909090909090909090909090909090909090909090909")); } @@ -227,7 +227,7 @@ void contractWasmKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.HASH)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is("hash-0909090909090909090909090909090909090909090909090909090909090909")); + assertThat(toJson(key), is("hash-0909090909090909090909090909090909090909090909090909090909090909")); assertThat(key.getAlgoTaggedHex(), is("010909090909090909090909090909090909090909090909090909090909090909")); } @@ -239,7 +239,7 @@ void contractKeyFromKeyString() throws Exception { assertThat(key, is(notNullValue(Key.class))); assertThat(key.getTag(), is(KeyTag.HASH)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is("hash-0909090909090909090909090909090909090909090909090909090909090909")); + assertThat(toJson(key), is("hash-0909090909090909090909090909090909090909090909090909090909090909")); assertThat(key.getAlgoTaggedHex(), is("010909090909090909090909090909090909090909090909090909090909090909")); } @@ -251,7 +251,7 @@ void bidAddrKeyUnifiedFromKeyString() throws Exception { assertThat(key, is(instanceOf(BidAddrKey.class))); assertThat(key.getTag(), is(KeyTag.BID_ADDR)); assertThat(key.getKey(), is(Hex.decode("002f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0f002f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309")); assertThat(((BidAddrKey) key).getBidAddr(), is(BidAddr.UNIFIED)); } @@ -264,7 +264,7 @@ void bidAddrKeyValidatorFromKeyString() throws Exception { assertThat(key, is(instanceOf(BidAddrKey.class))); assertThat(key.getTag(), is(KeyTag.BID_ADDR)); assertThat(key.getKey(), is(Hex.decode("012f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0f012f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c458007309")); assertThat(((BidAddrKey) key).getBidAddr(), is(BidAddr.VALIDATOR)); } @@ -277,7 +277,7 @@ void bidAddrKeyDelegatorFromKeyString() throws Exception { assertThat(key, is(instanceOf(BidAddrKey.class))); assertThat(key.getTag(), is(KeyTag.BID_ADDR)); assertThat(key.getKey(), is(Hex.decode("022f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c4580073099fa1fc0808d3a5b9ea9f3af4ca7c8c3655568fdf378d8afdf8a7e56e58abbfd4"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0f022f3fb80d362ad0a922f446915a259c9aaec9ba99292b3e50ff2359c4580073099fa1fc0808d3a5b9ea9f3af4ca7c8c3655568fdf378d8afdf8a7e56e58abbfd4")); assertThat(((BidAddrKey) key).getBidAddr(), is(BidAddr.DELEGATOR)); } @@ -290,7 +290,7 @@ void bidAddrKeyCreditFromKeyString() throws Exception { assertThat(key, is(instanceOf(BidAddrKey.class))); assertThat(key.getTag(), is(KeyTag.BID_ADDR)); assertThat(key.getKey(), is(Hex.decode("04520037cd249ccbcfeb0b9feae07d8d4f7d922cf88adc4f3e8691f9d34ccc8d097f00000000000000"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("0f04520037cd249ccbcfeb0b9feae07d8d4f7d922cf88adc4f3e8691f9d34ccc8d097f00000000000000")); assertThat(((BidAddrKey) key).getBidAddr(), is(BidAddr.CREDIT)); } @@ -303,7 +303,7 @@ void packageKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(Key.class))); assertThat(key.getTag(), is(KeyTag.PACKAGE)); assertThat(key.getKey(), is(Hex.decode("0909090909090909090909090909090909090909090909090909090909090909"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("100909090909090909090909090909090909090909090909090909090909090909")); } @@ -315,7 +315,7 @@ void addressableEntitySystemKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(AddressableEntityKey.class))); assertThat(key.getTag(), is(KeyTag.ADDRESSABLE_ENTITY)); assertThat(key.getKey(), is(Hex.decode("000101010101010101010101010101010101010101010101010101010101010101"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("11000101010101010101010101010101010101010101010101010101010101010101")); assertThat(((AddressableEntityKey) key).getEntityAddressTag(), is(EntityAddr.SYSTEM)); } @@ -328,7 +328,7 @@ void addressableEntityAccountKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(AddressableEntityKey.class))); assertThat(key.getTag(), is(KeyTag.ADDRESSABLE_ENTITY)); assertThat(key.getKey(), is(Hex.decode("010101010101010101010101010101010101010101010101010101010101010101"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("11010101010101010101010101010101010101010101010101010101010101010101")); assertThat(((AddressableEntityKey) key).getEntityAddressTag(), is(EntityAddr.ACCOUNT)); } @@ -341,7 +341,7 @@ void addressableEntityContractKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(AddressableEntityKey.class))); assertThat(key.getTag(), is(KeyTag.ADDRESSABLE_ENTITY)); assertThat(key.getKey(), is(Hex.decode("020101010101010101010101010101010101010101010101010101010101010101"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("11020101010101010101010101010101010101010101010101010101010101010101")); assertThat(((AddressableEntityKey) key).getEntityAddressTag(), is(EntityAddr.SMART_CONTRACT)); } @@ -354,7 +354,7 @@ void byteCodeEmptyKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(ByteCodeKey.class))); assertThat(key.getTag(), is(KeyTag.BYTE_CODE)); assertThat(key.getKey(), is(Hex.decode("010101010101010101010101010101010101010101010101010101010101010101"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("12010101010101010101010101010101010101010101010101010101010101010101")); assertThat(((ByteCodeKey) key).getByteCodeAddr(), is(ByteCodeAddr.EMPTY)); } @@ -367,7 +367,7 @@ void byteCodeV1WasmKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(ByteCodeKey.class))); assertThat(key.getTag(), is(KeyTag.BYTE_CODE)); assertThat(key.getKey(), is(Hex.decode("000101010101010101010101010101010101010101010101010101010101010101"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("12000101010101010101010101010101010101010101010101010101010101010101")); assertThat(((ByteCodeKey) key).getByteCodeAddr(), is(ByteCodeAddr.V1_CASPER_WASM)); } @@ -380,7 +380,7 @@ void messageTopicKeyFromKeyString() throws Exception { assertThat(key, is(instanceOf(MessageKey.class))); assertThat(key.getTag(), is(KeyTag.MESSAGE)); assertThat(key.getKey(), is(Hex.decode("0255d4a6915291da12afded37fa5bc01f0803a2f0faf6acb7ec4c7ca6ab76f33305721a6d9d7a9afe5dfdb35276fb823bed0f825350e4d865a5ec0110c380de4e100"))); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("130255d4a6915291da12afded37fa5bc01f0803a2f0faf6acb7ec4c7ca6ab76f33305721a6d9d7a9afe5dfdb35276fb823bed0f825350e4d865a5ec0110c380de4e100")); assertThat(((MessageKey) key).getEntityAddrHash(), is(new Digest("55d4a6915291da12afded37fa5bc01f0803a2f0faf6acb7ec4c7ca6ab76f3330"))); assertThat(((MessageKey) key).getTopicHash(), is(new Digest("5721a6d9d7a9afe5dfdb35276fb823bed0f825350e4d865a5ec0110c380de4e1"))); @@ -394,7 +394,7 @@ void messageIndexKeyFromKeyString() throws Exception { final Key key = fromJson(strKey); assertThat(key, is(instanceOf(MessageKey.class))); assertThat(key.getTag(), is(KeyTag.MESSAGE)); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("130255d4a6915291da12afded37fa5bc01f0803a2f0faf6acb7ec4c7ca6ab76f33305721a6d9d7a9afe5dfdb35276fb823bed0f825350e4d865a5ec0110c380de4e1010f000000")); assertThat(((MessageKey) key).getEntityAddrHash(), is(new Digest("55d4a6915291da12afded37fa5bc01f0803a2f0faf6acb7ec4c7ca6ab76f3330"))); assertThat(((MessageKey) key).getTopicHash(), is(new Digest("5721a6d9d7a9afe5dfdb35276fb823bed0f825350e4d865a5ec0110c380de4e1"))); @@ -410,7 +410,7 @@ void namedKeyFromKeyString() throws Exception { final Key key = fromJson(strKey); assertThat(key, is(instanceOf(NamedKeyKey.class))); assertThat(key.getTag(), is(KeyTag.NAMED_KEY)); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("14000001010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202")); assertThat(((NamedKeyKey) key).getBaseAddr().getKey(), is(Hex.decode("000101010101010101010101010101010101010101010101010101010101010101"))); assertThat(((NamedKeyKey) key).getStringBytes(), is(Hex.decode("0202020202020202020202020202020202020202020202020202020202020202"))); @@ -424,7 +424,7 @@ void blockTimeKeyFromKeyString() throws Exception { final Key key = fromJson(strKey); assertThat(key, is(instanceOf(BlockGlobalKey.class))); assertThat(key.getTag(), is(KeyTag.BLOCK_GLOBAL)); - assertThat(key.toString(), is("block-time-0909090909090909090909090909090909090909090909090909090909090909")); + assertThat(toJson(key), is("block-time-0909090909090909090909090909090909090909090909090909090909090909")); assertThat(key.getAlgoTaggedHex(), is("15000909090909090909090909090909090909090909090909090909090909090909")); assertThat(((BlockGlobalKey) key).getBlockGlobalAddr(), is(BlockGlobalAddr.BLOCK_TIME)); assertThat(key.getKey(), is(Hex.decode("000909090909090909090909090909090909090909090909090909090909090909"))); @@ -437,7 +437,7 @@ void blockMessageCountKeyFromKeyString() throws Exception { final Key key = fromJson(strKey); assertThat(key, is(instanceOf(BlockGlobalKey.class))); assertThat(key.getTag(), is(KeyTag.BLOCK_GLOBAL)); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("15010909090909090909090909090909090909090909090909090909090909090909")); assertThat(((BlockGlobalKey) key).getBlockGlobalAddr(), is(BlockGlobalAddr.MESSAGE_COUNT)); assertThat(key.getKey(), is(Hex.decode("010909090909090909090909090909090909090909090909090909090909090909"))); @@ -450,7 +450,7 @@ void entryPointKeyFromKeyString() throws Exception { final Key key = fromJson(strKey); assertThat(key, is(instanceOf(EntryPointKey.class))); assertThat(key.getTag(), is(KeyTag.ENTRY_POINT)); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("1701022a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a01000000")); assertThat(((EntryPointKey) key).getEntryPointAddr(), is(EntryPointAddr.VM_CASPER_V2)); assertThat(((EntryPointKey) key).getHashAddr(), is(Hex.decode("2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a"))); @@ -465,7 +465,7 @@ void balanceHoldKeyFromKeyString() throws Exception { final Key key = fromJson(strKey); assertThat(key, is(instanceOf(BalanceHoldKey.class))); assertThat(key.getTag(), is(KeyTag.BALANCE_HOLD)); - assertThat(key.toString(), is(strKey)); + assertThat(toJson(key), is(strKey)); assertThat(key.getAlgoTaggedHex(), is("160000000000000000000000000000000000000000000000000000000000000000000000000000000000")); assertThat(((BalanceHoldKey) key).getBalanceHoldAddr(), is(BalanceHoldAddr.GAS)); assertThat(key.getKey(), is(Hex.decode("0000000000000000000000000000000000000000000000000000000000000000000000000000000000"))); @@ -481,5 +481,9 @@ private Key fromJson(final String strKey) throws JsonProcessingException { //noinspection VulnerableCodeUsages return new ObjectMapper().readValue(strKey != null ? "\"" + strKey + "\"" : "null", Key.class); } + + private String toJson(final Key key) throws JsonProcessingException { + return new ObjectMapper().writeValueAsString(key).replace("\"", ""); + } } From b86eed8bdd9d018bab7c3722c20b53c35376841e Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:48:55 +0100 Subject: [PATCH 4/6] issues/349 - Added AccountHashKey --- .../sdk/model/account/AssociatedKey.java | 3 ++- .../casper/sdk/model/key/AccountHashKey.java | 9 +++++++ .../java/com/casper/sdk/model/key/KeyTag.java | 2 +- .../sdk/model/transaction/EffectTest.java | 4 ++- .../sdk/service/CasperServiceTests.java | 27 +++++++++++-------- 5 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/casper/sdk/model/key/AccountHashKey.java diff --git a/src/main/java/com/casper/sdk/model/account/AssociatedKey.java b/src/main/java/com/casper/sdk/model/account/AssociatedKey.java index 31090eaa..ceef21c5 100644 --- a/src/main/java/com/casper/sdk/model/account/AssociatedKey.java +++ b/src/main/java/com/casper/sdk/model/account/AssociatedKey.java @@ -1,5 +1,6 @@ package com.casper.sdk.model.account; +import com.casper.sdk.model.key.AccountHashKey; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; import lombok.Builder; @@ -25,7 +26,7 @@ public class AssociatedKey { * account_hash(String) Hex-encoded account hash. */ @JsonProperty("account_hash") - private String accountHash; + private AccountHashKey accountHash; /** * weight(Integer) diff --git a/src/main/java/com/casper/sdk/model/key/AccountHashKey.java b/src/main/java/com/casper/sdk/model/key/AccountHashKey.java new file mode 100644 index 00000000..d42dbe41 --- /dev/null +++ b/src/main/java/com/casper/sdk/model/key/AccountHashKey.java @@ -0,0 +1,9 @@ +package com.casper.sdk.model.key; + +/** + * A key for an account hash + * + * @author ian@meywood.com + */ +public class AccountHashKey extends Key { +} diff --git a/src/main/java/com/casper/sdk/model/key/KeyTag.java b/src/main/java/com/casper/sdk/model/key/KeyTag.java index fffccec0..c08e4e94 100644 --- a/src/main/java/com/casper/sdk/model/key/KeyTag.java +++ b/src/main/java/com/casper/sdk/model/key/KeyTag.java @@ -17,7 +17,7 @@ */ @Getter public enum KeyTag implements Tag { - ACCOUNT((byte) 0x00, Key.class, "account-hash-"), + ACCOUNT((byte) 0x00, AccountHashKey.class, "account-hash-"), HASH((byte) 0x01, Key.class, "hash-", "contract-"), UREF((byte) 0x02, URefKey.class, "uref-"), TRANSFER((byte) 0x03, Key.class, "transfer-"), diff --git a/src/test/java/com/casper/sdk/model/transaction/EffectTest.java b/src/test/java/com/casper/sdk/model/transaction/EffectTest.java index 65c732c8..bdc9f989 100644 --- a/src/test/java/com/casper/sdk/model/transaction/EffectTest.java +++ b/src/test/java/com/casper/sdk/model/transaction/EffectTest.java @@ -14,6 +14,7 @@ import com.casper.sdk.model.entity.contract.ByteCode; import com.casper.sdk.model.entity.contract.NamedKey; import com.casper.sdk.model.entity.contract.Package; +import com.casper.sdk.model.key.AccountHashKey; import com.casper.sdk.model.key.PublicKey; import com.casper.sdk.model.transaction.execution.Effect; import com.casper.sdk.model.transaction.kind.*; @@ -470,7 +471,8 @@ void writeKindAddressableEntity() throws JsonProcessingException { assertThat(entity.getPackageHash(), is("package-ef39f3794dfde8641acc43a8f63d4c0a72a4b33bbb2e4eed29421ee6cfd0d87e")); assertThat(entity.getByteCodeHash(), is("byte-code-376c7a7483df3ed53e8fb112c256bb0a99782d4f0260e5608858740a36681ac3")); assertThat(Hex.encode(entity.getMainPurse().getAddress()), is("faa9c882c9721274290109abba23f1baa8d7603debc11bf26dfc6250a6f56cc2")); - assertThat(entity.getAssociatedKeys().get(0).getAccountHash(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); + assertThat(entity.getAssociatedKeys().get(0).getAccountHash(), is(instanceOf(AccountHashKey.class))); + assertThat(entity.getAssociatedKeys().get(0).getAccountHash().toString(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); assertThat(entity.getAssociatedKeys().get(0).getWeight(), is(1)); assertThat(entity.getActionThresholds().getDeployment(), is(1)); assertThat(entity.getActionThresholds().getUpgradeManagement(), is(1)); diff --git a/src/test/java/com/casper/sdk/service/CasperServiceTests.java b/src/test/java/com/casper/sdk/service/CasperServiceTests.java index af27145f..71f23df5 100644 --- a/src/test/java/com/casper/sdk/service/CasperServiceTests.java +++ b/src/test/java/com/casper/sdk/service/CasperServiceTests.java @@ -41,6 +41,7 @@ import com.casper.sdk.model.era.EraEndV2; import com.casper.sdk.model.era.EraInfoData; import com.casper.sdk.model.globalstate.GlobalStateData; +import com.casper.sdk.model.key.AccountHashKey; import com.casper.sdk.model.key.AlgorithmTag; import com.casper.sdk.model.key.Key; import com.casper.sdk.model.key.PublicKey; @@ -665,7 +666,7 @@ void infoGetContractTransactionByHash() throws NoSuchAlgorithmException { assertThat(transaction.getHeader().getBodyHash(), is(new Digest("9bcc99c4d493764463c278cf17b3e1ff5b1357d1f5d0676b3fbeaafb260bcb76"))); assertThat(transaction.getHeader().getPricingMode(), is(instanceOf(FixedPricingMode.class))); - assertThat(((FixedPricingMode)transaction.getHeader().getPricingMode()).getGasPriceTolerance(), is(8)); + assertThat(((FixedPricingMode) transaction.getHeader().getPricingMode()).getGasPriceTolerance(), is(8)); assertThat(transaction.getHeader().getInitiatorAddr(), is(instanceOf(InitiatorPublicKey.class))); assertThat(transaction.getHeader().getInitiatorAddr().getAddress(), is(PublicKey.fromTaggedHexString("0138329930033bca4773a6623574ad7870ee39c554f153f15609e200e50049a7de"))); @@ -685,7 +686,7 @@ void infoGetContractTransactionByHash() throws NoSuchAlgorithmException { assertThat(executionResult.getConsumed(), is(new BigInteger("225932824299"))); assertThat(executionResult.getSizeEstimate(), is(325997L)); - assertThat(((Session)transaction.getBody().getTarget()).getModuleBytes().length, is(325614) ); + assertThat(((Session) transaction.getBody().getTarget()).getModuleBytes().length, is(325614)); assertThat(executionResult.getEffects().size(), is(66)); //Effects are tested in EffectTest @@ -762,7 +763,8 @@ void stateGetEntityAccount() throws NoSuchAlgorithmException, NoSuchKeyTagExcept assertThat(account.getAccount(), is(Key.create("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10"))); assertThat(entity.getEntity().getMainPurse().getJsonURef(), is("uref-3dfdbde34845bd2e731cf39fba450745eba645b75d5feb3dcf953fd06d69bf14-007")); - assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); + assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is(instanceOf(AccountHashKey.class))); + assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is(Key.create("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10"))); assertThat(entity.getEntity().getAssociatedKeys().get(0).getWeight(), is(1)); assertThat(entity.getEntity().getActionThresholds().getDeployment(), is(1)); assertThat(entity.getEntity().getActionThresholds().getKeyManagement(), is(1)); @@ -792,7 +794,7 @@ void stateGetEntitySmartContract() { assertThat(smartContract.getSmartContract().name(), is((SmartContract.TransactionRuntime.VMCASPERV1.name()))); assertThat(entity.getEntity().getMainPurse().getJsonURef(), is("uref-3dfdbde34845bd2e731cf39fba450745eba645b75d5feb3dcf953fd06d69bf14-007")); - assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); + assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash().toString(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); assertThat(entity.getEntity().getAssociatedKeys().get(0).getWeight(), is(1)); assertThat(entity.getEntity().getActionThresholds().getDeployment(), is(1)); assertThat(entity.getEntity().getActionThresholds().getKeyManagement(), is(1)); @@ -828,7 +830,7 @@ void stateGetEntitySystemEntryPointV1() { assertThat(entity.getEntryPoints().size(), is(12)); assertInstanceOf(EntryPointValue.class, entity.getEntryPoints().get(0)); - EntryPoint entryPointV1 = entity.getEntryPoints().get(0).getV1(); + EntryPoint entryPointV1 = entity.getEntryPoints().get(0).getV1(); assertThat(entryPointV1.getAccess().getValue().toString(), is(EntryPoint.EntryPointAccessEnum.PUBLIC.name())); @@ -898,7 +900,8 @@ void stateGetEntityLegacyAccount() { assertThat(account.getHash(), is("account-hash-f1075fce3b8cd4eab748b8705ca02444a5e35c0248662649013d8a5cb2b1a87c")); assertThat(account.getMainPurse(), is("uref-b22bc80d357df47447074e243b4d888de67c1cc7565fa82d0bb2b9b023146748-007")); - assertThat(account.getAssociatedKeys().get(0).getAccountHash(), is("account-hash-f1075fce3b8cd4eab748b8705ca02444a5e35c0248662649013d8a5cb2b1a87c")); + assertThat(account.getAssociatedKeys().get(0).getAccountHash(), is(instanceOf(AccountHashKey.class))); + assertThat(account.getAssociatedKeys().get(0).getAccountHash().toString(), is("account-hash-f1075fce3b8cd4eab748b8705ca02444a5e35c0248662649013d8a5cb2b1a87c")); assertThat(account.getAssociatedKeys().get(0).getWeight(), is(1)); } @@ -926,18 +929,20 @@ void stateGetEntityAccountWithHashBlockIdentifier() throws NoSuchKeyTagException assertThat(account.getAccount(), is(Key.create("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10"))); assertThat(entity.getEntity().getMainPurse().getJsonURef(), is("uref-3dfdbde34845bd2e731cf39fba450745eba645b75d5feb3dcf953fd06d69bf14-007")); - assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); + assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash(), is(instanceOf(AccountHashKey.class))); + assertThat(entity.getEntity().getAssociatedKeys().get(0).getAccountHash().toString(), is("account-hash-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); assertThat(entity.getEntity().getAssociatedKeys().get(0).getWeight(), is(1)); assertThat(entity.getEntity().getActionThresholds().getDeployment(), is(1)); assertThat(entity.getEntity().getActionThresholds().getKeyManagement(), is(1)); assertThat(entity.getEntity().getActionThresholds().getUpgradeManagement(), is(1)); } + @Test - void stateGetEntityContractActualCCTLReturnedData() { + void stateGetEntityCiontractActualCCTLReturnedData() { mockNode.withRcpResponseDispatcher().withMethod("state_get_entity").withBody("$.params.entity_identifier.EntityAddr", "entity-contract-77a0481b28572054cbdd19c944a7176ce9670be616d6de0e2ec3f89ca378dd79").thenDispatch(getClass().getResource("/entity/getstateentity-cctl-returned-contract-entity.json")); - final StateEntityResult stateEntityResult = casperServiceMock.getStateEntity(new EntityAddrIdentifier("entity-contract-77a0481b28572054cbdd19c944a7176ce9670be616d6de0e2ec3f89ca378dd79"),null); + final StateEntityResult stateEntityResult = casperServiceMock.getStateEntity(new EntityAddrIdentifier("entity-contract-77a0481b28572054cbdd19c944a7176ce9670be616d6de0e2ec3f89ca378dd79"), null); assertThat(stateEntityResult.getApiVersion(), is("2.0.0")); @@ -958,8 +963,8 @@ void stateGetEntityContractActualCCTLReturnedData() { assertThat(entity.getNamedKeys().size(), is(11)); - assertThat(entity.getNamedKeys().get(0).getKey() , is("uref-a18b3997bc0bafe8612973531e6782be4f1251f90f0513c0b34fea69df1a95ff-007")); - assertThat(entity.getNamedKeys().get(0).getName() , is("allowances")); + assertThat(entity.getNamedKeys().get(0).getKey(), is("uref-a18b3997bc0bafe8612973531e6782be4f1251f90f0513c0b34fea69df1a95ff-007")); + assertThat(entity.getNamedKeys().get(0).getName(), is("allowances")); assertThat(entity.getEntryPoints().get(14).getV1().getAccess(), is(EntryPoint.EntryPointAccessEnum.PUBLIC)); assertThat(entity.getEntryPoints().get(14).getV1().getArgs().get(0).getName(), is("address")); From 48ea3503afa4843c50503bfee5fc658a4c75702a Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Mon, 23 Sep 2024 14:56:35 +0100 Subject: [PATCH 5/6] issues/349 - Migrated Account hash from String to AccountHashKey --- src/main/java/com/casper/sdk/model/account/Account.java | 3 ++- .../java/com/casper/sdk/service/CasperServiceTests.java | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/casper/sdk/model/account/Account.java b/src/main/java/com/casper/sdk/model/account/Account.java index 7c7d0a9f..94015479 100644 --- a/src/main/java/com/casper/sdk/model/account/Account.java +++ b/src/main/java/com/casper/sdk/model/account/Account.java @@ -1,6 +1,7 @@ package com.casper.sdk.model.account; import com.casper.sdk.model.contract.NamedKey; +import com.casper.sdk.model.key.AccountHashKey; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; import lombok.Builder; @@ -28,7 +29,7 @@ public class Account { * account_hash(String) Hex-encoded account hash. */ @JsonProperty("account_hash") - private String hash; + private AccountHashKey hash; /** * {@link ActionThresholds} that have to be met diff --git a/src/test/java/com/casper/sdk/service/CasperServiceTests.java b/src/test/java/com/casper/sdk/service/CasperServiceTests.java index 71f23df5..e3d13a78 100644 --- a/src/test/java/com/casper/sdk/service/CasperServiceTests.java +++ b/src/test/java/com/casper/sdk/service/CasperServiceTests.java @@ -400,7 +400,7 @@ void getAccountStateInfoByBlockHash() { assertNotNull(account); assertNotNull(account.getAccount()); - assertEquals("account-hash-5079fc7bdd40f9226931d5863cb73f263c7944d6c8a8f3570213e9eb3e9464fe", account.getAccount().getHash()); + assertEquals("account-hash-5079fc7bdd40f9226931d5863cb73f263c7944d6c8a8f3570213e9eb3e9464fe", account.getAccount().getHash().toString()); } @Test @@ -416,7 +416,8 @@ void getAccountStateInfoByBlockHeight() { assertNotNull(account); assertNotNull(account.getAccount()); - assertEquals("account-hash-5079fc7bdd40f9226931d5863cb73f263c7944d6c8a8f3570213e9eb3e9464fe", account.getAccount().getHash()); + assertThat(account.getAccount().getHash(), is(instanceOf(AccountHashKey.class))); + assertEquals("account-hash-5079fc7bdd40f9226931d5863cb73f263c7944d6c8a8f3570213e9eb3e9464fe", account.getAccount().getHash().toString()); } @Test @@ -898,7 +899,7 @@ void stateGetEntityLegacyAccount() { LegacyAccount account = (LegacyAccount) stateEntityResult.getEntity(); - assertThat(account.getHash(), is("account-hash-f1075fce3b8cd4eab748b8705ca02444a5e35c0248662649013d8a5cb2b1a87c")); + assertThat(account.getHash().toString(), is("account-hash-f1075fce3b8cd4eab748b8705ca02444a5e35c0248662649013d8a5cb2b1a87c")); assertThat(account.getMainPurse(), is("uref-b22bc80d357df47447074e243b4d888de67c1cc7565fa82d0bb2b9b023146748-007")); assertThat(account.getAssociatedKeys().get(0).getAccountHash(), is(instanceOf(AccountHashKey.class))); assertThat(account.getAssociatedKeys().get(0).getAccountHash().toString(), is("account-hash-f1075fce3b8cd4eab748b8705ca02444a5e35c0248662649013d8a5cb2b1a87c")); From a390a2401f6ec5e6b1ffde20f999ba7501c555d8 Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:32:54 +0100 Subject: [PATCH 6/6] issues/349 - Migrated Effect key field from String to Key class --- .../casper/sdk/model/contract/NamedKey.java | 3 +- .../model/deploy/executionresult/Success.java | 3 +- .../com/casper/sdk/model/entity/Account.java | 5 +- .../com/casper/sdk/model/key/ByteCodeKey.java | 3 +- .../model/transaction/execution/Effect.java | 4 +- .../casper/sdk/model/transfer/TransferV1.java | 5 +- src/test/java/com/HowTo.java | 5 +- .../sdk/model/transaction/EffectTest.java | 86 +++++++++---------- .../sdk/service/CasperServiceTests.java | 23 +++-- .../service/EventServiceIntegrationTest.java | 6 +- 10 files changed, 69 insertions(+), 74 deletions(-) diff --git a/src/main/java/com/casper/sdk/model/contract/NamedKey.java b/src/main/java/com/casper/sdk/model/contract/NamedKey.java index ae9859f7..fbe0e444 100644 --- a/src/main/java/com/casper/sdk/model/contract/NamedKey.java +++ b/src/main/java/com/casper/sdk/model/contract/NamedKey.java @@ -1,5 +1,6 @@ package com.casper.sdk.model.contract; +import com.casper.sdk.model.key.Key; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; import lombok.Builder; @@ -25,7 +26,7 @@ public class NamedKey { * key(String) The value of the entry: a casper `Key` type. */ @JsonProperty("key") - private String key; + private Key key; /** * name(String) The name of the entry. diff --git a/src/main/java/com/casper/sdk/model/deploy/executionresult/Success.java b/src/main/java/com/casper/sdk/model/deploy/executionresult/Success.java index 8a363d85..8af69fec 100644 --- a/src/main/java/com/casper/sdk/model/deploy/executionresult/Success.java +++ b/src/main/java/com/casper/sdk/model/deploy/executionresult/Success.java @@ -1,6 +1,7 @@ package com.casper.sdk.model.deploy.executionresult; import com.casper.sdk.model.deploy.ExecutionEffect; +import com.casper.sdk.model.key.Key; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; @@ -30,7 +31,7 @@ public class Success { private ExecutionEffect effect; /** List of Hex-encoded transfer address. */ - private List transfers; + private List transfers; /** The cost of executing the deploy. */ @JsonSerialize(using = ToStringSerializer.class) diff --git a/src/main/java/com/casper/sdk/model/entity/Account.java b/src/main/java/com/casper/sdk/model/entity/Account.java index c020188e..0dd057e7 100644 --- a/src/main/java/com/casper/sdk/model/entity/Account.java +++ b/src/main/java/com/casper/sdk/model/entity/Account.java @@ -1,6 +1,7 @@ package com.casper.sdk.model.entity; import com.casper.sdk.exception.NoSuchKeyTagException; +import com.casper.sdk.model.key.AccountHashKey; import com.casper.sdk.model.key.Key; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.annotation.JsonValue; @@ -20,9 +21,9 @@ public class Account implements EntityAddressKind { @JsonValue - private Key account; + private AccountHashKey account; public Account(final String accountSt) throws NoSuchKeyTagException { - this.account = Key.create(accountSt); + this.account = (AccountHashKey) Key.create(accountSt); } } diff --git a/src/main/java/com/casper/sdk/model/key/ByteCodeKey.java b/src/main/java/com/casper/sdk/model/key/ByteCodeKey.java index 3b9dc426..6a5e3de2 100644 --- a/src/main/java/com/casper/sdk/model/key/ByteCodeKey.java +++ b/src/main/java/com/casper/sdk/model/key/ByteCodeKey.java @@ -33,7 +33,8 @@ protected void fromStringCustom(final String strKey) { final byte[] key = new byte[33]; final String[] split = strKey.split("-"); key[0] = byteCodeAddr.getByteTag(); - System.arraycopy(Hex.decode(split[split.length - 1]), 0, key, 1, 32); + final byte[] decode = Hex.decode(split[split.length - 1]); + System.arraycopy(decode, 0, key, 1, decode.length); setKey(key); } diff --git a/src/main/java/com/casper/sdk/model/transaction/execution/Effect.java b/src/main/java/com/casper/sdk/model/transaction/execution/Effect.java index 057a1e42..4e2e4c1e 100644 --- a/src/main/java/com/casper/sdk/model/transaction/execution/Effect.java +++ b/src/main/java/com/casper/sdk/model/transaction/execution/Effect.java @@ -1,5 +1,6 @@ package com.casper.sdk.model.transaction.execution; +import com.casper.sdk.model.key.Key; import com.casper.sdk.model.transaction.kind.Kind; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.*; @@ -16,9 +17,8 @@ @Builder public class Effect { - // TODO convert EntityAddr @JsonProperty("key") - private String key; + private Key key; @JsonProperty("kind") private Kind kind; diff --git a/src/main/java/com/casper/sdk/model/transfer/TransferV1.java b/src/main/java/com/casper/sdk/model/transfer/TransferV1.java index af7e3c1d..abc6efc3 100644 --- a/src/main/java/com/casper/sdk/model/transfer/TransferV1.java +++ b/src/main/java/com/casper/sdk/model/transfer/TransferV1.java @@ -1,6 +1,7 @@ package com.casper.sdk.model.transfer; import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport; +import com.casper.sdk.model.key.AccountHashKey; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.*; @@ -25,11 +26,11 @@ public class TransferV1 { /** Hex-encoded account hash. */ @JsonProperty("to") - private String to; + private AccountHashKey to; /** Hex-encoded account hash. */ @JsonProperty("from") - private String from; + private AccountHashKey from; /** Amount transferred */ @JsonIgnore diff --git a/src/test/java/com/HowTo.java b/src/test/java/com/HowTo.java index 8211ed1c..16cafd92 100644 --- a/src/test/java/com/HowTo.java +++ b/src/test/java/com/HowTo.java @@ -21,6 +21,7 @@ import com.casper.sdk.model.entity.AddressableEntity; import com.casper.sdk.model.entity.StateEntityResult; import com.casper.sdk.model.era.EraInfoData; +import com.casper.sdk.model.key.Key; import com.casper.sdk.model.key.PublicKey; import com.casper.sdk.model.reward.GetRewardResult; import com.casper.sdk.model.stateroothash.StateRootHashData; @@ -288,8 +289,8 @@ void getStateEntity() { assert stateEntityPublicKey.getEntity() != null; //By contract identifier - final String contractKey = ((AddressableEntity) stateEntityPublicKey.getEntity()).getNamedKeys().get(0).getKey(); - final StateEntityResult stateEntityContract = casperService.getStateEntity(new EntityAddrIdentifier(contractKey), null); + final Key contractKey = ((AddressableEntity) stateEntityPublicKey.getEntity()).getNamedKeys().get(0).getKey(); + final StateEntityResult stateEntityContract = casperService.getStateEntity(new EntityAddrIdentifier(contractKey.toString()), null); assert stateEntityContract.getEntity() != null; } diff --git a/src/test/java/com/casper/sdk/model/transaction/EffectTest.java b/src/test/java/com/casper/sdk/model/transaction/EffectTest.java index bdc9f989..bb16a61f 100644 --- a/src/test/java/com/casper/sdk/model/transaction/EffectTest.java +++ b/src/test/java/com/casper/sdk/model/transaction/EffectTest.java @@ -14,8 +14,7 @@ import com.casper.sdk.model.entity.contract.ByteCode; import com.casper.sdk.model.entity.contract.NamedKey; import com.casper.sdk.model.entity.contract.Package; -import com.casper.sdk.model.key.AccountHashKey; -import com.casper.sdk.model.key.PublicKey; +import com.casper.sdk.model.key.*; import com.casper.sdk.model.transaction.execution.Effect; import com.casper.sdk.model.transaction.kind.*; import com.casper.sdk.model.uref.URef; @@ -51,7 +50,8 @@ void pruneKindEffect() throws JsonProcessingException { "}"; final Effect pruneEffect = new ObjectMapper().readValue(json, Effect.class); - assertThat(pruneEffect.getKey(), is("balance-hold-01fe139a5aa36aa69c04a6b630c9993bc03d868ffde46d3f60c3fbe6e6e762016f78bec10c90010000")); + assertThat(pruneEffect.getKey(), is(instanceOf(BalanceHoldKey.class))); + assertThat(pruneEffect.getKey().toString(), is("balance-hold-01fe139a5aa36aa69c04a6b630c9993bc03d868ffde46d3f60c3fbe6e6e762016f78bec10c90010000")); assertThat(pruneEffect, is(notNullValue())); assertThat(pruneEffect.getKind(), is(instanceOf(PruneKind.class))); assertThat(((PruneKind) pruneEffect.getKind()).getPrune(), is("balance-hold-01fe139a5aa36aa69c04a6b630c9993bc03d868ffde46d3f60c3fbe6e6e762016f78bec10c90010000")); @@ -104,11 +104,11 @@ void writeKindBidKindCreditEffect() throws JsonProcessingException, NoSuchAlgori final Effect writeEffect = new ObjectMapper().readValue(json, Effect.class); assertThat(writeEffect, is(notNullValue())); assertThat(writeEffect.getKind(), is(instanceOf(WriteKind.class))); - WriteKind kind = writeEffect.getKind(); + final WriteKind kind = writeEffect.getKind(); assertThat(kind.getWrite().getValue(), is(instanceOf(BidKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(BidKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(ValidatorCredit.class))); - ValidatorCredit value = (ValidatorCredit) kind.getWrite().getValue(); + final ValidatorCredit value = (ValidatorCredit) kind.getWrite().getValue(); assertThat(value.getValidatorPublicKey(), is(PublicKey.fromTaggedHexString("01284a8cd097808afdea9876ccb91f28702143c04266dea057cc19b8fc382cc258"))); assertThat(value.getEraId(), is(377L)); assertThat(value.getAmount(), is(new BigInteger("10000"))); @@ -134,11 +134,11 @@ void writeKindBidKindBridgeEffect() throws JsonProcessingException, NoSuchAlgori final Effect writeEffect = new ObjectMapper().readValue(json, Effect.class); assertThat(writeEffect, is(notNullValue())); assertThat(writeEffect.getKind(), is(instanceOf(WriteKind.class))); - WriteKind kind = writeEffect.getKind(); + final WriteKind kind = writeEffect.getKind(); assertThat(kind.getWrite().getValue(), is(instanceOf(BidKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(BidKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(Bridge.class))); - Bridge value = (Bridge) kind.getWrite().getValue(); + final Bridge value = (Bridge) kind.getWrite().getValue(); assertThat(value.getOldValidatorPublicKey(), is(PublicKey.fromTaggedHexString("01284a8cd097808afdea9876ccb91f28702143c04266dea057cc19b8fc382cc258"))); assertThat(value.getNewValidatorPublicKey(), is(PublicKey.fromTaggedHexString("01026ca707c348ed8012ac6a1f28db031fadd6eb67203501a353b867a08c8b9a80"))); assertThat(value.getEraId(), is(378L)); @@ -207,7 +207,8 @@ void identityKindEffect() throws JsonProcessingException { "}"; final Effect identityEffect = new ObjectMapper().readValue(json, Effect.class); - assertThat(identityEffect.getKey(), is("entity-system-86c4525a60cb6532342f5f598666711219f3bdcc6a8936152ec1c670c510c75f")); + assertThat(identityEffect.getKey(), is(instanceOf(AddressableEntityKey.class))); + assertThat(identityEffect.getKey().toString(), is("entity-system-86c4525a60cb6532342f5f598666711219f3bdcc6a8936152ec1c670c510c75f")); assertThat(identityEffect, is(notNullValue())); assertThat(identityEffect.getKind(), is(instanceOf(IdentityKind.class))); } @@ -218,22 +219,22 @@ void identityKindAddUint512() throws JsonProcessingException { final String json = "{\n" + " \"key\": \"balance-1c29560834540520e147468c3bf86f09e5bd60cda2cb8380d94ee1c348a4281d\",\n" + " \"kind\": {\n" + - " \"AddUInt512\" : 2500000000\n" + - "}\n" + + " \"AddUInt512\" : 2500000000\n" + + "}\n" + "}"; final Effect identityEffect = new ObjectMapper().readValue(json, Effect.class); - assertThat(identityEffect.getKey(), is("balance-1c29560834540520e147468c3bf86f09e5bd60cda2cb8380d94ee1c348a4281d")); + assertThat(identityEffect.getKey(), is(instanceOf(Key.class))); + assertThat(identityEffect.getKey().toString(), is("balance-1c29560834540520e147468c3bf86f09e5bd60cda2cb8380d94ee1c348a4281d")); assertThat(identityEffect, is(notNullValue())); assertThat(identityEffect.getKind(), is(instanceOf(Transform.class))); - } @Test void writeKindByteCode() throws JsonProcessingException { final String json = "{\n" + - " \"key\": \"byte-code-v1-wasm-aaa0345086a4f80601ec93d78053c13f5599afeb752d3d9b88fe5878b611675\",\n" + + " \"key\": \"byte-code-v1-wasm-3beb396c91ff7ae62d08857cc8a787146cd4f0771b8a21d385b3f4ac6077854a\",\n" + " \"kind\": {\n" + " \"Write\": {\n" + " \"ByteCode\": {\n" + @@ -245,19 +246,17 @@ void writeKindByteCode() throws JsonProcessingException { "}"; final Effect writeEffect = new ObjectMapper().readValue(json, Effect.class); + assertThat(writeEffect.getKey().toString(), is("byte-code-v1-wasm-3beb396c91ff7ae62d08857cc8a787146cd4f0771b8a21d385b3f4ac6077854a")); - assertThat(writeEffect.getKey(), is("byte-code-v1-wasm-aaa0345086a4f80601ec93d78053c13f5599afeb752d3d9b88fe5878b611675")); - WriteKind kind = writeEffect.getKind(); - + final WriteKind kind = writeEffect.getKind(); assertThat(kind.getWrite(), is(instanceOf(ByteCodeKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(ByteCode.class))); - ByteCode byteCodeKind = (ByteCode) kind.getWrite().getValue(); - + final ByteCode byteCodeKind = (ByteCode) kind.getWrite().getValue(); assertThat(byteCodeKind.getBytes(), is("0061736d01000000017f106002")); assertThat(byteCodeKind.getKind(), is(ByteCode.ByteCodes.V1CasperWasm)); - } + @Test void writeKindNamedKey() throws JsonProcessingException { @@ -285,25 +284,25 @@ void writeKindNamedKey() throws JsonProcessingException { final Effect writeEffect = new ObjectMapper().readValue(json, Effect.class); - assertThat(writeEffect.getKey(), is("named-key-entity-account-a897e2e1c25b1149e96b774bdfd758d4a44ec392bd914a19ff780d3905ff45c0-e8265cd5ef8e0275971c6d4ff28263ef787412f034631a11bbc508761ffd7119")); - WriteKind kind = writeEffect.getKind(); + assertThat(writeEffect.getKey(), is(instanceOf(NamedKeyKey.class))); + assertThat(writeEffect.getKey().toString(), is("named-key-entity-account-a897e2e1c25b1149e96b774bdfd758d4a44ec392bd914a19ff780d3905ff45c0-e8265cd5ef8e0275971c6d4ff28263ef787412f034631a11bbc508761ffd7119")); + final WriteKind kind = writeEffect.getKind(); assertThat(kind.getWrite(), is(instanceOf(NamedKeyKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(NamedKey.class))); - NamedKey namedKeyKind = (NamedKey) kind.getWrite().getValue(); - - AbstractCLValue namedKey = namedKeyKind.getNamedKey(); + final NamedKey namedKeyKind = (NamedKey) kind.getWrite().getValue(); + final AbstractCLValue namedKey = namedKeyKind.getNamedKey(); assertThat(namedKey.getBytes(), is("028c5da3dd186f82c27dd4689dca3f838ac17f93ef10a5159fa40a30ca78f9e93207")); assertThat(namedKey.getParsed(), is("uref-8c5da3dd186f82c27dd4689dca3f838ac17f93ef10a5159fa40a30ca78f9e932-007")); assertThat(namedKey.getClType().getTypeName(), is("Key")); - AbstractCLValue name = namedKeyKind.getName(); + final AbstractCLValue name = namedKeyKind.getName(); assertThat(name.getBytes(), is("2800000063657031385f636f6e74726163745f7061636b6167655f6163636573735f41636d6520546f6b656e")); assertThat(name.getParsed(), is("cep18_contract_package_access_Acme Token")); assertThat(name.getClType().getTypeName(), is("String")); - } + @Test void writeKindEntryPoint() throws JsonProcessingException { @@ -336,16 +335,15 @@ void writeKindEntryPoint() throws JsonProcessingException { final Effect writeEffect = new ObjectMapper().readValue(json, Effect.class); + assertThat(writeEffect.getKey(), is(instanceOf(EntryPointKey.class))); + assertThat(writeEffect.getKey().toString(), is("entry-point-v1-entity-contract-3b6b4d8a3d815372508faa92f3a05dcb50c9c98de05d9a7668cb94b04f1ef9af-768c370eb010604bd19029a409dca8b5fbf9af9bc14a36c2b294a2a7a922161e")); - assertThat(writeEffect.getKey(), is("entry-point-v1-entity-contract-3b6b4d8a3d815372508faa92f3a05dcb50c9c98de05d9a7668cb94b04f1ef9af-768c370eb010604bd19029a409dca8b5fbf9af9bc14a36c2b294a2a7a922161e")); - WriteKind kind = writeEffect.getKind(); - + final WriteKind kind = writeEffect.getKind(); assertThat(kind.getWrite(), is(instanceOf(EntryPointKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(EntryPointValue.class))); - EntryPointValue entryPointValue = (EntryPointValue) kind.getWrite().getValue(); - EntryPoint entryPoint = entryPointValue.getV1(); - + final EntryPointValue entryPointValue = (EntryPointValue) kind.getWrite().getValue(); + final EntryPoint entryPoint = entryPointValue.getV1(); assertThat(entryPoint.getName(), is("burn")); assertThat(entryPoint.getArgs().size(), is(2)); assertThat(entryPoint.getArgs().get(0).getName(), is("owner")); @@ -354,8 +352,8 @@ void writeKindEntryPoint() throws JsonProcessingException { assertThat(entryPoint.getAccess().getValue(), is(EntryPoint.EntryPointAccessEnum.PUBLIC)); assertThat(entryPoint.getType(), is(EntryPoint.EntryPointType.CALLED)); assertThat(entryPoint.getPayment(), is(EntryPoint.EntryPointPayment.CALLER)); - } + @Test void writeKindPackage() throws JsonProcessingException { @@ -400,28 +398,24 @@ void writeKindPackage() throws JsonProcessingException { final Effect writeEffect = new ObjectMapper().readValue(json, Effect.class); - assertThat(writeEffect.getKey(), is("package-ef39f3794dfde8641acc43a8f63d4c0a72a4b33bbb2e4eed29421ee6cfd0d87e")); - WriteKind kind = writeEffect.getKind(); + assertThat(writeEffect.getKey(), is(instanceOf(Key.class))); + assertThat(writeEffect.getKey().toString(), is("package-ef39f3794dfde8641acc43a8f63d4c0a72a4b33bbb2e4eed29421ee6cfd0d87e")); + final WriteKind kind = writeEffect.getKind(); assertThat(kind.getWrite(), is(instanceOf(PackageKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(Package.class))); - Package contractPackage = (Package) kind.getWrite().getValue(); - + final Package contractPackage = (Package) kind.getWrite().getValue(); assertThat(contractPackage.getVersions().get(0).getAddressableEntityHash(), is("addressable-entity-3b6b4d8a3d815372508faa92f3a05dcb50c9c98de05d9a7668cb94b04f1ef9af")); assertThat(contractPackage.getVersions().get(0).getEntityVersionKey().getEntityVersion(), is(3)); assertThat(contractPackage.getVersions().get(0).getEntityVersionKey().getProtocolVersionMajor(), is(2)); - assertThat(contractPackage.getDisabledVersions().size(), is(2)); assertThat(contractPackage.getDisabledVersions().get(1).getAddressableEntityHash(), is("addressable-entity-aab0da01340446cee477f28410f8af5d6e0f3a88fb26c0cafb8d1625f5cc9c10")); assertThat(contractPackage.getDisabledVersions().get(1).getEntityVersionKey().getEntityVersion(), is(2)); assertThat(contractPackage.getDisabledVersions().get(1).getEntityVersionKey().getProtocolVersionMajor(), is(2)); - assertThat(contractPackage.getGroups().size(), is(3)); assertThat(contractPackage.getGroups().get(1), is("group_B")); - assertThat(contractPackage.getLockStatus(), is(Package.PackageStatus.Unlocked)); - } @Test @@ -457,15 +451,14 @@ void writeKindAddressableEntity() throws JsonProcessingException { "}"; final Effect writeEffect = new ObjectMapper().readValue(json, Effect.class); - assertThat(writeEffect.getKey(), is("entity-contract-3b6b4d8a3d815372508faa92f3a05dcb50c9c98de05d9a7668cb94b04f1ef9af")); - - WriteKind kind = writeEffect.getKind(); + assertThat(writeEffect.getKey(), is(instanceOf(AddressableEntityKey.class))); + assertThat(writeEffect.getKey().toString(), is("entity-contract-3b6b4d8a3d815372508faa92f3a05dcb50c9c98de05d9a7668cb94b04f1ef9af")); + final WriteKind kind = writeEffect.getKind(); assertThat(kind.getWrite(), is(instanceOf(AddressableEntityKind.class))); assertThat(kind.getWrite().getValue(), is(instanceOf(Entity.class))); - Entity entity = (Entity) kind.getWrite().getValue(); - + final Entity entity = (Entity) kind.getWrite().getValue(); assertThat(entity.getProtocolVersion(), is("2.0.0")); assertThat(((SmartContract) entity.getEntityAddressKind()).getSmartContract().name(), is(SmartContract.TransactionRuntime.VMCASPERV1.name())); assertThat(entity.getPackageHash(), is("package-ef39f3794dfde8641acc43a8f63d4c0a72a4b33bbb2e4eed29421ee6cfd0d87e")); @@ -478,6 +471,5 @@ void writeKindAddressableEntity() throws JsonProcessingException { assertThat(entity.getActionThresholds().getUpgradeManagement(), is(1)); assertThat(entity.getActionThresholds().getUpgradeManagement(), is(1)); assertThat(entity.getMessageTopics().size(), is(0)); - } } diff --git a/src/test/java/com/casper/sdk/service/CasperServiceTests.java b/src/test/java/com/casper/sdk/service/CasperServiceTests.java index e3d13a78..be70ef71 100644 --- a/src/test/java/com/casper/sdk/service/CasperServiceTests.java +++ b/src/test/java/com/casper/sdk/service/CasperServiceTests.java @@ -41,10 +41,7 @@ import com.casper.sdk.model.era.EraEndV2; import com.casper.sdk.model.era.EraInfoData; import com.casper.sdk.model.globalstate.GlobalStateData; -import com.casper.sdk.model.key.AccountHashKey; -import com.casper.sdk.model.key.AlgorithmTag; -import com.casper.sdk.model.key.Key; -import com.casper.sdk.model.key.PublicKey; +import com.casper.sdk.model.key.*; import com.casper.sdk.model.peer.PeerData; import com.casper.sdk.model.reward.GetRewardResult; import com.casper.sdk.model.stateroothash.StateRootHashData; @@ -235,7 +232,7 @@ void retrieveLastBlockTransfers() { } @Test - void getTransferByHeight() { + void getTransferByHeight() throws NoSuchKeyTagException { mockNode.withRcpResponseDispatcher() .withMethod("chain_get_block_transfers") @@ -248,13 +245,13 @@ void getTransferByHeight() { final TransferV1 transaction = transferData.getTransfers().get(0).getTransferV1(); assertEquals("c709e727b7eaadb3b7f76450aa5d3ac3dd28b0271b7471a6dcc828cfd29f745a", transaction.getDeployHash()); - assertEquals("account-hash-496d542527e1a29f576ab7c3f4c947bfcdc9b4145f75f6ec40e36089432d7351", transaction.getFrom()); - assertEquals("account-hash-8a35e688eac33089b13f91a78c94221b669a0b13a6ed199228b1da018ecfa9df", transaction.getTo()); + assertEquals(Key.create("account-hash-496d542527e1a29f576ab7c3f4c947bfcdc9b4145f75f6ec40e36089432d7351"), transaction.getFrom()); + assertEquals(Key.create("account-hash-8a35e688eac33089b13f91a78c94221b669a0b13a6ed199228b1da018ecfa9df"), transaction.getTo()); assertEquals(BigInteger.valueOf(445989400000L), transaction.getAmount()); } @Test - void getTransferByHash() { + void getTransferByHash() throws NoSuchKeyTagException { mockNode.withRcpResponseDispatcher() .withMethod("chain_get_block_transfers") @@ -267,8 +264,8 @@ void getTransferByHash() { final TransferV1 transaction = transferData.getTransfers().get(0).getTransferV1(); assertEquals("c709e727b7eaadb3b7f76450aa5d3ac3dd28b0271b7471a6dcc828cfd29f745a", transaction.getDeployHash()); - assertEquals("account-hash-496d542527e1a29f576ab7c3f4c947bfcdc9b4145f75f6ec40e36089432d7351", transaction.getFrom()); - assertEquals("account-hash-8a35e688eac33089b13f91a78c94221b669a0b13a6ed199228b1da018ecfa9df", transaction.getTo()); + assertEquals(Key.create("account-hash-496d542527e1a29f576ab7c3f4c947bfcdc9b4145f75f6ec40e36089432d7351"), transaction.getFrom()); + assertEquals(Key.create("account-hash-8a35e688eac33089b13f91a78c94221b669a0b13a6ed199228b1da018ecfa9df"), transaction.getTo()); assertEquals(BigInteger.valueOf(445989400000L), transaction.getAmount()); } @@ -384,7 +381,7 @@ void getStateItemContract() { assertInstanceOf(StoredValueContract.class, storedValueData.getStoredValue()); assertEquals("contract-package-d63c44078a1931b5dc4b80a7a0ec586164fd0470ce9f8b23f6d93b9e86c5944d", ((StoredValueContract) storedValueData.getStoredValue()).getValue().getPackageHash()); assertEquals("contract-wasm-08d9634a72c58df5d837740174c6e88e52b0370ce8c74960d600e54966a0a4cf", ((StoredValueContract) storedValueData.getStoredValue()).getValue().getWasmHash()); - assertEquals("uref-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef-007", ((StoredValueContract) storedValueData.getStoredValue()).getValue().getNamedKeys().get(0).getKey()); + assertEquals("uref-fe327f9815a1d016e1143db85e25a86341883949fd75ac1c1e7408a26c5b62ef-007", ((StoredValueContract) storedValueData.getStoredValue()).getValue().getNamedKeys().get(0).getKey().toString()); } @Test @@ -963,8 +960,8 @@ void stateGetEntityCiontractActualCCTLReturnedData() { assertThat(entity.getEntryPoints().size(), is(15)); assertThat(entity.getNamedKeys().size(), is(11)); - - assertThat(entity.getNamedKeys().get(0).getKey(), is("uref-a18b3997bc0bafe8612973531e6782be4f1251f90f0513c0b34fea69df1a95ff-007")); + assertThat(entity.getNamedKeys().get(0).getKey(), is(instanceOf(URefKey.class))); + assertThat(entity.getNamedKeys().get(0).getKey().toString(), is("uref-a18b3997bc0bafe8612973531e6782be4f1251f90f0513c0b34fea69df1a95ff-007")); assertThat(entity.getNamedKeys().get(0).getName(), is("allowances")); assertThat(entity.getEntryPoints().get(14).getV1().getAccess(), is(EntryPoint.EntryPointAccessEnum.PUBLIC)); diff --git a/src/test/java/com/casper/sdk/service/EventServiceIntegrationTest.java b/src/test/java/com/casper/sdk/service/EventServiceIntegrationTest.java index f04154ce..589137ce 100644 --- a/src/test/java/com/casper/sdk/service/EventServiceIntegrationTest.java +++ b/src/test/java/com/casper/sdk/service/EventServiceIntegrationTest.java @@ -379,10 +379,10 @@ void stepEventV2Pojo() throws Exception { Step step = (Step) data; assertThat(step.getExecutionEffects(), hasSize(37)); - assertThat(step.getExecutionEffects().get(0).getKey(), is("entity-system-001501686b8d0a46fa1d47462b11b07f25ff80a13f0d889e7fd56cda81f4d642")); + assertThat(step.getExecutionEffects().get(0).getKey().toString(), is("entity-system-001501686b8d0a46fa1d47462b11b07f25ff80a13f0d889e7fd56cda81f4d642")); assertThat(step.getExecutionEffects().get(0).getKind(), is(instanceOf(IdentityKind.class))); - assertThat(step.getExecutionEffects().get(34).getKey(), is("uref-da840e3134fed2d355e2cc24e762649b74d46493815de375053f5f82f0e18852-000")); + assertThat(step.getExecutionEffects().get(34).getKey().toString(), is("uref-da840e3134fed2d355e2cc24e762649b74d46493815de375053f5f82f0e18852-000")); assertThat(step.getExecutionEffects().get(34).getKind(), is(instanceOf(WriteKind.class))); assertThat(((WriteKind) step.getExecutionEffects().get(34).getKind()).getWrite().getValue(), is(instanceOf(CLValueMap.class))); CLValueMap clValueMap = (CLValueMap) ((WriteKind) step.getExecutionEffects().get(34).getKind()).getWrite().getValue(); @@ -390,7 +390,7 @@ void stepEventV2Pojo() throws Exception { assertThat(clValueMap.getClType().getKeyValueTypes().getValueType().getTypeName(), is(AbstractCLType.MAP)); assertThat(clValueMap.getBytes(), is("0300000001000000000000000500000001297c041e1af9b14f2ea215641adbe231cf395364aed00d288fdeba849f34b0ac08010064a7b3b6e00d0101000000017fab55ab50c80193b142ddf9c6cbd877a0ed1c64831889620750b1c327eb99ef08010064a7b3b6e00d0131afbb18e108ea19dea4ec2142a8de1623dcaab87823ed87ec70922c8d05217b08020064a7b3b6e00d0201000000010ac86a5c07d75343ca3dfa1fbcadd0723b1e3df3863907520be8dbd5a229ccd908020064a7b3b6e00d01653b55b810f692c8d8d8c6ea2db60d25fc661578f07fc9b9be9ae26f47af8d4508040064a7b3b6e00d04010000000167cfe9c230684c4b3cbb2b410493da66de7764271730584cfb8d418b28d06d3e08040064a7b3b6e00d01c0bd8e47de46c1ae7d477eb245a235efabd0c03ff29991d1c2a766186c80e03a08030064a7b3b6e00d0301000000010b37cccaf40ed3723c0933770f0b291b01a82ddb0c9086365424ce5e0407201208030064a7b3b6e00d01e1c98abaead1998334e69c73ef0f75af74844568273bbb523295604b1af0dea608050064a7b3b6e00d050100000001676de7c258615c085a9ca7bd6d597d07ec88ba3184c653de60ab7fc6f8b4b67c08050064a7b3b6e00d02000000000000000500000001297c041e1af9b14f2ea215641adbe231cf395364aed00d288fdeba849f34b0ac08010064a7b3b6e00d0101000000017fab55ab50c80193b142ddf9c6cbd877a0ed1c64831889620750b1c327eb99ef08010064a7b3b6e00d0131afbb18e108ea19dea4ec2142a8de1623dcaab87823ed87ec70922c8d05217b08020064a7b3b6e00d0201000000010ac86a5c07d75343ca3dfa1fbcadd0723b1e3df3863907520be8dbd5a229ccd908020064a7b3b6e00d01653b55b810f692c8d8d8c6ea2db60d25fc661578f07fc9b9be9ae26f47af8d4508040064a7b3b6e00d04010000000167cfe9c230684c4b3cbb2b410493da66de7764271730584cfb8d418b28d06d3e08040064a7b3b6e00d01c0bd8e47de46c1ae7d477eb245a235efabd0c03ff29991d1c2a766186c80e03a08030064a7b3b6e00d0301000000010b37cccaf40ed3723c0933770f0b291b01a82ddb0c9086365424ce5e0407201208030064a7b3b6e00d01e1c98abaead1998334e69c73ef0f75af74844568273bbb523295604b1af0dea608050064a7b3b6e00d050100000001676de7c258615c085a9ca7bd6d597d07ec88ba3184c653de60ab7fc6f8b4b67c08050064a7b3b6e00d03000000000000000500000001297c041e1af9b14f2ea215641adbe231cf395364aed00d288fdeba849f34b0ac080290eac47bfded0d0101000000017fab55ab50c80193b142ddf9c6cbd877a0ed1c64831889620750b1c327eb99ef0801f0334d2ebaed0d0131afbb18e108ea19dea4ec2142a8de1623dcaab87823ed87ec70922c8d05217b0804804763a98ef30d0201000000010ac86a5c07d75343ca3dfa1fbcadd0723b1e3df3863907520be8dbd5a229ccd90802802a9d7cd1f20d01653b55b810f692c8d8d8c6ea2db60d25fc661578f07fc9b9be9ae26f47af8d450806802f940f78f90d04010000000167cfe9c230684c4b3cbb2b410493da66de7764271730584cfb8d418b28d06d3e080400965a9290f70d01c0bd8e47de46c1ae7d477eb245a235efabd0c03ff29991d1c2a766186c80e03a080430a13cc940ee0d0301000000010b37cccaf40ed3723c0933770f0b291b01a82ddb0c9086365424ce5e040720120803507dd5e076ed0d01e1c98abaead1998334e69c73ef0f75af74844568273bbb523295604b1af0dea6080720e87e734dff0d050100000001676de7c258615c085a9ca7bd6d597d07ec88ba3184c653de60ab7fc6f8b4b67c0806e0305eaa63fc0d")); - assertThat(step.getExecutionEffects().get(36).getKey(), is("uref-10ad3536d8be35beb06864d5589ac74d47b9440148c07c92ca1edaf1176469d0-000")); + assertThat(step.getExecutionEffects().get(36).getKey().toString(), is("uref-10ad3536d8be35beb06864d5589ac74d47b9440148c07c92ca1edaf1176469d0-000")); assertThat(step.getExecutionEffects().get(36).getKind(), is(instanceOf(WriteKind.class))); assertThat(((WriteKind) step.getExecutionEffects().get(36).getKind()).getWrite().getValue(), is(instanceOf(CLValueU64.class))); CLValueU64 clValueU64 = (CLValueU64) ((WriteKind) step.getExecutionEffects().get(36).getKind()).getWrite().getValue();