Search in sources :

Example 1 with WalletFactory

use of org.bitcoinj.wallet.WalletProtobufSerializer.WalletFactory in project catena-java by alinush.

the class WrongPkTest method testWrongPk.

/**
 * Couldn't make this test work initially, not easily at least. Because the root-of-trust TXN is not saved in the wallet
 * when the wallet is initialized with the wrong PK, since the TXN doesn't transfer any funds and seems irrelevant
 * to the wallet. As a result, processRootOfTrustTxn fails because there's no TXN in the wallet. So we had to call
 * processRootOfTrustTxn manually here.
 *
 * @throws InsufficientMoneyException
 * @throws IOException
 * @throws InterruptedException
 * @throws TimeoutException
 */
@Test
public void testWrongPk() throws InsufficientMoneyException, IOException, InterruptedException, TimeoutException {
    log.info("Test: Fail if the chain PK in the root-of-trust TX differs from the expected one");
    // Replace the expected chain PK with a random one.
    ECKey key = new ECKey();
    super.expectedChainAddr = key.toAddress(params);
    // The Catena client should stop when it encounters a different PK in the root-of-trust TX
    final Semaphore sem = new Semaphore(0);
    walletFactory = new WalletFactory() {

        @Override
        public Wallet create(NetworkParameters params, KeyChainGroup keyChainGroup) {
            log.info("Creating custom wallet that whistleblows on a wrong chain address...");
            ClientWallet wallet = new ClientWallet.Factory().create(params, keyChainGroup);
            wallet.addWhistleblowListener(new CatenaWhistleblowListener() {

                @Override
                public void onWhistleblow(Transaction tx, String message) {
                    log.info("Whistleblowing for tx {}: {}", tx.getHash(), message);
                    sem.release();
                    catenaClient.stopAsync();
                }
            });
            return wallet;
        }
    };
    createCatenaClient(txid, false);
    catenaClient.startAsync();
    catenaClient.awaitRunning();
    // We are cheating here: We're feeding the Catena server-created root-of-trust TXN to the Catena client manually
    catenaClient.getCatenaWallet().processRootOfTrustTxn(rootOfTrustTxn);
    Assert.assertTrue("Catena client should have whistleblown due to wrong PK", sem.tryAcquire(5, TimeUnit.SECONDS));
    catenaClient.stopAsync();
    catenaClient.awaitTerminated(3, TimeUnit.SECONDS);
    Assert.assertTrue(catenaClient.state() == State.TERMINATED);
}
Also used : Transaction(org.bitcoinj.core.Transaction) NetworkParameters(org.bitcoinj.core.NetworkParameters) Wallet(org.bitcoinj.wallet.Wallet) KeyChainGroup(org.bitcoinj.wallet.KeyChainGroup) WalletFactory(org.bitcoinj.wallet.WalletProtobufSerializer.WalletFactory) LoggerFactory(org.slf4j.LoggerFactory) WalletFactory(org.bitcoinj.wallet.WalletProtobufSerializer.WalletFactory) ECKey(org.bitcoinj.core.ECKey) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test) ClientServerTest(org.catena.common.ClientServerTest)

Aggregations

Semaphore (java.util.concurrent.Semaphore)1 ECKey (org.bitcoinj.core.ECKey)1 NetworkParameters (org.bitcoinj.core.NetworkParameters)1 Transaction (org.bitcoinj.core.Transaction)1 KeyChainGroup (org.bitcoinj.wallet.KeyChainGroup)1 Wallet (org.bitcoinj.wallet.Wallet)1 WalletFactory (org.bitcoinj.wallet.WalletProtobufSerializer.WalletFactory)1 ClientServerTest (org.catena.common.ClientServerTest)1 Test (org.junit.Test)1 LoggerFactory (org.slf4j.LoggerFactory)1