Skip to content

Commit

Permalink
Added working transaction formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
landgenoot committed Jun 8, 2017
1 parent d46d72d commit 5af2b2a
Show file tree
Hide file tree
Showing 7 changed files with 608 additions and 319 deletions.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
exclude module: 'recyclerview-v7'
}
androidTestCompile 'org.mockito:mockito-android:2.7.22'
testCompile 'org.mockito:mockito-android:2.7.22'
androidTestCompile ('com.android.support.test.espresso:espresso-intents:2.2.2') {
exclude module: 'support-annotations'
}
Expand All @@ -56,4 +57,6 @@ dependencies {
compile group: 'ch.qos.logback', name:'logback-core', version: '1.0.9'

compile project(':bitcoinj-core')

testCompile 'com.google.guava:guava:22.0'
}
55 changes: 29 additions & 26 deletions app/src/main/java/com/digitalvotingpass/blockchain/BlockChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
import android.util.Log;

import com.digitalvotingpass.passportconnection.PassportConnection;
import com.digitalvotingpass.passportconnection.PassportTransaction;
import com.digitalvotingpass.passportconnection.PassportTransactionFormatter;
import com.digitalvotingpass.utilities.MultiChainAddressGenerator;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.Service;

import org.bitcoinj.core.Address;
import org.bitcoinj.core.Asset;
import org.bitcoinj.core.AssetBalance;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.PeerAddress;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionOutput;
import org.bitcoinj.kits.WalletAppKit;
import org.bitcoinj.params.MultiChainParams;
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.utils.BriefLogFormatter;
import org.bitcoinj.wallet.Wallet;

Expand Down Expand Up @@ -47,6 +45,7 @@ public class BlockChain {
addressChecksum,
0xf5dec1feL
);
private Address masterAddress = Address.fromBase58(params, "1GoqgbPZUV2yuPZXohtAvB2NZbjcew8Rk93mMn");

private BlockChain() {
try {
Expand Down Expand Up @@ -101,7 +100,6 @@ public void disconnect() {

/**
* Gets the amount of voting tokens associated with the given public key.
*
* @param pubKey - The Public Key read from the ID of the voter
* @param mcAsset - The asset (election) that is chosen at app start-up.
* @return - The amount of voting tokens available
Expand All @@ -115,45 +113,50 @@ public int getVotingPassAmount(PublicKey pubKey, Asset mcAsset) {
return (int) kit.wallet().getAssetBalance(mcAsset, mcAddress).getBalance();
}

public AssetBalance getVotingPassBalance(PublicKey pubKey) {

Address mcAddress = Address.fromBase58(params, MultiChainAddressGenerator.getPublicAddress(version, Long.toString(addressChecksum), pubKey));
System.out.println(mcAddress);
Asset mcAsset = kit.wallet().getAvailableAssets().get(1); // TODO: remove this
Log.e(this.toString(), "asset 0: " + kit.wallet().getAssetBalance(mcAsset, mcAddress).getBalance());
return kit.wallet().getAssetBalance(mcAsset, mcAddress);
/**
* Get the balance of a public key based on the information on the blockchain.
* @param pubKey
* @return
*/
public AssetBalance getVotingPassBalance(PublicKey pubKey, Asset asset) {
Address address = Address.fromBase58(params, MultiChainAddressGenerator.getPublicAddress(version, Long.toString(addressChecksum), pubKey));
return kit.wallet().getAssetBalance(asset, address);
}

/**
* Create a new transaction, signes it with the travel document and broadcasts it to the
* network
*
* Spends all outputs in this balance to the master address.
* @param balance
* @param pcon
*/
public void confirmVotingPass(AssetBalance balance, PassportConnection pcon) {
for (TransactionOutput utxo : balance) {
this.spendUtxo(utxo, this.masterAddress, pcon);
}
}

PassportTransaction transaction = new PassportTransaction(params);
TransactionOutput original = balance.get(0);

byte[] bytes = original.getScriptBytes();
TransactionOutput output = new TransactionOutput(params, transaction, Coin.ZERO, bytes);
/**
* Create a new transaction, signes it with the travel document and broadcasts it to the
* network.
* @param utxo
* @param destination
* @param pcon
*/
public void spendUtxo(TransactionOutput utxo, Address destination, PassportConnection pcon) {

transaction.addOutput(original);
transaction.addPassportSignedInput(original, pcon);
Transaction tx = new PassportTransactionFormatter(utxo, destination)
.buildAndSign(pcon)
.getTransaction(params);

final Wallet.SendResult result = new Wallet.SendResult();
result.tx = transaction;
result.broadcast = kit.peerGroup().broadcastTransaction(transaction);
result.tx = tx;
result.broadcast = kit.peerGroup().broadcastTransaction(tx);
result.broadcastComplete = result.broadcast.future();

result.broadcastComplete.addListener(new Runnable() {
@Override
public void run() {
// The wallet has changed now, it'll get auto saved shortly or when the app shuts down.
System.out.println("Sent coins onwards! Transaction hash is " + result.tx.getHashAsString());
System.out.println("Asset spent! txid: " + result.tx.getHashAsString());
}
}, MoreExecutors.directExecutor());
}

}
Loading

0 comments on commit 5af2b2a

Please sign in to comment.