@@ -118,6 +118,8 @@ async def create_request(
118118 signed_request : dict = None ,
119119 expires_time : str = None ,
120120 endorser_write_txn : bool = None ,
121+ author_goal_code : str = None ,
122+ signer_goal_code : str = None ,
121123 ):
122124 """
123125 Create a new Transaction Request.
@@ -142,8 +144,12 @@ async def create_request(
142144 "context" : TransactionRecord .SIGNATURE_CONTEXT ,
143145 "method" : TransactionRecord .ADD_SIGNATURE ,
144146 "signature_type" : TransactionRecord .SIGNATURE_TYPE ,
145- "signer_goal_code" : TransactionRecord .ENDORSE_TRANSACTION ,
146- "author_goal_code" : TransactionRecord .WRITE_TRANSACTION ,
147+ "signer_goal_code" : signer_goal_code
148+ if signer_goal_code
149+ else TransactionRecord .ENDORSE_TRANSACTION ,
150+ "author_goal_code" : author_goal_code
151+ if author_goal_code
152+ else TransactionRecord .WRITE_TRANSACTION ,
147153 }
148154 transaction .signature_request .clear ()
149155 transaction .signature_request .append (signature_request )
@@ -233,6 +239,7 @@ async def create_endorse_response(
233239
234240 transaction ._type = TransactionRecord .SIGNATURE_RESPONSE
235241 transaction_json = transaction .messages_attach [0 ]["data" ]["json" ]
242+ ledger_response = {}
236243
237244 async with self ._profile .session () as session :
238245 wallet : BaseWallet = session .inject_or (BaseWallet )
@@ -266,9 +273,40 @@ async def create_endorse_response(
266273 raise LedgerError (reason = reason )
267274
268275 async with ledger :
269- endorsed_msg = await shield (
270- ledger .txn_endorse (transaction_json , endorse_did = endorser_did_info )
276+ # check our goal code!
277+ txn_goal_code = (
278+ transaction .signature_request [0 ]["signer_goal_code" ]
279+ if transaction .signature_request
280+ and "signer_goal_code" in transaction .signature_request [0 ]
281+ else TransactionRecord .ENDORSE_TRANSACTION
271282 )
283+ if txn_goal_code == TransactionRecord .ENDORSE_TRANSACTION :
284+ endorsed_msg = await shield (
285+ ledger .txn_endorse (transaction_json , endorse_did = endorser_did_info )
286+ )
287+ elif txn_goal_code == TransactionRecord .WRITE_DID_TRANSACTION :
288+ # get DID info from transaction.meta_data
289+ meta_data = json .loads (transaction_json )
290+ (success , txn ) = await shield (
291+ ledger .register_nym (
292+ meta_data ["did" ],
293+ meta_data ["verkey" ],
294+ meta_data ["alias" ],
295+ meta_data ["role" ],
296+ )
297+ )
298+ # we don't have an endorsed transaction so just return did meta-data
299+ ledger_response = {
300+ "result" : {
301+ "txn" : {"type" : "1" , "data" : {"dest" : meta_data ["did" ]}}
302+ },
303+ "meta_data" : meta_data ,
304+ }
305+ endorsed_msg = json .dumps (ledger_response )
306+ else :
307+ raise TransactionManagerError (
308+ f"Invalid goal code for transaction record:" f" { txn_goal_code } "
309+ )
272310
273311 # need to return the endorsed msg or else the ledger will reject the
274312 # eventual transaction write
@@ -278,7 +316,7 @@ async def create_endorse_response(
278316 "message_id" : transaction .messages_attach [0 ]["@id" ],
279317 "context" : TransactionRecord .SIGNATURE_CONTEXT ,
280318 "method" : TransactionRecord .ADD_SIGNATURE ,
281- "signer_goal_code" : TransactionRecord . ENDORSE_TRANSACTION ,
319+ "signer_goal_code" : txn_goal_code ,
282320 "signature_type" : TransactionRecord .SIGNATURE_TYPE ,
283321 "signature" : {endorser_did : endorsed_msg or endorser_verkey },
284322 }
@@ -291,8 +329,12 @@ async def create_endorse_response(
291329 async with self ._profile .session () as session :
292330 await transaction .save (session , reason = "Created an endorsed response" )
293331
294- if transaction .endorser_write_txn :
295- ledger_response = await self .complete_transaction (transaction )
332+ if (
333+ transaction .endorser_write_txn
334+ and txn_goal_code == TransactionRecord .ENDORSE_TRANSACTION
335+ ):
336+ # running as the endorser, we've been asked to write the transaction
337+ ledger_response = await self .complete_transaction (transaction , True )
296338 endorsed_transaction_response = EndorsedTransactionResponse (
297339 transaction_id = transaction .thread_id ,
298340 thread_id = transaction ._id ,
@@ -310,6 +352,7 @@ async def create_endorse_response(
310352 signature_response = signature_response ,
311353 state = state ,
312354 endorser_did = endorser_did ,
355+ ledger_response = ledger_response ,
313356 )
314357
315358 return transaction , endorsed_transaction_response
@@ -357,7 +400,9 @@ async def receive_endorse_response(self, response: EndorsedTransactionResponse):
357400
358401 return transaction
359402
360- async def complete_transaction (self , transaction : TransactionRecord ):
403+ async def complete_transaction (
404+ self , transaction : TransactionRecord , endorser : bool = False
405+ ):
361406 """
362407 Complete a transaction.
363408
@@ -371,24 +416,34 @@ async def complete_transaction(self, transaction: TransactionRecord):
371416 The updated transaction
372417
373418 """
419+
374420 ledger_transaction = transaction .messages_attach [0 ]["data" ]["json" ]
375421
376- ledger = self ._profile .inject (BaseLedger )
377- if not ledger :
378- reason = "No ledger available"
379- if not self ._profile .context .settings .get_value ("wallet.type" ):
380- reason += ": missing wallet-type?"
381- raise TransactionManagerError (reason )
422+ # check if we (author) have requested the endorser to write the transaction
423+ if (endorser and transaction .endorser_write_txn ) or (
424+ (not endorser ) and (not transaction .endorser_write_txn )
425+ ):
426+ ledger = self ._profile .inject (BaseLedger )
427+ if not ledger :
428+ reason = "No ledger available"
429+ if not self ._profile .context .settings .get_value ("wallet.type" ):
430+ reason += ": missing wallet-type?"
431+ raise TransactionManagerError (reason )
382432
383- async with ledger :
384- try :
385- ledger_response_json = await shield (
386- ledger .txn_submit (ledger_transaction , sign = False , taa_accept = False )
387- )
388- except (IndyIssuerError , LedgerError ) as err :
389- raise TransactionManagerError (err .roll_up ) from err
433+ async with ledger :
434+ try :
435+ ledger_response_json = await shield (
436+ ledger .txn_submit (
437+ ledger_transaction , sign = False , taa_accept = False
438+ )
439+ )
440+ except (IndyIssuerError , LedgerError ) as err :
441+ raise TransactionManagerError (err .roll_up ) from err
442+
443+ ledger_response = json .loads (ledger_response_json )
390444
391- ledger_response = json .loads (ledger_response_json )
445+ else :
446+ ledger_response = ledger_transaction
392447
393448 transaction .state = TransactionRecord .STATE_TRANSACTION_ACKED
394449
@@ -397,7 +452,7 @@ async def complete_transaction(self, transaction: TransactionRecord):
397452
398453 # this scenario is where the endorser is writing the transaction
399454 # (called from self.create_endorse_response())
400- if transaction .endorser_write_txn :
455+ if endorser and transaction .endorser_write_txn :
401456 return ledger_response
402457
403458 connection_id = transaction .connection_id
@@ -733,6 +788,9 @@ async def endorsed_txn_post_processing(
733788 would be stored in wallet.
734789 """
735790
791+ if isinstance (ledger_response , str ):
792+ ledger_response = json .loads (ledger_response )
793+
736794 ledger = self ._profile .inject (BaseLedger )
737795 if not ledger :
738796 reason = "No ledger available"
0 commit comments