Java Code Examples for org.bitcoinj.wallet.Protos#Wallet

The following examples show how to use org.bitcoinj.wallet.Protos#Wallet . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WalletProtobufSerializerTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void extensions() throws Exception {
    myWallet.addExtension(new FooWalletExtension("com.whatever.required", true));
    Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
    // Initial extension is mandatory: try to read it back into a wallet that doesn't know about it.
    try {
        new WalletProtobufSerializer().readWallet(UNITTEST, null, proto);
        fail();
    } catch (UnreadableWalletException e) {
        assertTrue(e.getMessage().contains("mandatory"));
    }
    Wallet wallet = new WalletProtobufSerializer().readWallet(UNITTEST,
            new WalletExtension[]{new FooWalletExtension("com.whatever.required", true)},
            proto);
    assertTrue(wallet.getExtensions().containsKey("com.whatever.required"));

    // Non-mandatory extensions are ignored if the wallet doesn't know how to read them.
    Wallet wallet2 = new Wallet(UNITTEST);
    wallet2.addExtension(new FooWalletExtension("com.whatever.optional", false));
    Protos.Wallet proto2 = new WalletProtobufSerializer().walletToProto(wallet2);
    Wallet wallet5 = new WalletProtobufSerializer().readWallet(UNITTEST, null, proto2);
    assertEquals(0, wallet5.getExtensions().size());
}
 
Example 2
Source File: WalletTool.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
private static Protos.Wallet attemptHexConversion(Protos.Wallet proto) {
    // Try to convert any raw hashes and such to textual equivalents for easier debugging. This makes it a bit
    // less "raw" but we will just abort on any errors.
    try {
        Protos.Wallet.Builder builder = proto.toBuilder();
        for (Protos.Transaction.Builder tx : builder.getTransactionBuilderList()) {
            tx.setHash(bytesToHex(tx.getHash()));
            for (int i = 0; i < tx.getBlockHashCount(); i++)
                tx.setBlockHash(i, bytesToHex(tx.getBlockHash(i)));
            for (Protos.TransactionInput.Builder input : tx.getTransactionInputBuilderList())
                input.setTransactionOutPointHash(bytesToHex(input.getTransactionOutPointHash()));
            for (Protos.TransactionOutput.Builder output : tx.getTransactionOutputBuilderList()) {
                if (output.hasSpentByTransactionHash())
                    output.setSpentByTransactionHash(bytesToHex(output.getSpentByTransactionHash()));
            }
            // TODO: keys, ip addresses etc.
        }
        return builder.build();
    } catch (Throwable throwable) {
        log.error("Failed to do hex conversion on wallet proto", throwable);
        return proto;
    }
}
 
Example 3
Source File: WalletProtobufSerializerTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void extensions() throws Exception {
    myWallet.addExtension(new FooWalletExtension("com.whatever.required", true));
    Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
    // Initial extension is mandatory: try to read it back into a wallet that doesn't know about it.
    try {
        new WalletProtobufSerializer().readWallet(PARAMS, null, proto);
        fail();
    } catch (UnreadableWalletException e) {
        assertTrue(e.getMessage().contains("mandatory"));
    }
    Wallet wallet = new WalletProtobufSerializer().readWallet(PARAMS,
            new WalletExtension[]{ new FooWalletExtension("com.whatever.required", true) },
            proto);
    assertTrue(wallet.getExtensions().containsKey("com.whatever.required"));

    // Non-mandatory extensions are ignored if the wallet doesn't know how to read them.
    Wallet wallet2 = new Wallet(PARAMS);
    wallet2.addExtension(new FooWalletExtension("com.whatever.optional", false));
    Protos.Wallet proto2 = new WalletProtobufSerializer().walletToProto(wallet2);
    Wallet wallet5 = new WalletProtobufSerializer().readWallet(PARAMS, null, proto2);
    assertEquals(0, wallet5.getExtensions().size());
}
 
Example 4
Source File: WalletTool.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
private static Protos.Wallet attemptHexConversion(Protos.Wallet proto) {
    // Try to convert any raw hashes and such to textual equivalents for easier debugging. This makes it a bit
    // less "raw" but we will just abort on any errors.
    try {
        Protos.Wallet.Builder builder = proto.toBuilder();
        for (Protos.Transaction.Builder tx : builder.getTransactionBuilderList()) {
            tx.setHash(bytesToHex(tx.getHash()));
            for (int i = 0; i < tx.getBlockHashCount(); i++)
                tx.setBlockHash(i, bytesToHex(tx.getBlockHash(i)));
            for (Protos.TransactionInput.Builder input : tx.getTransactionInputBuilderList())
                input.setTransactionOutPointHash(bytesToHex(input.getTransactionOutPointHash()));
            for (Protos.TransactionOutput.Builder output : tx.getTransactionOutputBuilderList()) {
                if (output.hasSpentByTransactionHash())
                    output.setSpentByTransactionHash(bytesToHex(output.getSpentByTransactionHash()));
            }
            // TODO: keys, ip addresses etc.
        }
        return builder.build();
    } catch (Throwable throwable) {
        log.error("Failed to do hex conversion on wallet proto", throwable);
        return proto;
    }
}
 
Example 5
Source File: WalletProtobufSerializerTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void extensions() throws Exception {
    myWallet.addExtension(new FooWalletExtension("com.whatever.required", true));
    Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
    // Initial extension is mandatory: try to read it back into a wallet that doesn't know about it.
    try {
        new WalletProtobufSerializer().readWallet(PARAMS, null, proto);
        fail();
    } catch (UnreadableWalletException e) {
        assertTrue(e.getMessage().contains("mandatory"));
    }
    Wallet wallet = new WalletProtobufSerializer().readWallet(PARAMS,
            new WalletExtension[]{ new FooWalletExtension("com.whatever.required", true) },
            proto);
    assertTrue(wallet.getExtensions().containsKey("com.whatever.required"));

    // Non-mandatory extensions are ignored if the wallet doesn't know how to read them.
    Wallet wallet2 = new Wallet(PARAMS);
    wallet2.addExtension(new FooWalletExtension("com.whatever.optional", false));
    Protos.Wallet proto2 = new WalletProtobufSerializer().walletToProto(wallet2);
    Wallet wallet5 = new WalletProtobufSerializer().readWallet(PARAMS, null, proto2);
    assertEquals(0, wallet5.getExtensions().size());
}
 
Example 6
Source File: WalletProtobufSerializerTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void oneTx() throws Exception {
    // Check basic tx serialization.
    Coin v1 = COIN;
    Transaction t1 = createFakeTx(UNITTEST, v1, myAddress);
    t1.getConfidence().markBroadcastBy(new PeerAddress(UNITTEST, InetAddress.getByName("1.2.3.4")));
    t1.getConfidence().markBroadcastBy(new PeerAddress(UNITTEST, InetAddress.getByName("5.6.7.8")));
    t1.getConfidence().setSource(TransactionConfidence.Source.NETWORK);
    myWallet.receivePending(t1, null);
    Wallet wallet1 = roundTrip(myWallet);
    assertEquals(1, wallet1.getTransactions(true).size());
    assertEquals(v1, wallet1.getBalance(Wallet.BalanceType.ESTIMATED));
    Transaction t1copy = wallet1.getTransaction(t1.getHash());
    assertArrayEquals(t1.unsafeBitcoinSerialize(), t1copy.unsafeBitcoinSerialize());
    assertEquals(2, t1copy.getConfidence().numBroadcastPeers());
    assertNotNull(t1copy.getConfidence().getLastBroadcastedAt());
    assertEquals(TransactionConfidence.Source.NETWORK, t1copy.getConfidence().getSource());

    Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(myWallet);
    assertEquals(Protos.Key.Type.ORIGINAL, walletProto.getKey(0).getType());
    assertEquals(0, walletProto.getExtensionCount());
    assertEquals(1, walletProto.getTransactionCount());
    assertEquals(6, walletProto.getKeyCount());

    Protos.Transaction t1p = walletProto.getTransaction(0);
    assertEquals(0, t1p.getBlockHashCount());
    assertArrayEquals(t1.getHash().getBytes(), t1p.getHash().toByteArray());
    assertEquals(Protos.Transaction.Pool.PENDING, t1p.getPool());
    assertFalse(t1p.hasLockTime());
    assertFalse(t1p.getTransactionInput(0).hasSequence());
    assertArrayEquals(t1.getInputs().get(0).getOutpoint().getHash().getBytes(),
            t1p.getTransactionInput(0).getTransactionOutPointHash().toByteArray());
    assertEquals(0, t1p.getTransactionInput(0).getTransactionOutPointIndex());
    assertEquals(t1p.getTransactionOutput(0).getValue(), v1.value);
}
 
Example 7
Source File: WalletProtobufSerializerTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testLastBlockSeenHash() throws Exception {
    // Test the lastBlockSeenHash field works.

    // LastBlockSeenHash should be empty if never set.
    Wallet wallet = new Wallet(UNITTEST);
    Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(wallet);
    ByteString lastSeenBlockHash = walletProto.getLastSeenBlockHash();
    assertTrue(lastSeenBlockHash.isEmpty());

    // Create a block.
    Block block = UNITTEST.getDefaultSerializer()
            .makeBlock(ByteStreams.toByteArray(BlockTest.class.getResourceAsStream("block_testnet700000.dat")));
    Sha256Hash blockHash = block.getHash();
    wallet.setLastBlockSeenHash(blockHash);
    wallet.setLastBlockSeenHeight(1);

    // Roundtrip the wallet and check it has stored the blockHash.
    Wallet wallet1 = roundTrip(wallet);
    assertEquals(blockHash, wallet1.getLastBlockSeenHash());
    assertEquals(1, wallet1.getLastBlockSeenHeight());

    // Test the Satoshi genesis block (hash of all zeroes) is roundtripped ok.
    Block genesisBlock = MAINNET.getGenesisBlock();
    wallet.setLastBlockSeenHash(genesisBlock.getHash());
    Wallet wallet2 = roundTrip(wallet);
    assertEquals(genesisBlock.getHash(), wallet2.getLastBlockSeenHash());
}
 
Example 8
Source File: WalletProtobufSerializerTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void extensionsWithError() throws Exception {
    WalletExtension extension = new WalletExtension() {
        @Override
        public String getWalletExtensionID() {
            return "test";
        }

        @Override
        public boolean isWalletExtensionMandatory() {
            return false;
        }

        @Override
        public byte[] serializeWalletExtension() {
            return new byte[0];
        }

        @Override
        public void deserializeWalletExtension(Wallet containingWallet, byte[] data) throws Exception {
            throw new NullPointerException();  // Something went wrong!
        }
    };
    myWallet.addExtension(extension);
    Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
    Wallet wallet = new WalletProtobufSerializer().readWallet(UNITTEST, new WalletExtension[]{extension}, proto);
    assertEquals(0, wallet.getExtensions().size());
}
 
Example 9
Source File: WalletProtobufSerializerTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void oneTx() throws Exception {
    // Check basic tx serialization.
    Coin v1 = COIN;
    Transaction t1 = createFakeTx(PARAMS, v1, myAddress);
    t1.getConfidence().markBroadcastBy(new PeerAddress(PARAMS, InetAddress.getByName("1.2.3.4")));
    t1.getConfidence().markBroadcastBy(new PeerAddress(PARAMS, InetAddress.getByName("5.6.7.8")));
    t1.getConfidence().setSource(TransactionConfidence.Source.NETWORK);
    myWallet.receivePending(t1, null);
    Wallet wallet1 = roundTrip(myWallet);
    assertEquals(1, wallet1.getTransactions(true).size());
    assertEquals(v1, wallet1.getBalance(Wallet.BalanceType.ESTIMATED));
    Transaction t1copy = wallet1.getTransaction(t1.getHash());
    assertArrayEquals(t1.unsafeBitcoinSerialize(), t1copy.unsafeBitcoinSerialize());
    assertEquals(2, t1copy.getConfidence().numBroadcastPeers());
    assertNotNull(t1copy.getConfidence().getLastBroadcastedAt());
    assertEquals(TransactionConfidence.Source.NETWORK, t1copy.getConfidence().getSource());
    
    Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(myWallet);
    assertEquals(Protos.Key.Type.ORIGINAL, walletProto.getKey(0).getType());
    assertEquals(0, walletProto.getExtensionCount());
    assertEquals(1, walletProto.getTransactionCount());
    assertEquals(6, walletProto.getKeyCount());
    
    Protos.Transaction t1p = walletProto.getTransaction(0);
    assertEquals(0, t1p.getBlockHashCount());
    assertArrayEquals(t1.getHash().getBytes(), t1p.getHash().toByteArray());
    assertEquals(Protos.Transaction.Pool.PENDING, t1p.getPool());
    assertFalse(t1p.hasLockTime());
    assertFalse(t1p.getTransactionInput(0).hasSequence());
    assertArrayEquals(t1.getInputs().get(0).getOutpoint().getHash().getBytes(),
            t1p.getTransactionInput(0).getTransactionOutPointHash().toByteArray());
    assertEquals(0, t1p.getTransactionInput(0).getTransactionOutPointIndex());
    assertEquals(t1p.getTransactionOutput(0).getValue(), v1.value);
}
 
Example 10
Source File: WalletProtobufSerializerTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testLastBlockSeenHash() throws Exception {
    // Test the lastBlockSeenHash field works.

    // LastBlockSeenHash should be empty if never set.
    Wallet wallet = new Wallet(PARAMS);
    Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(wallet);
    ByteString lastSeenBlockHash = walletProto.getLastSeenBlockHash();
    assertTrue(lastSeenBlockHash.isEmpty());

    // Create a block.
    Block block = PARAMS.getDefaultSerializer().makeBlock(BlockTest.blockBytes);
    Sha256Hash blockHash = block.getHash();
    wallet.setLastBlockSeenHash(blockHash);
    wallet.setLastBlockSeenHeight(1);

    // Roundtrip the wallet and check it has stored the blockHash.
    Wallet wallet1 = roundTrip(wallet);
    assertEquals(blockHash, wallet1.getLastBlockSeenHash());
    assertEquals(1, wallet1.getLastBlockSeenHeight());

    // Test the Satoshi genesis block (hash of all zeroes) is roundtripped ok.
    Block genesisBlock = MainNetParams.get().getGenesisBlock();
    wallet.setLastBlockSeenHash(genesisBlock.getHash());
    Wallet wallet2 = roundTrip(wallet);
    assertEquals(genesisBlock.getHash(), wallet2.getLastBlockSeenHash());
}
 
Example 11
Source File: WalletProtobufSerializerTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void extensionsWithError() throws Exception {
    WalletExtension extension = new WalletExtension() {
        @Override
        public String getWalletExtensionID() {
            return "test";
        }

        @Override
        public boolean isWalletExtensionMandatory() {
            return false;
        }

        @Override
        public byte[] serializeWalletExtension() {
            return new byte[0];
        }

        @Override
        public void deserializeWalletExtension(Wallet containingWallet, byte[] data) throws Exception {
            throw new NullPointerException();  // Something went wrong!
        }
    };
    myWallet.addExtension(extension);
    Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
    Wallet wallet = new WalletProtobufSerializer().readWallet(PARAMS, new WalletExtension[]{extension}, proto);
    assertEquals(0, wallet.getExtensions().size());
}
 
Example 12
Source File: WalletProtobufSerializerTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void oneTx() throws Exception {
    // Check basic tx serialization.
    Coin v1 = COIN;
    Transaction t1 = createFakeTx(PARAMS, v1, myAddress);
    t1.getConfidence().markBroadcastBy(new PeerAddress(PARAMS, InetAddress.getByName("1.2.3.4")));
    t1.getConfidence().markBroadcastBy(new PeerAddress(PARAMS, InetAddress.getByName("5.6.7.8")));
    t1.getConfidence().setSource(TransactionConfidence.Source.NETWORK);
    myWallet.receivePending(t1, null);
    Wallet wallet1 = roundTrip(myWallet);
    assertEquals(1, wallet1.getTransactions(true).size());
    assertEquals(v1, wallet1.getBalance(Wallet.BalanceType.ESTIMATED));
    Transaction t1copy = wallet1.getTransaction(t1.getHash());
    assertArrayEquals(t1.unsafeBitcoinSerialize(), t1copy.unsafeBitcoinSerialize());
    assertEquals(2, t1copy.getConfidence().numBroadcastPeers());
    assertNotNull(t1copy.getConfidence().getLastBroadcastedAt());
    assertEquals(TransactionConfidence.Source.NETWORK, t1copy.getConfidence().getSource());
    
    Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(myWallet);
    assertEquals(Protos.Key.Type.ORIGINAL, walletProto.getKey(0).getType());
    assertEquals(0, walletProto.getExtensionCount());
    assertEquals(1, walletProto.getTransactionCount());
    assertEquals(6, walletProto.getKeyCount());
    
    Protos.Transaction t1p = walletProto.getTransaction(0);
    assertEquals(0, t1p.getBlockHashCount());
    assertArrayEquals(t1.getHash().getBytes(), t1p.getHash().toByteArray());
    assertEquals(Protos.Transaction.Pool.PENDING, t1p.getPool());
    assertFalse(t1p.hasLockTime());
    assertFalse(t1p.getTransactionInput(0).hasSequence());
    assertArrayEquals(t1.getInputs().get(0).getOutpoint().getHash().getBytes(),
            t1p.getTransactionInput(0).getTransactionOutPointHash().toByteArray());
    assertEquals(0, t1p.getTransactionInput(0).getTransactionOutPointIndex());
    assertEquals(t1p.getTransactionOutput(0).getValue(), v1.value);
}
 
Example 13
Source File: WalletProtobufSerializerTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testLastBlockSeenHash() throws Exception {
    // Test the lastBlockSeenHash field works.

    // LastBlockSeenHash should be empty if never set.
    Wallet wallet = new Wallet(PARAMS);
    Protos.Wallet walletProto = new WalletProtobufSerializer().walletToProto(wallet);
    ByteString lastSeenBlockHash = walletProto.getLastSeenBlockHash();
    assertTrue(lastSeenBlockHash.isEmpty());

    // Create a block.
    Block block = PARAMS.getDefaultSerializer().makeBlock(BlockTest.blockBytes);
    Sha256Hash blockHash = block.getHash();
    wallet.setLastBlockSeenHash(blockHash);
    wallet.setLastBlockSeenHeight(1);

    // Roundtrip the wallet and check it has stored the blockHash.
    Wallet wallet1 = roundTrip(wallet);
    assertEquals(blockHash, wallet1.getLastBlockSeenHash());
    assertEquals(1, wallet1.getLastBlockSeenHeight());

    // Test the Satoshi genesis block (hash of all zeroes) is roundtripped ok.
    Block genesisBlock = MainNetParams.get().getGenesisBlock();
    wallet.setLastBlockSeenHash(genesisBlock.getHash());
    Wallet wallet2 = roundTrip(wallet);
    assertEquals(genesisBlock.getHash(), wallet2.getLastBlockSeenHash());
}
 
Example 14
Source File: WalletProtobufSerializerTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void extensionsWithError() throws Exception {
    WalletExtension extension = new WalletExtension() {
        @Override
        public String getWalletExtensionID() {
            return "test";
        }

        @Override
        public boolean isWalletExtensionMandatory() {
            return false;
        }

        @Override
        public byte[] serializeWalletExtension() {
            return new byte[0];
        }

        @Override
        public void deserializeWalletExtension(Wallet containingWallet, byte[] data) throws Exception {
            throw new NullPointerException();  // Something went wrong!
        }
    };
    myWallet.addExtension(extension);
    Protos.Wallet proto = new WalletProtobufSerializer().walletToProto(myWallet);
    Wallet wallet = new WalletProtobufSerializer().readWallet(PARAMS, new WalletExtension[]{extension}, proto);
    assertEquals(0, wallet.getExtensions().size());
}
 
Example 15
Source File: WalletProtobufSerializer.java    From bither-android with Apache License 2.0 4 votes vote down vote up
private static Protos.Wallet parseToProto(InputStream input) throws IOException {
    return Protos.Wallet.parseFrom(input);
}
 
Example 16
Source File: WalletProtobufSerializer.java    From bither-android with Apache License 2.0 3 votes vote down vote up
public ECKey readWallet(InputStream input) throws Exception {

        Protos.Wallet walletProto = parseToProto(input);
        return readWallet(walletProto);


    }