Your test contains assertFalse(WalletProtobufSerializer.isWallet(test))
and that fails -- so actually the isWallet() method returns true, which
is expected.


On 10/22/2017 01:57 PM, curio...@gmail.com wrote:
> 
> Hello everybody,
> 
> I am playing around with bitcoinj again, especially with married
> wallets. I tried to reproduce the following code:
> 
> https://programtalk.com/vs/?source=bitcoinj/core/src/test/java/org/bitcoinj/store/WalletProtobufSerializerTest.java
> 
> However, it seems that WalletProtobufSerializer.isWallet tests wrongly
> or the wallet format changed or I did something wrongly.
> 
> The version of bitcoinj is 0.14.5, and my code is this:
> 
> package test;
> 
> import java.io.ByteArrayInputStream;
> import java.io.ByteArrayOutputStream;
> import java.security.SecureRandom;
> import java.util.Date;
> 
> import org.bitcoinj.core.Address;
> import org.bitcoinj.core.Coin;
> import org.bitcoinj.core.ECKey;
> import org.bitcoinj.core.NetworkParameters;
> import org.bitcoinj.crypto.DeterministicKey;
> import org.bitcoinj.params.MainNetParams;
> import org.bitcoinj.wallet.DeterministicKeyChain;
> import org.bitcoinj.wallet.MarriedKeyChain;
> import org.bitcoinj.wallet.Wallet;
> import org.bitcoinj.wallet.KeyChain;
> import org.bitcoinj.wallet.WalletProtobufSerializer;
> 
> import static org.junit.Assert.*;
> 
> public class Main {
>     private static final NetworkParameters params = new MainNetParams();
>     private ECKey key;
>     private ECKey watched_key;
>     private Address address;
>     private Wallet wallet;
>    
>     public static String WALLET_DESCRIPTION  = "The quick brown fox
> lives in \u4f26\u6566"; // Beijing in Chinese
>     private long mScriptCreationTime;
>    
>     public static void main(String[] args) throws Exception{
>         // TODO Auto-generated method stub
>        
>         Main m = new Main();
>         m.setUp();
>         System.out.println("setup finished");
>         m.testRoundTripNormalWallet();
>         System.out.println("normal wallet tested");
>         m.testRoundTripMarriedWallet();
>         System.out.println("married wallet tested");
>     }
>    
>     public void setUp() throws Exception {
>         this.watched_key = new ECKey();
>         this.wallet = new Wallet(params);
>         this.key = new ECKey();
>         this.key.setCreationTimeSeconds(123456789L);
>         this.wallet.importKey(this.key);
>         this.address = this.key.toAddress(params);
>         this.wallet = new Wallet(params);
>         this.wallet.importKey(this.key);
>         this.mScriptCreationTime = new Date().getTime() / 1000 - 1234;
>        
> this.wallet.addWatchedAddress(this.watched_key.toAddress(params),
> this.mScriptCreationTime);
>         this.wallet.setDescription(WALLET_DESCRIPTION);
>     }
>    
>     public void testRoundTripNormalWallet() throws Exception {
>         Wallet wallet1 = roundTrip(this.wallet);
>         assertEquals(0, wallet1.getTransactions(true).size());
>         assertEquals(Coin.ZERO, wallet1.getBalance());
>         assertArrayEquals(this.key.getPubKey(),
> wallet1.findKeyFromPubHash(this.key.getPubKeyHash()).getPubKey());
>         assertArrayEquals(this.key.getPrivKeyBytes(),
> wallet1.findKeyFromPubHash(this.key.getPubKeyHash()).getPrivKeyBytes());
>         assertEquals(this.key.getCreationTimeSeconds(),
> wallet1.findKeyFromPubHash(this.key.getPubKeyHash()).getCreationTimeSeconds());
>     }
> 
>     public void testRoundTripMarriedWallet() throws Exception {
>         // create 2-of-2 married wallet
>         this.wallet = new Wallet(params);
>         final DeterministicKeyChain partner_chain = new
> DeterministicKeyChain(new SecureRandom());
>         DeterministicKey partner_key =
> DeterministicKey.deserializeB58(null,
> partner_chain.getWatchingKey().serializePubB58(params), params);
>         MarriedKeyChain chain = MarriedKeyChain.builder().random(new
> SecureRandom()).followingKeys(partner_key).threshold(2).build();
>         this.wallet.addAndActivateHDChain(chain);
> 
>         this.address =
> this.wallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
> 
>         Wallet wallet1 = roundTrip(this.wallet);
>         assertEquals(0, wallet1.getTransactions(true).size());
>         assertEquals(Coin.ZERO, wallet1.getBalance());
>         assertEquals(2,
> wallet1.getActiveKeyChain().getSigsRequiredToSpend());
>         assertEquals(this.address,
> wallet1.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
>     }
> 
>     private static Wallet roundTrip(Wallet wallet) throws Exception {
>         ByteArrayOutputStream output = new ByteArrayOutputStream();
>         new WalletProtobufSerializer().writeWallet(wallet, output);
>         ByteArrayInputStream test = new
> ByteArrayInputStream(output.toByteArray());
>         assertFalse(WalletProtobufSerializer.isWallet(test));
>         ByteArrayInputStream input = new
> ByteArrayInputStream(output.toByteArray());
>         return new WalletProtobufSerializer().readWallet(input);
>     }
> }
> 
> Output is:
> 
> Oct 22, 2017 1:55:41 PM org.bitcoinj.core.Context getOrCreate
> WARNING: Implicitly creating context. This is a migration step and this
> message will eventually go away.
> Oct 22, 2017 1:55:41 PM org.bitcoinj.core.Context <init>
> INFO: Creating bitcoinj 0.14.5 context.
> Oct 22, 2017 1:55:41 PM org.bitcoinj.crypto.MnemonicCode toSeed
> INFO: PBKDF2 took 106.8 ms
> Oct 22, 2017 1:55:41 PM org.bitcoinj.wallet.KeyChainGroup
> addAndActivateHDChain
> INFO: Creating and activating a new HD chain:
> org.bitcoinj.wallet.DeterministicKeyChain@47c62251
> Oct 22, 2017 1:55:41 PM org.bitcoinj.crypto.MnemonicCode toSeed
> INFO: PBKDF2 took 32.68 ms
> Oct 22, 2017 1:55:41 PM org.bitcoinj.wallet.KeyChainGroup
> addAndActivateHDChain
> INFO: Creating and activating a new HD chain:
> org.bitcoinj.wallet.DeterministicKeyChain@239963d8
> setup finished
> Exception in thread "main" java.lang.AssertionError
>     at org.junit.Assert.fail(Assert.java:86)
>     at org.junit.Assert.assertTrue(Assert.java:41)
>     at org.junit.Assert.assertFalse(Assert.java:64)
>     at org.junit.Assert.assertFalse(Assert.java:74)
>     at test.Main.roundTrip(Main.java:88)
>     at test.Main.testRoundTripNormalWallet(Main.java:59)
>     at test.Main.main(Main.java:38)
> 
> thanks in adavance.
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "bitcoinj" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to bitcoinj+unsubscr...@googlegroups.com
> <mailto:bitcoinj+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups 
"bitcoinj" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bitcoinj+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to