#!/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')))