def _submit_solana_earn_batch_tx( self, batch: EarnBatch, service_config: tx_pb.GetServiceConfigResponse, commitment: Commitment, transfer_sender: Optional[PublicKey] = None, ) -> SubmitTransactionResult: subsidizer_id = (batch.subsidizer.public_key if batch.subsidizer else PublicKey(service_config.subsidizer_account.value)) transfer_sender = transfer_sender if transfer_sender else batch.sender.public_key instructions = [ token.transfer( transfer_sender, earn.destination, batch.sender.public_key, earn.quarks, ) for earn in batch.earns] invoices = [earn.invoice for earn in batch.earns if earn.invoice] invoice_list = InvoiceList(invoices) if invoices else None if batch.memo: instructions = [memo.memo_instruction(batch.memo)] + instructions elif self._app_index > 0: fk = invoice_list.get_sha_224_hash() if invoice_list else b'' agora_memo = AgoraMemo.new(1, TransactionType.EARN, self._app_index, fk) instructions = [memo.memo_instruction(base64.b64encode(agora_memo.val).decode('utf-8'))] + instructions tx = solana.Transaction.new(subsidizer_id, instructions) if batch.subsidizer: signers = [batch.subsidizer, batch.sender] else: signers = [batch.sender] return self._sign_and_submit_solana_tx(signers, tx, commitment, invoice_list=invoice_list, dedupe_id=batch.dedupe_id)
def test_submit_earn_batch_with_invoices(self, grpc_channel, executor, app_index_client): sender = PrivateKey.random() earns = [ Earn(PrivateKey.random().public_key, 100000, invoice=Invoice([LineItem('title1', 100000, 'description1', b'somesku')])), Earn(PrivateKey.random().public_key, 100000, invoice=Invoice([LineItem('title2', 100000, 'description2', b'somesku')])), ] future = executor.submit(app_index_client.submit_earn_batch, sender, earns) account_req = self._set_successful_get_account_info_response(grpc_channel, sender, 10) result_xdr = gen_result_xdr(xdr_const.txSUCCESS, [gen_payment_op_result(xdr_const.PAYMENT_SUCCESS)]) submit_req = self._set_successful_submit_transaction_response(grpc_channel, b'somehash', result_xdr) batch_earn_result = future.result() assert len(batch_earn_result.succeeded) == 2 assert len(batch_earn_result.failed) == 0 for idx, earn_result in enumerate(batch_earn_result.succeeded): assert earn_result.tx_hash == b'somehash' assert earn_result.earn == earns[idx] assert not earn_result.error assert account_req.account_id.value == sender.public_key.stellar_address il = InvoiceList([earn.invoice for earn in earns]) expected_memo = memo.HashMemo(AgoraMemo.new(1, TransactionType.EARN, 1, il.get_sha_224_hash()).val) self._assert_earn_batch_envelope(submit_req.envelope_xdr, [sender], sender, 100, 11, expected_memo, sender, earns) assert len(submit_req.invoice_list.invoices) == 2 assert submit_req.invoice_list.SerializeToString() == il.to_proto().SerializeToString()
def _submit_earn_batch_tx( self, sender: PrivateKey, earns: List[Earn], source: Optional[PrivateKey] = None, memo: Optional[str] = None) -> SubmitStellarTransactionResult: """ Submits a single transaction for a batch of earns. An error will be raised if the number of earns exceeds the capacity of a single transaction. :param sender: The :class:`PrivateKey <agora.model.keys.PrivateKey` of the sender :param earns: A list of :class:`Earn <agora.model.earn.Earn>` objects. :param source: (optional) The :class:`PrivateKey <agora.model.keys.PrivateKey` of the transaction source account. If not set, the sender will be used as the source. :param memo: (optional) The memo to include in the transaction. If set, none of the invoices included in earns will be applied. :return: a list of :class:`BatchEarnResult <agora.model.result.EarnResult>` objects """ if len(earns) > 100: raise ValueError("cannot send more than 100 earns") builder = self._get_stellar_builder(source if source else sender) invoices = [earn.invoice for earn in earns if earn.invoice] invoice_list = InvoiceList(invoices) if invoices else None if memo: builder.add_text_memo(memo) elif self.app_index > 0: fk = invoice_list.get_sha_224_hash() if invoice_list else b'' memo = AgoraMemo.new(1, TransactionType.EARN, self.app_index, fk) builder.add_hash_memo(memo.val) for earn in earns: builder.append_payment_op( earn.destination.stellar_address, quarks_to_kin(earn.quarks), source=sender.public_key.stellar_address, ) if source: signers = [source, sender] else: signers = [sender] if self.whitelist_key: signers.append(self.whitelist_key) result = self._sign_and_submit_builder(signers, builder, invoice_list) if result.invoice_errors: # Invoice errors should not be triggered on earns. This indicates there is something wrong with the service. raise Error("unexpected invoice errors present") return result
def test_to_proto(self): invoice_list = InvoiceList([ Invoice([LineItem(title='t1', amount=100)]), Invoice([LineItem(title='t2', amount=200)]) ]) proto = invoice_list.to_proto() assert len(proto.invoices) == len(invoice_list.invoices) for idx, proto_invoice in enumerate(proto.invoices): invoice = invoice_list.invoices[idx] assert proto_invoice.items[0].title == invoice.items[0].title assert proto_invoice.items[0].amount == invoice.items[0].amount
def _submit_stellar_earn_batch_tx( self, batch: EarnBatch) -> SubmitTransactionResult: if len(batch.earns) > 100: raise ValueError('cannot send more than 100 earns') builder = self._get_stellar_builder( batch.channel if batch.channel else batch.sender) invoices = [earn.invoice for earn in batch.earns if earn.invoice] invoice_list = InvoiceList(invoices) if invoices else None if batch.memo: builder.add_text_memo(batch.memo) elif self.app_index > 0: fk = invoice_list.get_sha_224_hash() if invoice_list else b'' memo = AgoraMemo.new(1, TransactionType.EARN, self.app_index, fk) builder.add_hash_memo(memo.val) for earn in batch.earns: # Inside the kin_base module, the base currency has been 'scaled' by a factor of 100 from # Stellar (i.e., the smallest denomination used is 1e-5 instead of 1e-7). However, Kin 2 uses the minimum # Stellar denomination of 1e-7. # # The Kin amounts provided to `append_payment_op` get converted to the smallest denomination inside the # submitted transaction and the conversion occurs assuming a smallest denomination of 1e-5. Therefore, for # Kin 2 transactions, we must multiple by 100 to account for the scaling factor. builder.append_payment_op( earn.destination.stellar_address, quarks_to_kin(earn.quarks * 100 if self._kin_version == 2 else earn.quarks), source=batch.sender.public_key.stellar_address, asset_issuer=self._asset_issuer if self._kin_version == 2 else None, ) if batch.channel: signers = [batch.channel, batch.sender] else: signers = [batch.sender] if self.whitelist_key: signers.append(self.whitelist_key) result = self._sign_and_submit_builder(signers, builder, invoice_list) if result.invoice_errors: # Invoice errors should not be triggered on earns. This indicates there is something wrong with the service. raise Error('unexpected invoice errors present') return result
def test_submit_payment_invoice_error(self, grpc_channel, executor, app_index_client): sender = PrivateKey.random() dest = PrivateKey.random().public_key invoice = Invoice([LineItem('title1', 100000, 'description1', b'somesku1')]) payment = Payment(sender, dest, TransactionType.EARN, 100000, invoice=invoice) future = executor.submit(app_index_client.submit_payment, payment) account_req = self._set_successful_get_account_info_response(grpc_channel, sender, 10) resp = tx_pb.SubmitTransactionResponse( result=tx_pb.SubmitTransactionResponse.Result.INVOICE_ERROR, invoice_errors=[ tx_pb.SubmitTransactionResponse.InvoiceError( op_index=0, invoice=invoice.to_proto(), reason=tx_pb.SubmitTransactionResponse.InvoiceError.Reason.ALREADY_PAID, ) ] ) submit_req = self._set_submit_transaction_response(grpc_channel, resp) with pytest.raises(AlreadyPaidError): future.result() assert account_req.account_id.value == sender.public_key.stellar_address expected_memo = memo.HashMemo( AgoraMemo.new(1, TransactionType.EARN, 1, InvoiceList([invoice]).get_sha_224_hash()).val) self._assert_payment_envelope(submit_req.envelope_xdr, [sender], sender, 100, 11, expected_memo, payment) assert len(submit_req.invoice_list.invoices) == 1 assert submit_req.invoice_list.invoices[0].SerializeToString() == invoice.to_proto().SerializeToString()
def from_json(cls, data: dict): kin_version = data.get('kin_version') if not kin_version: raise ValueError('kin_version is required') tx_hash = base64.b64decode( data.get('tx_hash') if 'tx_hash' in data else b'') if len(tx_hash) == 0: raise ValueError('tx_hash is required') il = data.get('invoice_list') if il: proto_il = model_pb2.InvoiceList() proto_il.ParseFromString(il) invoice_list = InvoiceList.from_proto(proto_il) else: invoice_list = None data = data.get('stellar_data') stellar_data = StellarData.from_json(data) if data else None return cls(kin_version, tx_hash, invoice_list=invoice_list, stellar_data=stellar_data)
def _generate_tx(with_il: Optional[bool] = False) -> Tuple[solana.Transaction, Optional[model_pb2.InvoiceList]]: il = None instructions = [] if with_il: il = model_pb2.InvoiceList( invoices=[ model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='title1', description='desc1', amount=50, sku=b'somesku') ] ) ] ) fk = InvoiceList.from_proto(il).get_sha_224_hash() memo = AgoraMemo.new(1, TransactionType.P2P, 0, fk) instructions.append(solana.memo_instruction(base64.b64encode(memo.val).decode('utf-8'))) keys = [key.public_key for key in generate_keys(3)] instructions.append(solana.transfer( keys[0], keys[1], keys[2], 20, ), ) return solana.Transaction.new( _SIGNING_KEY.public_key, instructions ), il
def from_json(cls, data: dict) -> 'TransactionEvent': kin_version = data.get('kin_version') if not kin_version: raise ValueError('kin_version is required') if kin_version > 4 or kin_version < 2: raise ValueError(f'invalid kin version: {kin_version}') tx_id = base64.b64decode(data.get('tx_id')) if 'tx_id' in data else b'' if len(tx_id) == 0: tx_id = base64.b64decode( data.get('tx_hash')) if 'tx_hash' in data else b'' if len(tx_id) == 0: raise ValueError('`tx_id` or `tx_hash` is required') il = data.get('invoice_list') if il: proto_il = model_pb2.InvoiceList() proto_il.ParseFromString(il) invoice_list = InvoiceList.from_proto(proto_il) else: invoice_list = None tx_event = cls(kin_version, tx_id, invoice_list=invoice_list) solana_data = data.get('solana_event', None) if solana_data: tx_event.solana_event = SolanaEvent.from_json(solana_data) stellar_data = data.get('stellar_event', None) if stellar_data: tx_event.stellar_event = StellarEvent.from_json(stellar_data) return tx_event
def from_proto( cls, item: tx_pb_v4.HistoryItem, state: tx_pb_v4.GetTransactionResponse.State) -> 'TransactionData': payments = [] if item.invoice_list and item.invoice_list.invoices: if len(item.payments) != len(item.invoice_list.invoices): raise ValueError( 'number of invoices does not match number of payments') il = InvoiceList.from_proto(item.invoice_list) else: il = None tx_type = TransactionType.UNKNOWN memo = None if item.solana_transaction.value: solana_tx = solana.Transaction.unmarshal( item.solana_transaction.value) program_idx = solana_tx.message.instructions[0].program_index if solana_tx.message.accounts[ program_idx] == solana.MEMO_PROGRAM_KEY: decompiled_memo = solana.decompile_memo(solana_tx.message, 0) memo_data = decompiled_memo.data.decode('utf-8') try: agora_memo = AgoraMemo.from_b64_string(memo_data) tx_type = agora_memo.tx_type() except ValueError: memo = memo_data elif item.stellar_transaction.envelope_xdr: env = te.TransactionEnvelope.from_xdr( base64.b64encode(item.stellar_transaction.envelope_xdr)) tx = env.tx if isinstance(tx.memo, stellar_memo.HashMemo): try: agora_memo = AgoraMemo.from_base_memo(tx.memo) tx_type = agora_memo.tx_type() except ValueError: pass elif isinstance(tx.memo, stellar_memo.TextMemo): memo = tx.memo.text.decode() for idx, p in enumerate(item.payments): inv = il.invoices[idx] if il and il.invoices else None payments.append( ReadOnlyPayment(PublicKey(p.source.value), PublicKey(p.destination.value), tx_type, p.amount, invoice=inv, memo=memo)) return cls( item.transaction_id.value, TransactionState.from_proto_v4(state), payments, error=TransactionErrors.from_proto_error(item.transaction_error) if item.transaction_error else None, )
def test_cross_language(self): """Test parsing memos generated using the Go memo implementation. """ # memo with an empty FK b64_encoded_memo = 'PVwrAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=' hash_memo = memo.HashMemo(base64.b64decode(b64_encoded_memo)) m = AgoraMemo.from_base_memo(hash_memo, False) assert m.version() == 7 assert m.tx_type() == TransactionType.EARN assert m.app_index() == 51927 assert m.foreign_key() == bytes(29) # memo with unknown tx type b64_encoded_memo = 'RQUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=' hash_memo = memo.HashMemo(base64.b64decode(b64_encoded_memo)) m = AgoraMemo.from_base_memo(hash_memo, False) assert m.version() == 1 assert m.tx_type() == TransactionType.UNKNOWN assert m.tx_type_raw() == 10 assert m.app_index() == 1 assert m.foreign_key() == bytes(29) # memo with an invoice list hash b64_encoded_memo = 'ZQQAiLyJQCfEDmO0QOygz/PZOLDcbwP1FmbdtZ9E+wM=' hash_memo = memo.HashMemo(base64.b64decode(b64_encoded_memo)) expected_il = InvoiceList([ Invoice([ LineItem("Important Payment", 100000, description="A very important payment", sku=b'some sku') ]) ]) expected_fk = expected_il.get_sha_224_hash() m = AgoraMemo.from_base_memo(hash_memo, True) assert m.version() == 1 assert m.tx_type() == TransactionType.P2P assert m.app_index() == 1 # invoice hashes are only 28 bytes, so we ignore the 29th byte in the foreign key assert m.foreign_key()[:28] == expected_fk
def test_payments_from_transaction_with_invoice_list(self): il = model_pb2.InvoiceList(invoices=[ model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='t1', amount=10), ] ), model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='t1', amount=15), ] ), ]) fk = InvoiceList.from_proto(il).get_sha_224_hash() memo = AgoraMemo.new(1, TransactionType.P2P, 0, fk) keys = [key.public_key for key in generate_keys(5)] token_program = keys[4] tx = solana.Transaction.new( keys[0], [ solana.memo_instruction(base64.b64encode(memo.val).decode('utf-8')), solana.transfer( keys[1], keys[2], keys[3], 20, token_program, ), solana.transfer( keys[2], keys[3], keys[1], 40, token_program, ), ] ) payments = ReadOnlyPayment.payments_from_transaction(tx, il) assert len(payments) == 2 assert payments[0].sender == keys[1] assert payments[0].destination == keys[2] assert payments[0].tx_type == TransactionType.P2P assert payments[0].quarks == 20 assert payments[0].invoice == Invoice.from_proto(il.invoices[0]) assert not payments[0].memo assert payments[1].sender == keys[2] assert payments[1].destination == keys[3] assert payments[1].tx_type == TransactionType.P2P assert payments[1].quarks == 40 assert payments[1].invoice == Invoice.from_proto(il.invoices[1]) assert not payments[1].memo
def test_get_transaction(self, grpc_channel, executor, app_index_client): tx_hash = b'somehash' future = executor.submit(app_index_client.get_transaction, tx_hash) _, request, rpc = grpc_channel.take_unary_unary( tx_pb.DESCRIPTOR.services_by_name['Transaction'].methods_by_name['GetTransaction'] ) # Create full response op_result = gen_payment_op_result(xdr_const.PAYMENT_SUCCESS) result_xdr = gen_result_xdr(xdr_const.txSUCCESS, [op_result, op_result]) il = model_pb2.InvoiceList(invoices=[ model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='t1', amount=15), ] ), ]) fk = InvoiceList.from_proto(il).get_sha_224_hash() memo = AgoraMemo.new(1, TransactionType.EARN, 1, fk) hash_memo = gen_hash_memo(memo.val) acc1 = gen_account_id() acc2 = gen_account_id() operations = [gen_payment_op(acc2, amount=15)] envelope_xdr = gen_tx_envelope_xdr(acc1, 1, operations, hash_memo) history_item = tx_pb.HistoryItem( hash=model_pb2.TransactionHash(value=tx_hash), result_xdr=result_xdr, envelope_xdr=envelope_xdr, cursor=tx_pb.Cursor(value=b'cursor1'), invoice_list=il, ) resp = tx_pb.GetTransactionResponse( state=tx_pb.GetTransactionResponse.State.SUCCESS, ledger=10, item=history_item, ) rpc.terminate(resp, (), grpc.StatusCode.OK, '') tx_data = future.result() assert tx_data.tx_hash == tx_hash assert len(tx_data.payments) == 1 assert not tx_data.error payment1 = tx_data.payments[0] assert payment1.sender.raw == acc1.ed25519 assert payment1.destination.raw == acc2.ed25519 assert payment1.tx_type == memo.tx_type() assert payment1.quarks == 15 assert (payment1.invoice.to_proto().SerializeToString() == il.invoices[0].SerializeToString()) assert not payment1.memo assert request.transaction_hash.value == tx_hash
def test_from_proto_agora_memo(self): op_result = gen_payment_op_result(xdr_const.PAYMENT_SUCCESS) result_xdr = gen_result_xdr(xdr_const.txSUCCESS, [op_result, op_result]) il = model_pb2.InvoiceList(invoices=[ model_pb2.Invoice(items=[ model_pb2.Invoice.LineItem(title='t1', amount=10), ]), model_pb2.Invoice(items=[ model_pb2.Invoice.LineItem(title='t1', amount=15), ]), ]) fk = InvoiceList.from_proto(il).get_sha_224_hash() memo = AgoraMemo.new(1, TransactionType.P2P, 0, fk) hash_memo = gen_hash_memo(memo.val) acc1 = gen_account_id() acc2 = gen_account_id() acc3 = gen_account_id() operations = [ gen_payment_op(acc2, src=acc1, amount=10), gen_payment_op(acc1, src=acc2, amount=15), ] envelope_xdr = gen_tx_envelope_xdr(acc3, 1, operations, hash_memo) history_item = tx_pb.HistoryItem( hash=model_pb2.TransactionHash(value=b'somehash'), result_xdr=result_xdr, envelope_xdr=envelope_xdr, cursor=tx_pb.Cursor(value=b'cursor1'), invoice_list=il, ) data = TransactionData.from_proto(history_item) assert data.tx_hash == b'somehash' assert len(data.payments) == 2 payment1 = data.payments[0] assert payment1.sender.raw == acc1.ed25519 assert payment1.destination.raw == acc2.ed25519 assert payment1.tx_type == memo.tx_type() assert payment1.quarks == 10 assert (payment1.invoice.to_proto().SerializeToString() == il.invoices[0].SerializeToString()) assert not payment1.memo payment2 = data.payments[1] assert payment2.sender.raw == acc2.ed25519 assert payment2.destination.raw == acc1.ed25519 assert payment2.tx_type == TransactionType.P2P assert payment2.quarks == 15 assert (payment2.invoice.to_proto().SerializeToString() == il.invoices[1].SerializeToString()) assert not payment2.memo
def _submit_solana_payment_tx( self, payment: Payment, service_config: tx_pb.GetServiceConfigResponse, commitment: Commitment, transfer_source: Optional[PublicKey] = None, create_instructions: List[solana.Instruction] = None, create_signer: Optional[PrivateKey] = None, ) -> SubmitTransactionResult: subsidizer_id = (payment.subsidizer.public_key if payment.subsidizer else PublicKey(service_config.subsidizer_account.value)) instructions = [] invoice_list = None if payment.memo: instructions = [memo.memo_instruction(payment.memo)] elif self._app_index > 0: if payment.invoice: invoice_list = InvoiceList(invoices=[payment.invoice]) fk = invoice_list.get_sha_224_hash() if payment.invoice else b'' m = AgoraMemo.new(1, payment.tx_type, self._app_index, fk) instructions = [memo.memo_instruction(base64.b64encode(m.val).decode('utf-8'))] if create_instructions: instructions += create_instructions sender = transfer_source if transfer_source else payment.sender.public_key instructions.append(token.transfer( sender, payment.destination, payment.sender.public_key, payment.quarks, )) tx = solana.Transaction.new(subsidizer_id, instructions) if payment.subsidizer: signers = [payment.subsidizer, payment.sender] else: signers = [payment.sender] if create_signer: signers.append(create_signer) return self._sign_and_submit_solana_tx(signers, tx, commitment, invoice_list=invoice_list, dedupe_id=payment.dedupe_id)
def _submit_stellar_payment_tx( self, payment: Payment) -> SubmitTransactionResult: builder = self._get_stellar_builder( payment.channel if payment.channel else payment.sender) invoice_list = None if payment.memo: builder.add_text_memo(payment.memo) elif self.app_index > 0: if payment.invoice: invoice_list = InvoiceList(invoices=[payment.invoice]) fk = invoice_list.get_sha_224_hash() if payment.invoice else b'' memo = AgoraMemo.new(1, payment.tx_type, self.app_index, fk) builder.add_hash_memo(memo.val) # Inside the kin_base module, the base currency has been 'scaled' by a factor of 100 from # Stellar (i.e., the smallest denomination used is 1e-5 instead of 1e-7). However, Kin 2 uses the minimum # Stellar denomination of 1e-7. # # The Kin amounts provided to `append_payment_op` get converted to the smallest denomination inside the # submitted transaction and the conversion occurs assuming a smallest denomination of 1e-5. Therefore, for # Kin 2 transactions, we must multiple by 100 to account for the scaling factor. builder.append_payment_op( payment.destination.stellar_address, quarks_to_kin(payment.quarks * 100 if self._kin_version == 2 else payment.quarks), source=payment.sender.public_key.stellar_address, asset_issuer=self._asset_issuer if self._kin_version == 2 else None, ) if payment.channel: signers = [payment.channel, payment.sender] else: signers = [payment.sender] if self.whitelist_key: signers.append(self.whitelist_key) return self._sign_and_submit_builder(signers, builder, invoice_list)
def test_submit_earn_batch_invoice_error(self, grpc_channel, executor, app_index_client): sender = PrivateKey.random() earns = [ Earn(PrivateKey.random().public_key, 100000, invoice=Invoice([LineItem('title1', 100000, 'description1', b'somesku')])), Earn(PrivateKey.random().public_key, 100000, invoice=Invoice([LineItem('title1', 100000, 'description1', b'somesku')])), ] future = executor.submit(app_index_client.submit_earn_batch, sender, earns) account_req = self._set_successful_get_account_info_response(grpc_channel, sender, 10) resp = tx_pb.SubmitTransactionResponse( result=tx_pb.SubmitTransactionResponse.Result.INVOICE_ERROR, invoice_errors=[ tx_pb.SubmitTransactionResponse.InvoiceError( op_index=0, invoice=earns[0].invoice.to_proto(), reason=tx_pb.SubmitTransactionResponse.InvoiceError.Reason.ALREADY_PAID, ), tx_pb.SubmitTransactionResponse.InvoiceError( op_index=0, invoice=earns[1].invoice.to_proto(), reason=tx_pb.SubmitTransactionResponse.InvoiceError.Reason.WRONG_DESTINATION, ) ] ) submit_req = self._set_submit_transaction_response(grpc_channel, resp) batch_earn_result = future.result() assert len(batch_earn_result.succeeded) == 0 assert len(batch_earn_result.failed) == 2 for idx, earn_result in enumerate(batch_earn_result.failed): assert earn_result.earn == earns[idx] assert not earn_result.tx_hash assert isinstance(earn_result.error, Error) assert account_req.account_id.value == sender.public_key.stellar_address expected_memo = memo.HashMemo( AgoraMemo.new(1, TransactionType.EARN, 1, InvoiceList([earn.invoice for earn in earns]).get_sha_224_hash()).val) self._assert_earn_batch_envelope(submit_req.envelope_xdr, [sender], sender, 100, 11, expected_memo, sender, earns) assert len(submit_req.invoice_list.invoices) == 2 assert (submit_req.invoice_list.invoices[0].SerializeToString() == earns[0].invoice.to_proto().SerializeToString()) assert (submit_req.invoice_list.invoices[1].SerializeToString() == earns[1].invoice.to_proto().SerializeToString())
def _submit_solana_payment_tx( self, payment: Payment, service_config: tx_pb.GetServiceConfigResponse, commitment: Commitment, transfer_sender: Optional[PublicKey] = None ) -> SubmitTransactionResult: token_program = PublicKey(service_config.token_program.value) subsidizer_id = (payment.subsidizer.public_key if payment.subsidizer else PublicKey( service_config.subsidizer_account.value)) instructions = [] invoice_list = None if payment.memo: instructions = [memo_instruction(payment.memo)] elif self.app_index > 0: if payment.invoice: invoice_list = InvoiceList(invoices=[payment.invoice]) fk = invoice_list.get_sha_224_hash() if payment.invoice else b'' memo = AgoraMemo.new(1, payment.tx_type, self.app_index, fk) instructions = [ memo_instruction(base64.b64encode(memo.val).decode('utf-8')) ] sender = transfer_sender if transfer_sender else payment.sender.public_key instructions.append( transfer(sender, payment.destination, payment.sender.public_key, payment.quarks, token_program)) tx = solana.Transaction.new(subsidizer_id, instructions) if payment.subsidizer: signers = [payment.subsidizer, payment.sender] else: signers = [payment.sender] return self._sign_and_submit_solana_tx(signers, tx, commitment, invoice_list)
def test_from_proto(self): proto = model_pb2.InvoiceList(invoices=[ model_pb2.Invoice(items=[ model_pb2.Invoice.LineItem(title='t1', amount=100), ]), model_pb2.Invoice(items=[ model_pb2.Invoice.LineItem(title='t2', amount=150), ]) ]) invoice_list = InvoiceList.from_proto(proto) assert len(invoice_list.invoices) == len(proto.invoices) for idx, invoice in enumerate(invoice_list.invoices): proto_invoice = invoice_list.invoices[idx] assert len(invoice.items) == len(proto_invoice.items) assert invoice.items[0].title == proto_invoice.items[0].title assert invoice.items[0].amount == proto_invoice.items[0].amount
def from_json(cls, data: dict) -> 'TransactionEvent': tx_id = base64.b64decode(data.get('tx_id')) if 'tx_id' in data else b'' if len(tx_id) == 0: raise ValueError('`tx_id` is required') il = data.get('invoice_list') if il: proto_il = Parse(json.dumps(il), model_pb2.InvoiceList()) invoice_list = InvoiceList.from_proto(proto_il) else: invoice_list = None solana_data = data.get('solana_event', None) if not solana_data: raise ValueError('`solana_event` is required') return cls(tx_id, SolanaEvent.from_json(solana_data), invoice_list=invoice_list)
def test_from_json_kin_4(self): il = model_pb2.InvoiceList( invoices=[ model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='title1', description='desc1', amount=50, sku=b'somesku') ] ) ] ) fk = InvoiceList.from_proto(il).get_sha_224_hash() memo = AgoraMemo.new(1, TransactionType.P2P, 0, fk) keys = [key.public_key for key in generate_keys(4)] token_program = keys[3] tx = solana.Transaction.new( keys[0], [ solana.memo_instruction(base64.b64encode(memo.val).decode('utf-8')), solana.transfer( keys[1], keys[2], keys[3], 20, token_program, ), ] ) data = { 'kin_version': 4, 'solana_transaction': base64.b64encode(tx.marshal()), 'invoice_list': base64.b64encode(il.SerializeToString()), } req = SignTransactionRequest.from_json(data, Environment.TEST) assert len(req.payments) == 1 assert req.payments[0].invoice == Invoice.from_proto(il.invoices[0]) assert req.kin_version == data['kin_version'] assert req.transaction == tx
def test_payments_from_envelope_with_invoice_list(self): il = model_pb2.InvoiceList(invoices=[ model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='t1', amount=10), ] ), model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='t1', amount=15), ] ), ]) fk = InvoiceList.from_proto(il).get_sha_224_hash() memo = AgoraMemo.new(1, TransactionType.P2P, 0, fk) hash_memo = gen_hash_memo(memo.val) acc1 = gen_account_id() acc2 = gen_account_id() acc3 = gen_account_id() operations = [gen_payment_op(acc2, amount=20), gen_payment_op(acc3, src=acc2, amount=40)] envelope_xdr = gen_tx_envelope_xdr(acc1, 1, operations, hash_memo) env = te.TransactionEnvelope.from_xdr(base64.b64encode(envelope_xdr)) payments = ReadOnlyPayment.payments_from_envelope(env, il) assert len(payments) == 2 assert payments[0].sender.raw == acc1.ed25519 assert payments[0].destination.raw == acc2.ed25519 assert payments[0].tx_type == TransactionType.P2P assert payments[0].quarks == 20 assert payments[0].invoice == Invoice.from_proto(il.invoices[0]) assert not payments[0].memo assert payments[1].sender.raw == acc2.ed25519 assert payments[1].destination.raw == acc3.ed25519 assert payments[1].tx_type == TransactionType.P2P assert payments[1].quarks == 40 assert payments[1].invoice == Invoice.from_proto(il.invoices[1]) assert not payments[1].memo
def from_json(cls, data: dict): envelope_xdr = data.get('envelope_xdr', "") if len(envelope_xdr) == 0: raise ValueError('envelope_xdr is required') env = te.TransactionEnvelope.from_xdr(envelope_xdr) kin_version = data.get('kin_version') if not kin_version: raise ValueError('kin_version is required') il_str = data.get('invoice_list') if il_str: proto_il = model_pb2.InvoiceList() proto_il.ParseFromString(base64.b64decode(il_str)) il = InvoiceList.from_proto(proto_il) else: il = None return cls(ReadOnlyPayment.payments_from_envelope(env, il), kin_version, envelope=env)
def test_submit_payment_with_invoice(self, grpc_channel, executor, app_index_client): sender = PrivateKey.random() dest = PrivateKey.random().public_key invoice = Invoice([LineItem('title1', 100000, 'description1', b'somesku')]) payment = Payment(sender, dest, TransactionType.EARN, 100000, invoice=invoice) future = executor.submit(app_index_client.submit_payment, payment) account_req = self._set_successful_get_account_info_response(grpc_channel, sender, 10) result_xdr = gen_result_xdr(xdr_const.txSUCCESS, [gen_payment_op_result(xdr_const.PAYMENT_SUCCESS)]) submit_req = self._set_successful_submit_transaction_response(grpc_channel, b'somehash', result_xdr) assert future.result() == b'somehash' assert account_req.account_id.value == sender.public_key.stellar_address expected_memo = memo.HashMemo( AgoraMemo.new(1, TransactionType.EARN, 1, InvoiceList([invoice]).get_sha_224_hash()).val) self._assert_payment_envelope(submit_req.envelope_xdr, [sender], sender, 100, 11, expected_memo, payment) assert len(submit_req.invoice_list.invoices) == 1 assert submit_req.invoice_list.invoices[0].SerializeToString() == invoice.to_proto().SerializeToString()
def test_payments_from_transaction_invalid(self): il = model_pb2.InvoiceList(invoices=[ model_pb2.Invoice( items=[ model_pb2.Invoice.LineItem(title='t1', amount=10), ] ), ]) fk = InvoiceList.from_proto(il).get_sha_224_hash() memo = AgoraMemo.new(1, TransactionType.P2P, 0, fk) keys = [key.public_key for key in generate_keys(5)] token_program = keys[4] tx = solana.Transaction.new( keys[0], [ solana.memo_instruction(base64.b64encode(memo.val).decode('utf-8')), solana.transfer( keys[1], keys[2], keys[3], 20, token_program, ), solana.transfer( keys[2], keys[3], keys[1], 40, token_program, ), ] ) # mismatching number of invoices and instructions with pytest.raises(ValueError): ReadOnlyPayment.payments_from_transaction(tx, il)
def test_from_proto_solana_agora_memo(self): acc1, acc2, token_program = [ key.public_key for key in generate_keys(3) ] il = model_pb_v3.InvoiceList(invoices=[ model_pb_v3.Invoice(items=[ model_pb_v3.Invoice.LineItem(title='t1', amount=10), ]), model_pb_v3.Invoice(items=[ model_pb_v3.Invoice.LineItem(title='t1', amount=15), ]), ]) fk = InvoiceList.from_proto(il).get_sha_224_hash() agora_memo = AgoraMemo.new(1, TransactionType.P2P, 0, fk) tx = Transaction.new(PrivateKey.random().public_key, [ memo_instruction(base64.b64encode(agora_memo.val).decode('utf-8')), transfer(acc1, acc2, PrivateKey.random().public_key, 10), transfer(acc2, acc1, PrivateKey.random().public_key, 15), ]) history_item = tx_pb.HistoryItem( transaction_id=model_pb.TransactionId(value=b'somehash'), cursor=tx_pb.Cursor(value=b'cursor1'), solana_transaction=model_pb.Transaction(value=tx.marshal(), ), payments=[ tx_pb.HistoryItem.Payment( source=model_pb.SolanaAccountId(value=acc1.raw), destination=model_pb.SolanaAccountId(value=acc2.raw), amount=10, ), tx_pb.HistoryItem.Payment( source=model_pb.SolanaAccountId(value=acc2.raw), destination=model_pb.SolanaAccountId(value=acc1.raw), amount=15, ), ], invoice_list=il, ) data = TransactionData.from_proto( history_item, tx_pb.GetTransactionResponse.State.SUCCESS) assert data.tx_id == b'somehash' assert data.transaction_state == TransactionState.SUCCESS assert len(data.payments) == 2 payment1 = data.payments[0] assert payment1.sender.raw == acc1.raw assert payment1.destination.raw == acc2.raw assert payment1.tx_type == TransactionType.P2P assert payment1.quarks == 10 assert (payment1.invoice.to_proto().SerializeToString() == il.invoices[0].SerializeToString()) assert not payment1.memo payment2 = data.payments[1] assert payment2.sender.raw == acc2.raw assert payment2.destination.raw == acc1.raw assert payment2.tx_type == TransactionType.P2P assert payment2.quarks == 15 assert (payment2.invoice.to_proto().SerializeToString() == il.invoices[1].SerializeToString()) assert not payment2.memo
def submit_payment(self, payment: Payment) -> bytes: if self._kin_version not in _SUPPORTED_VERSIONS: raise UnsupportedVersionError() if payment.invoice and self.app_index <= 0: raise ValueError( "cannot submit a payment with an invoice without an app index") builder = self._get_stellar_builder( payment.source if payment.source else payment.sender) invoice_list = None if payment.memo: builder.add_text_memo(payment.memo) elif self.app_index > 0: if payment.invoice: invoice_list = InvoiceList(invoices=[payment.invoice]) fk = invoice_list.get_sha_224_hash() if payment.invoice else b'' memo = AgoraMemo.new(1, payment.tx_type, self.app_index, fk) builder.add_hash_memo(memo.val) builder.append_payment_op( payment.destination.stellar_address, quarks_to_kin(payment.quarks), source=payment.sender.public_key.stellar_address, ) if payment.source: signers = [payment.source, payment.sender] else: signers = [payment.sender] if self.whitelist_key: signers.append(self.whitelist_key) result = self._sign_and_submit_builder(signers, builder, invoice_list) if result.tx_error: if len(result.tx_error.op_errors) > 0: if len(result.tx_error.op_errors) != 1: raise Error( "invalid number of operation errors, expected 0 or 1, got {}" .format(len(result.tx_error.op_errors))) raise result.tx_error.op_errors[0] if result.tx_error.tx_error: raise result.tx_error.tx_error if result.invoice_errors: if len(result.invoice_errors) != 1: raise Error( "invalid number of invoice errors, expected 0 or 1, got {}" .format(len(result.invoice_errors))) if result.invoice_errors[ 0].reason == InvoiceErrorReason.ALREADY_PAID: raise AlreadyPaidError() if result.invoice_errors[ 0].reason == InvoiceErrorReason.WRONG_DESTINATION: raise WrongDestinationError() if result.invoice_errors[ 0].reason == InvoiceErrorReason.SKU_NOT_FOUND: raise SkuNotFoundError() raise Error("unknown invoice error: {}".format( result.invoice_errors[0].reason)) return result.tx_hash