Skip to content

Instantly share code, notes, and snippets.

@ntwi
Forked from Warchant/tn2mn.py
Last active October 25, 2021 15:38
Show Gist options
  • Save ntwi/1d070e0dbb4e872ea36c91959dd3bb4f to your computer and use it in GitHub Desktop.
Save ntwi/1d070e0dbb4e872ea36c91959dd3bb4f to your computer and use it in GitHub Desktop.
Convert multiple phase 1 private keys to phase 2 private keys
#!/usr/bin/env python
import base58
from bottle import run, get, post, request
import json
main = bytes([128])
test = bytes([239])
def decodeSecret(key, network):
assert isinstance(network, bytes), network
k = base58.b58decode_check(key)
len_correct = len(k) == 32 + len(network) or (len(k) == 33 + len(network) and k[-1] == 1)
assert len_correct, "Bad length: {}".format(len(k))
prefix_ok = k[0:1] == network
assert prefix_ok, "Bad prefix: {}. Expected {}".format(k[0:1], network)
is_compressed = len(k) == 33 + len(network)
return k[len(network):len(network) + 32], is_compressed
def encode_secret(key, network, is_compressed):
assert isinstance(network, bytes), network
out = bytearray([])
out += network
out += key
if is_compressed:
out += bytes([1])
assert len(out) <= 34, len(out)
ret = base58.b58encode_check(out)
return ret
def tn2mn(key):
data, compressed = decodeSecret(key, test)
return encode_secret(data, main, compressed)
@get('/')
def form():
return '''<div style="min-height:100%"><form style="display:flex;flex-direction:column;align-items:center
;justify-content:center;height:50vh" action="/convert" method="post"> <label for="p1key">Phase 1 Keys (one per line):</label>
<textarea style="width:55ch;max-width:100%;margin:1rem auto" name="p1key" rows="10" placeholder="Private keys separated by a new line"></textarea><input value="Submit" type="submit" /></form></div> '''
@post('/convert')
def convert():
p1input = request.forms.get('p1key')
p2keys = []
p1ks = p1input.split('\n')
for k in p1ks:
p2k = tn2mn(k)
p2keys.append(format(p2k.decode('ascii')))
return json.dumps(p2keys)
run(host='localhost', port=8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment