org.bitcoinj.wallet.Protos Java Examples

The following examples show how to use org.bitcoinj.wallet.Protos. 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: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void serializationUnencrypted() throws UnreadableWalletException {
    Utils.setMockClock();
    Date now = Utils.now();
    final ECKey key1 = new ECKey();
    Utils.rollMockClock(5000);
    final ECKey key2 = new ECKey();
    chain.importKeys(ImmutableList.of(key1, key2));
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(2, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertArrayEquals(key2.getPubKey(), keys.get(1).getPublicKey().toByteArray());
    assertArrayEquals(key1.getPrivKeyBytes(), keys.get(0).getSecretBytes().toByteArray());
    assertArrayEquals(key2.getPrivKeyBytes(), keys.get(1).getSecretBytes().toByteArray());
    long normTime = (long) (Math.floor(now.getTime() / 1000) * 1000);
    assertEquals(normTime, keys.get(0).getCreationTimestamp());
    assertEquals(normTime + 5000 * 1000, keys.get(1).getCreationTimestamp());

    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(2, chain.getKeys().size());
    assertEquals(key1, chain.getKeys().get(0));
    assertEquals(key2, chain.getKeys().get(1));
}
 
Example #2
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 #3
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void serializationUnencrypted() throws UnreadableWalletException {
    Utils.setMockClock();
    Date now = Utils.now();
    final ECKey key1 = new ECKey();
    Utils.rollMockClock(5000);
    final ECKey key2 = new ECKey();
    chain.importKeys(ImmutableList.of(key1, key2));
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(2, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertArrayEquals(key2.getPubKey(), keys.get(1).getPublicKey().toByteArray());
    assertArrayEquals(key1.getPrivKeyBytes(), keys.get(0).getSecretBytes().toByteArray());
    assertArrayEquals(key2.getPrivKeyBytes(), keys.get(1).getSecretBytes().toByteArray());
    long normTime = (long) (Math.floor(now.getTime() / 1000) * 1000);
    assertEquals(normTime, keys.get(0).getCreationTimestamp());
    assertEquals(normTime + 5000 * 1000, keys.get(1).getCreationTimestamp());

    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(2, chain.getKeys().size());
    assertEquals(key1, chain.getKeys().get(0));
    assertEquals(key2, chain.getKeys().get(1));
}
 
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: 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 #6
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void serializationUnencrypted() throws UnreadableWalletException {
    Utils.setMockClock();
    Date now = Utils.now();
    final ECKey key1 = new ECKey();
    Utils.rollMockClock(5000);
    final ECKey key2 = new ECKey();
    chain.importKeys(ImmutableList.of(key1, key2));
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(2, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertArrayEquals(key2.getPubKey(), keys.get(1).getPublicKey().toByteArray());
    assertArrayEquals(key1.getPrivKeyBytes(), keys.get(0).getSecretBytes().toByteArray());
    assertArrayEquals(key2.getPrivKeyBytes(), keys.get(1).getSecretBytes().toByteArray());
    long normTime = (long) (Math.floor(now.getTime() / 1000) * 1000);
    assertEquals(normTime, keys.get(0).getCreationTimestamp());
    assertEquals(normTime + 5000 * 1000, keys.get(1).getCreationTimestamp());

    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(2, chain.getKeys().size());
    assertEquals(key1, chain.getKeys().get(0));
    assertEquals(key2, chain.getKeys().get(1));
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: KeyCrypterScryptTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder()
            .setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt()));
    scryptParameters = scryptParametersBuilder.build();

    BriefLogFormatter.init();
}
 
Example #12
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void serializationEncrypted() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    chain.importKeys(key1);
    chain = chain.toEncrypted("foo bar");
    key1 = chain.getKeys().get(0);
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertFalse(keys.get(0).hasSecretBytes());
    assertTrue(keys.get(0).hasEncryptedData());
    chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter()));
    assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey());
    assertTrue(chain.checkPassword("foo bar"));
}
 
Example #13
Source File: BasicKeyChainTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void watching() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    ECKey pub = ECKey.fromPublicOnly(key1.getPubKeyPoint());
    chain.importKeys(pub);
    assertEquals(1, chain.numKeys());
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertTrue(keys.get(0).hasPublicKey());
    assertFalse(keys.get(0).hasSecretBytes());
    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(1, chain.numKeys());
    assertFalse(chain.findKeyFromPubKey(pub.getPubKey()).hasPrivKey());
}
 
Example #14
Source File: ECKeyTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder().setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt()));
    ScryptParameters scryptParameters = scryptParametersBuilder.build();
    keyCrypter = new KeyCrypterScrypt(scryptParameters);

    BriefLogFormatter.init();
}
 
Example #15
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 #16
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 #17
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 #18
Source File: ECKeyTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder().setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt()));
    ScryptParameters scryptParameters = scryptParametersBuilder.build();
    keyCrypter = new KeyCrypterScrypt(scryptParameters);

    BriefLogFormatter.init();
}
 
Example #19
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void watching() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    ECKey pub = ECKey.fromPublicOnly(key1.getPubKeyPoint());
    chain.importKeys(pub);
    assertEquals(1, chain.numKeys());
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertTrue(keys.get(0).hasPublicKey());
    assertFalse(keys.get(0).hasSecretBytes());
    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(1, chain.numKeys());
    assertFalse(chain.findKeyFromPubKey(pub.getPubKey()).hasPrivKey());
}
 
Example #20
Source File: BasicKeyChainTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void serializationEncrypted() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    chain.importKeys(key1);
    chain = chain.toEncrypted("foo bar");
    key1 = chain.getKeys().get(0);
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertFalse(keys.get(0).hasSecretBytes());
    assertTrue(keys.get(0).hasEncryptedData());
    chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter()));
    assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey());
    assertTrue(chain.checkPassword("foo bar"));
}
 
Example #21
Source File: KeyCrypterScryptTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder()
            .setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt()));
    scryptParameters = scryptParametersBuilder.build();

    BriefLogFormatter.init();
}
 
Example #22
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 #23
Source File: ScryptUtil.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public static KeyCrypterScrypt getKeyCrypterScrypt() {
    Protos.ScryptParameters scryptParameters = Protos.ScryptParameters.newBuilder()
            .setP(6)
            .setR(8)
            .setN(32768)
            .setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt()))
            .build();
    return new KeyCrypterScrypt(scryptParameters);
}
 
Example #24
Source File: BisqKeyChainFactory.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public DeterministicKeyChain makeWatchingKeyChain(Protos.Key key, Protos.Key firstSubKey, DeterministicKey accountKey,
                                                  boolean isFollowingKey, boolean isMarried) throws UnreadableWalletException {
    ImmutableList<ChildNumber> accountPath = useBitcoinDeterministicKeyChain ? BtcDeterministicKeyChain.BIP44_BTC_ACCOUNT_PATH : BisqDeterministicKeyChain.BIP44_BSQ_ACCOUNT_PATH;
    if (!accountKey.getPath().equals(accountPath))
        throw new UnreadableWalletException("Expecting account key but found key with path: " +
                HDUtils.formatPath(accountKey.getPath()));
    return useBitcoinDeterministicKeyChain ? new BtcDeterministicKeyChain(accountKey, isFollowingKey) : new BisqDeterministicKeyChain(accountKey, isFollowingKey);
}
 
Example #25
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 #26
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 #27
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 #28
Source File: ECKeyTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Protos.ScryptParameters.Builder scryptParametersBuilder = Protos.ScryptParameters.newBuilder().setSalt(ByteString.copyFrom(KeyCrypterScrypt.randomSalt()));
    ScryptParameters scryptParameters = scryptParametersBuilder.build();
    keyCrypter = new KeyCrypterScrypt(scryptParameters);

    BriefLogFormatter.init();
}
 
Example #29
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void watching() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    ECKey pub = ECKey.fromPublicOnly(key1.getPubKeyPoint());
    chain.importKeys(pub);
    assertEquals(1, chain.numKeys());
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertTrue(keys.get(0).hasPublicKey());
    assertFalse(keys.get(0).hasSecretBytes());
    chain = BasicKeyChain.fromProtobufUnencrypted(keys);
    assertEquals(1, chain.numKeys());
    assertFalse(chain.findKeyFromPubKey(pub.getPubKey()).hasPrivKey());
}
 
Example #30
Source File: BasicKeyChainTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void serializationEncrypted() throws UnreadableWalletException {
    ECKey key1 = new ECKey();
    chain.importKeys(key1);
    chain = chain.toEncrypted("foo bar");
    key1 = chain.getKeys().get(0);
    List<Protos.Key> keys = chain.serializeToProtobuf();
    assertEquals(1, keys.size());
    assertArrayEquals(key1.getPubKey(), keys.get(0).getPublicKey().toByteArray());
    assertFalse(keys.get(0).hasSecretBytes());
    assertTrue(keys.get(0).hasEncryptedData());
    chain = BasicKeyChain.fromProtobufEncrypted(keys, checkNotNull(chain.getKeyCrypter()));
    assertEquals(key1.getEncryptedPrivateKey(), chain.getKeys().get(0).getEncryptedPrivateKey());
    assertTrue(chain.checkPassword("foo bar"));
}