8000 fix(mnemonic): support derived paths for importing mnemonic word by lxcmyf · Pull Request #817 · tronprotocol/wallet-cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(mnemonic): support derived paths for importing mnemonic word #817

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

Merged
merged 1 commit into from
Mar 18, 2025
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1590,15 +1590,15 @@ Example:
wallet> ExportWalletKeystore tronlink /tmp
Please input your password.
password:
exported keystore file : /tmp/TYdhEg8b7tXm92UDbRDXPtJNU6T9xVGbbo.txt
exported keystore file : /tmp/TYdhEg8b7tXm92UDbRDXPtJNU6T9xVGbbo.json
exportWalletKeystore successful !!
```
>ImportWalletByKeystore
>import the keystore file of tronlink wallet to wallet-cli

Example:
```console
wallet> ImportWalletByKeystore tronlink /tmp/tronlink.txt
wallet> ImportWalletByKeystore tronlink /tmp/tronlink.json
Please input password.
password:
Please input password again.
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/org/tron/ledger/wrapper/HidServicesWrapper.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package org.tron.ledger.wrapper;

import lombok.Getter;
import static org.tron.ledger.console.ConsoleColor.ANSI_RED;
import static org.tron.ledger.console.ConsoleColor.ANSI_RESET;
import static org.tron.ledger.console.ConsoleColor.ANSI_YELLOW;

import java.util.ArrayList;
import java.util.List;
import org.hid4java.HidDevice;
import org.hid4java.HidManager;
import org.hid4java.HidServices;
import org.hid4java.HidServicesSpecification;
import org.tron.ledger.listener.LedgerEventListener;
import org.tron.ledger.sdk.LedgerConstant;

import java.util.ArrayList;
import java.util.List;

import static org.tron.ledger.console.ConsoleColor.ANSI_RED;
import static org.tron.ledger.console.ConsoleColor.ANSI_RESET;
import static org.tron.ledger.console.ConsoleColor.ANSI_YELLOW;

public class HidServicesWrapper {
private HidServices hidServices;
private HidDevice hidDevice;
Expand Down Expand Up @@ -57,11 +55,11 @@ public HidServices initHidServices() {
hidServicesSpecification.setAutoStart(false);
hidServicesSpecification.setAutoDataRead(true);
hidServicesSpecification.setDataReadInterval(1000);
HidServices hidServices = HidManager.getHidServices(hidServicesSpecification);
hidServices.addHidServicesListener(LedgerEventListener.getInstance());
hidServices.start();
HidServices hs = HidManager.getHidServices(hidServicesSpecification);
hs.addHidServicesListener(LedgerEventListener.getInstance());
hs.start();

return hidServices;
return hs;
}


Expand Down
21 changes: 6 additions & 15 deletions src/main/java/org/tron/mnemonic/MnemonicUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.apache.commons.lang3.ArrayUtils;
import org.tron.common.crypto.SignInterface;
import org.tron.common.utils.ByteArray;
import org.tron.core.exception.CipherException;
import org.web3j.crypto.Bip32ECKeyPair;
import org.web3j.crypto.Credentials;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.ArrayUtils;
import org.tron.common.crypto.SignInterface;
import org.tron.common.utils.ByteArray;
import org.tron.core.exception.CipherException;
import org.web3j.crypto.Bip32ECKeyPair;
import org.web3j.crypto.Credentials;

public class MnemonicUtils {
private static final String FilePath = "Mnemonic";
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/tron/mnemonic/SubAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.typesafe.config.Config;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.AttributedStringBuilder;
Expand Down Expand Up @@ -40,6 +41,9 @@ public class SubAccount {

private static final String PATH_PREFIX = "m/44'/195'/";
private static final String PATH_MIDDLE = "'/0/";
@Getter
@Setter
private int type; // 0:subAccount, 1:importWalletByMnemonic

@Data
@Builder
Expand All @@ -66,13 +70,14 @@ public String getDetailString() {
}
}

public SubAccount(byte[] password, String mnemonic) throws Exception {
public SubAccount(byte[] password, String mnemonic, int type) throws Exception {
Config config = Configuration.getByPath("config.conf");
if (config.hasPath("crypto.engine")) {
isEckey = config.getString("crypto.engine").equalsIgnoreCase("eckey");
}
this.mnemonic = mnemonic;
this.password = password;
this.type = type;
this.terminal = TerminalBuilder.builder()
.system(true)
.dumb(true)
Expand Down Expand Up @@ -217,11 +222,11 @@ private boolean handleSelectAddress(int selectedIndex) throws Exception {
, MnemonicUtils.stringToMnemonicWords(mnemonic)
);
String keystoreName = WalletApi.store2Keystore(walletFile);
System.out.println("Generate a sub account successful, keystore file name is " + keystoreName);
System.out.println(getStringByType(getType()) + " successful, keystore file name is " + keystoreName);
selected.setGenerated(true);
return true;
} else {
System.out.println(selected.getDetailString() + ", this sub account already exists.");
System.out.println(selected.getDetailString() + ", this address already exists.");
return false;
}
} else {
Expand All @@ -237,7 +242,6 @@ private boolean handleSelectAddress(int selectedIndex) throws Exception {
}
}


private void showError(String message) {
terminal.writer().println("\n" + message);
terminal.writer().println("Press Enter to continue...");
Expand Down Expand Up @@ -266,7 +270,7 @@ public void start() throws Exception {
break;
}

terminal.writer().println("\n=== Sub Account Generator ===");
terminal.writer().println("\n=== " + getStringByType(getType()) + " Generator ===");

terminal.writer().println("-------------------------------");
terminal.writer().println("Default Address: " + walletAddress.getAddress());
Expand Down Expand Up @@ -315,7 +319,7 @@ public void genDefaultPath(String path, WalletAddress walletAddress) throws Exce
, MnemonicUtils.stringToMnemonicWords(mnemonic)
);
String keystoreName = WalletApi.store2Keystore(walletFile);
System.out.println("Generate a sub account successful, keystore file name is " + keystoreName);
System.out.println(getStringByType(getType()) + " successful, keystore file name is " + keystoreName);

try {
int subAccountIndex = getSubAccountIndex(path);
Expand Down Expand Up @@ -379,11 +383,15 @@ public boolean generateByCustomPath() {
return true;
}

public String getStringByType(int type) {
return type == 0 ? "GenerateSubAccount" : "importWalletByMnemonic";
}

private void generateSubAccountByCustomPath(String path) throws CipherException, IOException {
WalletAddress walletAddress = this.generateWalletAddressByCustomPath(
mnemonic, path);
if (walletAddress == null) {
System.out.println("Generate Subaccount by Custom Path failed");
System.out.println(getStringByType(getType()) + " by Custom Path failed");
return;
}
if (MnemonicUtils.generatedAddress(walletAddress.getAddress())) {
Expand All @@ -398,7 +406,7 @@ private void generateSubAccountByCustomPath(String path) throws CipherException,
.append("\n");
terminal.writer().println(result.toAnsi());
terminal.flush();
String response = reader.readLine("Input y/yes to generate the subaccount? (y/yes): ").trim().toLowerCase();
String response = reader.readLine("Input y/yes to " + getStringByType(getType()) + "? (y/yes): ").trim().toLowerCase();
if (!response.equalsIgnoreCase("y")
&& !response.equalsIgnoreCase("yes")) {
return;
Expand All @@ -408,7 +416,7 @@ private void generateSubAccountByCustomPath(String path) throws CipherException,
, MnemonicUtils.stringToMnemonicWords(mnemonic)
);
String keystoreName = WalletApi.store2Keystore(walletFile);
System.out.println("Generate a sub account successful, keystore file name is " + keystoreName);
System.out.println(getStringByType(getType()) + " successful, keystore file name is " + keystoreName);

try {
int subAccountIndex = getSubAccountIndex(path);
Expand Down
91 changes: 57 additions & 34 deletions src/main/java/org/tron/walletcli/Client.java
< F438 td id="diff-e5af621ff4f8a0767433be3b8e8c8961795b4eb6163d75ba93b4852805f7ef5bL5" data-line-number="5" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package org.tron.walletcli;

import static org.tron.keystore.StringUtils.char2Byte;
import static org.tron.ledger.console.ConsoleColor.ANSI_RED;
import static org.tron.ledger.console.ConsoleColor.ANSI_RESET;

import com.beust.jcommander.JCommander;
import com.google.common.primitives.Longs;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.util.internal.StringUtil;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.beust.jcommander.JCommander;
import com.google.common.primitives.Longs;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.bouncycastle.util.encoders.Hex;
import org.hid4java.HidDevice;
Expand All @@ -26,7 +38,30 @@
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.tron.api.GrpcAPI.*;
import org.tron.api.GrpcAPI.AccountNetMessage;
import org.tron.api.GrpcAPI.AccountResourceMessage;
import org.tron.api.GrpcAPI.AddressPrKeyPairMessage;
import org.tron.api.GrpcAPI.AssetIssueList;
import org.tron.api.GrpcAPI.BlockExtention;
import org.tron.api.GrpcAPI.BlockList;
import org.tron.api.GrpcAPI.BlockListExtention;
import org.tron.api.GrpcAPI.CanDelegatedMaxSizeResponseMessage;
import org.tron.api.GrpcAPI.CanWithdrawUnfreezeAmountResponseMessage;
import org.tron.api.GrpcAPI.DelegatedResourceList;
import org.tron.api.GrpcAPI.ExchangeList;
import org.tron.api.GrpcAPI.GetAvailableUnfreezeCountResponseMessage;
import org.tron.api.GrpcAPI.Node;
import org.tron.api.GrpcAPI.NodeList;
import org.tron.api.GrpcAPI.Note;
import org.tron.api.GrpcAPI.NumberMessage;
import org.tron.api.GrpcAPI.PricesResponseMessage;
import org.tron.api.GrpcAPI.ProposalList;
import org.tron.api.GrpcAPI.TransactionApprovedList;
import org.tron.api.GrpcAPI.TransactionInfoList;
import org.tron.api.GrpcAPI.TransactionList;
import org.tron.api.GrpcAPI.TransactionListExtention;
import org.tron.api.GrpcAPI.TransactionSignWeight;
import org.tron.api.GrpcAPI.WitnessList;
import org.tron.common.crypto.Hash;
import org.tron.common.crypto.SignInterface;
import org.tron.common.crypto.SignUtils;
Expand All @@ -53,32 +88,27 @@
import org.tron.core.zen.address.PaymentAddress;
import org.tron.core.zen.address.SpendingKey;
import org.tron.keystore.StringUtils;

import org.tron.ledger.TronLedgerGetAddress;
import org.tron.ledger.listener.TransactionSignManager;
import org.tron.ledger.wrapper.LedgerUserHelper;

import org.tron.mnemonic.MnemonicUtils;
import org.tron.protos.Protocol.MarketOrder;
import org.tron.protos.Protocol.MarketOrderList;
import org.tron.protos.Protocol.MarketOrderPairList;
import org.tron.protos.Protocol.MarketPriceList;
import org.tron.protos.contract.AssetIssueContractOuterClass.AssetIssueContract;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.Block;
import org.tron.protos.Protocol.ChainParameters;
import org.tron.protos.Protocol.DelegatedResourceAccountIndex;
import org.tron.protos.Protocol.Exchange;
import org.tron.protos.Protocol.MarketOrder;
import org.tron.protos.Protocol.MarketOrderList;
import org.tron.protos.Protocol.MarketOrderPairList;
import org.tron.protos.Protocol.MarketPriceList;
import org.tron.protos.Protocol.Proposal;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
import org.tron.protos.Protocol.Transaction;
import org.tron.protos.Protocol.TransactionInfo;
import org.tron.protos.contract.AssetIssueContractOuterClass.AssetIssueContract;
import org.tron.protos.contract.Common.ResourceCode;
import org.tron.protos.contract.SmartContractOuterClass.SmartContract;
import org.tron.protos.contract.SmartContractOuterClass.SmartContractDataWrapper;
import org.tron.walletserver.WalletApi;
import org.tron.protos.contract.Common.ResourceCode;

import static org.tron.ledger.console.ConsoleColor.ANSI_RED;
import static org.tron.ledger.console.ConsoleColor.ANSI_RESET;


public class Client {
Expand Down Expand Up @@ -600,21 +630,14 @@ private void importWalletByMnemonic() throws CipherException, IOException {

char[] password = Utils.inputPassword2Twice();
List<String> mnemonicWords = inputMnemonicWords();

byte[] priKey = MnemonicUtils.getPrivateKeyFromMnemonic(mnemonicWords);

String fileName = walletApiWrapper.importWallet(password, priKey, mnemonicWords);
mnemonicWords.clear();
StringUtils.clear(password);
StringUtils.clear(priKey);

if (null == fileName) {
System.out.println("Import wallet failed !!");
return;
boolean result = walletApiWrapper.importWalletByMnemonic(mnemonicWords, char2Byte(password));
if (!result) {
System.out.println("importWalletByMnemonic failed.");
}
System.out.println("Import a wallet successful, keystore file : ."
+ File.separator + "Wallet" + File.separator
+ fileName);
if (mnemonicWords != null && !mnemonicWords.isEmpty()) {
mnemonicWords.clear();
}
StringUtils.clear(password);
}

private void importWalletByLedger() throws CipherException, IOException {
Expand Down Expand Up @@ -829,7 +852,7 @@ private void importWalletByKeystore(String[] parameters) throws CipherException,

System.out.println("Please enter the password for the keystore file, enter it once.");
char[] keystorePassword = Utils.inputPasswordWithoutCheck();
byte[] keystorepasswdByte = StringUtils.char2Byte(keystorePassword);
byte[] keystorepasswdByte = char2Byte(keystorePassword);

try {
String fileName = walletApiWrapper.importWalletByKeystore(keystorepasswdByte, importFile);
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/org/tron/walletcli/WalletApiWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public boolean generateSubAccount() throws CipherException, IOException {
if (mnemonic == null || mnemonic.length == 0) {
return false;
}
subAccount = new SubAccount(passwd, new String(mnemonic));
subAccount = new SubAccount(passwd, new String(mnemonic), 0);
subAccount.start();
} catch (Exception e) {
System.out.println("Warning: GenerateSubAccount failed, e :" + e.getMessage());
Expand All @@ -411,6 +411,26 @@ public boolean generateSubAccount() throws CipherException, IOException {
return true;
}

public boolean importWalletByMnemonic(List<String> mnemonicWords, byte[] passwd) throws CipherException, IOException {
SubAccount subAccount = null;
try {
if (mnemonicWords == null || mnemonicWords.isEmpty()) {
return false;
}
subAccount = new SubAccount(passwd, String.join(" ", mnemonicWords), 1);
subAccount.start();
} catch (Exception e) {
System.out.println("Warning: importWalletByMnemonic failed, e :" + e.getMessage());
return false;
} finally {
if (subAccount != null) {
subAccount.clearSensitiveData();
}
StringUtils.clear(passwd);
}
return true;
}

//password is current, will be enc by password2.
public byte[] backupWallet() throws IOException, CipherException {
if (wallet == null || !wallet.isLoginState()) {
Expand Down
Loading
0