Skip to content

Commit

Permalink
Merge pull request #413 from bloxbean/cip105
Browse files Browse the repository at this point in the history
feat: #368 CIP105 - Governance keys derivation and bech32 encoding
  • Loading branch information
satran004 authored Aug 13, 2024
2 parents 76c4402 + 678768a commit 0427ecd
Show file tree
Hide file tree
Showing 14 changed files with 2,081 additions and 11 deletions.
119 changes: 115 additions & 4 deletions core/src/main/java/com/bloxbean/cardano/client/account/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import com.bloxbean.cardano.client.exception.AddressRuntimeException;
import com.bloxbean.cardano.client.exception.CborDeserializationException;
import com.bloxbean.cardano.client.exception.CborSerializationException;
import com.bloxbean.cardano.client.governance.DRepId;
import com.bloxbean.cardano.client.governance.keys.CommitteeColdKey;
import com.bloxbean.cardano.client.governance.keys.CommitteeHotKey;
import com.bloxbean.cardano.client.governance.keys.DRepKey;
import com.bloxbean.cardano.client.transaction.TransactionSigner;
import com.bloxbean.cardano.client.transaction.spec.Transaction;
import com.bloxbean.cardano.client.util.HexUtil;
Expand Down Expand Up @@ -347,19 +349,77 @@ public HdKeyPair drepHdKeyPair() {

public String drepId() {
if (drepId == null || drepId.isEmpty()) {
var drepHdKeyPair = drepHdKeyPair();
var drepKeyHash = drepHdKeyPair.getPublicKey().getKeyHash();
drepId = DRepId.fromKeyHash(HexUtil.encodeHexString(drepKeyHash));
drepId = DRepKey.from(drepHdKeyPair()).dRepId();
}

return drepId;
}

/**
* Get {@link DRepKey} from DRep Hd key pair {@link HdKeyPair}
* @return DRepKey
*/
public DRepKey drepKey() {
return DRepKey.from(drepHdKeyPair());
}

public Credential drepCredential() {
var drepHdKeyPair = drepHdKeyPair();
return Credential.fromKey(drepHdKeyPair.getPublicKey().getKeyHash());
}

/**
* Get Hd key pair for Constitutional Committee Cold Keys
* @return HdKeyPair
*/
@JsonIgnore
public HdKeyPair committeeColdKeyPair() {
return getCommitteeColdKeyPair();
}

/**
* Get {@link CommitteeColdKey} from Constitutional Committee Cold Key Hd key pair
* @return CommitteeColdKey
*/
public CommitteeColdKey committeeColdKey() {
return CommitteeColdKey.from(committeeColdKeyPair());
}

/**
* Get Credential for Constitutional Committee Cold Keys
* @return Credential
*/
public Credential committeeColdCredential() {
var ccColdHdKeyPair = committeeColdKeyPair();
return Credential.fromKey(ccColdHdKeyPair.getPublicKey().getKeyHash());
}

/**
* Get Hd key pair for Constitutional Committee Hot Keys
* @return
*/
@JsonIgnore
public HdKeyPair committeeHotKeyPair() {
return getCommitteeHotKeyPair();
}

/**
* Get {@link CommitteeHotKey} from Constitutional Committee Hot Key Hd key pair
* @return CommitteeHotKey
*/
public CommitteeHotKey committeeHotKey() {
return CommitteeHotKey.from(committeeHotKeyPair());
}

/**
* Get Credential for Constitutional Committee Hot Keys
* @return Credential
*/
public Credential committeeHotCredential() {
var ccHotHdKeyPair = committeeHotKeyPair();
return Credential.fromKey(ccHotHdKeyPair.getPublicKey().getKeyHash());
}

/**
* @deprecated Use {@link Account#sign(Transaction)}
* @param txnHex
Expand Down Expand Up @@ -398,10 +458,33 @@ public Transaction signWithStakeKey(Transaction transaction) {
return TransactionSigner.INSTANCE.sign(transaction, getStakeKeyPair());
}

/**
* Sign a transaction object with DRep key
* @param transaction Transaction
* @return Signed Transaction
*/
public Transaction signWithDRepKey(Transaction transaction) {
return TransactionSigner.INSTANCE.sign(transaction, getDRepKeyPair());
}

/**
* Sign a transaction object with Constitutional Committee Cold Key
* @param transaction Transaction
* @return Signed Transaction
*/
public Transaction signWithCommitteeColdKey(Transaction transaction) {
return TransactionSigner.INSTANCE.sign(transaction, getCommitteeColdKeyPair());
}

/**
* Sign a transaction object with Constitutional Committee Hot Key
* @param transaction Transaction
* @return Signed Transaction
*/
public Transaction signWithCommitteeHotKey(Transaction transaction) {
return TransactionSigner.INSTANCE.sign(transaction, getCommitteeHotKeyPair());
}

private void generateNew(Words noOfWords) {
String mnemonic = null;
try {
Expand Down Expand Up @@ -477,6 +560,34 @@ private HdKeyPair getDRepKeyPair() {
return hdKeyPair;
}

private HdKeyPair getCommitteeColdKeyPair() {
HdKeyPair hdKeyPair;
DerivationPath drepDerivationPath =
DerivationPath.createCommitteeColdKeyDerivationPathForAccount(derivationPath.getAccount().getValue());

if (mnemonic == null || mnemonic.trim().length() == 0) {
hdKeyPair = new CIP1852().getKeyPairFromAccountKey(this.accountKey, drepDerivationPath);
} else {
hdKeyPair = new CIP1852().getKeyPairFromMnemonic(mnemonic, drepDerivationPath);
}

return hdKeyPair;
}

private HdKeyPair getCommitteeHotKeyPair() {
HdKeyPair hdKeyPair;
DerivationPath drepDerivationPath =
DerivationPath.createCommitteeHotKeyDerivationPathForAccount(derivationPath.getAccount().getValue());

if (mnemonic == null || mnemonic.trim().length() == 0) {
hdKeyPair = new CIP1852().getKeyPairFromAccountKey(this.accountKey, drepDerivationPath);
} else {
hdKeyPair = new CIP1852().getKeyPairFromMnemonic(mnemonic, drepDerivationPath);
}

return hdKeyPair;
}

@Override
public String toString() {
try {
Expand Down
Loading

0 comments on commit 0427ecd

Please sign in to comment.