Skip to content

Commit

Permalink
Merge pull request #360 from hypersign-protocol/develop
Browse files Browse the repository at this point in the history
v0.1.8-rc.2: Release
  • Loading branch information
arnabghose997 committed Jun 29, 2023
2 parents eb47d48 + bb5fe7e commit fad6330
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 69 deletions.
173 changes: 106 additions & 67 deletions tests/e2e/ssi_tests/e2e_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,104 @@
query_did, form_create_schema_tx, form_did_deactivate_tx_multisig, form_create_cred_status_tx
from constants import DEFAULT_BLOCKCHAIN_ACCOUNT_NAME

def unique_wallet_address_test():
print("\n---1. FAIL: Alice Creates a DID Doc. Bob attempts to create a DID Document by adding one of Alice's VM.---\n")

kp_alice = generate_key_pair("recover-eth")
signers = []
did_doc_string = generate_did_document(kp_alice, "recover-eth")
did_doc_alice = did_doc_string["id"]
did_doc_alice_vm = did_doc_string["verificationMethod"][0]
signPair_alice = {
"kp": kp_alice,
"verificationMethodId": did_doc_string["verificationMethod"][0]["id"],
"signing_algo": "recover-eth"
}
signers.append(signPair_alice)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering Alice's DID with Id: {did_doc_alice}")

# Create Bob's DID with Alice's VM
kp_bob = generate_key_pair("recover-eth")
signers = []
did_doc_string = generate_did_document(kp_bob, "recover-eth")
did_doc_bob = did_doc_string["id"]
did_doc_string["controller"] = [did_doc_alice]
did_doc_string["verificationMethod"] = [did_doc_alice_vm]
signers.append(signPair_alice)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering Bob's DID with Id: {did_doc_bob}", True)

print("\n---2. FAIL: Charlie creates a DID Document. After that, Charlie attempts to update its DID Document by adding one of Alice's VM for which valid signature is passed.---\n")

kp_charlie = generate_key_pair("recover-eth")
signers = []
did_doc_string_charlie = generate_did_document(kp_charlie, "recover-eth")
did_doc_charlie_id = did_doc_string_charlie["id"]
did_doc_charlie_vm = did_doc_string_charlie["verificationMethod"][0]
signPair_charlie = {
"kp": kp_charlie,
"verificationMethodId": did_doc_string_charlie["verificationMethod"][0]["id"],
"signing_algo": "recover-eth"
}
signers.append(signPair_charlie)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string_charlie, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering Charlie's DID with Id: {did_doc_charlie_id}")

signers = []
did_doc_string_charlie["verificationMethod"] = [did_doc_charlie_vm, did_doc_alice_vm]
signers.append(signPair_charlie)
signers.append(signPair_alice)
update_tx_cmd = form_did_update_tx_multisig(did_doc_string_charlie, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Charlie (controller) attempts to add Alice's VM in its DID Id: {did_doc_charlie_id}", True)

print("\n---3. PASS: Alice deactivates her DID Document. Charlie attempts to update its DID Document by adding one of Alice's VM for which valid signature is passed.---\n")

# Alice Deactivates their DID Document
signers = []
signers.append(signPair_alice)
deactivate_tx_cmd = form_did_deactivate_tx_multisig(did_doc_alice, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(deactivate_tx_cmd, f"Deactivation of Org's DID with Id: {did_doc_alice}")

# Charlie attempts to add the VM again
signers = []
signers.append(signPair_charlie)
signers.append(signPair_alice)
run_blockchain_command(update_tx_cmd, f"Charlie (controller) attempts to add Alice's VM in DID Id: {did_doc_charlie_id}")

print("\n---4. PASS: Charlie removes one of its Verification Methods. George creates a didDoc for himself. He then proceed to update his DID Document by adding the Verification method removed by Charlie---\n")

# Create George's DIDDoc
kp_george = generate_key_pair("recover-eth")
signers = []
did_doc_string_george = generate_did_document(kp_george, "recover-eth")
did_doc_george_id = did_doc_string_george["id"]
did_doc_george_vm = did_doc_string_george["verificationMethod"][0]
signPair_george = {
"kp": kp_george,
"verificationMethodId": did_doc_string_george["verificationMethod"][0]["id"],
"signing_algo": "recover-eth"
}
signers.append(signPair_george)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string_george, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering George's DID with Id: {did_doc_george_id}")

# Charlie Removes one of their DIDDoc
signers = []
charlie_vm_to_be_removed = did_doc_string_charlie["verificationMethod"][0] # Controller is Charlie
signers.append(signPair_charlie)
did_doc_string_charlie["verificationMethod"] = [did_doc_string_charlie["verificationMethod"][1]] # Controller for this VM is alice
update_tx_cmd = form_did_update_tx_multisig(did_doc_string_charlie, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Charlie (controller) attempts to remove one of its VM from its DID: {did_doc_charlie_id}")

# George updates it DID Document by adding the VM that Charlie Removed
signers = []
did_doc_string_george["verificationMethod"] = [charlie_vm_to_be_removed, did_doc_george_vm]
signers.append(signPair_charlie)
signers.append(signPair_george)
update_tx_cmd = form_did_update_tx_multisig(did_doc_string_george, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"George attempts add the VM removed by Charlie: {did_doc_george_id}")

# TC - I : Create DID scenarios
def create_did_test():
print("\n--- Create DID Test ---\n")
Expand Down Expand Up @@ -109,15 +207,15 @@ def create_did_test():

# Alice creates a DID where they keep the VM of their friend Eve in the verificationMethod list of the document
print("6. FAIL: Alice creates an Org DID where Alice is the controller, and she adds a verification method of her friend Eve. Only Alice sends the singature.\n")
kp_eve = generate_key_pair("secp256k1")
did_doc_string = generate_did_document(kp_eve, algo="secp256k1")
kp_eve = generate_key_pair("ed25519")
did_doc_string = generate_did_document(kp_eve, algo="ed25519")
did_doc_eve = did_doc_string["id"]
did_doc_eve_vms = did_doc_string["verificationMethod"]
signers = []
signPair_eve = {
"kp": kp_eve,
"verificationMethodId": did_doc_string["verificationMethod"][0]["id"],
"signing_algo": "secp256k1"
"signing_algo": "ed25519"
}
signers.append(signPair_eve)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
Expand Down Expand Up @@ -280,7 +378,6 @@ def update_did_test():
update_tx_cmd = form_did_update_tx_multisig(did_doc_string_org, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Removal of George's controller by Alice's signature")


# Addition of George's controller and removal of Alice's controller at same time
print("7. PASS: Addition of George as a controller and simultaneous removal of Alice as a controller. Both alice's and george's signature are passed.\n")
signers = []
Expand All @@ -290,73 +387,15 @@ def update_did_test():
update_tx_cmd = form_did_update_tx_multisig(did_doc_string_org, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Removal of Alice's controller and addition of Bob's controller")

#Alice creates a DID where the controller only has Alice's DID and the verfication method has two ETH wallets added. Signature of all hot wallets are passed
print("8. FAIL: Alice has already created two didDocs, each representing a wallet. Now, she creates a DID where she is the controller, and the VMs from two DIDDocs are just added in the VM List. Each of these VMs have different controllers. One of the VMs attempts to update DIDDoc.\n")
kp_hot_wallet_1 = generate_key_pair("recover-eth")
signers = []
did_doc_string = generate_did_document(kp_hot_wallet_1, algo="recover-eth")
did_doc_hw1 = did_doc_string["id"]
did_doc_hw1_vm = did_doc_string["verificationMethod"][0]
signPair_hw1 = {
"kp": kp_hot_wallet_1,
"verificationMethodId": did_doc_hw1_vm["id"],
"signing_algo": "recover-eth"
}
signers.append(signPair_hw1)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering Hot Wallet 1 with Id: {did_doc_hw1}")

kp_hot_wallet_2 = generate_key_pair("recover-eth")
signers = []
did_doc_string = generate_did_document(kp_hot_wallet_2, algo="recover-eth")
did_doc_hw2 = did_doc_string["id"]
did_doc_hw2_vm = did_doc_string["verificationMethod"][0]
signPair_hw2 = {
"kp": kp_hot_wallet_2,
"verificationMethodId": did_doc_hw2_vm["id"],
"signing_algo": "recover-eth"
}
signers.append(signPair_hw2)
create_tx_cmd = form_did_create_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_tx_cmd, f"Registering Hot Wallet 2 with Id: {did_doc_hw2}")

kp_org = generate_key_pair()
signers = []
did_doc_string = generate_did_document(kp_org)
did_doc_org = did_doc_string["id"]
did_doc_string["controller"] = [did_doc_alice]
did_doc_string["verificationMethod"] = [
did_doc_hw1_vm,
did_doc_hw2_vm
]
signers.append(signPair_alice)
signers.append(signPair_hw1)
signers.append(signPair_hw2)
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 Org DID with Id: {did_doc_org}")

print("Hot-Wallet 1 attemps to update Org DID. It is expected to fail")
did_doc_string["context"] = ["exempler.org"]
signers = []
signers.append(signPair_hw1)
update_tx_cmd = form_did_update_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Hot Wallet 1 attempts to update Tx", True)

print("9. PASS: Alice has already created two didDocs, each representing a wallet. Now, she creates a DID where she is the controller, and the VMs from two DIDDocs are just added in the VM List. Each of these VMs have different controllers. Alice attempts to update DIDDoc.\n")
signers = []
signers.append(signPair_alice)
update_tx_cmd = form_did_update_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Alice (controller) attempts to update Tx")

print("10. FAIL: Alice tries to update her DID Document without changing anything\n")
print("8. FAIL: Alice tries to update her DID Document without changing anything\n")
did_doc_string = query_did(did_doc_alice)["didDocument"]
signers = []
signers.append(signPair_alice)
update_tx_cmd = form_did_update_tx_multisig(did_doc_string, signers, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(update_tx_cmd, f"Alice attempts update without any change Tx", True)

# Register Alice's DID
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")
print("9. 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()
signers = []
did_doc_string = generate_did_document(kp_jenny)
Expand All @@ -378,7 +417,7 @@ 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. FAIL: Jenny creates a DID. She then attempts to update the DIDDoc by adding a new Verification method. She passes signature only for old VM\n")
print("10. FAIL: Jenny creates a DID. She then attempts to update the DIDDoc by adding a new Verification method. She passes signature only for old VM\n")
kp_jenny = generate_key_pair()
kp_jenny_2 = generate_key_pair()

Expand Down Expand Up @@ -420,15 +459,15 @@ 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 attempts to update the DIDDoc with only old VM's signature", True)

print("13. PASS: Jenny attempts to update the same didDoc by passing signatures for both old and new verification methods\n")
print("11. PASS: Jenny attempts to update the same didDoc by passing signatures for both old and new verification methods\n")

signers = []
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 attempts to update the DIDDoc with both new and old VM's signature")

print("14. PASS: Jenny removes the inital verification method she had added. She passes only one signature corresponding to the lastest VM")
print("12. PASS: Jenny removes the inital verification method she had added. She passes only one signature corresponding to the lastest VM")

did_doc_string["verificationMethod"] = [
new_vm,
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/ssi_tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def run_all_tests():
caip10_cosmos_support_test()
vm_type_test()
method_specific_id_test()

unique_wallet_address_test()

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

if __name__=='__main__':
Expand Down
18 changes: 18 additions & 0 deletions x/ssi/keeper/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,21 @@ func (k Keeper) RegisterDidDocumentInStore(ctx sdk.Context, didDoc *types.DidDoc
k.SetDidCount(ctx, didCount+1)
return didCount
}

// Set the BlockchainAccountId in Store
func (k Keeper) SetBlockchainAddressInStore(ctx *sdk.Context, blockchainAccountId string, didId string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.BlockchainAccountIdStoreKey))
store.Set([]byte(blockchainAccountId), []byte(didId))
}

// Get the BlockchainAccountId from Store
func (k Keeper) GetBlockchainAddressFromStore(ctx *sdk.Context, blockchainAccountId string) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.BlockchainAccountIdStoreKey))
return store.Get([]byte(blockchainAccountId))
}

// Remove the BlockchainAccountId from Store
func (k Keeper) RemoveBlockchainAddressInStore(ctx *sdk.Context, blockchainAccountId string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.BlockchainAccountIdStoreKey))
store.Delete([]byte(blockchainAccountId))
}
23 changes: 23 additions & 0 deletions x/ssi/keeper/msg_server_create_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t
return nil, sdkerrors.Wrap(types.ErrDidDocExists, msgDidDocument.Id)
}

// Check if any of the blockchainAccountId is present in any registered DID Document. If so, throw error
for _, vm := range msgDidDocument.VerificationMethod {
if vm.BlockchainAccountId != "" {
if existingDidDocId := k.GetBlockchainAddressFromStore(&ctx, vm.BlockchainAccountId); len(existingDidDocId) != 0 {
return nil, sdkerrors.Wrapf(
types.ErrInvalidDidDoc,
"blockchainAccountId %v of verification method %v is already part of DID Document %v",
vm.BlockchainAccountId,
vm.Id,
string(existingDidDocId),
)
}
}
}

// Get the list of controllers and check the non-subject controller's existance in the state
controllerList := getControllersForCreateDID(msgDidDocument)

Expand Down Expand Up @@ -79,6 +94,14 @@ func (k msgServer) CreateDID(goCtx context.Context, msg *types.MsgCreateDID) (*t
// Register DID Document in Store once all validation checks are passed
id := k.RegisterDidDocumentInStore(ctx, &didDocumentState)

// After successful registration of the DID Document, every blockchainAccountIds
// can be added to the store
for _, vm := range didDocumentState.DidDocument.VerificationMethod {
if vm.BlockchainAccountId != "" {
k.SetBlockchainAddressInStore(&ctx, vm.BlockchainAccountId, vm.Controller)
}
}

return &types.MsgCreateDIDResponse{Id: id}, nil
}

Expand Down
7 changes: 7 additions & 0 deletions x/ssi/keeper/msg_server_deactivate_did.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func (k msgServer) DeactivateDID(goCtx context.Context, msg *types.MsgDeactivate
return nil, err
}

// Remove the BlockchainAccountId from BlockchainAddressStore
for _, vm := range didDocumentState.DidDocument.VerificationMethod {
if vm.BlockchainAccountId != "" {
k.RemoveBlockchainAddressInStore(&ctx, vm.BlockchainAccountId)
}
}

return &types.MsgDeactivateDIDResponse{Id: 1}, nil
}

Expand Down

0 comments on commit fad6330

Please sign in to comment.