Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

Bitcoin to Nostr private key

# bitcoin private key to nostr import bech32 import base58 import hashlib # Uncompressed bitcoin private key in WIF format private = "5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF" # Compressed private = "L53fCHmQhbNp1B4JipfBtfeHZH7cAibzG9oK19XfiFzxHgAkz6JK" decoded = base58.b58decode(private) hex_key = decoded[1:-4] # remove version byte and 4 bytes checksum if len(decoded) == 38: # when compressed hex_key = decoded[1:-5] data = bech32.convertbits(hex_key, 8, 5, True) nsec = bech32....
Read post

Huffman code

# Huffman binary tree C = dict([('a', 45), ('b', 13), ('c', 12), ('d', 16), ('f', 5), ('e', 9)]) class BNode: def __init__(self, left, right, key): self.left = left self.right = right self.key = key self.index = None def build_huffman_tree(C): l = [create_els(el[0], el[1]) for el in C.items()] mheap = Heap(l) mheap.heap_size = len(mheap) build_min_heap(mheap) # exactly n - 1 times while mheap.heap_size > 1: a = min_heap_e...
Read post

Draw a heap

Draw a heap tree in graphviz: example = [16, 4, 10, 14, 7, 9, 3, 2, 8, 1] Heap = type('Heap', (list,), {'heap_size':0}) a = Heap(example) a.heap_size = len(a) def edges(n): edges = [] level = 1 while True: for j in range(2**level - 1, 2**(level + 1) - 1): if j >= n: return edges edges.append((parent(j), j)) level += 1 def draw_graphviz(A): nodes_str = "\n".join([f"{i}[label={a}]" for i, a in enumerate(A)]) edges_str...
Read post

til

Quick way to define an array with an attribute: Heap = type('Heap', (list,), {'heap_size':0}) ...
Read post

combinations

Return all 3-combinations of the n-set def bottom_up(n): t = [] for i in range(n - 2): for j in range(i + 1, n - 1): for k in range(j + 1, n): print((i,j,k)) t.append((i,j,k)) return t for n=13 -> 286 13!/3!10! = 111213 / 6 = 286 (number of 3-combinations of 13 elements) Q: Can we generalize this by making k parameter of the function? A: Yes, itertools.combinations(range(n), k) ...
Read post

crc-16-ccitt

import binascii def generate_hqx(): polynomial = 0x1021 table = [] for byte in range(256): crc = byte << 8 for _ in range(8): if crc & 0x8000: crc = ((crc << 1) ^ polynomial) & 0xFFFF else: crc = (crc << 1) & 0xFFFF table.append(crc) return table def calc_crc_hqx(bts): table = generate_hqx() crc = 0x0000 for b in bts: index = (crc >> 8) ^ b ...
Read post