How to import a 64-byte long private key

2

Private keys are 256 bits (32-bytes) long, according to bitcoin wiki: https://en.bitcoin.it/wiki/Private_key

Diamond Circle, an Australian startup, sent me a pair of bitcoin address and its private key. Blockchain.info shows that the money is still in the given address.

The problem is that the private key is 64-bytes long. It's base-64 encoded evident by the trailing ==

Since it's not in WIF, I wonder how to import it. Has anyone seen 64bit private keys before?

Tankman六四

Posted 2016-03-21T08:21:13.297

Reputation: 131

1Is it base64 encoded or hexadecimal? In base64 a private key would be around 44 characters. A private key is certainly not 32 bit, 64 bit or 64 bytes; they are 256 bits, which can be encoded as 64 characters in hexadecimal without compression flag.Pieter Wuille 2016-03-26T06:44:37.547

Answers

1

Taking into account that 64bytes keys are probably seed keys in bip32 format you might need to derive the key first. I modified the code to reflect that

npm install coinstring hdkey
node 
var HDKey = require('hdkey')
var cs = require('coinstring')

// This is your base64 key 
var seed = 'oMQqnDrGq/K6aplGroOvGPUb8cn6fazEySUTzE3QFYNDQcd13NTA+sc1R8VmLYGp6TYaCqxgSnOjIb2RA7zorw=='
var hdkey = HDKey.fromMasterSeed(new Buffer(seed, 'base64'))

var hex = hdkey.privateKey
var version = 0x80; //Bitcoin private key
console.log(cs.encode(hex, version)) 

It should give you the familiar WIF format private key.

5HyfJgfUm7WVgUrLuo9uwtJR6ME7MYfGRhK2aeckZRooSKYGXP8

galileopy

Posted 2016-03-21T08:21:13.297

Reputation: 314

It writes a very long private key to the console, 95 characters starting with 2H instead of 5H like in your example. This key, once imported to electrum wallet, shows a balance of 0 at the address 1LDZ8Z3Cjh6tn4xfR65Tw9wxjNap69nzjp while the address (according to the email) should be 158fiVvrKfRJzA4DLEo2fkQh4DdpBCHTrU with a balance of 23.971mBTC. I'm a bit worried that you provided a general solution for converting private key to WIF format without regard to the special case in question (64-bitness of the key)?Tankman六四 2016-03-25T05:14:45.010

In the question you mention that the key is 64 bytes, (512 bits). Here you say it is 64 bits. Can you clarify what is your situation?. You are right I wrote the first script thinking of regular private keys. This script however should work with bip32 private keys. And the first derivation should be the one from your address.galileopy 2016-03-26T20:58:45.877

0

The following python code should work. Transform the private key to HEX, and use it:

import binascii, hashlib
def privateKey_to_WIF(pk): #wallet import format ==> pra importar nas carteiras

    privWIF1 = hashlib.sha256(binascii.unhexlify('80'+pk)).hexdigest()
    privWIF2 = hashlib.sha256(binascii.unhexlify(privWIF1)).hexdigest()
    privWIF3 = '80'+pk+privWIF2[:8]

    pubnum = int(privWIF3,16)
    pubnumlist = []
    while pubnum!=0: pubnumlist.append(pubnum%58); pubnum/=58
    WIF = ''
    for l in ['123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'[x] for x in pubnumlist]:
        WIF=l+WIF
    #print WIF
    return WIF

It will return the Wallet Import Format which can be used in another client.

Example:

Private key WIF: 5KNjwhPMnXbFS7bVeHEzpE4He6jkQeRF9wtBtr1p1anH25rDnys

anonimou

Posted 2016-03-21T08:21:13.297

Reputation: 80

I had to convert my 64-bit base64 to 64-bit hex before feeding into this function. Then it produced a very long private key, 95 characters starting with 2H instead of 51 like in your example. This key, once imported to electrum wallet, shows a balance of 0 at the address 1LDZ8Z3Cjh6tn4xfR65Tw9wxjNap69nzjp while the address (according to the email) should be 158fiVvrKfRJzA4DLEo2fkQh4DdpBCHTrU with a balance of 23.971mBTC. I'm a bit worried that you are providing a general solution for converting private key to WIF format without regard to the special case in question (64-bitness of the key)Tankman六四 2016-03-25T05:11:11.160

Zhang Weimu, try this: "pk = hashlib.sha256("abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789").hexdigest()" and use it in the above code... with this sample string, I get: Private key: b49c0a968de2df1367dfd9301a26b9fdbc5db0bfe892f453f2e0156873f8ef74 Private key WIF: 5KBq1WwaHRSUcJwB5wAycR8C37TaFFNpMYsLL25gECoVoKeo9sa Address: 12BgTfZUgoC8FAMZSVSo1ZXbMbbtLohpkzanonimou 2016-03-25T22:16:23.213

note that with the above code (in the comment), the private key is not what you have, but a "seed" to the private key.. let me know if that worked out!anonimou 2016-03-25T22:17:52.040