This is an automated email from the ASF dual-hosted git repository.

cmorris pushed a commit to branch splitroles-blockchain
in repository https://gitbox.apache.org/repos/asf/incubator-milagro-dta.git

commit 7016314eaf56e66a6f1b4bb7363244c9c675fc7e
Author: Christopher Morris <[email protected]>
AuthorDate: Mon Sep 30 14:34:36 2019 +0100

    Refactor RecipientList
---
 libs/documents/docs.proto     |   2 -
 pkg/common/common.go          | 116 ++++--------------------------------------
 pkg/defaultservice/fulfill.go |  33 +++++-------
 3 files changed, 21 insertions(+), 130 deletions(-)

diff --git a/libs/documents/docs.proto b/libs/documents/docs.proto
index 1f326ef..65b2282 100644
--- a/libs/documents/docs.proto
+++ b/libs/documents/docs.proto
@@ -81,8 +81,6 @@ message OrderDocument {
         Beneficiary_Known_at_start = 1;
         Beneficiary_Unknown_at_Start = 2;
     }
-
-
     string Type           = 1; //This can be used to extend the types of 
things that an order can do.
     OrderBeneficiaryType BeneficiaryType = 2;
     int64 Coin               = 3 [(validator.field) = {int_gt: -1, int_lt: 
999}];
diff --git a/pkg/common/common.go b/pkg/common/common.go
index 694019d..0ce6ff0 100644
--- a/pkg/common/common.go
+++ b/pkg/common/common.go
@@ -22,7 +22,6 @@ package common
 
 import (
        "encoding/hex"
-       "fmt"
        "io"
        "sync"
        "time"
@@ -118,100 +117,6 @@ func RetrieveSeed(store *datastore.Store, reference 
string) (seedHex string, err
        return seedHex, nil
 }
 
-// CreateAndStoreOrderPart2 -
-func CreateAndStoreOrderPart2(ipfs ipfs.Connector, store *datastore.Store, 
order *documents.OrderDoc, orderPart1CID, commitmentPublicKey, nodeID string, 
recipients map[string]documents.IDDoc) (orderPart2CID string, err error) {
-       Part2 := documents.OrderPart2{
-               CommitmentPublicKey: commitmentPublicKey,
-               PreviousOrderCID:    orderPart1CID,
-               Timestamp:           time.Now().Unix(),
-       }
-       order.OrderPart2 = &Part2
-       //Write the updated doc back to IPFS
-       orderPart2CID, err = WriteOrderToIPFS(nodeID, ipfs, store, nodeID, 
order, recipients)
-       if err != nil {
-               return "", err
-       }
-       return orderPart2CID, nil
-}
-
-// CreateAndStorePart3 adds part 3 "redemption request" to the order doc
-func CreateAndStorePart3(ipfs ipfs.Connector, store *datastore.Store, order 
*documents.OrderDoc, orderPart2CID, nodeID string, beneficiaryEncryptedData 
[]byte, recipients map[string]documents.IDDoc) (*documents.OrderDoc, error) {
-       //Add part 3 "redemption request" to the order doc
-       redemptionRequest := documents.OrderPart3{
-               //TODO
-               Redemption:               "SignedReferenceNumber",
-               PreviousOrderCID:         orderPart2CID,
-               BeneficiaryEncryptedData: beneficiaryEncryptedData,
-               Timestamp:                time.Now().Unix(),
-       }
-       order.OrderPart3 = &redemptionRequest
-       //Write the updated doc back to IPFS
-       return order, nil
-}
-
-// CreateAndStoreOrderPart4 -
-func CreateAndStoreOrderPart4(ipfs ipfs.Connector, store *datastore.Store, 
order *documents.OrderDoc, commitmentPrivateKey, orderPart3CID, nodeID string, 
recipients map[string]documents.IDDoc) (orderPart4CID string, err error) {
-       Part4 := documents.OrderPart4{
-               Secret:           commitmentPrivateKey,
-               PreviousOrderCID: orderPart3CID,
-               Timestamp:        time.Now().Unix(),
-       }
-       order.OrderPart4 = &Part4
-       //Write the updated doc back to IPFS
-       orderPart4CID, err = WriteOrderToIPFS(nodeID, ipfs, store, nodeID, 
order, recipients)
-       if err != nil {
-               return "", nil
-       }
-       return orderPart4CID, nil
-}
-
-// WriteOrderToIPFS writes the order document to IPFS network
-func WriteOrderToIPFS(nodeID string, ipfs ipfs.Connector, store 
*datastore.Store, id string, order *documents.OrderDoc, recipients 
map[string]documents.IDDoc) (ipfsAddress string, err error) { // Get the secret 
keys
-       secrets := &IdentitySecrets{}
-       if err := store.Get("id-doc", nodeID, secrets); err != nil {
-               return "", errors.New("load secrets from store")
-       }
-       blsSecretKey, err := hex.DecodeString(secrets.BLSSecretKey)
-       if err != nil {
-               return "", errors.Wrap(err, "Decode identity secrets")
-       }
-
-       ////Mutex Lock - only one thread at once can write to IPFS as we need 
to keep track of the previous ID and put it into the next doc to create a chain
-       previousIDMutex.Lock()
-       previousIDKey := fmt.Sprintf("previous-id-%s", nodeID)
-       var previousID string
-       if err := store.Get("id-doc", previousIDKey, &previousID); err != nil {
-               previousID = "genesis"
-               if err := store.Set("id-doc", previousIDKey, previousID, nil); 
err != nil {
-                       return "", err
-               }
-       }
-
-       rawDoc, err := documents.EncodeOrderDocument(nodeID, *order, 
blsSecretKey, previousID, recipients)
-       if err != nil {
-               return "", errors.Wrap(err, "Failed to encode IDDocument")
-       }
-       ipfsAddress, err = ipfs.Add(rawDoc)
-       if err != nil {
-               return "", errors.Wrap(err, "Failed to Save Raw Document into 
IPFS")
-       }
-       if err := store.Set("id-doc", previousIDKey, ipfsAddress, nil); err != 
nil {
-               return "", err
-       }
-       previousIDMutex.Unlock()
-
-       //Write order to store
-       //orderRef := fmt.Sprintf("order-ref-%s", order.Reference)
-       if err := WriteOrderToStore(store, order.Reference, ipfsAddress); err 
!= nil {
-               return "", errors.New("Save Order to store")
-       }
-
-       // if err := store.Set("order", order.Reference, ipfsAddress, 
map[string]string{"time": time.Now().UTC().Format(time.RFC3339)}); err != nil {
-       //      return "", errors.New("Save Order to store")
-       // }
-       return ipfsAddress, nil
-}
-
 func WriteOrderToStore(store *datastore.Store, orderReference string, address 
string) error {
        if err := store.Set("order", orderReference, address, 
map[string]string{"time": time.Now().UTC().Format(time.RFC3339)}); err != nil {
                return errors.New("Save Order to store")
@@ -255,20 +160,17 @@ func RetrieveIdentitySecrets(store *datastore.Store, 
nodeID string) (name string
 }
 
 // BuildRecipientList builds a list of recipients who are able to decrypt the 
encrypted envelope
-func BuildRecipientList(ipfs ipfs.Connector, localNodeDocCID, remoteNodeDocCID 
string) (map[string]documents.IDDoc, error) {
-       remoteNodeDoc, err := RetrieveIDDocFromIPFS(ipfs, remoteNodeDocCID)
-       if err != nil {
-               return nil, err
-       }
+func BuildRecipientList(ipfs ipfs.Connector, IDDocs ...string) 
(map[string]documents.IDDoc, error) {
 
-       localNodeDoc, err := RetrieveIDDocFromIPFS(ipfs, localNodeDocCID)
-       if err != nil {
-               return nil, err
-       }
+       recipients := make(map[string]documents.IDDoc)
 
-       recipients := map[string]documents.IDDoc{
-               remoteNodeDocCID: remoteNodeDoc,
-               localNodeDocCID:  localNodeDoc,
+       for _, v := range IDDocs {
+               iddoc, err := RetrieveIDDocFromIPFS(ipfs, v)
+               if err != nil {
+                       return nil, err
+               }
+               recipients[v] = iddoc
        }
+
        return recipients, nil
 }
diff --git a/pkg/defaultservice/fulfill.go b/pkg/defaultservice/fulfill.go
index 27d865b..37748c2 100644
--- a/pkg/defaultservice/fulfill.go
+++ b/pkg/defaultservice/fulfill.go
@@ -29,20 +29,15 @@ import (
 )
 
 func (s *Service) FulfillOrder(tx *api.BlockChainTX) (string, error) {
-
+       nodeID := s.NodeID()
        reqPayload := tx.Payload
        txHashString := hex.EncodeToString(tx.TXhash)
 
-       //Decode the incoming TX
-       //Peek inside the TX
-       //Pull out the header - to get PrincipalID
+       //Get signer by peeking inside the document
        signerID, err := documents.OrderDocumentSigner(reqPayload)
        if err != nil {
                return "", err
        }
-
-       //orderPart1CID := req.OrderPart1CID
-       nodeID := s.NodeID()
        remoteIDDocCID := signerID
 
        _, _, _, sikeSK, err := common.RetrieveIdentitySecrets(s.Store, nodeID)
@@ -55,13 +50,14 @@ func (s *Service) FulfillOrder(tx *api.BlockChainTX) 
(string, error) {
                return "", err
        }
 
-       //Decode the supplied order
+       //Decode the Order from the supplied TX
        order := &documents.OrderDoc{}
        err = documents.DecodeOrderDocument(reqPayload, txHashString, order, 
sikeSK, nodeID, remoteIDDoc.BLSPublicKey)
        if err != nil {
                return "", err
        }
 
+       //Recipient list is principal and self
        recipientList, err := common.BuildRecipientList(s.Ipfs, 
order.PrincipalCID, nodeID)
        if err != nil {
                return "", err
@@ -79,22 +75,15 @@ func (s *Service) FulfillOrder(tx *api.BlockChainTX) 
(string, error) {
                return "", err
        }
 
-       //Create an order part 2
+       //Populate Order part 2
        order.OrderPart2 = &documents.OrderPart2{
                CommitmentPublicKey: commitmentPublicKey,
                PreviousOrderCID:    txHashString,
                Timestamp:           time.Now().Unix(),
        }
 
+       //Create a new Transaction payload and TX
        txHash, payload, err := common.CreateTX(nodeID, s.Store, nodeID, order, 
recipientList)
-       //_ = txHashID
-
-       // orderPart2CID, err := common.CreateAndStoreOrderPart2(s.Ipfs, 
s.Store, order, orderPart1CID, commitmentPublicKey, nodeID, recipientList)
-       // if err != nil {
-       //      return "", err
-       // }
-
-       //marshaledRequest, _ := json.Marshal(response)
 
        //Write the requests to the chain
        chainTX := &api.BlockChainTX{
@@ -111,16 +100,15 @@ func (s *Service) FulfillOrder(tx *api.BlockChainTX) 
(string, error) {
 
 // FulfillOrderSecret -
 func (s *Service) FulfillOrderSecret(tx *api.BlockChainTX) (string, error) {
-       //Initialise values from Request object
+       nodeID := s.NodeID()
        reqPayload := tx.Payload
        txHashString := hex.EncodeToString(tx.TXhash)
 
+       //Get signer by peeking inside the document
        signerID, err := documents.OrderDocumentSigner(reqPayload)
        if err != nil {
                return "", err
        }
-
-       nodeID := s.NodeID()
        remoteIDDocCID := signerID
 
        _, _, _, sikeSK, err := common.RetrieveIdentitySecrets(s.Store, nodeID)
@@ -133,13 +121,14 @@ func (s *Service) FulfillOrderSecret(tx 
*api.BlockChainTX) (string, error) {
                return "", err
        }
 
-       //Decode the supplied order
+       //Decode the Order from the supplied TX
        order := &documents.OrderDoc{}
        err = documents.DecodeOrderDocument(reqPayload, txHashString, order, 
sikeSK, nodeID, remoteIDDoc.BLSPublicKey)
        if err != nil {
                return "", err
        }
 
+       //Recipient list is beneficiary and self
        recipientList, err := common.BuildRecipientList(s.Ipfs, nodeID, 
order.BeneficiaryCID)
        if err != nil {
                return "", err
@@ -157,12 +146,14 @@ func (s *Service) FulfillOrderSecret(tx 
*api.BlockChainTX) (string, error) {
                return "", err
        }
 
+       //Populate Order part 4
        order.OrderPart4 = &documents.OrderPart4{
                Secret:           commitmentPrivateKey,
                PreviousOrderCID: txHashString,
                Timestamp:        time.Now().Unix(),
        }
 
+       //Create a new Transaction payload and TX
        txHash, payload, err := common.CreateTX(nodeID, s.Store, nodeID, order, 
recipientList)
 
        //Write the requests to the chain

Reply via email to