forked from bitcoinj/bitcoinj
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kmels/release-0.14
Add BIP47 classes
- Loading branch information
Showing
22 changed files
with
2,317 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.bitcoinj.crypto.bip47; | ||
|
||
import org.bitcoinj.wallet.bip47.PaymentCode; | ||
|
||
import org.bitcoinj.core.Address; | ||
import org.bitcoinj.core.ECKey; | ||
import org.bitcoinj.core.NetworkParameters; | ||
import org.bitcoinj.crypto.ChildNumber; | ||
import org.bitcoinj.crypto.DeterministicKey; | ||
import org.bitcoinj.crypto.HDKeyDerivation; | ||
|
||
import static org.bitcoinj.wallet.bip47.PaymentCode.createMasterPubKeyFromPaymentCode; | ||
|
||
/** | ||
* Created by jimmy on 8/4/17. | ||
*/ | ||
|
||
public class Account { | ||
private NetworkParameters mNetworkParameters; | ||
private DeterministicKey mKey; | ||
private int mIndex; | ||
private PaymentCode mPaymentCode; | ||
private String mXPub; | ||
|
||
public Account(NetworkParameters parameters, DeterministicKey deterministicKey, int index) { | ||
mNetworkParameters = parameters; | ||
mIndex = index; | ||
mKey = HDKeyDerivation.deriveChildKey(deterministicKey, mIndex | ChildNumber.HARDENED_BIT); | ||
mPaymentCode = new PaymentCode(mKey.getPubKey(), mKey.getChainCode()); | ||
mXPub = mKey.serializePubB58(parameters); | ||
} | ||
|
||
public Account(NetworkParameters parameters, String strPaymentCode) { | ||
mNetworkParameters = parameters; | ||
mIndex = 0; | ||
mKey = createMasterPubKeyFromPaymentCode(strPaymentCode); | ||
mPaymentCode = new PaymentCode(strPaymentCode); | ||
mXPub = mKey.serializePubB58(parameters); | ||
} | ||
|
||
public String getStringPaymentCode() { | ||
return mPaymentCode.toString(); | ||
} | ||
|
||
public String getXPub() { | ||
return mXPub; | ||
} | ||
|
||
public Address getNotificationAddress() { | ||
return HDKeyDerivation.deriveChildKey(mKey, ChildNumber.ZERO).toAddress(mNetworkParameters); | ||
} | ||
|
||
public ECKey getNotificationKey() { | ||
return HDKeyDerivation.deriveChildKey(mKey, ChildNumber.ZERO); | ||
} | ||
|
||
public PaymentCode getPaymentCode() { | ||
return mPaymentCode; | ||
} | ||
|
||
public org.bitcoinj.crypto.bip47.Address addressAt(int idx) { | ||
return new org.bitcoinj.crypto.bip47.Address(mNetworkParameters, mKey, idx); | ||
} | ||
|
||
public ECKey keyAt(int idx) { | ||
return HDKeyDerivation.deriveChildKey(mKey, new ChildNumber(idx, false)); | ||
} | ||
|
||
public byte[] getPrivKey(int index) { | ||
return HDKeyDerivation.deriveChildKey(mKey, index).getPrivKeyBytes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// Source code recreated from a .class file by IntelliJ IDEA | ||
// (powered by Fernflower decompiler) | ||
// | ||
|
||
package org.bitcoinj.crypto.bip47; | ||
|
||
import java.math.BigInteger; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.bitcoinj.core.ECKey; | ||
import org.bitcoinj.core.NetworkParameters; | ||
import org.bitcoinj.core.Utils; | ||
import org.bitcoinj.crypto.ChildNumber; | ||
import org.bitcoinj.crypto.DeterministicKey; | ||
import org.bitcoinj.crypto.HDKeyDerivation; | ||
|
||
public class Address { | ||
private int childNum; | ||
private String strPath = null; | ||
private ECKey ecKey = null; | ||
private byte[] pubKey = null; | ||
private byte[] pubKeyHash = null; | ||
private NetworkParameters params = null; | ||
|
||
private Address() { | ||
} | ||
|
||
public Address(NetworkParameters params, DeterministicKey cKey, int child) { | ||
this.params = params; | ||
this.childNum = child; | ||
DeterministicKey dk = HDKeyDerivation.deriveChildKey(cKey, new ChildNumber(this.childNum, false)); | ||
if(dk.hasPrivKey()) { | ||
byte[] now = ArrayUtils.addAll(new byte[1], dk.getPrivKeyBytes()); | ||
this.ecKey = ECKey.fromPrivate(new BigInteger(now), true); | ||
} else { | ||
this.ecKey = ECKey.fromPublicOnly(dk.getPubKey()); | ||
} | ||
|
||
long now1 = Utils.now().getTime() / 1000L; | ||
this.ecKey.setCreationTimeSeconds(now1); | ||
this.pubKey = this.ecKey.getPubKey(); | ||
this.pubKeyHash = this.ecKey.getPubKeyHash(); | ||
this.strPath = dk.getPathAsString(); | ||
} | ||
|
||
public byte[] getPubKey() { | ||
return this.pubKey; | ||
} | ||
|
||
public byte[] getPubKeyHash() { | ||
return this.pubKeyHash; | ||
} | ||
|
||
public String getAddressString() { | ||
return this.ecKey.toAddress(this.params).toString(); | ||
} | ||
|
||
public String getPrivateKeyString() { | ||
return this.ecKey.hasPrivKey()?this.ecKey.getPrivateKeyEncoded(this.params).toString():null; | ||
} | ||
|
||
public org.bitcoinj.core.Address getAddress() { | ||
return this.ecKey.toAddress(this.params); | ||
} | ||
|
||
public String getPath() { | ||
return this.strPath; | ||
} | ||
|
||
/*STASH:FIXME*/ | ||
/*public JSONObject toJSON() { | ||
try { | ||
JSONObject ex = new JSONObject(); | ||
ex.put("address", this.getAddressString()); | ||
if(this.ecKey.hasPrivKey()) { | ||
ex.put("key", this.getPrivateKeyString()); | ||
} | ||
ex.put("path", this.getPath()); | ||
return ex; | ||
} catch (JSONException var2) { | ||
throw new RuntimeException(var2); | ||
} | ||
}*/ | ||
} |
52 changes: 52 additions & 0 deletions
52
core/src/main/java/org/bitcoinj/crypto/bip47/Blockchain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* Copyright (c) 2017 Stash | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.bitcoinj.crypto.bip47; | ||
|
||
import org.bitcoinj.core.NetworkParameters; | ||
|
||
public class Blockchain { | ||
private int mId; | ||
private NetworkParameters mNetworkParameters; | ||
private final String mCoin; | ||
private final String mLabel; | ||
|
||
|
||
public Blockchain(int id, NetworkParameters networkParameters, String coin, String label) { | ||
mId = id; | ||
mNetworkParameters = networkParameters; | ||
mCoin = coin; | ||
mLabel = label; | ||
} | ||
|
||
public int getId() { | ||
return mId; | ||
} | ||
|
||
public void setId(int mId) { | ||
this.mId = mId; | ||
} | ||
|
||
public NetworkParameters getNetworkParameters() { | ||
return mNetworkParameters; | ||
} | ||
|
||
public void setNetworkParameters(NetworkParameters mNetworkParameters) { | ||
this.mNetworkParameters = mNetworkParameters; | ||
} | ||
|
||
public String getCoin() { | ||
return mCoin; | ||
} | ||
|
||
public String getLabel() { | ||
return mLabel; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return mLabel; | ||
} | ||
} |
Oops, something went wrong.