From ac405d5879115bdfbc987065606244c68cd966f0 Mon Sep 17 00:00:00 2001 From: Satya <35016438+satran004@users.noreply.github.com> Date: Tue, 30 Jan 2024 15:54:51 +0800 Subject: [PATCH] feat: #372 Add method to get script DRepId and tests (#373) --- .../bloxbean/cardano/client/account/AccountTest.java | 10 ++++++++++ .../bloxbean/cardano/client/governance/DRepId.java | 6 ++++++ .../cardano/client/governance/DRepIdTest.java | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java b/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java index 210b169a..9ba80f4f 100644 --- a/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java +++ b/core/src/test/java/com/bloxbean/cardano/client/account/AccountTest.java @@ -419,4 +419,14 @@ void testPubKeyFromParentPubKey() { assertThat(publicKey.getKeyData()).isEqualTo(childHdKeyPair.getPublicKey().getKeyData()); } + + @Test + void testDRepId() { + String mnemonicPhrase = "punch smile segment tumble sauce oak mosquito clay service switch still federal chicken economy saddle galaxy reunion trust dinosaur demise illegal pupil trip lyrics"; + + Account account = new Account(Networks.testnet(), mnemonicPhrase); + String drepId = account.drepId(); + + assertThat(drepId).isEqualTo("drep18hf6wcv9aaq426duj8kcc5kp9pauz9ac8znh8jmckm80sf7fetw"); + } } diff --git a/governance/src/main/java/com/bloxbean/cardano/client/governance/DRepId.java b/governance/src/main/java/com/bloxbean/cardano/client/governance/DRepId.java index 200a6ac1..d10ef96f 100644 --- a/governance/src/main/java/com/bloxbean/cardano/client/governance/DRepId.java +++ b/governance/src/main/java/com/bloxbean/cardano/client/governance/DRepId.java @@ -9,6 +9,7 @@ public class DRepId { public static final String DREP_ID_PREFIX = "drep"; + public static final String DREP_ID_SCRIPT_PREFIX = "drep_script"; public static String fromVerificationKey(VerificationKey verificationKey) { String keyHash = KeyGenUtil.getKeyHash(verificationKey); @@ -27,6 +28,11 @@ public static String fromKeyHash(String keyHash) { return drepId; } + public static String fromScriptHash(String scriptHash) { + String drepId = Bech32.encode(HexUtil.decodeHexString(scriptHash), DREP_ID_SCRIPT_PREFIX); + return drepId; + } + public static DRep toDrep(String drepId, DRepType drepType) { byte[] bytes = Bech32.decode(drepId).data; diff --git a/governance/src/test/java/com/bloxbean/cardano/client/governance/DRepIdTest.java b/governance/src/test/java/com/bloxbean/cardano/client/governance/DRepIdTest.java index 52a35b25..872f0d26 100644 --- a/governance/src/test/java/com/bloxbean/cardano/client/governance/DRepIdTest.java +++ b/governance/src/test/java/com/bloxbean/cardano/client/governance/DRepIdTest.java @@ -25,6 +25,18 @@ void fromVerificationKeyBytes() { assertThat(drepId).isEqualTo("drep1vlmeg4vu96detgkf5srw427363svqemth2xde2e2xp5ywfsx2gn"); } + @Test + void fromKeyHash() { + String drepId = DRepId.fromKeyHash("74984fae4ca1715fa1f8759f9d871015ac87f449a85dea6cf9956da1"); + assertThat(drepId).isEqualTo("drep1wjvyltjv59c4lg0cwk0empcszkkg0azf4pw75m8ej4k6zuqfvt5"); + } + + @Test + void fromScriptHash() { + String drepId = DRepId.fromScriptHash("41868c2b4e5289022a3a1f6f47f86823bc605c609d2c47a2db58e04a"); + assertThat(drepId).isEqualTo("drep_script1gxrgc26w22ysy236rah507rgyw7xqhrqn5ky0gkmtrsy553rrq7"); + } + //TODO: Implement this @Test void toDrep() {