source reformat
This commit is contained in:
parent
78ea92a172
commit
7953d1c32c
|
@ -4,20 +4,14 @@ import static org.bitcoinj.script.ScriptOpCodes.OP_RETURN;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.InputMismatchException;
|
import java.util.InputMismatchException;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import joptsimple.OptionParser;
|
|
||||||
import joptsimple.OptionSet;
|
|
||||||
import joptsimple.OptionSpec;
|
|
||||||
import org.abstractj.kalium.keys.SigningKey;
|
import org.abstractj.kalium.keys.SigningKey;
|
||||||
import org.abstractj.kalium.keys.VerifyKey;
|
import org.abstractj.kalium.keys.VerifyKey;
|
||||||
import org.bitcoinj.core.CheckpointManager;
|
|
||||||
import org.bitcoinj.core.Coin;
|
import org.bitcoinj.core.Coin;
|
||||||
import org.bitcoinj.core.ECKey;
|
|
||||||
import org.bitcoinj.core.InsufficientMoneyException;
|
import org.bitcoinj.core.InsufficientMoneyException;
|
||||||
import org.bitcoinj.core.NetworkParameters;
|
import org.bitcoinj.core.NetworkParameters;
|
||||||
import org.bitcoinj.core.Sha256Hash;
|
import org.bitcoinj.core.Sha256Hash;
|
||||||
|
@ -25,23 +19,22 @@ import org.bitcoinj.core.Transaction;
|
||||||
import org.bitcoinj.core.TransactionConfidence;
|
import org.bitcoinj.core.TransactionConfidence;
|
||||||
import org.bitcoinj.core.TransactionOutput;
|
import org.bitcoinj.core.TransactionOutput;
|
||||||
import org.bitcoinj.core.Utils;
|
import org.bitcoinj.core.Utils;
|
||||||
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
|
|
||||||
import org.bitcoinj.script.Script;
|
import org.bitcoinj.script.Script;
|
||||||
import org.bitcoinj.script.ScriptBuilder;
|
import org.bitcoinj.script.ScriptBuilder;
|
||||||
import org.bitcoinj.wallet.KeyChain.KeyPurpose;
|
import org.bitcoinj.wallet.KeyChain.KeyPurpose;
|
||||||
import org.bitcoinj.wallet.SendRequest;
|
import org.bitcoinj.wallet.SendRequest;
|
||||||
import org.bitcoinj.wallet.Wallet;
|
|
||||||
import org.bitcoinj.wallet.Wallet.SendResult;
|
import org.bitcoinj.wallet.Wallet.SendResult;
|
||||||
import org.bitcoinj.wallet.listeners.KeyChainEventListener;
|
|
||||||
import org.bitcoinj.wallet.listeners.ScriptsChangeEventListener;
|
|
||||||
import org.bitcoinj.wallet.listeners.WalletCoinsReceivedEventListener;
|
|
||||||
import org.bitcoinj.wallet.listeners.WalletCoinsSentEventListener;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.tcpid.opretj.*;
|
import org.tcpid.opretj.OPRETECParser;
|
||||||
|
import org.tcpid.opretj.OPRETWallet;
|
||||||
|
import org.tcpid.opretj.OPRETWalletAppKit;
|
||||||
|
|
||||||
import com.google.common.primitives.Bytes;
|
import com.google.common.primitives.Bytes;
|
||||||
|
|
||||||
|
import joptsimple.OptionParser;
|
||||||
|
import joptsimple.OptionSet;
|
||||||
|
import joptsimple.OptionSpec;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(App.class);
|
private static final Logger logger = LoggerFactory.getLogger(App.class);
|
||||||
|
@ -70,11 +63,35 @@ public class App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
private static String executeCommand(final String command) {
|
||||||
OptionParser parser = new OptionParser();
|
|
||||||
OptionSpec<NetworkEnum> net = parser.accepts("net", "The network to run the examples on").withRequiredArg().ofType(NetworkEnum.class).defaultsTo(NetworkEnum.TEST);
|
final StringBuffer output = new StringBuffer();
|
||||||
|
|
||||||
|
Process p;
|
||||||
|
try {
|
||||||
|
p = Runtime.getRuntime().exec(command);
|
||||||
|
p.waitFor();
|
||||||
|
final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||||
|
|
||||||
|
String line = "";
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
output.append(line + "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (final Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(final String[] args) throws Exception {
|
||||||
|
final OptionParser parser = new OptionParser();
|
||||||
|
final OptionSpec<NetworkEnum> net = parser.accepts("net", "The network to run the examples on")
|
||||||
|
.withRequiredArg().ofType(NetworkEnum.class).defaultsTo(NetworkEnum.TEST);
|
||||||
parser.accepts("help", "Displays program options");
|
parser.accepts("help", "Displays program options");
|
||||||
OptionSet opts = parser.parse(args);
|
final OptionSet opts = parser.parse(args);
|
||||||
if (opts.has("help") || !opts.has(net)) {
|
if (opts.has("help") || !opts.has(net)) {
|
||||||
System.err.println("usage: App --net=MAIN/TEST/REGTEST");
|
System.err.println("usage: App --net=MAIN/TEST/REGTEST");
|
||||||
parser.printHelpOn(System.err);
|
parser.printHelpOn(System.err);
|
||||||
|
@ -84,13 +101,7 @@ public class App {
|
||||||
|
|
||||||
final OPRETECParser bs = new OPRETECParser();
|
final OPRETECParser bs = new OPRETECParser();
|
||||||
|
|
||||||
bs.addOPRETECRevokeEventListener(new OPRETECRevokeEventListener() {
|
bs.addOPRETECRevokeEventListener((pkhash, sig) -> check(pkhash, sig));
|
||||||
public void onOPRETRevoke(final byte[] pkhash, final byte[] sig) {
|
|
||||||
// logger.warn("REVOKE PK {} - SIG {}",
|
|
||||||
// Utils.HEX.encode(pkhash), Utils.HEX.encode(sig));
|
|
||||||
check(pkhash, sig);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
long earliestTime;
|
long earliestTime;
|
||||||
if (params.getId().equals(NetworkParameters.ID_REGTEST)) {
|
if (params.getId().equals(NetworkParameters.ID_REGTEST)) {
|
||||||
|
@ -102,8 +113,9 @@ public class App {
|
||||||
}
|
}
|
||||||
|
|
||||||
bs.addOPRET(pkhash, earliestTime);
|
bs.addOPRET(pkhash, earliestTime);
|
||||||
//bs.addOPRET(Sha256Hash.hash("test1".getBytes()), earliestTime);
|
// bs.addOPRET(Sha256Hash.hash("test1".getBytes()), earliestTime);
|
||||||
//bs.addOPRET(Utils.HEX.decode("0f490dee643b01b06e0ea84c253a90050a3543cfb7c74319fb47b04afee5b872"), earliestTime);
|
// bs.addOPRET(Utils.HEX.decode("0f490dee643b01b06e0ea84c253a90050a3543cfb7c74319fb47b04afee5b872"),
|
||||||
|
// earliestTime);
|
||||||
|
|
||||||
// Now we initialize a new WalletAppKit. The kit handles all the
|
// Now we initialize a new WalletAppKit. The kit handles all the
|
||||||
// boilerplate for us and is the easiest way to get everything up and
|
// boilerplate for us and is the easiest way to get everything up and
|
||||||
|
@ -117,7 +129,7 @@ public class App {
|
||||||
// connect to localhost.
|
// connect to localhost.
|
||||||
// You must do that in reg test mode.
|
// You must do that in reg test mode.
|
||||||
if (params.getId().equals(NetworkParameters.ID_REGTEST)) {
|
if (params.getId().equals(NetworkParameters.ID_REGTEST)) {
|
||||||
kit.connectToLocalHost();
|
kit.connectToLocalHost();
|
||||||
}
|
}
|
||||||
kit.setCheckpoints(App.class.getResourceAsStream("/" + params.getId() + ".checkpoints"));
|
kit.setCheckpoints(App.class.getResourceAsStream("/" + params.getId() + ".checkpoints"));
|
||||||
// Now we start the kit and sync the blockchain.
|
// Now we start the kit and sync the blockchain.
|
||||||
|
@ -130,46 +142,31 @@ public class App {
|
||||||
|
|
||||||
final OPRETWallet wallet = kit.opretwallet();
|
final OPRETWallet wallet = kit.opretwallet();
|
||||||
|
|
||||||
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
|
wallet.addCoinsReceivedEventListener((wallet1, tx, prevBalance, newBalance) -> {
|
||||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
System.out.println("-----> coins resceived: " + tx.getHashAsString());
|
||||||
System.out.println("-----> coins resceived: " + tx.getHashAsString());
|
System.out.println("received: " + tx.getValue(wallet1));
|
||||||
System.out.println("received: " + tx.getValue(wallet));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
wallet.addCoinsSentEventListener(new WalletCoinsSentEventListener() {
|
wallet.addCoinsSentEventListener((wallet1, tx, prevBalance, newBalance) -> System.out.println("coins sent"));
|
||||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
|
||||||
System.out.println("coins sent");
|
wallet.addKeyChainEventListener(keys -> System.out.println("new key added"));
|
||||||
}
|
|
||||||
|
wallet.addScriptsChangeEventListener(
|
||||||
|
(wallet1, scripts, isAddingScripts) -> System.out.println("new script added"));
|
||||||
|
|
||||||
|
wallet.addTransactionConfidenceEventListener((wallet1, tx) -> {
|
||||||
|
System.out.println("-----> confidence changed: " + tx.getHashAsString());
|
||||||
|
final TransactionConfidence confidence = tx.getConfidence();
|
||||||
|
System.out.println("new block depth: " + confidence.getDepthInBlocks());
|
||||||
});
|
});
|
||||||
|
|
||||||
wallet.addKeyChainEventListener(new KeyChainEventListener() {
|
// wallet.allowSpendingUnconfirmedTransactions();
|
||||||
public void onKeysAdded(List<ECKey> keys) {
|
|
||||||
System.out.println("new key added");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
wallet.addScriptsChangeEventListener(new ScriptsChangeEventListener() {
|
|
||||||
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
|
|
||||||
System.out.println("new script added");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
wallet.addTransactionConfidenceEventListener(new TransactionConfidenceEventListener() {
|
|
||||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
|
||||||
System.out.println("-----> confidence changed: " + tx.getHashAsString());
|
|
||||||
final TransactionConfidence confidence = tx.getConfidence();
|
|
||||||
System.out.println("new block depth: " + confidence.getDepthInBlocks());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//wallet.allowSpendingUnconfirmedTransactions();
|
|
||||||
|
|
||||||
// Ready to run. The kit syncs the blockchain and our wallet event
|
// Ready to run. The kit syncs the blockchain and our wallet event
|
||||||
// listener gets notified when something happens.
|
// listener gets notified when something happens.
|
||||||
// To test everything we create and print a fresh receiving address.
|
// To test everything we create and print a fresh receiving address.
|
||||||
// Send some coins to that address and see if everything works.
|
// Send some coins to that address and see if everything works.
|
||||||
String receiveStr = wallet.freshReceiveAddress().toString();
|
final String receiveStr = wallet.freshReceiveAddress().toString();
|
||||||
final Scanner input = new Scanner(System.in);
|
final Scanner input = new Scanner(System.in);
|
||||||
|
|
||||||
display: while (true) {
|
display: while (true) {
|
||||||
|
@ -190,9 +187,9 @@ public class App {
|
||||||
case 2:
|
case 2:
|
||||||
System.out.println("send money to: " + receiveStr);
|
System.out.println("send money to: " + receiveStr);
|
||||||
try {
|
try {
|
||||||
System.out.print(executeCommand("qrencodes -t UTF8 -o - " + receiveStr));
|
System.out.print(executeCommand("qrencodes -t UTF8 -o - " + receiveStr));
|
||||||
} catch (Exception e) {
|
} catch (final Exception e) {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
|
@ -219,7 +216,7 @@ public class App {
|
||||||
kit.awaitTerminated();
|
kit.awaitTerminated();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean sendOPReturn(OPRETWalletAppKit kit) {
|
private static boolean sendOPReturn(final OPRETWalletAppKit kit) {
|
||||||
final OPRETWallet wallet = kit.opretwallet();
|
final OPRETWallet wallet = kit.opretwallet();
|
||||||
final NetworkParameters params = wallet.getNetworkParameters();
|
final NetworkParameters params = wallet.getNetworkParameters();
|
||||||
|
|
||||||
|
@ -270,28 +267,4 @@ public class App {
|
||||||
logger.debug("SendRequest {}", request);
|
logger.debug("SendRequest {}", request);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String executeCommand(String command) {
|
|
||||||
|
|
||||||
StringBuffer output = new StringBuffer();
|
|
||||||
|
|
||||||
Process p;
|
|
||||||
try {
|
|
||||||
p = Runtime.getRuntime().exec(command);
|
|
||||||
p.waitFor();
|
|
||||||
BufferedReader reader =
|
|
||||||
new BufferedReader(new InputStreamReader(p.getInputStream()));
|
|
||||||
|
|
||||||
String line = "";
|
|
||||||
while ((line = reader.readLine())!= null) {
|
|
||||||
output.append(line + "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return output.toString();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,21 +23,19 @@ import org.bitcoinj.params.RegTestParams;
|
||||||
import org.bitcoinj.params.TestNet3Params;
|
import org.bitcoinj.params.TestNet3Params;
|
||||||
|
|
||||||
public enum NetworkEnum {
|
public enum NetworkEnum {
|
||||||
MAIN,
|
MAIN, PROD, // alias for MAIN
|
||||||
PROD, // alias for MAIN
|
TEST, REGTEST;
|
||||||
TEST,
|
|
||||||
REGTEST;
|
|
||||||
|
|
||||||
public NetworkParameters get() {
|
public NetworkParameters get() {
|
||||||
switch(this) {
|
switch (this) {
|
||||||
case MAIN:
|
case MAIN:
|
||||||
case PROD:
|
case PROD:
|
||||||
return MainNetParams.get();
|
return MainNetParams.get();
|
||||||
case TEST:
|
case TEST:
|
||||||
return TestNet3Params.get();
|
return TestNet3Params.get();
|
||||||
case REGTEST:
|
case REGTEST:
|
||||||
default:
|
default:
|
||||||
return RegTestParams.get();
|
return RegTestParams.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue