org.bitcoinj.crypto.ChildNumber Java Examples

The following examples show how to use org.bitcoinj.crypto.ChildNumber. 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: SeedTest.java    From snowblossom with Apache License 2.0 6 votes vote down vote up
private void testVector(String seed, String data, String pw, String xprv)
  throws Exception
{
  ByteString expected = HexUtil.hexStringToBytes(data);

  ByteString found = SeedUtil.decodeSeed(seed, pw);

  Assert.assertEquals( HexUtil.getHexString(expected), HexUtil.getHexString(found));

  DeterministicKey dk = HDKeyDerivation.createMasterPrivateKey(found.toByteArray());
  DeterministicHierarchy dh = new DeterministicHierarchy(dk);

  DeterministicKey dk_acct = dh.get( ImmutableList.of(
    new ChildNumber(44,true),
    new ChildNumber(0,true),
    new ChildNumber(0,true)),
    true, true);

  Assert.assertEquals(
    xprv,
    dk_acct.serializePrivB58(org.bitcoinj.params.MainNetParams.get()));
}
 
Example #2
Source File: SeedUtil.java    From snowblossom with Apache License 2.0 6 votes vote down vote up
public static String getSeedXpub(NetworkParams params, String seed_str, String pass, int account)
{
  ByteString seed = decodeSeed(seed_str,pass);
  DeterministicKey dk = HDKeyDerivation.createMasterPrivateKey(seed.toByteArray());
  DeterministicHierarchy dh = new DeterministicHierarchy(dk);


  DeterministicKey dk_acct = dh.get( ImmutableList.of(
    	new ChildNumber(44,true),
    	new ChildNumber(params.getBIP44CoinNumber(),true),
    	new ChildNumber(account,true)
	),
    true, true);

  String xpub = dk_acct.serializePubB58( org.bitcoinj.params.MainNetParams.get() );
  return xpub;
}
 
Example #3
Source File: SeedUtil.java    From snowblossom with Apache License 2.0 6 votes vote down vote up
public static ByteString getSeedId(NetworkParams params, String seed_str, String pass, int account)
{
  ByteString seed = decodeSeed(seed_str,pass);
  DeterministicKey dk = HDKeyDerivation.createMasterPrivateKey(seed.toByteArray());
  DeterministicHierarchy dh = new DeterministicHierarchy(dk);


  DeterministicKey dk_acct = dh.get( ImmutableList.of(
    	new ChildNumber(44,true),
    	new ChildNumber(params.getBIP44CoinNumber(),true),
    	new ChildNumber(account,true)
	),
    true, true);

  String xpub = dk_acct.serializePubB58( org.bitcoinj.params.MainNetParams.get() );
  ByteString seed_id = ByteString.copyFrom(dk_acct.getIdentifier());

  return seed_id;
}
 
Example #4
Source File: Tools.java    From thundernetwork with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Call to get the MasterKey for a new Channel.
 * TODO: Change to request master node key..
 *
 * @param number Query the Database to get the latest unused number
 * @return DeterministicKey for the new Channel
 */
public static DeterministicKey getMasterKey (int number) {

    DeterministicKey hd = DeterministicKey.deserializeB58(SideConstants.KEY_B58, Constants.getNetwork());
    //		DeterministicKey hd =  DeterministicKey.deserializeB58(null,KEY_B58);
    //        DeterministicKey hd = HDKeyDerivation.createMasterPrivateKey(KEY.getBytes());
    DeterministicHierarchy hi = new DeterministicHierarchy(hd);

    List<ChildNumber> childList = new ArrayList<ChildNumber>();
    ChildNumber childNumber = new ChildNumber(number, true);
    childList.add(childNumber);

    DeterministicKey key = hi.get(childList, true, true);
    return key;

}
 
Example #5
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
public void createWallet2(String passphrase, Runnable callback) {

        wallet = new Wallet(params);
        DeterministicSeed seed = wallet.getKeyChainSeed();
        mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
        sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));

        walletFriendlyAddress = wallet.currentReceiveAddress().toString();

        ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
        DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
        xprvKey = de.serializePrivB58(params);

        wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

        callback.run();
    }
 
Example #6
Source File: V3MnemonicKeystore.java    From token-core-android with Apache License 2.0 6 votes vote down vote up
private V3MnemonicKeystore(Metadata metadata, String password, List<String> mnemonicCodes, String path, String id) {
  MnemonicUtil.validateMnemonics(mnemonicCodes);
  DeterministicSeed seed = new DeterministicSeed(mnemonicCodes, null, "", 0L);
  DeterministicKeyChain keyChain = DeterministicKeyChain.builder().seed(seed).build();

  this.mnemonicPath = path;
  List<ChildNumber> zeroPath = BIP44Util.generatePath(path);

  byte[] prvKeyBytes = keyChain.getKeyByPath(zeroPath, true).getPrivKeyBytes();
  this.crypto = Crypto.createPBKDF2CryptoWithKDFCached(password, prvKeyBytes);
  this.encMnemonic = crypto.deriveEncPair(password, Joiner.on(" ").join(mnemonicCodes).getBytes());
  this.crypto.clearCachedDerivedKey();

  this.address = AddressCreatorManager.getInstance(metadata.getChainType(), metadata.isMainNet(), metadata.getSegWit()).fromPrivateKey(prvKeyBytes);
  metadata.setTimestamp(DateUtil.getUTCTime());
  metadata.setWalletType(Metadata.V3);
  this.metadata = metadata;
  this.version = VERSION;
  this.id = Strings.isNullOrEmpty(id) ? UUID.randomUUID().toString() : id;
}
 
Example #7
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock2(String mnemonicCode, Runnable callback) {
    if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') {
        mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1);
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.run();

    RequestorBtc.getUTXOListLtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #8
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) {
    mnemonicCode = mnemonicCode.trim();
    if (mnemonicCode.equals("")) {
        callback.onWalletCreated(null);
        return;
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.onWalletCreated(wallet);

    RequestorBtc.getUTXOListLtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #9
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private List<ChildNumber> getPathParent() {
    List<ChildNumber> pathParent = new ArrayList<>();
    pathParent.add(new ChildNumber(88, true));
    pathParent.add(new ChildNumber(0, true));
    pathParent.add(new ChildNumber(0, true));
    return pathParent;
}
 
Example #10
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) {
    mnemonicCode = mnemonicCode.trim();
    if (mnemonicCode.equals("")) {
        callback.onWalletCreated(null);
        return;
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));

    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.onWalletCreated(wallet);


    RequestorBtc.getUTXOList(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            UTXOListResponse utxoResponse = (UTXOListResponse) response;
            setUTXO(utxoResponse.getUTXOList());
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #11
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock2(String mnemonicCode, Runnable callback) {
    if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') {
        mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1);
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));

    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.run();


    RequestorBtc.getUTXOList(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            UTXOListResponse utxoResponse = (UTXOListResponse) response;
            setUTXO(utxoResponse.getUTXOList());
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #12
Source File: KeycardTest.java    From status-keycard with Apache License 2.0 5 votes vote down vote up
private DeterministicKey deriveKey(KeyPair keyPair, byte[] chainCode, int[] path) {
  DeterministicKey key = HDKeyDerivation.createMasterPrivKeyFromBytes(((org.bouncycastle.jce.interfaces.ECPrivateKey) keyPair.getPrivate()).getD().toByteArray(), chainCode);

  for (int i : path) {
    key = HDKeyDerivation.deriveChildKey(key, new ChildNumber(i));
  }

  return key;
}
 
Example #13
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) {
    mnemonicCode = mnemonicCode.trim();
    if (mnemonicCode.equals("")) {
        callback.onWalletCreated(null);
        return;
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.onWalletCreated(wallet);

    RequestorBtc.getUTXOListBtgNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #14
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock2(String mnemonicCode, Runnable callback) {
    if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') {
        mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1);
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.run();

    RequestorBtc.getUTXOListBtgNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #15
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));
}
 
Example #16
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) {
    mnemonicCode = mnemonicCode.trim();
    if (mnemonicCode.equals("")) {
        callback.onWalletCreated(null);
        return;
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));

    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.onWalletCreated(wallet);


    RequestorBtc.getUTXOListBch(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #17
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock2(String mnemonicCode, Runnable callback) {
    if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') {
        mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1);
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));

    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.run();


    RequestorBtc.getUTXOListBch(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #18
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock2(String mnemonicCode, Runnable callback) {
    if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') {
        mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1);
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.run();

    RequestorBtc.getUTXOListSbtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #19
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) {
    mnemonicCode = mnemonicCode.trim();
    if (mnemonicCode.equals("")) {
        callback.onWalletCreated(null);
        return;
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.onWalletCreated(wallet);

    RequestorBtc.getUTXOListSbtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItem> utxos = (List<UTXOItem>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #20
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock2(String mnemonicCode, Runnable callback) {
    if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') {
        mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1);
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    callback.run();

    RequestorBtc.getUTXOListDgbNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItemDgb> utxos = (List<UTXOItemDgb>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example #21
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) {
    mnemonicCode = mnemonicCode.trim();
    if (mnemonicCode.equals("")) {
        callback.onWalletCreated(null);
        return;
    }

    DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0);

    wallet = Wallet.fromSeed(params, seed);
    mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode());
    //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));
    walletFriendlyAddress = wallet.currentReceiveAddress().toString();

    ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT);
    DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber));
    xprvKey = de.serializePrivB58(params);

    wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params);

    RequestorBtc.getUTXOListDgbNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItemDgb> utxos = (List<UTXOItemDgb>)response;
            setUTXO(utxos);
            callback.onWalletCreated(wallet);
        }

        @Override
        public void onFailure(String msg) {
            callback.onWalletCreated(wallet);
        }
    });
}
 
Example #22
Source File: HDMnemonicKeystore.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
public String newReceiveAddress(int nextIdx) {
  NetworkParameters networkParameters = this.metadata.isMainNet() ? MainNetParams.get() : TestNet3Params.get();
  DeterministicKey key = DeterministicKey.deserializeB58(this.xpub, networkParameters);
  DeterministicKey changeKey = HDKeyDerivation.deriveChildKey(key, ChildNumber.ZERO);
  DeterministicKey indexKey = HDKeyDerivation.deriveChildKey(changeKey, new ChildNumber(nextIdx));
  if (Metadata.P2WPKH.equals(metadata.getSegWit())) {
    return new SegWitBitcoinAddressCreator(networkParameters).fromPrivateKey(indexKey).toBase58();
  } else {
    return indexKey.toAddress(networkParameters).toBase58();
  }
}
 
Example #23
Source File: BIP44Util.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
public static ImmutableList<ChildNumber> generatePath(String path) {
  List<ChildNumber> list = new ArrayList<>();
  for (String p : path.split("/")) {
    if ("m".equalsIgnoreCase(p) || "".equals(p.trim())) {
      continue;
    } else if (p.charAt(p.length() - 1) == '\'') {
      list.add(new ChildNumber(Integer.parseInt(p.substring(0, p.length() - 1)), true));
    } else {
      list.add(new ChildNumber(Integer.parseInt(p), false));
    }
  }

  ImmutableList.Builder<ChildNumber> builder = ImmutableList.builder();
  return builder.addAll(list).build();
}
 
Example #24
Source File: BitcoinTransaction.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
private void collectPrvKeysAndAddress(String segWit, String password, Wallet wallet) {
  this.network = wallet.getMetadata().isMainNet() ? MainNetParams.get() : TestNet3Params.get();
  if (wallet.getMetadata().getSource().equals(Metadata.FROM_WIF)) {
    changeAddress = Address.fromBase58(network, wallet.getAddress());
    BigInteger prvKey = DumpedPrivateKey.fromBase58(network, wallet.exportPrivateKey(password)).getKey().getPrivKey();
    prvKeys = Collections.singletonList(prvKey);
  } else {
    prvKeys = new ArrayList<>(getOutputs().size());
    String xprv = new String(wallet.decryptMainKey(password), Charset.forName("UTF-8"));
    DeterministicKey xprvKey = DeterministicKey.deserializeB58(xprv, network);
    DeterministicKey changeKey = HDKeyDerivation.deriveChildKey(xprvKey, ChildNumber.ONE);
    DeterministicKey indexKey = HDKeyDerivation.deriveChildKey(changeKey, new ChildNumber(getChangeIdx(), false));
    if (Metadata.P2WPKH.equals(segWit)) {
      changeAddress = new SegWitBitcoinAddressCreator(network).fromPrivateKey(indexKey);
    } else {
      changeAddress = indexKey.toAddress(network);
    }

    for (UTXO output : getOutputs()) {
      String derivedPath = output.getDerivedPath().trim();
      String[] pathIdxs = derivedPath.replace('/', ' ').split(" ");
      int accountIdx = Integer.parseInt(pathIdxs[0]);
      int changeIdx = Integer.parseInt(pathIdxs[1]);

      DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(xprvKey, new ChildNumber(accountIdx, false));
      DeterministicKey externalChangeKey = HDKeyDerivation.deriveChildKey(accountKey, new ChildNumber(changeIdx, false));
      prvKeys.add(externalChangeKey.getPrivKey());
    }
  }
}
 
Example #25
Source File: BisqKeyChainFactory.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DeterministicKeyChain makeWatchingKeyChain(Protos.Key key, Protos.Key firstSubKey, DeterministicKey accountKey,
                                                  boolean isFollowingKey, boolean isMarried) throws UnreadableWalletException {
    ImmutableList<ChildNumber> accountPath = useBitcoinDeterministicKeyChain ? BtcDeterministicKeyChain.BIP44_BTC_ACCOUNT_PATH : BisqDeterministicKeyChain.BIP44_BSQ_ACCOUNT_PATH;
    if (!accountKey.getPath().equals(accountPath))
        throw new UnreadableWalletException("Expecting account key but found key with path: " +
                HDUtils.formatPath(accountKey.getPath()));
    return useBitcoinDeterministicKeyChain ? new BtcDeterministicKeyChain(accountKey, isFollowingKey) : new BisqDeterministicKeyChain(accountKey, isFollowingKey);
}
 
Example #26
Source File: BisqKeyChainFactory.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DeterministicKeyChain makeWatchingKeyChain(Protos.Key key, Protos.Key firstSubKey, DeterministicKey accountKey,
                                                  boolean isFollowingKey, boolean isMarried) throws UnreadableWalletException {
    ImmutableList<ChildNumber> accountPath = useBitcoinDeterministicKeyChain ? BtcDeterministicKeyChain.BIP44_BTC_ACCOUNT_PATH : BisqDeterministicKeyChain.BIP44_BSQ_ACCOUNT_PATH;
    if (!accountKey.getPath().equals(accountPath))
        throw new UnreadableWalletException("Expecting account key but found key with path: " +
                HDUtils.formatPath(accountKey.getPath()));
    return useBitcoinDeterministicKeyChain ? new BtcDeterministicKeyChain(accountKey, isFollowingKey) : new BisqDeterministicKeyChain(accountKey, isFollowingKey);
}
 
Example #27
Source File: BisqDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected ImmutableList<ChildNumber> getAccountPath() {
    return BIP44_BSQ_ACCOUNT_PATH;
}
 
Example #28
Source File: BtcDeterministicKeyChain.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected ImmutableList<ChildNumber> getAccountPath() {
    return BIP44_BTC_ACCOUNT_PATH;
}
 
Example #29
Source File: SeedTest.java    From snowblossom with Apache License 2.0 4 votes vote down vote up
private void testGen(int words)
  throws Exception
{
  String seed = SeedUtil.generateSeed(words);

  List<String> lst = SeedUtil.getWordsFromSeed(seed);

  Assert.assertEquals(words, lst.size());

  ByteString seed_data = SeedUtil.decodeSeed(seed, "");
  System.out.println("Seed: " + seed + " " + HexUtil.getHexString(seed_data));

  DeterministicKey dk = HDKeyDerivation.createMasterPrivateKey(seed_data.toByteArray());
  DeterministicHierarchy dh = new DeterministicHierarchy(dk);

  System.out.println("Seed dk: " + dk.toString());

  System.out.println("Seed ser: " + dk.serializePrivB58(org.bitcoinj.params.MainNetParams.get()));

  DeterministicKey dk_acct = dh.get( ImmutableList.of(
    new ChildNumber(44,true),
    new ChildNumber(0,true),
    new ChildNumber(0,true)),
    true, true);
  System.out.println("Seed ser: " + dk_acct.serializePubB58(org.bitcoinj.params.MainNetParams.get()));

  System.out.println("Seed acct: " + dk_acct.toString());
  System.out.println("Seed acct xprv: " + dk_acct.serializePrivB58(org.bitcoinj.params.MainNetParams.get()));

  String xpub = SeedUtil.getSeedXpub( new NetworkParamsTestnet(), seed, "", 0);

for(int c=0; c<2; c++)
for(int i=0; i<20; i++)
{
  	WalletKeyPair wkp = SeedUtil.getKey( new NetworkParamsTestnet(), seed, "", 0, c, i);

    System.out.println("Seed wkp: " + HexUtil.getHexString(wkp.getSeedId()) + " " + wkp.getHdPath());
    Assert.assertTrue(wkp.getHdPath().startsWith("M/44H/2339H/0H/"));
    Assert.assertTrue(wkp.getSeedId().size()==20);

    AddressSpecHash addr = AddressUtil.getHashForSpec(SeedUtil.getAddressSpec(new NetworkParamsTestnet(), xpub, c, i));

    AddressSpecHash wkp_addr = AddressUtil.getHashForSpec(AddressUtil.getSimpleSpecForKey(wkp));

    Assert.assertEquals(addr, wkp_addr);

	testKeyPair(wkp, "hd");
}

  ByteString seed_id_seed = SeedUtil.getSeedId( new NetworkParamsTestnet(), seed, "", 0);
  ByteString seed_id_xpub = SeedUtil.getSeedIdFromXpub( xpub);
  Assert.assertEquals(seed_id_seed, seed_id_xpub);

}
 
Example #30
Source File: KeyChainTransactionSigner.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected SignatureAndKey getSignature(Sha256Hash sighash, List<ChildNumber> derivationPath) {
    ImmutableList<ChildNumber> keyPath = ImmutableList.copyOf(derivationPath);
    DeterministicKey key = keyChain.getKeyByPath(keyPath, true);
    return new SignatureAndKey(key.sign(sighash), key.dropPrivateBytes().dropParent());
}