Reputation: 123
I'm having a bit of trouble with a script I'm building at the moment. I'm using Python-BitcoinRPC (a fork of JSON-RPC) - but my Python experience is VERY limited, and my text encoding experience even moreso, so please bear with me.
A friend wants to sell his coins, but can't remember the key to his wallet. I've built a script to try various combinations and permutations of all his usual passwords, but we haven't come up with anything. Now we're trying some slightly more obscure characters, such as "¿". The problem is, if I put "¿" into a string and echo it back, it becomes "┬┐" - which is not seen as the same as "¿" by the bitcoin daemon (we've created a test wallet with the key "¿abc", and we can't unock it if we pass that as a string literal to the daemon.
This also throws my script out as it shuffles individual characters about, and hence sees "┬┐" as two chars and causes the script to crash with "'utf8' codec can't decode byte 0xc2 in position 10: invalid continuation byte" (although I don't get this if I use a string literal containing "¿".)
I'm a bit out of my depth here, and I'm hoping someone can help. What is happening when python is converting these characters to the multi-character strings, and how can I get it to pass them verbatim as a parameter, and for string operations to treat them as a single char?
Here's a quick bit of code to hopefully explain what I mean:
# -*- coding: utf-8 -*-
import bitcoinrpc
import sys
word = "abc"
chars = "¿?!"
phrase_a = chars[0:1] + word # = ┬abc, error: 'utf8' codec can't decode byte 0xc2 in position 0: invalid continuation byte
phrase_b = chars[1:2] + word # = ┐abc, error: 'utf8' codec can't decode byte 0xbf in position 0: invalid start byte
phrase_c = chars[2:3] + word # = ?abc, no error, but would expect the result to be "!abc"
phrase_d = chars + word # = ┬┐?!abc, no error
rpc_username = "bitcoinrpc"
rpc_password = "somepassword"
btc_conn = bitcoinrpc.connect_to_remote(rpc_username, rpc_password, host="localhost", port=8332, use_https=False)
try:
btc_conn.walletpassphrase(phrase_b, 20)
except bitcoinrpc.exceptions.WalletPassphraseIncorrect:
print "Phrase incorrect"
sys.exit(0)
except Exception as e:
print str(e)
sys.exit(0)
print "Phrase correct"
Any help will be greatly appreciated!
Upvotes: 0
Views: 255
Reputation: 527508
Use a Unicode string literal by adding a u
prefix:
>>> chars = u"¿?!"
>>> print chars[0]
¿
That way Python knows to handle the values as multibyte characters, rather than trying to manipulate it as a sequence of single-byte characters.
Upvotes: 1