def get(self, hash_and_pc, rtx): res = self.txos.get_by_hash(sha256(hash_and_pc), rtx=rtx) if not res: raise KeyError(hash_and_pc) utxo = IOput() utxo.deserialize_with_context(res) utxo.set_verified_and_correct() #Trust saved outputs return utxo
def append(self, utxo, wtx): assert utxo.verify() #Should be fast since cached assert utxo.address_excess_num_index self.txos.append(wtx=wtx, obj_index=sha256(utxo.serialized_index), obj=utxo.serialize_with_context()) self.commitments.append_unique(wtx=wtx, obj_index=utxo.commitment_index, obj=b"")
def find(self, hash_and_pc, rtx): ''' In contrast with __getitem__ find will try to find even spent outputs for other (syncing) nodes. ''' res = self.txos.find_by_hash(sha256(hash_and_pc), rtx=rtx) if not res: raise KeyError(hash_and_pc) utxo = IOput() utxo.deserialize_with_context(res) utxo.set_verified_and_correct() #Trust saved outputs return utxo
def sum(self, x1, x2): # each index is 33 bytes for commitments and 32 for hash pubkey1, hash1 = x1[:33], x1[33:65] pubkey2, hash2 = x2[:33], x2[33:65] pubkey1, pubkey2 = PublicKey(pubkey=pubkey1, raw=True), PublicKey(pubkey=pubkey2, raw=True) # consider pubkey1+pubkey2 pk = PublicKey() pk.combine([pubkey1.public_key, pubkey2.public_key]) first_part = pk.serialize() second_part = sha256(hash1 + hash2) return first_part + second_part
def sum(self, x1, x2): # each index is 33 bytes for commitments and 32 for hash comm1, hash1 = x1[:33], x1[33:65] comm2, hash2 = x2[:33], x2[33:65] comm1, comm2 = PedersenCommitment( commitment=comm1, raw=True), PedersenCommitment(commitment=comm2, raw=True) #XXX we definetely need sum of pedersen commitments on libsecp256 level. pk = PublicKey() pk.combine([ comm1.to_public_key().public_key, comm2.to_public_key().public_key ]) sm = pk.to_pedersen_commitment() first_part = sm.serialize() second_part = sha256(hash1 + hash2) return first_part + second_part
def evaluate_scripts(tx, prev_block_props, excess_lookup): """ Check wether all excesses in tx evaluate to True. """ def find_output(commitment): pc = commitment.to_pedersen_commitment() ser = pc.serialize() for o in tx.outputs: if ser == o.serialized_apc: return o return None def output_lookup(commitment): return bool(find_output(commitment)) burdens = [] excesses = tx.additional_excesses + list(tx.updated_excesses.values()) for excess in excesses: burden = [] result = execute(script = excess.message, prev_block_props = prev_block_props, excess_lookup = excess_lookup, output_lookup = output_lookup, burden = burden) if not result: return False if len(burden): for comm, pubkey in burden: output = find_output(comm) pk = pubkey.to_pubkey() if not pk.serialize() == output.address.serialized_pubkey: return False excess_index = pk.serialize() + sha256(b"\x01\x00"+output.serialized_apc) burdens.append((output.serialized_index, excess_index)) tx.burdens = burdens return True
def __init__(self, password): self.aead = None self.password = password if self.password: self.raw_private_key = sha256(self.password.encode("utf-8")) self.aead = ChaCha20Poly1305(self.raw_private_key, 'python')
def deterministic_nonce(self, payload): return sha256(payload + self.raw_private_key)[:12] if self.password else None
def spend(self, utxo, wtx, return_revert_obj=False): txos = self.txos.discard(sha256(utxo.serialized_index), wtx=wtx) commitment = self.commitments.clear(utxo.commitment_index, wtx=wtx) if return_revert_obj: return (txos, commitment)
def sum(self, x1, x2): return sha256(x1 + x2)
def find_wo_deser(self, hash_and_pc, rtx): res = self.txos.find_by_hash(sha256(hash_and_pc), rtx=rtx) if not res: raise KeyError(hash_and_pc) return res
def has(self, serialized_index, rtx): return bool(self.txos.get_by_hash(sha256(serialized_index), rtx=rtx))