| 1 |
""" |
| 2 |
******************************************************************************* |
| 3 |
* BTChip Bitcoin Hardware Wallet Python API |
| 4 |
* (c) 2014 BTChip - 1BTChip7VfTnrPra5jqci7ejnMguuHogTn |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
******************************************************************************** |
| 18 |
""" |
| 19 |
|
| 20 |
# Coinkite co-signer provisioning |
| 21 |
# To be run with the dongle in developer mode, PIN verified |
| 22 |
|
| 23 |
import hashlib |
| 24 |
from btchip.btchip import * |
| 25 |
from btchip.btchipUtils import * |
| 26 |
from base64 import b64encode |
| 27 |
|
| 28 |
# Replace with your own seed (preferably import it and store it), key path, and Testnet flag |
| 29 |
SEED = bytearray("fe721b95503a18a14d93914e02ff153f924737c336b01f98f2ff39395f630187".decode('hex')) |
| 30 |
KEYPATH = "0'/2/0" |
| 31 |
TESTNET = True |
| 32 |
|
| 33 |
# From Electrum |
| 34 |
|
| 35 |
__b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' |
| 36 |
__b58base = len(__b58chars) |
| 37 |
|
| 38 |
def b58encode(v): |
| 39 |
""" encode v, which is a string of bytes, to base58.""" |
| 40 |
|
| 41 |
long_value = 0L |
| 42 |
for (i, c) in enumerate(v[::-1]): |
| 43 |
long_value += (256**i) * ord(c) |
| 44 |
|
| 45 |
result = '' |
| 46 |
while long_value >= __b58base: |
| 47 |
div, mod = divmod(long_value, __b58base) |
| 48 |
result = __b58chars[mod] + result |
| 49 |
long_value = div |
| 50 |
result = __b58chars[long_value] + result |
| 51 |
|
| 52 |
# Bitcoin does a little leading-zero-compression: |
| 53 |
# leading 0-bytes in the input become leading-1s |
| 54 |
nPad = 0 |
| 55 |
for c in v: |
| 56 |
if c == '\0': nPad += 1 |
| 57 |
else: break |
| 58 |
|
| 59 |
return (__b58chars[0]*nPad) + result |
| 60 |
|
| 61 |
def EncodeBase58Check(vchIn): |
| 62 |
hash = Hash(vchIn) |
| 63 |
return b58encode(vchIn + hash[0:4]) |
| 64 |
|
| 65 |
def sha256(x): |
| 66 |
return hashlib.sha256(x).digest() |
| 67 |
|
| 68 |
|
| 69 |
def Hash(x): |
| 70 |
if type(x) is unicode: x=x.encode('utf-8') |
| 71 |
return sha256(sha256(x)) |
| 72 |
|
| 73 |
def i4b(self, x): |
| 74 |
return pack('>I', x) |
| 75 |
|
| 76 |
|
| 77 |
# /from Electrum |
| 78 |
|
| 79 |
def getXpub(publicKeyData, testnet=False): |
| 80 |
header = ("043587CF" if testnet else "0488B21E") |
| 81 |
result = header.decode('hex') + chr(publicKeyData['depth']) + str(publicKeyData['parentFingerprint']) + str(publicKeyData['childNumber']) + str(publicKeyData['chainCode']) + str(compress_public_key(publicKeyData['publicKey'])) |
| 82 |
return EncodeBase58Check(result) |
| 83 |
|
| 84 |
def signMessage(encodedPrivateKey, data): |
| 85 |
messageData = bytearray("\x18Bitcoin Signed Message:\n") |
| 86 |
writeVarint(len(data), messageData) |
| 87 |
messageData.extend(data) |
| 88 |
messageHash = Hash(messageData) |
| 89 |
signature = app.signImmediate(encodedPrivateKey, messageHash) |
| 90 |
|
| 91 |
# Parse the ASN.1 signature |
| 92 |
|
| 93 |
rLength = signature[3] |
| 94 |
r = signature[4 : 4 + rLength] |
| 95 |
sLength = signature[4 + rLength + 1] |
| 96 |
s = signature[4 + rLength + 2:] |
| 97 |
if rLength == 33: |
| 98 |
r = r[1:] |
| 99 |
if sLength == 33: |
| 100 |
s = s[1:] |
| 101 |
r = str(r) |
| 102 |
s = str(s) |
| 103 |
|
| 104 |
# And convert it |
| 105 |
|
| 106 |
return b64encode(chr(27 + 4 + (signature[0] & 0x01)) + r + s) |
| 107 |
|
| 108 |
dongle = getDongle(True) |
| 109 |
app = btchip(dongle) |
| 110 |
seed = app.importPrivateKey(SEED, TESTNET) |
| 111 |
privateKey = app.deriveBip32Key(seed, KEYPATH) |
| 112 |
publicKeyData = app.getPublicKey(privateKey) |
| 113 |
print getXpub(publicKeyData, TESTNET) |
| 114 |
print signMessage(privateKey, "Coinkite") |
| 115 |
dongle.close() |