Last active
November 29, 2023 20:51
-
-
Save iacchus/12f3f65f79b57d2703981b26200b2d9d to your computer and use it in GitHub Desktop.
How to convert Nostr npub (bech32) key to base16 (hexadecimal) using python
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "8bd9c754-e492-486d-ab85-fa9bb8fa7e9e", | |
"metadata": {}, | |
"source": [ | |
"# Noster key from bech32 to hexadecimal\n", | |
"\n", | |
"## Dependencies\n", | |
"\n", | |
"* python 3\n", | |
"* bech32 (pip install bech32)\n", | |
"\n", | |
"## References\n", | |
"\n", | |
"* https://en.bitcoin.it/wiki/BIP_0173#Bech32\n", | |
"* https://docs.python.org/3.11/library/base64.html\n", | |
"* https://github.com/fiatjaf/bech32/blob/master/bech32/__init__.py" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"id": "4c41b240-25ff-43e5-b4d3-5ecaf3c2ed96", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"from base64 import b32decode\n", | |
"from base64 import b16encode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"id": "77df06b6-3284-428a-9382-4adfe79e42d7", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"from bech32 import bech32_decode\n", | |
"from bech32 import bech32_encode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "93eb6b64-411b-4c0b-99a4-b7bf341fe062", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"HEX = \"0123456789abcdef\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"id": "c94b9188-c05f-4f71-b4a9-0f32655885e3", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"B32 = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 128, | |
"id": "b7f27843-1e97-46dd-a1de-0035db792743", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def add_padding(base32_len):\n", | |
" bits = base32_len * 5\n", | |
" padding_size = (8 - (bits % 8)) % 8\n", | |
" return padding_size" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "2db36f06-88bd-4092-8853-93ce5074e9ba", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"#Hex public key: 0000052c7b6230c64ed41d8ab5ac32ad84b225b20f94b8c7e401113cf698e4f5\n", | |
"#Hex private key: 93b7588fb2b1e87a9a2d2b5a6fe503850b8d3285810866916dda58f1507f982e\n", | |
"#Npub public key: npub1qqqq2trmvgcvvnk5rk9tttpj4kztyfdjp72t33lyqygnea5cun6swettq0\n", | |
"#Nsec private key: nsec1jwm43rajk8584x3d9ddxlegrs59c6v59syyxdytdmfv0z5rlnqhqvdel2y\n", | |
"#145753 iterations (about 1x10^5 hashes) in 2 seconds. Avg rate 72876 hashes/second\n", | |
"# generated with Rana -- https://github.com/grunch/rana\n", | |
"\n", | |
"\n", | |
"npub = \"npub1qqqq2trmvgcvvnk5rk9tttpj4kztyfdjp72t33lyqygnea5cun6swettq0\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"id": "92fc0d69-3ca2-4daa-9ebb-bce235d8d7c3", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"hrd, data = bech32_decode(npub)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "6256900f-e5a0-4174-a012-6fff7697e4c2", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'npub'" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"hrd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"id": "446435b4-0893-45c0-8dcd-d26474126440", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'[0, 0, 0, 0, 10, 11, 3, 27, 12, 8, 24, 12, 12, 19, 22, 20, 3, 22, 5, 11, 11, 11, 1, 18, 21, 22, 2, 11, 4, 9, 13, 18, 1, 30, 10, 11, 17, 17, 31, 4, 0, 4, 8, 19, 25, 29, 20, 24, 28, 19, 26, 16]'" | |
] | |
}, | |
"execution_count": 75, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"repr(data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"id": "3aaaef49-d191-47ae-9a87-ff5b230b70fa", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"b32_data = [B32[index] for index in data]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 135, | |
"id": "2f7d4d5c-cd64-4e49-bd87-b889802a5f83", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\"['A', 'A', 'A', 'A', 'K', 'L', 'D', '3', 'M', 'I', 'Y', 'M', 'M', 'T', 'W', 'U', 'D', 'W', 'F', 'L', 'L', 'L', 'B', 'S', 'V', 'W', 'C', 'L', 'E', 'J', 'N', 'S', 'B', '6', 'K', 'L', 'R', 'R', '7', 'E', 'A', 'E', 'I', 'T', 'Z', '5', 'U', 'Y', '4', 'T', '2']\"" | |
] | |
}, | |
"execution_count": 135, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"repr(b32_data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 148, | |
"id": "3381a0e3-5edc-4012-b616-f70248d93806", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data_str = \"\".join(b32_data)\n", | |
"data_length = len(data_str)\n", | |
"data_str += \"=\" * add_padding(data_length)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 149, | |
"id": "dd26851b-6f96-4adb-88fa-303bc7416c81", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'AAAAKLD3MIYMMTWUDWFLLLBSVWCLEJNSB6KLRR7EAEITZ5UY4T2Q===='" | |
] | |
}, | |
"execution_count": 149, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data_str" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 140, | |
"id": "9b44f407-e116-49f1-93a8-91fc6ef723c7", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"decoded_data = b32decode(data_str) # this is bech32.b32decode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 147, | |
"id": "0e6264d3-7999-4928-b256-d6cac0b8e4d0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"b'\\x00\\x00\\x05,{b0\\xc6N\\xd4\\x1d\\x8a\\xb5\\xac2\\xad\\x84\\xb2%\\xb2\\x0f\\x94\\xb8\\xc7\\xe4\\x01\\x11<\\xf6\\x98\\xe4\\xf5'" | |
] | |
}, | |
"execution_count": 147, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"decoded_data" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "b89e5ecf-3e00-4219-b1cf-2e91ce8fdb11", | |
"metadata": {}, | |
"source": [ | |
"we can compare this result with the \"hex public key\" above, generated by Rana" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 142, | |
"id": "d3f759e9-57f2-456b-9edd-f5fb1aba7fc2", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"b16_encoded_data = b16encode(decoded_data)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 143, | |
"id": "450b95f6-4a18-4a79-9676-a762d50506fb", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"b'0000052C7B6230C64ED41D8AB5AC32AD84B225B20F94B8C7E401113CF698E4F5'" | |
] | |
}, | |
"execution_count": 143, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"b16_encoded_data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 145, | |
"id": "de784bb6-1c1f-4e75-b01b-ec963403217b", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"hex_str = b16_encoded_data.decode(\"utf-8\").lower()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 146, | |
"id": "2e43d0b3-5463-4db9-9e27-e2a3c9598f59", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0000052c7b6230c64ed41d8ab5ac32ad84b225b20f94b8c7e401113cf698e4f5'" | |
] | |
}, | |
"execution_count": 146, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"hex_str" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment