Commit f964633f authored by matograine's avatar matograine
Browse files

* encrypt and sign the wallets file

* decrypt and use the wallets file
* add PUBKEY_PATTERN constant
* move save_json to common file
parent 7b61bb43
......@@ -24,8 +24,8 @@ if argv[1] == "generate":
elif argv[1] == "retrieve":
file = argv[2]
pubkey = argv [3]
Retrieve().retrieve(file, pubkey)
# pubkey = argv [3]
Retrieve().retrieve(file) #, pubkey)
else :
print ("Use \"generate <amount> <pages number> <delay days>\" to generate G1pourboires.")
......
from duniterpy.key import SigningKey
from duniterpy.key import SigningKey, ascii_armor
import datetime
from utils.generator import Generator
from utils.transfer import Transfer
from utils.silkaj.tools import coroutine
from utils.silkaj.auth import auth_by_scrypt
import os
import json
......@@ -26,34 +27,69 @@ class Creating:
self.pages = pages
self.amount = amount
self.folder = os.path.expanduser("~/Documents/G1pourboire/")
self.output = self.folder + datetime.datetime.now().strftime(
self.wallets_folder = os.path.expanduser("~/Documents/G1pourboire/Recuperation_ne_pas_supprimer/")
self.backup_folder = os.path.expanduser("~/Documents/G1pourboire/.backup/")
self.output = datetime.datetime.now().strftime(
"G1pourboire_%Y-%m-%dT%Hh%M"
)
self.key = auth_by_scrypt(None)
self.wallets = []
# Check date format
self.date = check_date(given_date)
# Create the folder
if not os.path.exists(self.folder):
os.makedirs(self.folder)
if not os.path.exists(self.wallets_folder):
os.makedirs(self.wallets_folder)
if not os.path.exists(self.backup_folder):
os.makedirs(self.backup_folder)
def create(self):
""" Generating, filling wallets."""
if self.pages <= MAX_PAGES_I_CAN_GENERATE:
Generator(self.output, self.wallets, self.amount, self.date, self.pages).generate()
Transfer(self.amount, self.pages, self.wallets, self.output, self.date).save_json()
Generator(self.folder + self.output, self.wallets, self.amount, self.date, self.pages).generate()
transfer = Transfer(self.amount, self.pages, self.wallets, self.output, self.date)
self.save_json(self.key)
else:
print ("Sorry, Ğ1Pourboire can generate maximum 10 pages at once. For now !")
exit()
if self.amount > 0:
Transfer(self.amount, self.pages, self.wallets, self.output, self.date).transfer()
transfer.transfer(self.key)
# @coroutine
# async def create(self):
# """ Generating, filling wallets."""
# self.tip_date()
# Generator(self.output, self.wallets, self.pages).generate()
# await Transfer(self.amount, self.pages, self.wallets, self.output).transfer()
def save_json(self, key):
"""
write wallets data in a json file.
"""
# create and encrypt wallet list
wallet_data = [
{
"pubkey": w["pubkey"],
"salt": w["salt"],
"password": w["password"],
}
for w in self.wallets
]
clear_json = json.dumps(wallet_data)
encrypted_wallets = ascii_armor.AsciiArmor.create(clear_json, key.pubkey)
# add date and creator info
data = {
"creator_pubkey": key.pubkey,
"peremption_date": self.date.isoformat(),
"wallets": encrypted_wallets,
}
data = json.dumps(data)
# sign the whole document
signed_data = ascii_armor.AsciiArmor.create(data, signing_keys=[key])
with open(
self.wallets_folder + self.output + ".json.signed", "a"
) as f:
f.write(signed_data)
with open(
self.backup_folder + self.output + ".json.signed", "a"
) as b:
b.write(signed_data)
# Ajouter la date au json
def tip_date(self):
......
from PIL import Image, ImageFont, ImageDraw
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from duniterpy.key import SigningKey
from duniterpy.key import SigningKey, ascii_armor
from utils.diceware import diceware
import os
......@@ -135,7 +135,15 @@ class Generator:
for wallets in chunks(self.wallets, 6):
self.gen_page(wallets)
self.c.save()
"""
def encrypt_wallets(self, wallets, key):
data = [
{"pubkey": w["pubkey"], "salt": w["salt"], "password": w["password"]}
for w in self.wallets
]
clear_json = json.dumps(data)
encrypted_json = ascii_armor.AsciiArmor(clear_json, signing_keys=[key])
"""
def save_json(self): # Is this function still necessary ?
data = [
{"pubkey": w["pubkey"], "salt": w["salt"], "password": w["password"]}
......
from duniterpy.key import SigningKey
from duniterpy.key import SigningKey, ascii_armor
from datetime import datetime
from time import sleep
from utils.diceware import diceware
from utils.silkaj.money import get_amount_from_pubkey
from utils.silkaj.network_tools import ClientInstance
from utils.silkaj.tx_retrieve import handle_intermediaries_transactions, generate_and_send_transaction, get_list_input_for_transaction
from utils.silkaj.auth import auth_by_scrypt_with_IDs
from utils.silkaj.auth import auth_by_scrypt_with_IDs, auth_by_scrypt
from utils.silkaj.tools import coroutine
from utils.silkaj.constants import PUBKEY_PATTERN
import os
import json
import re
MAX_ISSUERS = 20
class Retrieve():
# def __init__(self):
# self.key = auth_by_scrypt(None)
def open_file(self, file):
with open(file) as wallets_json:
return json.load(wallets_json)
......@@ -29,22 +34,35 @@ class Retrieve():
def is_date_passed(self, wallets_doc):
peremption_date = datetime.fromisoformat(wallets_doc["peremption_date"])
today = datetime.now()
if today > peremption_date:
return True
return False
if not today > peremption_date:
print ("Erreur : La date de péremption n'est pas encore atteinte")
exit()
@coroutine
async def retrieve(self, file, retrieving_pubkey):
with open(file) as wallets_json:
wallets_doc = json.load(wallets_json)
def get_pubkey(self, pubkey):
print("Entrez les identifiants pour les clef publique : ", pubkey)
key = auth_by_scrypt(None)
if re.search(pubkey, key.pubkey):
return key
print("Mauvaise clef!")
exit()
@coroutine
async def retrieve(self, file):
signed_data = open(file, "r").read()
pubkey = re.search(re.compile(PUBKEY_PATTERN), signed_data).group()
key = self.get_pubkey(pubkey)
#print(signed_data)
data = ascii_armor.AsciiArmor.parse(signed_data, sender_pubkeys=[key.pubkey])
#print(data)
#print(data["message"]["content"])
content = json.loads(data["message"]["content"])
# check if date is passed
if not self.is_date_passed(wallets_doc):
print ("Erreur : La date de péremption n'est pas encore atteinte")
exit()
self.is_date_passed(content)
# getting wallets infos
retrieving_wallets = wallets_doc["wallets"]
# print ("DEBUG : " + str(len(retrieving_wallets)))
encrypted_wallets = content["wallets"]
retrieving_wallets = ascii_armor.AsciiArmor.parse(encrypted_wallets, key)
retrieving_wallets = json.loads(retrieving_wallets["message"]["content"])
# send transactions
while len(retrieving_wallets) != 0:
issuers, key_list, amount, listinput = list(), list(), 0, list()
wallet_list = list()
......@@ -73,8 +91,8 @@ class Retrieve():
issuers,
amount,
total_listinput_and_amount,
[retrieving_pubkey],
"",
[key.pubkey],
"G1Don",
)
issuers, key_list, amount, listinput = list(), list(), 0, list()
# delete already used wallets
......
......@@ -23,4 +23,5 @@ G1_DEFAULT_ENDPOINT = "g1-test.duniter.org", "443"
G1_TEST_DEFAULT_ENDPOINT = "ts.gt.librelois.fr", "443"
CONNECTION_TIMEOUT = 10
ASYNC_SLEEP = 0.1
PUBKEY_PATTERN = "[1-9A-HJ-NP-Za-km-z]{43,44}"
SOURCES_PER_TX = 40
from duniterpy.key import SigningKey
from duniterpy.key import SigningKey, ascii_armor
from time import sleep
from utils.diceware import diceware
from utils.silkaj.money import get_amount_from_pubkey
......@@ -13,20 +13,22 @@ import datetime
class Transfer:
"""Create a transfer wallet, send amount to tips"""
def __init__(self, amount, pages, wallets, output, peremption_date):
self.output = output
def __init__(self, amount, pages, wallets, outputs, peremption_date):
self.output = outputs
self.wallets = wallets
self.pages = pages
self.Tips_Amount = amount
self.date = peremption_date
# self.key = auth_by_scrypt(None)
@coroutine
async def transfer(self):
async def transfer(self, key):
"""
Asks for authentification to send the tips.
"""
total_amount = int(100 * self.Tips_Amount * 6 * self.pages)
key = auth_by_scrypt(None)
# key = auth_by_scrypt(None)
# create outputs list
outputAddresses = []
......@@ -65,10 +67,11 @@ class Transfer:
)
def save_json(self):
def save_json(self, key):
"""
write wallets data in a json file.
"""
# create and encrypt wallet list
wallet_data = [
{
"pubkey": w["pubkey"],
......@@ -77,11 +80,20 @@ class Transfer:
}
for w in self.wallets
]
clear_json = json.dumps(wallet_data)
encrypted_wallets = ascii_armor.AsciiArmor.create(clear_json, key.pubkey)
# add date and creator info
data = {
"creator_pubkey": key.pubkey,
"peremption_date": self.date.isoformat(),
"wallets": wallet_data,
"wallets": encrypted_wallets,
}
data = json.dumps(data)
# sign the whole document
signed_data = ascii_armor.AsciiArmor.create(data, signing_keys=[key])
with open(
self.output + ".json", "a"
self.output + ".json.signed", "a"
) as f:
f.write(json.dumps(data, indent=4))
f.write(signed_data) #json.dumps(data, indent=4))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment