use of org.bitcoinj.core.Transaction in project sentinel-android by Samourai-Wallet.
the class SendFactory method signTransactionForSweep.
public Transaction signTransactionForSweep(Transaction unsignedTx, PrivKeyReader privKeyReader) {
HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
for (TransactionInput input : unsignedTx.getInputs()) {
try {
byte[] scriptBytes = input.getOutpoint().getConnectedPubKeyScript();
// String address = new BitcoinScript(scriptBytes).getAddress().toString();
String address = new Script(scriptBytes).getToAddress(MainNetParams.get()).toString();
// Log.i("address from script", address);
ECKey ecKey = null;
try {
DumpedPrivateKey pk = new DumpedPrivateKey(MainNetParams.get(), privKeyReader.getKey().getPrivateKeyAsWiF(MainNetParams.get()));
ecKey = pk.getKey();
// Log.i("SendFactory", "ECKey address:" + ecKey.toAddress(MainNetParams.get()).toString());
} catch (AddressFormatException afe) {
afe.printStackTrace();
continue;
}
if (ecKey != null) {
keyBag.put(input.getOutpoint().toString(), ecKey);
} else {
Toast.makeText(context, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
// Log.i("ECKey error", "cannot process private key");
}
} catch (ScriptException se) {
;
} catch (Exception e) {
;
}
}
Transaction signedTx = signTransaction(unsignedTx, keyBag);
if (signedTx == null) {
return null;
} else {
String hexString = new String(Hex.encode(signedTx.bitcoinSerialize()));
if (hexString.length() > (100 * 1024)) {
Toast.makeText(context, R.string.tx_length_error, Toast.LENGTH_SHORT).show();
// Log.i("SendFactory", "Transaction length too long");
}
return signedTx;
}
}
use of org.bitcoinj.core.Transaction in project catena-java by alinush.
the class SimpleWallet method txnsToList.
/**
* Returns a list of the Catena transactions in this wallet.
* Used for testing and in the Catena command line app.
*
* @param isFwd
* @return
*/
private List<CatenaStatement> txnsToList(boolean isFwd) {
CatenaWalletExtension ext = getCatenaExtension();
if (!ext.hasRootOfTrustTxid()) {
return ImmutableList.<CatenaStatement>of();
}
Transaction tip;
// We skip the root-of-trust TXN
Transaction rootTxn = getTransaction(ext.getRootOfTrustTxid());
tip = CatenaUtils.getNextCatenaTx(this, rootTxn);
if (tip == null) {
return ImmutableList.<CatenaStatement>of();
}
checkState(tip.getInput(0).getOutpoint().equals(rootTxn.getOutput(0).getOutPointFor()));
// Copy the transactions from the wallet as they could get modified by reorganizations, etc.
LinkedList<CatenaStatement> stmts = new LinkedList<CatenaStatement>();
// Create and copy the statements in the array
while (tip != null) {
CatenaStatement s = CatenaStatement.fromTxn(tip);
if (isFwd) {
stmts.addLast(s);
} else {
stmts.addFirst(s);
}
tip = CatenaUtils.getNextCatenaTx(this, tip);
}
return stmts;
}
use of org.bitcoinj.core.Transaction in project catena-java by alinush.
the class ReorganizeChainTest method testFork2or3.
public Semaphore testFork2or3(boolean generateBlock) throws InsufficientMoneyException, PrunedException, BlockStoreException, InterruptedException {
log.info("Test: Issue, withdraw, lie ({})", generateBlock ? "with confirmed TX" : "with pending TX");
// Generate statements, needed in the test case functions
int numStmts = 3;
String[] stmts = TestUtils.generateStatements(numStmts);
log.info("Test: Issue, withdraw, reissue, and issue some more");
// Step 1: Issue initial statements, before the first fork
log.debug("Issuing initial statements...");
for (int i = 0; i < numStmts; i++) {
issueStatement(stmts[i]);
}
// Step 2: Drop the last numStmts - 1 blocks, create another fork longer by 1 than the dropped blocks
int stmtsWithdrawn = numStmts - 1;
fork(stmtsWithdrawn, stmtsWithdrawn + 1);
// Wait for the withdrawn statements to be handled
assertTrue("did not get notified about withdrawn statements", semWithdrawn.tryAcquire(stmtsWithdrawn, 5, TimeUnit.SECONDS));
// Step 3: Install a "lie detector"
final Semaphore sem = new Semaphore(0);
wallet.addWhistleblowListener(new CatenaWhistleblowListener() {
@Override
public void onWhistleblow(Transaction tx, String message) {
Context.propagate(wallet.getContext());
log.debug("Whistleblowing for TX {}: {}", tx.getHash(), message);
sem.release();
}
});
return sem;
}
use of org.bitcoinj.core.Transaction in project catena-java by alinush.
the class ReorganizeChainTest method setUp.
@Before
public void setUp() throws Exception {
Context.propagate(new Context(params, 10000, Transaction.DEFAULT_TX_FEE, true));
semAppended = new Semaphore(0);
semWithdrawn = new Semaphore(0);
// Step 0: Catena wallet already started with a listener that signals when statements are appended and withdrawn
wallet = new ClientWallet(params);
wallet.addExtension(new CatenaWalletExtension());
// Generate a key to send mining rewards to
genCoinsKey = new ECKey();
wallet.importKey(genCoinsKey);
genCoinsAddr = genCoinsKey.toAddress(params);
// need to tell the wallet about the chain address by calling this
wallet.addWatchedAddress(genCoinsAddr);
// Generate a black hole key, where funds are send to be lost
blackholeAddr = new ECKey().toAddress(params);
// Add semaphores to wallet
wallet.addStatementListener(new SemaphoredStatementListener(wallet, semAppended, semWithdrawn));
wallet.addReorganizeEventListener(new WalletReorganizeEventListener() {
@Override
public void onReorganize(Wallet wallet) {
log.debug("Fork detected!");
}
});
// TODO: can set an onReorganize listener here
chain = new BlockChain(params, wallet, new MemoryBlockStore(params));
// Set a listener to wait for those coins
final Semaphore sem = new Semaphore(0);
wallet.addCoinsReceivedEventListener(new WalletCoinsReceivedEventListener() {
@Override
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
// log.debug("Got initial coins from mining reward: {}", newBalance);
sem.release();
}
});
// Create the first block, so we can get some coins.
lastBlock = params.getGenesisBlock().createNextBlock(genCoinsAddr);
chain.add(lastBlock);
// Bury the first block with enough blocks, so we can spend those coins
for (int i = 0; i < params.getSpendableCoinbaseDepth(); i++) {
lastBlock = lastBlock.createNextBlock(blackholeAddr);
chain.add(lastBlock);
}
log.debug("Created {} blocks.", chain.getBestChainHeight());
assertTrue("timed out waiting to receive coins from block reward", sem.tryAcquire(10, TimeUnit.SECONDS));
// Create the root-of-trust TXN
Transaction tx = issueStatement("testchain", false);
log.debug("Created root-of-trust TXN {}", tx.getHash());
// wallet.getCatenaExtension().setRootOfTrustTxid(rootOfTrustTxid); // already set by issueStatement
wallet.addChangeEventListener(Threading.SAME_THREAD, new CatenaWalletListener(wallet));
}
use of org.bitcoinj.core.Transaction in project catena-java by alinush.
the class TxUtils method findDoubleSpendsAgainst.
/**
* Checks if any of the txs in candidates double spends any of the outputs spent by the transaction in tx.
* (i.e., checks if tx and candidates conflict by spending the same output(s))
*
* Copied from the wallet class. Need it to check for lies.
*
* @param tx
* @param candidates
* @return
*/
public static Set<Transaction> findDoubleSpendsAgainst(Transaction tx, Map<Sha256Hash, Transaction> candidates) {
// Coinbase TXs cannot double spend
if (tx.isCoinBase())
return Sets.newHashSet();
// Compile a set of outpoints that are spent by tx.
HashSet<TransactionOutPoint> outpoints = new HashSet<TransactionOutPoint>();
for (TransactionInput input : tx.getInputs()) {
outpoints.add(input.getOutpoint());
}
// Now for each candidate transaction, see if it spends any outpoints as this tx.
Set<Transaction> doubleSpendTxns = Sets.newHashSet();
for (Transaction p : candidates.values()) {
for (TransactionInput input : p.getInputs()) {
TransactionOutPoint outpoint = input.getOutpoint();
if (outpoints.contains(outpoint)) {
// It does, it's a double spend against the candidates, which makes it relevant.
doubleSpendTxns.add(p);
}
}
}
return doubleSpendTxns;
}
Aggregations