async def get_local_status_and_history(self, address, history=None): if not history: address_details = await self.db.get_address(address=address) history = address_details['history'] or '' parts = history.split(':')[:-1] return (hexlify(sha256(history.encode())).decode() if history else None, list(zip(parts[0::2], map(int, parts[1::2]))))
def test_query_interpolation(self): self.maxDiff = None # tests that interpolation replaces longer keys first self.assertEqual( interpolate(*query( "select * from foo", a__not='b', b__in='select * from blah where c=:$c', d__any={ 'one__like': 'o', 'two': 2 }, a0=3, a00=1, a00a=2, a00aa=4, # <-- breaks without correct interpolation key order ahash=memoryview(sha256(b'hello world')), limit=10, order_by='b', **{'$c': 3})), "select * from foo WHERE a != 'b' AND " "b IN (select * from blah where c=3) AND " "(one LIKE 'o' OR two = 2) AND " "a0 = 3 AND a00 = 1 AND a00a = 2 AND a00aa = 4 " "AND ahash = e9cdefe2acf78890ee80537ae3ef84c4faab7ddad7522ea5083e4d93b9274db9 " "ORDER BY b LIMIT 10", )
def generate_signed_legacy(address: bytes, output: Output): decoded_address = Base58.decode(address) claim = OldClaimMessage() claim.ParseFromString(unhexlify( '080110011aee04080112a604080410011a2b4865726520617265203520526561736f6e73204920e29da4e' 'fb88f204e657874636c6f7564207c20544c4722920346696e64206f7574206d6f72652061626f7574204e' '657874636c6f75643a2068747470733a2f2f6e657874636c6f75642e636f6d2f0a0a596f752063616e206' '6696e64206d65206f6e20746865736520736f6369616c733a0a202a20466f72756d733a2068747470733a' '2f2f666f72756d2e6865617679656c656d656e742e696f2f0a202a20506f64636173743a2068747470733' 'a2f2f6f6666746f706963616c2e6e65740a202a2050617472656f6e3a2068747470733a2f2f7061747265' '6f6e2e636f6d2f7468656c696e757867616d65720a202a204d657263683a2068747470733a2f2f7465657' '37072696e672e636f6d2f73746f7265732f6f6666696369616c2d6c696e75782d67616d65720a202a2054' '77697463683a2068747470733a2f2f7477697463682e74762f786f6e64616b0a202a20547769747465723' 'a2068747470733a2f2f747769747465722e636f6d2f7468656c696e757867616d65720a0a2e2e2e0a6874' '7470733a2f2f7777772e796f75747562652e636f6d2f77617463683f763d4672546442434f535f66632a0' 'f546865204c696e75782047616d6572321c436f7079726967687465642028636f6e746163742061757468' '6f722938004a2968747470733a2f2f6265726b2e6e696e6a612f7468756d626e61696c732f46725464424' '34f535f666352005a001a41080110011a30040e8ac6e89c061f982528c23ad33829fd7146435bf7a4cc22' 'f0bff70c4fe0b91fd36da9a375e3e1c171db825bf5d1f32209766964656f2f6d70342a5c080110031a406' '2b2dd4c45e364030fbfad1a6fefff695ebf20ea33a5381b947753e2a0ca359989a5cc7d15e5392a0d354c' '0b68498382b2701b22c03beb8dcb91089031b871e72214feb61536c007cdf4faeeaab4876cb397feaf6b51' )) claim.ClearField("publisherSignature") digest = sha256(b''.join([ decoded_address, claim.SerializeToString(), output.claim_hash[::-1] ])) private_key = ecdsa.SigningKey.from_pem(output.private_key, hashfunc=hashlib.sha256) signature = private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256) claim.publisherSignature.version = 1 claim.publisherSignature.signatureType = 1 claim.publisherSignature.signature = signature claim.publisherSignature.certificateId = output.claim_hash[::-1] return claim
def test_query_interpolation(self): self.maxDiff = None # tests that interpolation replaces longer keys first self.assertEqual( interpolate(*query( "select * from foo", a__not='b', b__in='select * from blah where c=:$c', d__any={ 'one__like': 'o', 'two': 2 }, a0=3, a00=1, a00a=2, a00aa=4, # <-- breaks without correct interpolation key order ahash=memoryview(sha256(b'hello world')), limit=10, order_by='b', **{'$c': 3})), "select * from foo WHERE a != 'b' AND " "b IN (select * from blah where c=3) AND " "(one LIKE 'o' OR two = 2) AND " "a0 = 3 AND a00 = 1 AND a00a = 2 AND a00aa = 4 " "AND ahash = X'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9' " "ORDER BY b LIMIT 10", )
def is_signed_by(self, channel: 'Output', ledger=None): if self.claim.unsigned_payload: pieces = [ Base58.decode(self.get_address(ledger)), self.claim.unsigned_payload, self.claim.signing_channel_hash[::-1] ] else: pieces = [ self.tx_ref.tx.inputs[0].txo_ref.hash, self.claim.signing_channel_hash, self.claim.to_message_bytes() ] digest = sha256(b''.join(pieces)) public_key = load_der_public_key( channel.claim.channel.public_key_bytes, default_backend()) hash = hashes.SHA256() signature = hexlify(self.claim.signature) r = int(signature[:int(len(signature) / 2)], 16) s = int(signature[int(len(signature) / 2):], 16) encoded_sig = ecdsa.util.sigencode_der(r, s, len(signature) * 4) try: public_key.verify(encoded_sig, digest, ec.ECDSA(Prehashed(hash))) return True except (ValueError, InvalidSignature): pass return False
def sign_comment(comment: dict, channel: Output): timestamp = str(int(time.time())).encode() pieces = [timestamp, channel.claim_hash, comment['comment'].encode()] digest = sha256(b''.join(pieces)) signature = channel.private_key.sign_digest_deterministic( digest, hashfunc=hashlib.sha256) comment['signature'] = binascii.hexlify(signature).decode() comment['signing_ts'] = timestamp.decode()
async def create_tx_from_nothing(self, my_account, height): to_address = await my_account.receiving.get_or_create_usable_address() to_hash = ledger_class.address_to_hash160(to_address) tx = ledger_class.transaction_class(height=height, is_verified=True) \ .add_inputs([self.txi(self.txo(1, sha256(str(height).encode())))]) \ .add_outputs([self.txo(1, to_hash)]) await self.ledger.db.insert_transaction(tx) await self.ledger.db.save_transaction_io(tx, to_address, to_hash, '') return tx
def sign(self, channel: 'Output', first_input_id=None): self.channel = channel self.claim.signing_channel_hash = channel.claim_hash digest = sha256(b''.join([ first_input_id or self.tx_ref.tx.inputs[0].txo_ref.hash, self.claim.signing_channel_hash, self.claim.to_message_bytes() ])) self.claim.signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256) self.script.generate()
def sign_comment(comment: dict, channel: Output, abandon=False): timestamp = str(int(time.time())) signing_field = comment['comment_id'] if abandon else comment['comment'] pieces = [timestamp.encode(), channel.claim_hash, signing_field.encode()] digest = sha256(b''.join(pieces)) signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256) comment.update({ 'signature': binascii.hexlify(signature).decode(), 'signing_ts': timestamp })
def is_comment_signed_by_channel(comment: dict, channel: Output): try: pieces = [ comment['signing_ts'].encode(), channel.claim_hash, comment['comment'].encode() ] return Output.is_signature_valid( get_encoded_signature(comment['signature']), sha256(b''.join(pieces)), channel.claim.channel.public_key_bytes) except KeyError: pass return False
def get_signature_digest(self, ledger): if self.claim.unsigned_payload: pieces = [ Base58.decode(self.get_address(ledger)), self.claim.unsigned_payload, self.claim.signing_channel_hash[::-1] ] else: pieces = [ self.tx_ref.tx.inputs[0].txo_ref.hash, self.claim.signing_channel_hash, self.claim.to_message_bytes() ] return sha256(b''.join(pieces))
async def test_checkpointed_writer(self): headers = MainHeaders(':memory:') headers.checkpoint = 100, hexlify( sha256(self.get_bytes(block_bytes(100)))) genblocks = lambda start, end: self.get_bytes(block_bytes(end - start), block_bytes(start)) async with headers.checkpointed_connector() as buff: buff.write(genblocks(0, 10)) self.assertEqual(len(headers), 10) async with headers.checkpointed_connector() as buff: buff.write(genblocks(10, 100)) self.assertEqual(len(headers), 100) headers = MainHeaders(':memory:') async with headers.checkpointed_connector() as buff: buff.write(genblocks(0, 300)) self.assertEqual(len(headers), 300)
def is_comment_signed_by_channel(comment: dict, channel: Output, abandon=False): if type(channel) is Output: try: signing_field = comment['comment_id'] if abandon else comment['comment'] pieces = [ comment['signing_ts'].encode(), cid2hash(comment['channel_id']), signing_field.encode() ] return Output.is_signature_valid( get_encoded_signature(comment['signature']), sha256(b''.join(pieces)), channel.claim.channel.public_key_bytes ) except KeyError: pass return False
def hash(self): if self._hash is None: self._hash = sha256(sha256(self.tx.raw_sans_segwit)) return self._hash
def hash(self): if self._hash is None: self._hash = sha256(sha256(self.tx.raw)) return self._hash
def hash(self) -> bytes: return sha256(json.dumps(self.to_dict()).encode())
def hash(self) -> bytes: assert not self.encrypted, "Cannot hash an encrypted account." return sha256(json.dumps(self.to_dict()).encode())