Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Well defined default addresses #30

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.bloxbean.cardano.yacicli.commands.localcluster;

import com.bloxbean.cardano.yacicli.commands.common.Groups;
import com.bloxbean.cardano.yacicli.commands.localcluster.service.DefaultAddressService;
import com.bloxbean.cardano.yacicli.common.CommandContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.shell.Availability;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellMethodAvailability;

@ShellComponent
@ShellCommandGroup(Groups.NODE_CMD_GROUP)
@RequiredArgsConstructor
@Slf4j
public class DefaultAddressCommands {
private final DefaultAddressService defaultAddressService;

@ShellMethod(value = "Show default addresses", key = "default-addresses")
@ShellMethodAvailability("localClusterCmdAvailability")
public void listDefaultAddresses() {
defaultAddressService.printDefaultAddresses(false);
}

public Availability localClusterCmdAvailability() {
return CommandContext.INSTANCE.getCurrentMode() == CommandContext.Mode.LOCAL_CLUSTER
? Availability.available()
: Availability.unavailable("you are not in local-cluster modes");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.bloxbean.cardano.yacicli.commands.localcluster.events.FirstRunDone;
import com.bloxbean.cardano.yacicli.commands.localcluster.service.AccountService;
import com.bloxbean.cardano.yacicli.commands.localcluster.service.ClusterUtilService;
import com.bloxbean.cardano.yacicli.commands.localcluster.service.DefaultAddressService;
import com.bloxbean.cardano.yacicli.common.CommandContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -21,6 +22,7 @@ public class FirstRunTopupAccounts {
private final ClusterService localClusterService;
private final ClusterUtilService clusterUtilService;
private final AccountService accountService;
private final DefaultAddressService defaultAddressService;

@Value("${topup_addresses:#{null}}")
private String[] topupAddresses;
Expand All @@ -31,6 +33,7 @@ public void topupInitialAccounts(FirstRunDone firstRunDone) {
try {
String clusterName = firstRunDone.getCluster();
if (localClusterService.isFirstRunt(clusterName)) {
defaultAddressService.printDefaultAddresses(true);
if (topupAddresses != null) {
if (topupAddresses.length > 0) {
writeLn("First run. Let's topup configured addresses");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.bloxbean.cardano.yacicli.commands.localcluster.service;

import com.bloxbean.cardano.client.account.Account;
import com.bloxbean.cardano.client.address.Address;
import com.bloxbean.cardano.client.common.model.Networks;
import com.bloxbean.cardano.client.crypto.Bech32;
import com.bloxbean.cardano.client.crypto.Blake2bUtil;
import com.bloxbean.cardano.client.crypto.cip1852.DerivationPath;
import com.bloxbean.cardano.client.util.HexUtil;
import com.bloxbean.cardano.yacicli.commands.localcluster.service.model.DefaultAddress;
import com.bloxbean.cardano.yacicli.common.AnsiColors;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.bloxbean.cardano.yacicli.util.ConsoleWriter.header;
import static com.bloxbean.cardano.yacicli.util.ConsoleWriter.writeLn;

@Component
public class DefaultAddressService {
public static final String ED_25519_E_SK = "ed25519e_sk";
private final String mnemonic = "test test test test test test test test test test test test test test test test test test test test test test test sauce";
private final int noOfAccounts = 20;
private final BigDecimal defaultAdaAmount = new BigDecimal(10000);

private List<DefaultAddress> defaultAddresses = new ArrayList<>();

public List<DefaultAddress> getDefaultAddresses() {
if (defaultAddresses.size() > 0)
return defaultAddresses;

for (int i = 0; i < noOfAccounts; i++) {
DerivationPath derivationPath = DerivationPath.createExternalAddressDerivationPathForAccount(i);
var account = new Account(Networks.testnet(), mnemonic, derivationPath);

var defaultAddress = new DefaultAddress();
defaultAddress.setDerivationPath(derivationPath);
defaultAddress.setAddress(account.baseAddress());
defaultAddress.setStakeAddress(account.stakeAddress());
defaultAddress.setPaymentKey(Bech32.encode(account.privateKeyBytes(), ED_25519_E_SK));
defaultAddress.setStakingKey(Bech32.encode(account.stakeHdKeyPair().getPrivateKey().getKeyData(), ED_25519_E_SK));

String txHash = HexUtil.encodeHexString(Blake2bUtil.blake2bHash256(new Address(account.baseAddress()).getBytes()));
defaultAddress.setDefaultUtxoId(txHash + "#" + 0);
defaultAddress.setDefaultUtxoAmount(defaultAdaAmount);
defaultAddresses.add(defaultAddress);
}

return defaultAddresses;
}

public String getDefaultMnemonic() {
return mnemonic;
}

public void printDefaultAddresses(boolean showDefaultUtxos) {
writeLn("\n#################################");
writeLn(header(AnsiColors.CYAN_BOLD, "Default Addresses"));
writeLn("#################################\n");
writeLn(header(AnsiColors.BLUE_BOLD, "Mnemonic: ") + getDefaultMnemonic());
writeLn("");

var defaultAddresses = getDefaultAddresses();

for (int i = 0; i < defaultAddresses.size(); i++) {
var defaultAddress = defaultAddresses.get(i);
writeLn("%-30s : %s", header(AnsiColors.CYAN_BOLD, "Address" + " #" + i), defaultAddress.getAddress());
writeLn("%-30s : %s", header(AnsiColors.CYAN_BOLD, "Derivation Path"), derivationPathToString(defaultAddress.getDerivationPath()));
writeLn("%-30s : %s", header(AnsiColors.CYAN_BOLD, "Stake Address"), defaultAddress.getStakeAddress());
writeLn("%-30s : %s", header(AnsiColors.CYAN_BOLD, "Payment Key"), defaultAddress.getPaymentKey());
writeLn("%-30s : %s", header(AnsiColors.CYAN_BOLD, "Staking Key"), defaultAddress.getStakingKey());

if (showDefaultUtxos) {
writeLn("%-30s : %s - %s Ada", header(AnsiColors.CYAN_BOLD, "Utxo"), defaultAddress.getDefaultUtxoId(), defaultAddress.getDefaultUtxoAmount());
}
writeLn("");
}
}

private String derivationPathToString(DerivationPath derivationPath) {
var sb = new StringBuilder();
sb.append("m")
.append("/")
.append(derivationPath.getPurpose().getValue() + "'")
.append("/")
.append(derivationPath.getCoinType().getValue() + "'")
.append("/")
.append(derivationPath.getAccount().getValue() + "'")
.append("/")
.append(derivationPath.getRole().getValue())
.append("/")
.append(derivationPath.getIndex().getValue());

return sb.toString();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.bloxbean.cardano.yacicli.commands.localcluster.service.model;

import com.bloxbean.cardano.client.crypto.cip1852.DerivationPath;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class DefaultAddress {
private DerivationPath derivationPath;
private String address;
private String stakeAddress;
private String paymentKey;
private String stakingKey;
private String defaultUtxoId;
private BigDecimal defaultUtxoAmount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,29 @@
"007290ea8fa9433c1045a4c8473959ad608e6c03a58c7de33bdbd3ce6f295b987135610616f3c74e11c94d77b6ced5ccc93a7d719cfb135062": 300000000000,
"605276322ac7882434173dcc6441905f6737689bd309b68ad8b3614fd8": 3000000000000000,
"60a0f1aa7dca95017c11e7e373aebcf0c4568cf47ec12b94f8eb5bba8b": 3000000000000000,
"60ba957a0fff6816021b2afa7900beea68fd10f2d78fb5b64de0d2379c": 3000000000000000
},
"maxKESEvolutions": 60,
"60ba957a0fff6816021b2afa7900beea68fd10f2d78fb5b64de0d2379c": 3000000000000000,
"00c8c47610a36034aac6fc58848bdae5c278d994ff502c05455e3b3ee8f8ed3a0eea0ef835ffa7bbfcde55f7fe9d2cc5d55ea62cecb42bab3c": 10000000000,
"004048ff89ca4f88e66598e620aa0c7128c2145d9a181ae9a4a81ca8e3e849af38840c5562dd382be37c9e76545c8191f9d8f6df1d20cfcee0": 10000000000,
"00ca6e1b1f320d543a24adeabc0aa4627635c7349b639f86f74bdfdd78d31b28c9619a58b3792a7394ab85deb36889c4d7b0632c8167b855d2": 10000000000,
"0007d781fe8e33883e371f9550c2f1087321fc32e06e80b65e349ccb027702d6880e86e77a0520efa37ede45002a1de43b68692e175b742e67": 10000000000,
"00627b2598dd71129167825160c564067d1d245e79cc237094815c5cb2b125e30ec2f4ce4059a069e08c3cd82cdfc9451bfb22487f8a25ceef": 10000000000,
"00c6cf7bd50f37f7e4cc161fc00f07e9b2226ba5552ccaf30d315fa0135bbc8cbd9ab5379f368fc8d3500c37a9d14074cc6ddad89e3686f0e0": 10000000000,
"005164ab186715c86378020956d892cf72f67636b78967d67cfe7360479130dc89cf7a9bc89109f939956b66f93293ade4c3920b72fd40beea": 10000000000,
"003dd38742e9848c6f12c13ddb1f9464fc0ce0bb92102768087975317e5a9f869fcd913562c9b0e0f01f77e5359ea780d37f9355f9702eff8b": 10000000000,
"0088e7e670b45cab2322b518ef7b6f66d30aec0d923dc463e467091a790f67796b9fa71224f2846cebbcf4950c11e040ee124d30f6e164bcd5": 10000000000,
"00c70b8421617802d3f23956cab1957e1d306cd4808589b41760e97927ebfd6053ba12b38288b2b6d5d4c4618d6a8ce59d50580e9c6f704af5": 10000000000,
"00c0933b8238f6f3332e48c34cf1a8e0555943b33cd4abc53aefb7d6124b7ce40dd496bdc02b34602f3a773ff7cccee873991e4c8866f3a70b": 10000000000,
"0069f7d7289de2f01cd1e0265ac5be943b41775abae0ce6b3eac0edee0ce9cadb7cdec2bded3ef8a7bbe3352869bfc1387754c9ee6b1782d9c": 10000000000,
"00709a7070005c652c27df73dbbde3319a90b127bea96aded1c5fb87a59c51dbcf90fa890174497f3f66a0dad06eb7f131e06567995e9c50a5": 10000000000,
"00fc576df3a279885a7a4d0fc500372daa1d96f26c6763542ecd2ad8551753024adea37c134edebb68dc0cfaed5a7009e8305fe1fed8d0ccd1": 10000000000,
"003346a630e6972bf38cce87219db1d63061e7cd324cad88c18e504f2990cac68e973f51256ca938683fa4ea12173d7d047d940fbb883bd0e8": 10000000000,
"0028b862d001e6a64a02b3560cbc532eab4557593477c39cc523e0b9fc527100898c11e731194171b908aad463770d6cbf7ec8871c4cb1e518": 10000000000,
"005e0e57040b06e9d71e0f28f126262838a68db0b52b4fd1b3877dda2203d5d7d4f19c5ee3a1ed51bb670779de19d40aaff2e5e9468cc05c5e": 10000000000,
"00367f65ab69b1e6715c8d5a14964214c9505ed17032266b3209a2c40dcbae9a2a881e603ff39d36e987bacfb87ee98051f222c5fe3efd350c": 10000000000,
"00c5c4ca287f3b53948b5468e5e23b1c87fe61ce52c0d9afd65d070795038751a619d463e91eaed0a774ebdb2f8e12a01a378a153bc3627323": 10000000000,
"00ef198fb7c35e1968308a0b75cfee54a46e13e86dd3354283300831d624165c357b5a0413906a0bfea8ba57587331f0836a186d632ed041b8": 10000000000
},
"maxKESEvolutions": 60,
"maxLovelaceSupply": 45000000000000000,
"networkId": "Testnet",
"networkMagic": ${protocolMagic},
Expand Down
Loading