Skip to content

Commit

Permalink
Zerocoin improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Aug 17, 2018
1 parent 2b18039 commit db2191e
Show file tree
Hide file tree
Showing 149 changed files with 1,976 additions and 406 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ checkpoints.txt
*.db
core/2.0.32_pivx-wallet-backup_org.pivx.production-2017-11-20
*build/
*.DS_Store
*.DS_Store
*out/
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/core/BitcoinSerializer.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/core/BloomFilter.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/core/TransactionBag.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/core/TransactionOutput.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/core/VarInt.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/script/Script.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/KeyChainGroup.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos$1.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos$Key$1.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos$Key.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos$Script.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos$Tag$1.class
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos$Tag.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos$Wallet.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Protos.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$10.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$11.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$3.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$4.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$5.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$6.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$7.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$8.class
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet$9.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/production/classes/org/pivxj/wallet/Wallet.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/out/test/classes/org/pivxj/wallet/MultiWalletTest.class
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class GenWitMessage extends Message {

public static final AtomicInteger requestNumInd = new AtomicInteger(0);

private BloomFilter bloomFilter;
private int startHeight;
private CoinDenomination den;
Expand All @@ -28,16 +31,35 @@ public GenWitMessage(
long randomNonce
) {
super(params);
this.requestNum = 10;
this.startHeight = startHeight;
this.den = den;
this.bloomFilter = new BloomFilter(elements, falsePositiveRate, randomNonce);
this.bloomFilter = new BloomFilter(elements, falsePositiveRate, randomNonce,2);
}

public void complete(){
this.requestNum = requestNumInd.incrementAndGet();
}

public void setStartHeight(int startHeight) {
this.startHeight = startHeight;
}

public Integer getRequestNum() {
return requestNum;
}

public BloomFilter getFilter() {
return bloomFilter;
}

public int getStartHeight() {
return startHeight;
}

public CoinDenomination getDen() {
return den;
}

public void insert(BigInteger data){
this.bloomFilter.insert(Utils.serializeBigInteger(data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class PubcoinsMessage extends Message {
private BigInteger accWitnessValue;
private List<BigInteger> list;
private long requestNum;
private boolean hasRequestFailed;

public PubcoinsMessage(NetworkParameters params, byte[] payload, int offset, int lenght) throws ProtocolException {
super(
Expand All @@ -40,12 +41,15 @@ public PubcoinsMessage(NetworkParameters params, BigInteger accValue, BigInteger
protected void parse() throws ProtocolException {
list = new ArrayList<>();
requestNum = readUint32();
accValue = readBignum();
accWitnessValue = readBignum();
long size = readUint32();
for (int i = 0; i < size; i++) {
list.add(readBignum());
}
if (hasMoreBytes()) {
accValue = readBignum();
accWitnessValue = readBignum();
long size = readUint32();
for (int i = 0; i < size; i++) {
list.add(readBignum());
}
}else
hasRequestFailed = true;
length = cursor;
}

Expand All @@ -65,6 +69,10 @@ public byte[] bitcoinSerialize() {
}
}

public boolean isHasRequestFailed() {
return hasRequestFailed;
}

public List<BigInteger> getList() {
return list;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package host.furszy.zerocoinj.wallet;

public class CannotSpendCoinsException extends Exception {
public CannotSpendCoinsException() {
}

public CannotSpendCoinsException(String message) {
super(message);
}

public CannotSpendCoinsException(String message, Throwable cause) {
super(message, cause);
}

public CannotSpendCoinsException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package host.furszy.zerocoinj.wallet;

public class InvalidSpendException extends Exception {
public InvalidSpendException(String s) {
super(s);
}

public InvalidSpendException(Throwable cause) {
super(cause);
}
}
103 changes: 98 additions & 5 deletions core/src/main/java/host/furszy/zerocoinj/wallet/MultiWallet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package host.furszy.zerocoinj.wallet;

import com.zerocoinj.core.ZCoin;
import com.zerocoinj.core.context.ZerocoinContext;
import com.zerocoinj.utils.JniBridgeWrapper;
import host.furszy.zerocoinj.MultiWalletFiles;
import host.furszy.zerocoinj.WalletFilesInterface;
import host.furszy.zerocoinj.wallet.files.Listener;
Expand All @@ -20,6 +22,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
Expand All @@ -42,11 +46,22 @@ public class MultiWallet{

protected final ReentrantLock lock;

public enum WalletType{
PIV, ZPIV, ALL
}

public MultiWallet(NetworkParameters params, ZerocoinContext zContext, DeterministicSeed seed){
this(params,zContext,seed,-1);
}

public MultiWallet(NetworkParameters params, ZerocoinContext zContext, DeterministicSeed seed, int lookaheadSize){
this.lock = Threading.lock("MultiWallet_1");
this.seed = seed;
this.pivWallet = new Wallet(params,new KeyChainGroup(params, seed, BIP44_PIV));
this.zWallet = new ZWallet(params, zContext, seed);
KeyChainGroup keyChainGroup = new KeyChainGroup(params, seed, BIP44_PIV);
if (lookaheadSize > 0)
keyChainGroup.setLookaheadSize(lookaheadSize);
this.pivWallet = new Wallet(params,keyChainGroup);
this.zWallet = new ZWallet(params, zContext, seed, lookaheadSize);
}

public MultiWallet(DeterministicSeed seed, List<Wallet> wallets) {
Expand Down Expand Up @@ -94,6 +109,18 @@ public void addWalletFrom(BlockChain blockChain) {
zWallet.addWalletFrom(blockChain);
}

public boolean isEveryOutputSpent(Transaction tx, WalletType walletType){
if (walletType == WalletType.PIV){
return tx.isEveryOwnedOutputSpent(pivWallet);
}else if (walletType == WalletType.ZPIV){
return tx.isEveryOwnedOutputSpent(getZpivWallet());
}else {
boolean isSpent = tx.isEveryOwnedOutputSpent(pivWallet);
if (!isSpent) return false;
return tx.isEveryOwnedOutputSpent(getZpivWallet());
}
}

public void commitTx(Transaction tx) {
boolean isZcMint = false;
for (TransactionOutput output : tx.getOutputs()) {
Expand Down Expand Up @@ -141,6 +168,21 @@ public Set<Transaction> listTransactions(){
return list;
}

public Collection<Transaction> listPendingTransactions(){
Collection<Transaction> list = pivWallet.getPendingTransactions();
for (Transaction transaction : zWallet.getPendingTransactions()) {
if (!list.contains(transaction)){
list.add(transaction);
}
}
return list;
}

public void setKeyChainGroupLookaheadThreshold(int n) {
pivWallet.setKeyChainGroupLookaheadThreshold(n);
zWallet.getWallet().setKeyChainGroupLookaheadThreshold(n);
}

public void cleanup(){
pivWallet.cleanup();
zWallet.cleanup();
Expand Down Expand Up @@ -225,6 +267,10 @@ public List<TransactionOutput> listUnspent() {
return pivWallet.getUnspents();
}

public List<TransactionOutput> listZpivUnspent() {
return zWallet.getUnspents();
}

public boolean isAddressMine(Address address) {
return pivWallet.isPubKeyHashMine(address.getHash160());
}
Expand Down Expand Up @@ -260,6 +306,50 @@ public Coin getZpivUnspensableBalance() {
return zWallet.getUnspendableBalance();
}

public ZCoin getZcoinAssociated(BigInteger commitmentValue){
return zWallet.getZcoinAssociated(commitmentValue);
}

public ZCoin getZcoinAssociatedToSerial(BigInteger serial){
return zWallet.getZcoinAssociatedToSerial(serial);
}

public List<ZCoin> freshZcoins(int n) {
return zWallet.freshZcoins(n);
}

public TransactionOutput getMintTransaction(BigInteger serial, WalletType walletType){
ZCoin coin = zWallet.getZcoinAssociatedToSerial(serial);
if (coin == null) return null;
if (walletType == WalletType.ZPIV){
return getMintTransaction(zWallet.getWallet(), coin);
}else if (walletType == WalletType.PIV){
return getMintTransaction(pivWallet, coin);
}else {
TransactionOutput output = getMintTransaction(zWallet.getWallet(), coin);
if (output != null) return output;
return getMintTransaction(pivWallet, coin);
}
}

private TransactionOutput getMintTransaction(Wallet wallet, ZCoin coin){
for (Transaction transaction : wallet.getTransactions(true)) {
for (TransactionOutput output : transaction.getOutputs()) {
if (output.isZcMint() && Utils.areBigIntegersEqual(output.getScriptPubKey().getCommitmentValue(), coin.getCommitment().getCommitmentValue())){
return output;
}
}
}
return null;
}

public boolean addTx(Transaction tx, WalletType walletType){
if (walletType == WalletType.ZPIV){
return zWallet.getWallet().maybeCommitTx(tx);
}
return false;
}

/**
* TODO: Add fee here...
* @param amount
Expand All @@ -276,11 +366,14 @@ public SendRequest createMintRequest(Coin amount) throws InsufficientMoneyExcept
public SendRequest createSpendRequest(Address to, Coin amount) throws InsufficientMoneyException {
Transaction tx = zWallet.createSpend(amount);
tx.addOutput(amount, to);
return SendRequest.forTx(tx);
SendRequest sendRequest = SendRequest.forTx(tx);
// TODO: check if change this for a new zpiv mint is a good idea..
sendRequest.changeAddress = pivWallet.freshReceiveAddress();
return sendRequest;
}

public void spendZpiv(SendRequest request, PeerGroup peerGroup, ExecutorService executor) throws InsufficientMoneyException{
zWallet.completeSendRequestAndWaitSync(request,peerGroup,executor);
public Transaction spendZpiv(SendRequest request, PeerGroup peerGroup, ExecutorService executor, JniBridgeWrapper wrapper) throws InsufficientMoneyException, CannotSpendCoinsException {
return zWallet.completeSendRequestAndWaitSync(wrapper,request,peerGroup,executor);
}


Expand Down
Loading

0 comments on commit db2191e

Please sign in to comment.