Merge pull request #24 from apache/BeneficiaryFix

Beneficiary fix
diff --git a/cmd/servicetester/e2e_test.sh b/cmd/servicetester/e2e_test.sh
index 1b3588c..c5c2608 100755
--- a/cmd/servicetester/e2e_test.sh
+++ b/cmd/servicetester/e2e_test.sh
@@ -3,6 +3,7 @@
 
 apiVersion="v1"
 
+
 status () {
   #Determine if an extension is running
   statusOutput=$(curl -s -X GET "http://localhost:5556/$apiVersion/status" -H "accept: */*" -H "Content-Type: application/json")
@@ -20,10 +21,10 @@
 ###############################################################################################################################
 
 execute_bitcoin () {
-  #Run 2 Tests against the Bitcoin Extension
-  echo "Bitcoin Plugin Tests [2 Tests]"
+  # #Run 4 Tests against the Bitcoin Extension
+  echo "Bitcoin Plugin Tests [4 Tests]"
   output1=$(curl -s -X POST "http://localhost:5556/$apiVersion/order" -H "accept: */*" -H "Content-Type: application/json" -d "{\"beneficiaryIDDocumentCID\":\"\",\"extension\":{\"coin\":\"0\"}}")
-  echo $output1
+  #echo $output1
   op1=$(echo $output1 | jq .orderReference)
   commitment1=$(echo $output1 | jq .commitment)
   address1=$(echo $output1 | jq .extension.address)
@@ -71,6 +72,67 @@
       echo "Fail"
       exit 1
   fi
+
+
+   #make another BeneficiaryID
+   output5=$(curl -s -X POST "http://localhost:5556/$apiVersion/identity" -H "accept: */*" -H       "Content-Type: application/json" -d "{\"Name\":\"AA\"}")
+   benid=$(echo $output5 | jq -r .idDocumentCID)
+
+
+
+   #Tests against the Bitcoin Extension - different befificary
+   output6=$(curl -s -X POST "http://localhost:5556/$apiVersion/order" -H "accept: */*" -H "Content-Type: application/json" -d "{\"beneficiaryIDDocumentCID\":\"\",\"extension\":{\"coin\":\"0\"}}")
+   #echo $output6
+   op6=$(echo $output6 | jq .orderReference)
+   commitment6=$(echo $output6 | jq .commitment)
+   address6=$(echo $output6 | jq .extension.address)
+
+   output7=$(curl -s -X POST "http://localhost:5556/$apiVersion/order/secret" -H "accept: */*" -H "Content-Type: application/json" -d "{\"orderReference\":$op6,\"beneficiaryIDDocumentCID\":\"$benid\"}")
+   address7=$(echo $output7 | jq .extension.address)
+   commitment7=$(echo $output7 | jq .commitment)
+
+   echo "Committment5 $commitment6 $address6"
+   echo "Committment6 $commitment7 $address7"
+
+   if [ -z $commitment7 ]; then
+       echo "Failed Commitment is empty"
+       exit 1
+   fi
+
+   if [ $commitment6 == $commitment7 ]; then
+     echo "Pass - Id, Order & OrderSecret(Beneficiary)"
+   else
+     echo "Fail"
+     exit 1
+   fi
+
+  output8=$(curl -s -X POST "http://localhost:5556/$apiVersion/order" -H "accept: */*" -H "Content-Type: application/json" -d "{\"beneficiaryIDDocumentCID\":\"$benid\",\"extension\":{\"coin\":\"0\"}}")
+  op8=$(echo $output8 | jq .orderReference)
+  commitment8=$(echo $output8 | jq .commitment)
+  address8=$(echo $output8 | jq .extension.address)
+
+
+  output9=$(curl -s -X POST "http://localhost:5556/$apiVersion/order/secret" -H "accept: */*" -H "Content-Type: application/json" -d "{\"orderReference\":$op8}")
+  commitment9=$(echo $output9 | jq .commitment)
+  address9=$(echo $output9 | jq .extension.address)
+  orderReference=$(echo $output9 | jq .orderReference)
+  orderIndex=1
+
+  echo "Committment7 $commitment8 $address8"
+  echo "Committment8 $commitment9 $address9"
+
+  if [ -z $commitment9 ]; then
+      echo "Failed Commitment is empty"
+      exit 1
+  fi
+
+  if [ $commitment8 == $commitment9 ]; then
+    echo "Pass - Id, Order(Beneficiary) & OrderSecret"
+  else
+      echo "Fail"
+      exit 1
+  fi
+
 }
 
 
@@ -108,7 +170,7 @@
 execute_milagro () {
   echo "Milagro Tests [1 Test]"
   output1=$(curl -s -X POST "http://localhost:5556/$apiVersion/order" -H "accept: */*" -H "Content-Type: application/json" -d "{\"beneficiaryIDDocumentCID\":$identity}")
-  echo $output1  
+  echo $output1
   op1=$(echo $output1 | jq .orderReference)
 
 
diff --git a/pkg/bitcoinplugin/helpers.go b/pkg/bitcoinplugin/helpers.go
index c7b5e20..eb65167 100644
--- a/pkg/bitcoinplugin/helpers.go
+++ b/pkg/bitcoinplugin/helpers.go
@@ -53,10 +53,10 @@
 
 	//we are using the beneficiary specified in the order part 1
 	privateKeyPart2of2 := order.OrderDocument.OrderPart4.Secret
-	if order.OrderDocument.BeneficiaryCID != nodeID {
-		//need to forward this data to the beneficiary to complete redemption
-		return "", errors.New("Currently beneficiary must be the same as the Principal")
-	}
+	// if order.OrderDocument.BeneficiaryCID != nodeID {
+	// 	//need to forward this data to the beneficiary to complete redemption
+	// 	return "", errors.New("Currently beneficiary must be the same as the Principal")
+	// }
 	//restore the Seed
 	_, _, ecAddPrivateKey, err := cryptowallet.Bip44Address(beneficiariesSeed, cryptowallet.CoinTypeBitcoinMain, 0, 0, 0)
 	if err != nil {
diff --git a/pkg/defaultservice/order.go b/pkg/defaultservice/order.go
index d712618..4326ba0 100644
--- a/pkg/defaultservice/order.go
+++ b/pkg/defaultservice/order.go
@@ -208,7 +208,7 @@
 		return nil, err
 	}
 
-	_, seed, blsSK, sikeSK, err := common.RetrieveIdentitySecrets(s.Store, nodeID)
+	_, _, blsSK, sikeSK, err := common.RetrieveIdentitySecrets(s.Store, nodeID)
 	if err != nil {
 		return nil, err
 	}
@@ -219,6 +219,20 @@
 		return nil, errors.Wrap(err, "Fail to retrieve Order from IPFS")
 	}
 
+	var beneficiariesSikeSK []byte
+	var beneficiaryCID string
+
+	if req.BeneficiaryIDDocumentCID != "" {
+		beneficiaryCID = req.BeneficiaryIDDocumentCID
+	} else {
+		beneficiaryCID = order.BeneficiaryCID
+	}
+
+	_, beneficiariesSeed, _, beneficiariesSikeSK, err := common.RetrieveIdentitySecrets(s.Store, beneficiaryCID)
+	if err != nil {
+		return nil, err
+	}
+
 	if err := s.Plugin.ValidateOrderSecretRequest(req, *order); err != nil {
 		return nil, err
 	}
@@ -253,7 +267,7 @@
 		return nil, err
 	}
 
-	finalPrivateKey, finalPublicKey, ext, err := s.Plugin.ProduceFinalSecret(seed, sikeSK, order, orderPart4, req, response)
+	finalPrivateKey, finalPublicKey, ext, err := s.Plugin.ProduceFinalSecret(beneficiariesSeed, beneficiariesSikeSK, order, orderPart4, req, response)
 	if err != nil {
 		return nil, err
 	}