Skip to content

Commit

Permalink
Merge pull request #1 from kmels/release-0.14
Browse files Browse the repository at this point in the history
Add BIP47 classes
  • Loading branch information
justusranvier committed Feb 7, 2018
2 parents 604dc8a + f5f31dc commit c931219
Show file tree
Hide file tree
Showing 22 changed files with 2,317 additions and 6 deletions.
22 changes: 21 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.bitcoincashj</groupId>
<artifactId>bitcoincashj-parent</artifactId>
<version>0.14.5</version>
<version>0.14.5-bip47</version>
</parent>

<artifactId>bitcoincashj-core</artifactId>
Expand Down Expand Up @@ -447,6 +447,26 @@
<artifactId>okhttp</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.madgag.spongycastle</groupId>
<artifactId>prov</artifactId>
<version>1.56.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>

</project>
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/VersionMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class VersionMessage extends Message {
public boolean relayTxesBeforeFilter;

/** The version of this library release, as a string. */
public static final String BITCOINJ_VERSION = "0.14.5";
public static final String BITCOINJ_VERSION = "0.14.5-bip47";
/** The value that is prepended to the subVer field of this application. */
public static final String LIBRARY_SUBVER = "/bitcoincashj:" + BITCOINJ_VERSION + "/";

Expand Down
72 changes: 72 additions & 0 deletions core/src/main/java/org/bitcoinj/crypto/bip47/Account.java
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();
}
}
85 changes: 85 additions & 0 deletions core/src/main/java/org/bitcoinj/crypto/bip47/Address.java
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 core/src/main/java/org/bitcoinj/crypto/bip47/Blockchain.java
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;
}
}
Loading

0 comments on commit c931219

Please sign in to comment.