def get_membership_document(mtype, current_block, identity, salt, password): """ Get a Membership document :param str mtype: "IN" to ask for membership or "OUT" to cancel membership :param dict current_block: Current block data :param Identity identity: Identity document :param str salt: Passphrase of the account :param str password: Password of the account :rtype: Membership """ # get current block BlockStamp timestamp = BlockUID(current_block['number'], current_block['hash']) # create keys from credentials key = SigningKey(salt, password, ScryptParams(4096, 16, 1)) # create identity document membership = Membership(version=10, currency=current_block['currency'], issuer=key.pubkey, membership_ts=timestamp, membership_type=mtype, uid=identity.uid, identity_ts=identity.timestamp, signature=None) # sign document membership.sign([key]) return membership
def get_certification_document(current_block, self_cert_document, from_pubkey, salt, password): """ Create and return a Certification document :param dict current_block: Current block data :param Identity self_cert_document: Identity document :param str from_pubkey: Pubkey of the certifier :param str salt: Secret salt (DO NOT SHOW IT ANYWHERE, IT IS SECRET !!!) :param str password: Secret password (DO NOT SHOW IT ANYWHERE, IT IS SECRET !!!) :rtype: Certification """ # construct Certification Document certification = Certification(version=10, currency=current_block['currency'], pubkey_from=from_pubkey, pubkey_to=self_cert_document.pubkey, timestamp=BlockUID(current_block['number'], current_block['hash']), signature="") # sign document key = SigningKey(salt, password, ScryptParams(4096, 16, 1)) certification.sign(self_cert_document, [key]) return certification
def get_identity_document(current_block, uid, salt, password): """ Get an Identity document :param dict current_block: Current block data :param str uid: Unique Identifier :param str salt: Passphrase of the account :param str password: Password of the account :rtype: Identity """ # get current block BlockStamp timestamp = BlockUID(current_block['number'], current_block['hash']) # create keys from credentials key = SigningKey(salt, password, ScryptParams(4096, 16, 1)) # create identity document identity = Identity(version=10, currency=current_block['currency'], pubkey=key.pubkey, uid=uid, ts=timestamp, signature=None) # sign document identity.sign([key]) return identity
def __init__(self, parent): """ Constructor """ super().__init__(parent) self.setupUi(self) self.edit_uid.textChanged.connect(self.values_changed) self.edit_password.textChanged.connect(self.values_changed) self.edit_password_repeat.textChanged.connect(self.values_changed) self.edit_salt.textChanged.connect(self.values_changed) self.edit_pubkey.textChanged.connect(self.values_changed) self.button_generate.clicked.connect(self.action_show_pubkey) self.combo_scrypt_params.currentIndexChanged.connect(self.handle_combo_change) self.scrypt_params = ScryptParams(4096, 16, 1) self.spin_n.setMaximum(2 ** 20) self.spin_n.setValue(self.scrypt_params.N) self.spin_n.valueChanged.connect(self.handle_n_change) self.spin_r.setMaximum(128) self.spin_r.setValue(self.scrypt_params.r) self.spin_r.valueChanged.connect(self.handle_r_change) self.spin_p.setMaximum(128) self.spin_p.setValue(self.scrypt_params.p) self.spin_p.valueChanged.connect(self.handle_p_change) self.label_info.setTextFormat(Qt.RichText)
async def start(cls, port, currency, salt, password, loop): key = SigningKey(salt, password, ScryptParams(2**12, 16, 1)) node = cls(HTTPServer(port, loop), BlockForge(currency, key)) get_routes = { '/network/peering': node.peering, '/blockchain/block/{number}': node.block_by_number, '/blockchain/current': node.current_block, '/tx/sources/{pubkey}': node.sources, '/wot/lookup/{search}': node.lookup, '/wot/certifiers-of/{search}': node.certifiers_of, '/wot/certified-by/{search}': node.certified_by, '/wot/requirements/{pubkey}': node.requirements, '/blockchain/parameters': node.parameters, '/blockchain/with/ud': node.with_ud, '/blockchain/memberships/{search}': node.memberships, '/tx/history/{search}': node.tx_history, '/ud/history/{search}': node.ud_history, } post_routes = { '/wot/add': node.add, '/wot/certify': node.certify, '/tx/process': node.process } for r, h in get_routes.items(): node.http.add_route("GET", r, h) for r, h in post_routes.items(): node.http.add_route("POST", r, h) port, url = await node.http.create_server() print("Server started on {0}".format(url)) return node
async def main(): """ Main code """ # connection handler from BMA endpoint connection = next( BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION)) # prompt hidden user entry salt = getpass.getpass("Enter your passphrase (salt): ") # prompt hidden user entry password = getpass.getpass("Enter your password: "******"Enter your pubkey: ") # prompt entry pubkey_to = input("Enter recipient pubkey: ") # capture current block to get version and currency and blockstamp current_block = await bma.blockchain.current(connection) # capture sources of account response = await bma.tx.sources(connection, pubkey_from) if len(response['sources']) == 0: print("no sources found for account %s" % pubkey_to) exit(1) # get the first source source = response['sources'][0] # create the transaction document transaction = get_transaction_document(current_block, source, pubkey_from, pubkey_to) # create keys from credentials key = SigningKey(salt, password, ScryptParams(4096, 16, 1)) # sign document transaction.sign([key]) # send the Transaction document to the node response = await bma.tx.process(connection, transaction.signed_raw()) if response.status == 200: print(await response.text()) else: print("Error while publishing transaction: {0}".format( await response.text())) response.close()
def get_revoke_document(identity, salt, password): """ Generate account revocation document for given identity :param Identity identity: Self Certification of the identity :param str salt: Salt :param str password: Password :return: the revokation document :rtype: duniterpy.documents.certification.Revocation """ document = Revocation(PROTOCOL_VERSION, identity.currency, identity.pubkey, "") key = SigningKey(salt, password, ScryptParams(4096, 16, 1)) document.sign(identity, [key]) return document.signed_raw(identity)
async def main(): """ Main code """ # prompt hidden user entry salt = getpass.getpass("Enter your passphrase (salt): ") # prompt hidden user entry password = getpass.getpass("Enter your password: "******"Enter your public key: ") # init signer instance signer = SigningKey(salt, password, ScryptParams(4096, 16, 1)) # check public key if signer.pubkey != pubkey: print("Bad credentials!") exit(0) # connection handler from BMA endpoint connection = next( BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION)) # capture current block to get currency name current_block = await bma.blockchain.current(connection) # create our Identity document to sign the revoke document identity_document = await get_identity_document(connection, current_block['currency'], pubkey) # get the revoke document revoke_document = get_revoke_document(identity_document, salt, password) # save revoke document in a file fp = open(REVOKE_DOCUMENT_FILE_PATH, 'w') fp.write(revoke_document) fp.close() # document saved print("Revoke document saved in %s" % REVOKE_DOCUMENT_FILE_PATH)
def simple_forge(): return mirage.BlockForge.start("test_currency", "12356", "123456", ScryptParams(2**12, 16, 1))
def scrypt_params(self): return ScryptParams(self.scrypt_N, self.scrypt_r, self.scrypt_p)
import getpass from duniterpy.key import SigningKey, ScryptParams ################################################ if __name__ == '__main__': # prompt hidden user entry salt = getpass.getpass("Enter your passphrase (salt): ") # prompt hidden user entry password = getpass.getpass("Enter your password: "******"Public key for your credentials: %s" % key.pubkey)
def test_from_sign_to_verify(self): sign_key = SigningKey("saltsalt", "passwordpassword", ScryptParams(4096, 16, 1)) verify_key = VerifyingKey(sign_key.pubkey) self.assertEqual(verify_key.vk, sign_key.vk)
class User: """ The user generates identities documents and sign them """ SCRYPT_PARAMS = ScryptParams(2**12, 16, 1) currency = attr.ib(validator=attr.validators.instance_of(str)) uid = attr.ib(validator=attr.validators.instance_of(str)) key = attr.ib(validator=attr.validators.instance_of(SigningKey)) salt = attr.ib(validator=attr.validators.instance_of(str)) password = attr.ib(validator=attr.validators.instance_of(str)) blockstamp = attr.ib(validator=attr.validators.instance_of(BlockUID)) _logger = attr.ib( default=attr.Factory(lambda: logging.getLogger('mirage'))) @classmethod def create(cls, currency, uid, salt, password, blockstamp): return cls(currency, uid, SigningKey(salt, password, User.SCRYPT_PARAMS), salt, password, blockstamp) def identity(self): identity = Identity(10, self.currency, self.key.pubkey, self.uid, self.blockstamp, []) identity.sign([self.key]) return identity def join(self, blockstamp): ms_doc = Membership(10, self.currency, self.key.pubkey, blockstamp, 'IN', self.uid, self.blockstamp, []) ms_doc.sign([self.key]) return ms_doc def leave(self, blockstamp): ms_doc = Membership(10, self.currency, self.key.pubkey, blockstamp, 'OUT', self.uid, self.blockstamp, []) ms_doc.sign([self.key]) return ms_doc def certify(self, other, blockstamp): cert = Certification(10, self.currency, self.key.pubkey, other.key.pubkey, blockstamp, []) cert.sign(self.identity(), [self.key]) return cert def outputs_from_sources(self, amount, sources): # such a dirty algorithmm # shamelessly copy pasted from sakia def current_value(inputs, overhs): i = 0 for s in inputs: i += s.amount * (10**s.base) for o in overhs: i -= o[0] * (10**o[1]) return i amount, amount_base = reduce_base(amount, 0) current_base = max([src.base for src in sources]) result_sources = [] outputs = [] overheads = [] buf_sources = list(sources) while current_base >= 0: for s in [src for src in buf_sources if src.base == current_base]: test_sources = result_sources + [s] val = current_value(test_sources, overheads) # if we have to compute an overhead if current_value(test_sources, overheads) > amount * (10**amount_base): overhead = current_value( test_sources, overheads) - int(amount) * (10**amount_base) # we round the overhead in the current base # exemple : 12 in base 1 -> 1*10^1 overhead = int(round(float(overhead) / (10**current_base))) source_value = s.amount * (10**s.base) out = int( (source_value - (overhead * (10**current_base))) / (10**current_base)) if out * (10**current_base) <= amount * (10**amount_base): result_sources.append(s) buf_sources.remove(s) overheads.append((overhead, current_base)) outputs.append((out, current_base)) # else just add the output else: result_sources.append(s) buf_sources.remove(s) outputs.append((s.amount, s.base)) if current_value(result_sources, overheads) == amount * (10**amount_base): return result_sources, outputs, overheads current_base -= 1 raise ValueError("Not enough sources") def tx_outputs(self, receiver, outputs, overheads): total = [] outputs_bases = set(o[1] for o in outputs) for base in outputs_bases: output_sum = 0 for o in outputs: if o[1] == base: output_sum += o[0] total.append( OutputSource( output_sum, base, output.Condition.token( output.SIG.token(receiver.key.pubkey)))) overheads_bases = set(o[1] for o in overheads) for base in overheads_bases: overheads_sum = 0 for o in overheads: if o[1] == base: overheads_sum += o[0] total.append( OutputSource( overheads_sum, base, output.Condition.token(output.SIG.token(self.key.pubkey)))) return total def send_money(self, amount, sources, receiver, blockstamp, message): result = self.outputs_from_sources(amount, sources) inputs = result[0] computed_outputs = result[1] overheads = result[2] unlocks = [] for i, s in enumerate(sources): unlocks.append(Unlock(i, [SIGParameter(0)])) outputs = self.tx_outputs(receiver, computed_outputs, overheads) tx = Transaction(3, self.currency, blockstamp, 0, [self.key.pubkey], inputs, unlocks, outputs, message, None) tx.sign([self.key]) return tx