Last active
March 14, 2021 07:49
-
-
Save Nikolaj-K/e57cf38d78feb928e34d090946951ee5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
""" | |
Doing the same as on | |
https://wallet.compendia.org/ | |
https://wallet.nos.dev/ | |
See | |
https://compendia.org/ | |
See also | |
https://ark.dev/docs/core/transactions/cryptography#privatekey | |
Note: | |
Current project is implementing | |
http://homepages.cs.ncl.ac.uk/feng.hao/files/OpenVote_IET.pdf | |
using the Compendia blockchain aboven. | |
Don't write me emails asking how to install python or something of the sort. | |
If you ask me something, use an adult sentence structure and attire. | |
Disclaimer: Due to potential bugs, don't use functions from this script for anything of value! | |
""" | |
import base58 # https://github.com/serengil/crypto/blob/master/python/base58.py | |
# See also https://en.bitcoin.it/wiki/Base58Check_encoding | |
from binary.unsigned_integer.writer import write_bit8 # pip install binary-helpers | |
import binascii | |
import csv | |
import ecdsa # pip install ecdsa | |
import hashlib | |
from random import randint | |
import os | |
class Bytes: # Version bytes, etc. | |
# https://github.com/compendia/core/blob/main/packages/crypto/src/networks/realmainnet/network.json | |
COMPENDIA_MAIN_NET = 0x58 | |
COMPENDIA_MAIN_WIF = 0xab # 171 | |
COMPENDIA_DEV_NET = 0x5a | |
COMPENDIA_DEV_WIF = 0x0 | |
# https://ark.dev/docs/core/transactions/cryptography#privatekey | |
ARK_MAIN_NET = 0x17 | |
ARK_MAIN_WIF = 0xaa | |
ARK_DEV_NET = 0x1e | |
ARK_DEV_WIF = 0xaa | |
def _random_stram(alphabet): | |
max_idx = len(alphabet) - 1 | |
while True: | |
idx = randint(0, max_idx) | |
#print(idx) | |
yield alphabet[idx] | |
def _random_selection(alphabet, num_items): | |
assert num_items <= len(alphabet) | |
seen = [] | |
for item in _random_stram(alphabet): | |
if item not in seen: | |
yield item | |
seen.append(item) | |
if len(seen) == num_items: | |
return | |
def _generate_passphrase(num_words): # Note: Not implemented as in ARK/Compendia! | |
WORDLIST_FILEPATH = r"/Users/amoogle/Documents/Pages/pages_video/crypto/electrum_wordlist.csv" | |
# from electrum wallet: https://github.com/spesmilo/electrum/blob/master/electrum/wordlist/english.txt | |
with open(WORDLIST_FILEPATH) as infile: | |
all_words = [line[0] for line in csv.reader(infile)] | |
words = _random_selection(all_words, num_words) | |
return " ".join(words) | |
def _compressed_pubk_from_secret(secret): # See also https://en.bitcoin.it/wiki/Secp256k1 | |
pubk = ecdsa.SigningKey.from_secret_exponent(secret, curve=ecdsa.SECP256k1).verifying_key | |
pubk_as_string = binascii.b2a_hex(pubk.to_string()).decode('ascii') | |
prefix = "0" + str(2 + ord(bytearray.fromhex(pubk_as_string[-2:])) % 2) # 02 for even, 03 for odd | |
LEN_X_COORD = 64 | |
return prefix + pubk_as_string[:64] | |
def _address_from_pubk_compressed(byte_net, pubk_compressed): | |
pubk_compressed_ = binascii.unhexlify(pubk_compressed.encode()) | |
ripemd160 = hashlib.new('ripemd160', pubk_compressed_) | |
seed = write_bit8(byte_net) + ripemd160.digest() | |
address = base58.b58encode_check(seed).decode() | |
return address | |
def _from_passphrase(byte_net, byte_wif, passphrase): | |
sha = hashlib.sha256() | |
sha.update(passphrase.encode()) | |
h = sha.digest() | |
seed = write_bit8(byte_wif) + h + write_bit8(0x01) | |
wif = base58.b58encode_check(seed).decode() | |
secret_exponent = int(sha.hexdigest(), 16) | |
pubk_compressed = _compressed_pubk_from_secret(secret_exponent) | |
address = _address_from_pubk_compressed(byte_net, pubk_compressed) | |
# Log | |
print("passphrase:\t\t", passphrase) | |
print("pubk_compressed:\t", pubk_compressed) | |
print("address:\t\t", address) | |
print("wif:\t\t\t", wif) | |
print() | |
return pubk_compressed, address, wif | |
def _test(byte_net, byte_wif, passphrase, address_gt): | |
_pubk_compressed, address, _wif = _from_passphrase(byte_net, byte_wif, passphrase) | |
assert address == address_gt, address | |
if __name__=="__main__": | |
os.system("clear") | |
print("* Random passphrase example:") | |
passphrase = _generate_passphrase(12) | |
print(passphrase) | |
pubk_compressed, address, wif = _from_passphrase(Bytes.COMPENDIA_MAIN_NET, Bytes.COMPENDIA_MAIN_WIF, passphrase) | |
print("* ARK docs example:") # See https://ark.dev/docs/core/transactions/cryptography#privatekey | |
_test(Bytes.ARK_DEV_NET, Bytes.ARK_DEV_WIF, \ | |
"this is a top secret passphrase", \ | |
"D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib") | |
# Add any check here in the same way as above! | |
PASSPHRASE = "some phrase" | |
a = _from_passphrase(Bytes.ARK_MAIN_NET, Bytes.ARK_MAIN_WIF, PASSPHRASE) | |
print(PASSPHRASE, "=>", a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment