Last active
September 1, 2019 17:37
-
-
Save hal0x2328/51f8f00c08162a21d7ce0c3fe912d687 to your computer and use it in GitHub Desktop.
Python script to combine two Neo private keys into a third private key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
combineprivkeys.py - by hal0x2328 | |
This tool combines two private keys into a new private key, as part of | |
the "split-key" scheme used to generate vanity addresses without sharing | |
the private key with a third party generating the vanity address for you. | |
The party generating the vanity address only needs your public key. With it, | |
they can generate a private key that, when combined with your private | |
key, will unlock the vanity address. The system is secure in that it is | |
impossible to calculate the private key of the vanity address without having | |
both private keys. | |
Usage: copy this script into an existing neo-python installation and run: | |
pip3 install pyopenssl | |
python3 ./combineprivkeys.py | |
When prompted, enter your private key, then the key given to you by the | |
person who generated the vanity address, then the script will combine the | |
keys to generate the private key for the vanity address which you can then | |
import into any Neo 2.x wallet | |
""" | |
import binascii | |
from neo.Core.KeyPair import KeyPair | |
from prompt_toolkit import prompt | |
from OpenSSL._util import( | |
lib as _lib, | |
ffi as _ffi | |
) | |
modulus = b'FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551' | |
in1 = prompt('Private key WIF 1: ') | |
key1 = KeyPair.PrivateKeyFromWIF(in1) | |
in2 = prompt('Private key WIF 2: ') | |
key2 = KeyPair.PrivateKeyFromWIF(in2) | |
ctx = _ffi.gc(_lib.BN_CTX_new(), _lib.BN_CTX_free) | |
m = _ffi.gc(_lib.BN_new(), _lib.BN_free) | |
m_ptr = _ffi.new("BIGNUM**") | |
m_ptr[0] = m | |
_lib.BN_hex2bn(m_ptr, modulus) | |
k1 = _ffi.gc(_lib.BN_new(), _lib.BN_free) | |
k1_ptr = _ffi.new("BIGNUM**") | |
k1_ptr[0] = k1 | |
_lib.BN_hex2bn(k1_ptr, binascii.hexlify(key1)) | |
k2 = _ffi.gc(_lib.BN_new(), _lib.BN_free) | |
k2_ptr = _ffi.new("BIGNUM**") | |
k2_ptr[0] = k2 | |
_lib.BN_hex2bn(k2_ptr, binascii.hexlify(key2)) | |
r = _ffi.gc(_lib.BN_new(), _lib.BN_free) | |
r_ptr = _ffi.new("BIGNUM**") | |
r_ptr[0] = r | |
_lib.BN_mod_add(r, k1, k2, m, ctx); | |
r_c = _lib.BN_bn2hex(r) | |
final = _ffi.string(r_c).decode() | |
_lib.OPENSSL_free(r_c) | |
kp = KeyPair(binascii.unhexlify(final)) | |
print("\n=== Resulting private key ==="); | |
print("Hex: {}".format(final)) | |
print("Address: {}".format(kp.GetAddress())) | |
print("WIF: {}".format(kp.Export())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment