Skip to content

Commit

Permalink
fixed update did rpc where users were not able to add a verification …
Browse files Browse the repository at this point in the history
…method in their didDoc
  • Loading branch information
arnabghose997 committed Mar 18, 2023
1 parent 1781ab2 commit 7981696
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 24 deletions.
90 changes: 90 additions & 0 deletions tests/e2e/ssi_tests/e2e_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,52 @@ def create_did_test():

print("--- Test Completed ---\n")


def run_something():
print("11. PASS: Jenny creates herself a DID with empty Controller list. She then attempts to update the DIDDoc by changing the context field and passes her signature only.\n")
kp_jenny = generate_key_pair()
kp_alice = generate_key_pair()

signers = []
did_doc_string = generate_did_document(kp_jenny)
did_doc_string["controller"] = []
did_doc_jenny = did_doc_string["id"]
did_doc_jenny_vm = did_doc_string["verificationMethod"][0]
signPair_jenny = {
"kp": kp_jenny,
"verificationMethodId": did_doc_jenny_vm["id"],
"signing_algo": "ed25519"
}
signers.append(signPair_jenny)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering of Jenny's DID with Id: {did_doc_jenny}")

signers = []
did_doc_string_alice = generate_did_document(kp_alice)
new_vm = did_doc_string_alice["verificationMethod"][0]
new_vm_id = did_doc_string["verificationMethod"][0]["id"] + "news"
new_vm["id"] = new_vm_id
new_vm["controller"] = did_doc_string["id"]

did_doc_string["verificationMethod"] = [
did_doc_string["verificationMethod"][0],
new_vm,
]
signPair_jenny_1 = {
"kp": kp_jenny,
"verificationMethodId": did_doc_jenny_vm["id"],
"signing_algo": "ed25519"
}
signPair_jenny_2 = {
"kp": kp_alice,
"verificationMethodId": new_vm_id,
"signing_algo": "ed25519"
}
signers.append(signPair_jenny_1)
signers.append(signPair_jenny_2)
update_tx_cmd = form_did_update_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Jenny (controller) attempts to update Tx")

# TC - II : Update DID scenarios
def update_did_test():
print("\n--- Update DID Test ---\n")
Expand Down Expand Up @@ -353,6 +399,50 @@ def update_did_test():
update_tx_cmd = form_did_update_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Jenny (controller) attempts to update Tx")

print("12. PASS: Jenny creates herself a DID with empty Controller list. She then attempts to update the DIDDoc by adding a new Verification method. She passes signatures for both new and old VMs\n")
kp_jenny = generate_key_pair()
kp_jenny_2 = generate_key_pair()

signers = []
did_doc_string = generate_did_document(kp_jenny)
did_doc_string["controller"] = []
did_doc_jenny = did_doc_string["id"]
did_doc_jenny_vm = did_doc_string["verificationMethod"][0]
signPair_jenny = {
"kp": kp_jenny,
"verificationMethodId": did_doc_jenny_vm["id"],
"signing_algo": "ed25519"
}
signers.append(signPair_jenny)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering of Jenny's DID with Id: {did_doc_jenny}")

signers = []
did_doc_string_alice = generate_did_document(kp_jenny_2)
new_vm = did_doc_string_alice["verificationMethod"][0]
new_vm_id = did_doc_string["verificationMethod"][0]["id"] + "news"
new_vm["id"] = new_vm_id
new_vm["controller"] = did_doc_string["id"]

did_doc_string["verificationMethod"] = [
did_doc_string["verificationMethod"][0],
new_vm,
]
signPair_jenny_1 = {
"kp": kp_jenny,
"verificationMethodId": did_doc_jenny_vm["id"],
"signing_algo": "ed25519"
}
signPair_jenny_2 = {
"kp": kp_jenny_2,
"verificationMethodId": new_vm_id,
"signing_algo": "ed25519"
}
signers.append(signPair_jenny_1)
signers.append(signPair_jenny_2)
update_tx_cmd = form_did_update_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Jenny (controller) attempts to update Tx")

print("--- Test Completed ---\n")

def deactivate_did():
Expand Down
15 changes: 8 additions & 7 deletions tests/e2e/ssi_tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ def generate_report(func):
def run_all_tests():
print("============= 🔧️ Running all x/ssi e2e tests ============== \n")

create_did_test()
# create_did_test()
update_did_test()
schema_test()
deactivate_did()
credential_status_test()
caip10_ethereum_support_test()
caip10_cosmos_support_test()
vm_type_test()
# schema_test()
# deactivate_did()
# credential_status_test()
# caip10_ethereum_support_test()
# caip10_cosmos_support_test()
# vm_type_test()
#run_something()

print("============= 😃️ All test cases completed successfully ============== \n")

Expand Down
2 changes: 2 additions & 0 deletions x/ssi/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (k msgServer) formMustControllerVmListMap(ctx sdk.Context,
if presentInControllerMap {
vmExtended := types.CreateExtendedVerificationMethod(vmMap[vmId], sign)
controllerMap[controller] = append(controllerMap[controller], vmExtended)
delete(inputSignMap, vmId)
}
// Check for VM from the respective controller's DID Doc
} else {
Expand All @@ -99,6 +100,7 @@ func (k msgServer) formMustControllerVmListMap(ctx sdk.Context,
if presentInControllerMap {
vmExtended := types.CreateExtendedVerificationMethod(vmState, sign)
controllerMap[controller] = append(controllerMap[controller], vmExtended)
delete(inputSignMap, vmId)
}
}
}
Expand Down
79 changes: 62 additions & 17 deletions x/ssi/keeper/msg_server_update_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,74 @@ func (k msgServer) UpdateDID(goCtx context.Context, msg *types.MsgUpdateDID) (*t
return nil, sdkerrors.Wrap(types.ErrUnexpectedDidVersion, errMsg)
}

// Look for any change in Controller array
mandatoryControllers, anyControllers := getControllersForUpdateDID(existingDidDocument, msgDidDocument)
if err := k.checkControllerPresenceInState(ctx, mandatoryControllers, msgDidDocument.Id); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
signMap := makeSignatureMap(msgSignatures)

// Check if there is any change in controllers
var optionalVmMap map[string][]*types.ExtendedVerificationMethod = map[string][]*types.ExtendedVerificationMethod{}
var requiredVmMap map[string][]*types.ExtendedVerificationMethod = map[string][]*types.ExtendedVerificationMethod{}
var vmMapErr error

existingDidDocumentControllers := existingDidDocument.Controller
incomingDidDocumentControllers := msgDidDocument.Controller

if len(existingDidDocumentControllers) == 0 {
existingDidDocumentControllers = append(existingDidDocumentControllers, existingDidDocument.Id)
}
if err := k.checkControllerPresenceInState(ctx, anyControllers, msgDidDocument.Id); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
if len(incomingDidDocumentControllers) == 0 {
incomingDidDocumentControllers = append(incomingDidDocumentControllers, msgDidDocument.Id)
}

// Gather Verification Methods
updatedVms := getVerificationMethodsForUpdateDID(existingDidDocument.VerificationMethod, msgDidDocument.VerificationMethod)
// check if both controller arrays are equal
if reflect.DeepEqual(existingDidDocumentControllers, incomingDidDocumentControllers) {
commonController := existingDidDocumentControllers
if err := k.checkControllerPresenceInState(ctx, commonController, msgDidDocument.Id); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

signMap := makeSignatureMap(msgSignatures)
// Check if verification Methods are similar
if reflect.DeepEqual(existingDidDocument.VerificationMethod, msgDidDocument.VerificationMethod) {
optionalVmMap, vmMapErr = k.formAnyControllerVmListMap(ctx, commonController, existingDidDocument.VerificationMethod, signMap)
if vmMapErr != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, vmMapErr.Error())
}
} else {
// if Vms are not similar
// Get the distinct VMs (new)
updatedVms := getVerificationMethodsForUpdateDID(existingDidDocument.VerificationMethod, msgDidDocument.VerificationMethod)

for _, vm := range updatedVms {
vmExtended := types.CreateExtendedVerificationMethod(vm, signMap[vm.Id])
requiredVmMap[vm.Controller] = append(requiredVmMap[vm.Controller], vmExtended)
delete(signMap, vm.Id)
}

requiredVmMap, err := k.formMustControllerVmListMap(ctx, mandatoryControllers, updatedVms, signMap)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}
optionalVmMap, vmMapErr = k.formAnyControllerVmListMap(ctx, commonController, existingDidDocument.VerificationMethod, signMap)
if err != vmMapErr {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, vmMapErr.Error())
}
}
} else {
// Look for any change in Controller array
mandatoryControllers, anyControllers := getControllersForUpdateDID(existingDidDocument, msgDidDocument)
if err := k.checkControllerPresenceInState(ctx, mandatoryControllers, msgDidDocument.Id); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}
if err := k.checkControllerPresenceInState(ctx, anyControllers, msgDidDocument.Id); err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
}

optionalVmMap, err := k.formAnyControllerVmListMap(ctx,
anyControllers, existingDidDocument.VerificationMethod, signMap)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, err.Error())
// Gather Verification Methods
updatedVms := getVerificationMethodsForUpdateDID(existingDidDocument.VerificationMethod, msgDidDocument.VerificationMethod)

requiredVmMap, vmMapErr = k.formMustControllerVmListMap(ctx, mandatoryControllers, updatedVms, signMap)
if vmMapErr != nil {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, vmMapErr.Error())
}

optionalVmMap, vmMapErr = k.formAnyControllerVmListMap(ctx, anyControllers, existingDidDocument.VerificationMethod, signMap)
if err != vmMapErr {
return nil, sdkerrors.Wrap(types.ErrInvalidDidDoc, vmMapErr.Error())
}
}

// Signature Verification
Expand Down

0 comments on commit 7981696

Please sign in to comment.