Import/Export wallet problem

91 views
Skip to first unread message

MrKwiatkowsky

unread,
Jan 12, 2015, 6:11:57 AM1/12/15
to bitc...@googlegroups.com
Hello guys,
I've got a little problem. I wrote import and export for bitcoinj 0.11 a lot time ago, but app has been updated to 0.12 and two days ago I was trying to use import and export and... fail. Export looks good, but when I'm trying to import, there are errors. Can someone give an advice or solution? I can add, that Crypto.class works well in 100%.

EXPORT:
Writer out;
try
{
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
new WalletProtobufSerializer().writeWallet(wallet, byteOutput);
byteOutput.close();
byte[] plain = byteOutput.toByteArray();

String encrypted = Crypto.encrypt(plain);

out = new OutputStreamWriter(new FileOutputStream(backupFile), "UTF-8");
out.write(encrypted);
out.flush();
}
catch (IOException e)
{
e.printStackTrace();
}


IMPORT
BufferedReader buffreader;
FileInputStream fIn;
try
{
StringBuffer datax = new StringBuffer("");
fIn = new FileInputStream(backupFile);
InputStreamReader isr = new InputStreamReader(fIn);
buffreader = new BufferedReader(isr);    
String readString = buffreader.readLine();
while (readString != null)
{
datax.append(readString);
readString = buffreader.readLine();
}

byte[] decrypted = Crypto.decrypt(text.toString());
if (decrypted == null)
return;
else
{
InputStream inputStream = new ByteArrayInputStream(decrypted);
Wallet wallet = new WalletProtobufSerializer().readWallet(inputStream);  //ERROR IN THIS LINE
inputStream.close();
                //etc.
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (UnreadableWalletException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}


LogCat

01-12 11:57:39.727: W/System.err(20751): org.bitcoinj.store.UnreadableWalletException: Could not parse input stream to protobuf
01-12 11:57:39.727: W/System.err(20751):  at org.bitcoinj.store.WalletProtobufSerializer.readWallet(WalletProtobufSerializer.java:391)
...
01-12 11:57:39.727: W/System.err(20751): Caused by: com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
01-12 11:57:39.732: W/System.err(20751):  at com.google.protobuf.CodedInputStream.readTag(CodedInputStream.java:108)
...


Mike Hearn

unread,
Jan 12, 2015, 6:17:53 AM1/12/15
to bitc...@googlegroups.com
Your encoding must be messed up. Nothing changed between 0.11 and 0.12 that might have caused that. What does Crypto.encrypt produce, base64?

--
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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

MrKwiatkowsky

unread,
Jan 12, 2015, 6:31:52 AM1/12/15
to bitc...@googlegroups.com, mi...@plan99.net
Exactly, base64. But even if I disable encrypting by Crypto, it still doesn't work how it supposed to do. HD wallets didn't change anything? Maybe do you have some simple working example?

Mike Hearn

unread,
Jan 12, 2015, 7:30:34 AM1/12/15
to bitc...@googlegroups.com
Where does the data differ between the exported bytes and imported bytes? I mean, this is your code so you're in the best position to debug it:   lots of people are serializing and deserializing wallets with 0.12 just fine, so I think the issue is in the extra stuff you're adding on top. Why are you even storing wallets as text?

MrKwiatkowsky

unread,
Jan 12, 2015, 8:04:12 AM1/12/15
to bitc...@googlegroups.com, mi...@plan99.net
I think, that just after "writeWallet(wallet, byteOutput)" are some problems. Do you have some other ideas for storing wallets? Which class/functions I should use? Text is easy to crypt it with password. Do you have some learning sources with import/export? 

Mike Hearn

unread,
Jan 12, 2015, 10:41:57 AM1/12/15
to bitc...@googlegroups.com
No crypto library I know of cares about whether you store the encrypted bytes as text or not.

Like I said, bitcoinj serialises to bytes and back, and I've never heard of the problem you are reporting. I suggest you debug the code, I can't help you any further.

MrKwiatkowsky

unread,
Jan 13, 2015, 4:03:45 AM1/13/15
to bitc...@googlegroups.com, mi...@plan99.net
Solved. I give up with text and serialise to bytes and now it's working perfectly. Thank you for the help.

Mike Hearn

unread,
Jan 13, 2015, 7:38:15 AM1/13/15
to bitc...@googlegroups.com
Great, good to hear you got it solved.

Reply all
Reply to author
Forward
0 new messages