From dc904751e89ec2502d99989979d3fa041e3d6bd9 Mon Sep 17 00:00:00 2001 From: qo-op Date: Sat, 7 Nov 2020 00:38:03 +0100 Subject: [PATCH] create pubsec secret.dunikey and convert into IPFS Identity --- .../MIZ/.~lock.Pieds_Freq_longueur_OR.ods# | 1 - zen/tools/crypto_pb2.py | 162 ++++++++++++++++++ zen/tools/key_create_dunikey_and_ipfs.py | 55 ++++++ 3 files changed, 217 insertions(+), 1 deletion(-) delete mode 100644 www/LOVELand/MIZ/.~lock.Pieds_Freq_longueur_OR.ods# create mode 100644 zen/tools/crypto_pb2.py create mode 100644 zen/tools/key_create_dunikey_and_ipfs.py diff --git a/www/LOVELand/MIZ/.~lock.Pieds_Freq_longueur_OR.ods# b/www/LOVELand/MIZ/.~lock.Pieds_Freq_longueur_OR.ods# deleted file mode 100644 index 7c925c3..0000000 --- a/www/LOVELand/MIZ/.~lock.Pieds_Freq_longueur_OR.ods# +++ /dev/null @@ -1 +0,0 @@ -,fred,ONELOVE,04.11.2020 21:21,file:///home/fred/.config/libreoffice/4; \ No newline at end of file diff --git a/zen/tools/crypto_pb2.py b/zen/tools/crypto_pb2.py new file mode 100644 index 0000000..d6b953a --- /dev/null +++ b/zen/tools/crypto_pb2.py @@ -0,0 +1,162 @@ +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: crypto.proto + +import sys +_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) +from google.protobuf.internal import enum_type_wrapper +from google.protobuf import descriptor as _descriptor +from google.protobuf import message as _message +from google.protobuf import reflection as _reflection +from google.protobuf import symbol_database as _symbol_database +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor.FileDescriptor( + name='crypto.proto', + package='crypto.pb', + syntax='proto2', + serialized_options=_b('Z*github.com/libp2p/go-libp2p-core/crypto/pb'), + serialized_pb=_b('\n\x0c\x63rypto.proto\x12\tcrypto.pb\";\n\tPublicKey\x12 \n\x04Type\x18\x01 \x02(\x0e\x32\x12.crypto.pb.KeyType\x12\x0c\n\x04\x44\x61ta\x18\x02 \x02(\x0c\"<\n\nPrivateKey\x12 \n\x04Type\x18\x01 \x02(\x0e\x32\x12.crypto.pb.KeyType\x12\x0c\n\x04\x44\x61ta\x18\x02 \x02(\x0c*9\n\x07KeyType\x12\x07\n\x03RSA\x10\x00\x12\x0b\n\x07\x45\x64\x32\x35\x35\x31\x39\x10\x01\x12\r\n\tSecp256k1\x10\x02\x12\t\n\x05\x45\x43\x44SA\x10\x03\x42,Z*github.com/libp2p/go-libp2p-core/crypto/pb') +) + +_KEYTYPE = _descriptor.EnumDescriptor( + name='KeyType', + full_name='crypto.pb.KeyType', + filename=None, + file=DESCRIPTOR, + values=[ + _descriptor.EnumValueDescriptor( + name='RSA', index=0, number=0, + serialized_options=None, + type=None), + _descriptor.EnumValueDescriptor( + name='Ed25519', index=1, number=1, + serialized_options=None, + type=None), + _descriptor.EnumValueDescriptor( + name='Secp256k1', index=2, number=2, + serialized_options=None, + type=None), + _descriptor.EnumValueDescriptor( + name='ECDSA', index=3, number=3, + serialized_options=None, + type=None), + ], + containing_type=None, + serialized_options=None, + serialized_start=150, + serialized_end=207, +) +_sym_db.RegisterEnumDescriptor(_KEYTYPE) + +KeyType = enum_type_wrapper.EnumTypeWrapper(_KEYTYPE) +RSA = 0 +Ed25519 = 1 +Secp256k1 = 2 +ECDSA = 3 + + + +_PUBLICKEY = _descriptor.Descriptor( + name='PublicKey', + full_name='crypto.pb.PublicKey', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='Type', full_name='crypto.pb.PublicKey.Type', index=0, + number=1, type=14, cpp_type=8, label=2, + has_default_value=False, default_value=0, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='Data', full_name='crypto.pb.PublicKey.Data', index=1, + number=2, type=12, cpp_type=9, label=2, + has_default_value=False, default_value=_b(""), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + serialized_options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=27, + serialized_end=86, +) + + +_PRIVATEKEY = _descriptor.Descriptor( + name='PrivateKey', + full_name='crypto.pb.PrivateKey', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='Type', full_name='crypto.pb.PrivateKey.Type', index=0, + number=1, type=14, cpp_type=8, label=2, + has_default_value=False, default_value=0, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='Data', full_name='crypto.pb.PrivateKey.Data', index=1, + number=2, type=12, cpp_type=9, label=2, + has_default_value=False, default_value=_b(""), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + serialized_options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + serialized_options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=88, + serialized_end=148, +) + +_PUBLICKEY.fields_by_name['Type'].enum_type = _KEYTYPE +_PRIVATEKEY.fields_by_name['Type'].enum_type = _KEYTYPE +DESCRIPTOR.message_types_by_name['PublicKey'] = _PUBLICKEY +DESCRIPTOR.message_types_by_name['PrivateKey'] = _PRIVATEKEY +DESCRIPTOR.enum_types_by_name['KeyType'] = _KEYTYPE +_sym_db.RegisterFileDescriptor(DESCRIPTOR) + +PublicKey = _reflection.GeneratedProtocolMessageType('PublicKey', (_message.Message,), dict( + DESCRIPTOR = _PUBLICKEY, + __module__ = 'crypto_pb2' + # @@protoc_insertion_point(class_scope:crypto.pb.PublicKey) + )) +_sym_db.RegisterMessage(PublicKey) + +PrivateKey = _reflection.GeneratedProtocolMessageType('PrivateKey', (_message.Message,), dict( + DESCRIPTOR = _PRIVATEKEY, + __module__ = 'crypto_pb2' + # @@protoc_insertion_point(class_scope:crypto.pb.PrivateKey) + )) +_sym_db.RegisterMessage(PrivateKey) + + +DESCRIPTOR._options = None +# @@protoc_insertion_point(module_scope) diff --git a/zen/tools/key_create_dunikey_and_ipfs.py b/zen/tools/key_create_dunikey_and_ipfs.py new file mode 100644 index 0000000..558cdd3 --- /dev/null +++ b/zen/tools/key_create_dunikey_and_ipfs.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# This Python script gets Duniter creddentials as arguments, and writes a PubSec file that should be compatible with Cesium and Silkaj(DuniterPy) clients. +# It also create ipfs Identity +######################################################################### +# sudo apt install protobuf-compiler +# pip3 install base58 google protobuf duniterpy +# wget https://github.com/libp2p/go-libp2p-core/raw/master/crypto/pb/crypto.proto +# protoc --python_out=. crypto.proto +######################################################################### +# launch with : +# python3 key_create_dnuikey_and_ipfs.py +import os +import base58 +import base64 +import cryptography.hazmat.primitives.asymmetric.ed25519 as ed25519 +from cryptography.hazmat.primitives import serialization +import crypto_pb2 +from sys import argv +from duniterpy.key import SigningKey + +# path to save to +path = "/tmp/secret.dunikey" + +key = SigningKey.from_credentials(argv[1], argv[2], None) +key.save_pubsec_file(path) +print( + key.pubkey, +) +# Capturing keys (from /tmp/secret.dunikey) +shared_key = os.popen('cat /tmp/secret.dunikey | grep pub | cut -d " " -f 2').read() +secure_key = os.popen('cat /tmp/secret.dunikey | grep sec | cut -d " " -f 2').read() + + +# Decoding keys +decoded_shared = base58.b58decode(shared_key) +decoded_secure = base58.b58decode(secure_key) +ipfs_shared = ed25519.Ed25519PublicKey.from_public_bytes(decoded_shared) +ipfs_secure = ed25519.Ed25519PrivateKey.from_private_bytes(decoded_secure[:32]) +ipfs_shared_bytes = ipfs_shared.public_bytes(encoding=serialization.Encoding.Raw, + format=serialization.PublicFormat.Raw) +ipfs_secure_bytes = ipfs_secure.private_bytes(encoding=serialization.Encoding.Raw, + format=serialization.PrivateFormat.Raw, + encryption_algorithm=serialization.NoEncryption()) + + +# Formulating PeerID +ipfs_pid = base58.b58encode(b'\x00$\x08\x01\x12 ' + ipfs_shared_bytes) +print('Peer ID: {}'.format(ipfs_pid.decode('ascii'))) + + +# Serializing private key in IPFS-native mode, the private key contains public one +pkey = crypto_pb2.PrivateKey() +pkey.Type = crypto_pb2.KeyType.Ed25519 +pkey.Data = ipfs_secure_bytes + ipfs_shared_bytes +print('Private key: {}'.format(base64.b64encode(pkey.SerializeToString()).decode('ascii')))