Java Code Examples for org.bitcoinj.wallet.DeterministicKeyChain#getKeyByPath()

The following examples show how to use org.bitcoinj.wallet.DeterministicKeyChain#getKeyByPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WalletManager.java    From token-core-android with Apache License 2.0 6 votes vote down vote up
public static Wallet findWalletByMnemonic(String chainType, String network, String mnemonic, String path, String segWit) {
  List<String> mnemonicCodes = Arrays.asList(mnemonic.split(" "));
  MnemonicUtil.validateMnemonics(mnemonicCodes);
  DeterministicSeed seed = new DeterministicSeed(mnemonicCodes, null, "", 0L);
  DeterministicKeyChain keyChain = DeterministicKeyChain.builder().seed(seed).build();
  if (Strings.isNullOrEmpty(path)) {
    throw new TokenException(Messages.INVALID_MNEMONIC_PATH);
  }

  if (ChainType.BITCOIN.equalsIgnoreCase(chainType)) {
    path += "/0/0";
  }

  DeterministicKey key = keyChain.getKeyByPath(BIP44Util.generatePath(path), true);
  Network net = new Network(network);
  String address = AddressCreatorManager.getInstance(chainType, net.isMainnet(), segWit).fromPrivateKey(key.getPrivateKeyAsHex());
  return findWalletByAddress(chainType, address);
}
 
Example 2
Source File: HDMnemonicKeystore.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
public HDMnemonicKeystore(Metadata metadata, String password, List<String> mnemonics, String path, String id) {
  MnemonicUtil.validateMnemonics(mnemonics);
  DeterministicSeed seed = new DeterministicSeed(mnemonics, null, "", 0L);
  DeterministicKeyChain keyChain = DeterministicKeyChain.builder().seed(seed).build();
  this.mnemonicPath = path;

  DeterministicKey parent = keyChain.getKeyByPath(BIP44Util.generatePath(path), true);
  NetworkParameters networkParameters = metadata.isMainNet() ? MainNetParams.get() : TestNet3Params.get();
  this.xpub = parent.serializePubB58(networkParameters);
  String xprv = parent.serializePrivB58(networkParameters);
  DeterministicKey mainAddressKey = keyChain.getKeyByPath(BIP44Util.generatePath(path + "/0/0"), true);
  if (Metadata.P2WPKH.equals(metadata.getSegWit())) {
    this.address = new SegWitBitcoinAddressCreator(networkParameters).fromPrivateKey(mainAddressKey.getPrivateKeyAsHex());
  } else {
    this.address = mainAddressKey.toAddress(networkParameters).toBase58();
  }
  if (metadata.getTimestamp() == 0) {
    metadata.setTimestamp(DateUtil.getUTCTime());
  }
  metadata.setWalletType(Metadata.HD);

  this.crypto = Crypto.createPBKDF2CryptoWithKDFCached(password, xprv.getBytes(Charset.forName("UTF-8")));
  this.metadata = metadata;
  this.encMnemonic = crypto.deriveEncPair(password, Joiner.on(" ").join(mnemonics).getBytes());
  this.crypto.clearCachedDerivedKey();

  this.version = VERSION;
  this.info = new Info();
  this.id = Strings.isNullOrEmpty(id) ? UUID.randomUUID().toString() : id;
}
 
Example 3
Source File: ValidatorTest.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testImportingValidationPrivateKey() {
  Metadata metadata = new Metadata();
  metadata.setSource(Metadata.FROM_PRIVATE);
  metadata.setChainType(ChainType.ETHEREUM);
  DeterministicSeed seed = new DeterministicSeed(Arrays.asList(SampleKey.MNEMONIC.split(" ")), null, "", 0L);
  DeterministicKeyChain keyChain = DeterministicKeyChain.builder().seed(seed).build();
  for (int i=0; i< 100; i++) {
    String path = String.format("m/44'/60'/0'/0/%d", i);
    DeterministicKey key = keyChain.getKeyByPath(BIP44Util.generatePath(path), true);
    new PrivateKeyValidator(key.getPrivateKeyAsHex()).validate();
  }

}
 
Example 4
Source File: GetCredentials.java    From Android-Wallet-Token-ERC20 with Apache License 2.0 5 votes vote down vote up
public Credentials FromSeed(String seedCode,
                            String passwordWallet) throws UnreadableWalletException {
    DeterministicSeed seed = new DeterministicSeed(seedCode, null, passwordWallet, 1409478661L);
    DeterministicKeyChain chain = DeterministicKeyChain.builder().seed(seed).build();
    List<ChildNumber> keyPath = HDUtils.parsePath("M/44H/60H/0H/0/0");
    DeterministicKey key = chain.getKeyByPath(keyPath, true);
    BigInteger privKey = key.getPrivKey();
    return Credentials.create(ECKeyPair.create(privKey));
}