Skip to content

Commit

Permalink
DeterministicSeed: New method getMnemonicString() to do the string co…
Browse files Browse the repository at this point in the history
…ncatenation.

(cherry picked from commit 6b309e486f23e9bf9bcbfd17d501bc6b6af279fc)
  • Loading branch information
Andreas Schildbach authored and HashEngineering committed Jun 25, 2024
1 parent 9e8bc19 commit ed21434
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions core/src/main/java/org/bitcoinj/wallet/DeterministicSeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public class DeterministicSeed implements EncryptableItem {
@Nullable private final EncryptedData encryptedSeed;
private long creationTimeSeconds;

public DeterministicSeed(String mnemonicCode, byte[] seed, String passphrase, long creationTimeSeconds) throws UnreadableWalletException {
this(decodeMnemonicCode(mnemonicCode), seed, passphrase, creationTimeSeconds);
public DeterministicSeed(String mnemonicString, byte[] seed, String passphrase, long creationTimeSeconds) throws UnreadableWalletException {
this(decodeMnemonicCode(mnemonicString), seed, passphrase, creationTimeSeconds);
}

public DeterministicSeed(byte[] seed, List<String> mnemonic, long creationTimeSeconds) {
Expand Down Expand Up @@ -139,11 +139,11 @@ public String toString() {
}

public String toString(boolean includePrivate) {
MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this);
MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this).omitNullValues();
if (isEncrypted())
helper.addValue("encrypted");
else if (includePrivate)
helper.addValue(toHexString()).add("mnemonicCode", Utils.SPACE_JOINER.join(mnemonicCode));
helper.addValue(toHexString()).add("mnemonicCode", getMnemonicString());
else
helper.addValue("unencrypted");
return helper.toString();
Expand Down Expand Up @@ -200,7 +200,7 @@ public DeterministicSeed encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) {
}

private byte[] getMnemonicAsBytes() {
return Utils.SPACE_JOINER.join(mnemonicCode).getBytes(StandardCharsets.UTF_8);
return getMnemonicString().getBytes(StandardCharsets.UTF_8);
}

public DeterministicSeed decrypt(KeyCrypter crypter, String passphrase, KeyParameter aesKey) {
Expand Down Expand Up @@ -247,6 +247,12 @@ public List<String> getMnemonicCode() {
return mnemonicCode;
}

/** Get the mnemonic code as string, or null if unknown. */
@Nullable
public String getMnemonicString() {
return mnemonicCode != null ? Utils.SPACE_JOINER.join(mnemonicCode) : null;
}

private static List<String> decodeMnemonicCode(byte[] mnemonicCode) {
return decodeMnemonicCode(new String(mnemonicCode, StandardCharsets.UTF_8));
}
Expand Down

0 comments on commit ed21434

Please sign in to comment.