Skip to content

Commit

Permalink
Merge pull request #325 from hypersign-protocol/324-bug-did-with-mult…
Browse files Browse the repository at this point in the history
…iple-controllers-except-itself-was-not-be-register-schmema-or-credential-documents

Bugfix: fixed `DocumentProofTypeCheck()` function
  • Loading branch information
arnabghose997 committed Dec 13, 2022
2 parents 66aabca + 1d465e4 commit cd3789d
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 42 deletions.
59 changes: 58 additions & 1 deletion tests/e2e/ssi_tests/e2e_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def simple_ssi_flow():
print("\n------ Test Completed ---------\n")

def controller_creates_schema_cred_status():
print("--- Test: Schema and Credential Status document registration by Controllers\n")
print("--- Test: Schema and Credential Status document registration by a single controller\n")
print("In this workflow, a DID document registered with another DID Id in its controller group. The controller is expected to register schema and credential status")

print("Registering DID for an Employee")
Expand Down Expand Up @@ -288,6 +288,63 @@ def controller_creates_schema_cred_status():
run_blockchain_command(register_cred_status_cmd, f"Registering credential status with Id: {cred_id} and {cred_author} being the author")
print("\n------ Test Completed ---------\n")

def controllers_create_schema_cred_status():
print("--- Test: Schema and Credential Status document registration by multiple controllers\n")
print("In this workflow, a DID document registered with mutiple DIDs in its controller group. The controllers are expected to register schema and credential status")

print("Registering DID for an Employee 1")
employee_kp_1 = generate_key_pair()
employee_did_1 = generate_did_document(employee_kp_1)
employee_did_1_id = employee_did_1["id"]
create_employee_did_tx_1 = form_did_create_tx(employee_did_1, employee_kp_1, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_employee_did_tx_1, f"Registering Employee DID Document with ID {employee_did_1_id}")

print("Registering DID for an Employee 2")
employee_kp_2 = generate_key_pair()
employee_did_2 = generate_did_document(employee_kp_2)
employee_did_2_id = employee_did_2["id"]
create_employee_did_tx_2 = form_did_create_tx(employee_did_2, employee_kp_2, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME)
run_blockchain_command(create_employee_did_tx_2, f"Registering Employee DID Document with ID {employee_did_2_id}")

print("Registering DID for an Organization")
org_kp = generate_key_pair()
org_did = generate_did_document(org_kp)
org_did["controller"] = [employee_did_1_id, employee_did_2_id]
org_did_id = org_did["id"]
create_org_did_tx = form_did_create_tx(org_did, employee_kp_2, DEFAULT_BLOCKCHAIN_ACCOUNT_NAME, employee_did_2["authentication"][0])
run_blockchain_command(create_org_did_tx, f"Registering Organisation DID Document with ID {org_did_id}")

print("Employee registering a Schema on behalf of Organization's DID")
schema_doc, schema_proof = generate_schema_document(
employee_kp_2,
org_did_id,
employee_did_2["authentication"][0]
)
create_schema_cmd = form_create_schema_tx(
schema_doc,
schema_proof,
DEFAULT_BLOCKCHAIN_ACCOUNT_NAME
)
schema_doc_id = schema_doc["id"]
schema_author = schema_doc["author"]
run_blockchain_command(create_schema_cmd, f"Registering Schema with Id: {schema_doc_id} with {schema_author} being the author")

print("Employee registering a Credential Status Document on behalf of Organization's DID")
cred_doc, cred_proof = generate_cred_status_document(
employee_kp_1,
org_did_id,
employee_did_1["authentication"][0]
)
register_cred_status_cmd = form_create_cred_status_tx(
cred_doc,
cred_proof,
DEFAULT_BLOCKCHAIN_ACCOUNT_NAME
)
cred_id = cred_doc["claim"]["id"]
cred_author = cred_doc["issuer"]
run_blockchain_command(register_cred_status_cmd, f"Registering credential status with Id: {cred_id} and {cred_author} being the author")
print("\n------ Test Completed ---------\n")

def invalid_case_controller_creates_schema_cred_status():
print("--- Test: Invalid Schema and Credential Status document registration by Non Controllers\n")
print("In this workflow, a DID document registered with another DID Id in its controller group. In this case, if the canon DID tries to create Schema or Credential Document, it should fail.\n")
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/ssi_tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def run_all_tests():

simple_ssi_flow()
controller_creates_schema_cred_status()
controllers_create_schema_cred_status()
invalid_case_controller_creates_schema_cred_status()
non_controller_did_trying_to_update_diddoc()
controller_did_trying_to_update_diddoc()
Expand Down
42 changes: 27 additions & 15 deletions x/ssi/tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func GetModifiedDidDocumentSignature(modifiedDidDocument *types.Did, keyPair ed2
}
}

func GenerateDidDocumentRPCElements(keyPair GenericKeyPair) DidRpcElements {
func GenerateDidDocumentRPCElements(keyPair GenericKeyPair, signingElements []DidSigningElements) DidRpcElements {
publicKey, optionalID := GetPublicKeyAndOptionalID(keyPair)
var didId string
if optionalID == "" {
Expand Down Expand Up @@ -97,12 +97,29 @@ func GenerateDidDocumentRPCElements(keyPair GenericKeyPair) DidRpcElements {
ServiceEndpoint: "http://www.example.com",
}

var controllers []string
if len(signingElements) > 0 {
for i := 0; i < len(signingElements); i++ {
controllers = append(
controllers,
stripDidFromVerificationMethod(signingElements[i].vmId))
}
} else {
signingElements = []DidSigningElements{
DidSigningElements{
keyPair: keyPair,
vmId: vm.Id,
},
}
controllers = []string{didId}
}

var didDocument *types.Did = &types.Did{
Context: []string{
"https://www.w3.org/ns/did/v1",
},
Id: didId,
Controller: []string{didId},
Controller: controllers,
VerificationMethod: []*types.VerificationMethod{
vm,
},
Expand All @@ -113,12 +130,7 @@ func GenerateDidDocumentRPCElements(keyPair GenericKeyPair) DidRpcElements {
AssertionMethod: []string{verificationMethodId},
}

signingElements := []DidSigningElements{
DidSigningElements{
keyPair: keyPair,
vmId: vm.Id,
},
}


var signInfo []*types.SignInfo = getDidSigningInfo(didDocument, signingElements)

Expand All @@ -129,13 +141,13 @@ func GenerateDidDocumentRPCElements(keyPair GenericKeyPair) DidRpcElements {
}
}

func GenerateSchemaDocumentRPCElements(keyPair GenericKeyPair, Id string, verficationMethodId string) SchemaRpcElements {
var schemaId string = "sch:" + DidMethod + ":" + "devnet" + ":" + strings.Split(Id, ":")[3] + ":1.0"
func GenerateSchemaDocumentRPCElements(keyPair GenericKeyPair, authorId string, verficationMethodId string) SchemaRpcElements {
var schemaId string = "sch:" + DidMethod + ":" + "devnet" + ":" + strings.Split(authorId, ":")[3] + ":1.0"
var schemaDocument *types.SchemaDocument = &types.SchemaDocument{
Type: "https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json",
ModelVersion: "v1.0",
Name: "HS Credential",
Author: Id,
Author: authorId,
Id: schemaId,
Authored: "2022-04-10T04:07:12Z",
Schema: &types.SchemaProperty{
Expand Down Expand Up @@ -175,16 +187,16 @@ func GenerateSchemaDocumentRPCElements(keyPair GenericKeyPair, Id string, verfic
}
}

func GenerateCredStatusRPCElements(keyPair GenericKeyPair, Id string, verficationMethod *types.VerificationMethod) CredRpcElements {
var credentialId = "vc:" + DidMethod + ":" + "devnet:" + strings.Split(Id, ":")[3]
func GenerateCredStatusRPCElements(keyPair GenericKeyPair, issuerId string, verficationMethod *types.VerificationMethod) CredRpcElements {
var credentialId = "vc:" + DidMethod + ":" + "devnet:" + strings.Split(issuerId, ":")[3]
var credHash = sha256.Sum256([]byte("Hash1234"))
var credentialStatus *types.CredentialStatus = &types.CredentialStatus{
Claim: &types.Claim{
Id: credentialId,
CurrentStatus: "Live",
StatusReason: "Valid",
},
Issuer: Id,
Issuer: issuerId,
IssuanceDate: "2022-04-10T04:07:12Z",
ExpirationDate: "2023-02-22T13:45:55Z",
CredentialHash: hex.EncodeToString(credHash[:]),
Expand Down Expand Up @@ -251,7 +263,7 @@ func GenerateSecp256k1KeyPair() secp256k1KeyPair {
}

func CreateDidTx(msgServer types.MsgServer, ctx context.Context, keyPair ed25519KeyPair) (string, error) {
rpcElements := GenerateDidDocumentRPCElements(keyPair)
rpcElements := GenerateDidDocumentRPCElements(keyPair, []DidSigningElements{})

msgCreateDID := &types.MsgCreateDID{
DidDocString: rpcElements.DidDocument,
Expand Down
4 changes: 2 additions & 2 deletions x/ssi/tests/query_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestQueryCredential(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateEd25519KeyPair()
didRpcElements := GenerateDidDocumentRPCElements(keyPair1)
didRpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
didId := didRpcElements.DidDocument.GetId()
t.Logf("Registering DID with DID Id: %s", didId)

Expand Down Expand Up @@ -92,7 +92,7 @@ func TestQueryCredentials(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateEd25519KeyPair()
didRpcElements := GenerateDidDocumentRPCElements(keyPair1)
didRpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
didId := didRpcElements.DidDocument.GetId()
t.Logf("Registering DID with DID Id: %s", didId)

Expand Down
4 changes: 2 additions & 2 deletions x/ssi/tests/query_did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestQueryDidDocument(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateEd25519KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
didId := rpcElements.DidDocument.GetId()
t.Logf("Registering DID with DID Id: %s", didId)

Expand Down Expand Up @@ -64,7 +64,7 @@ func TestQueryDidDocuments(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateEd25519KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
didId := rpcElements.DidDocument.GetId()
t.Logf("Registering DID with DID Id: %s", didId)

Expand Down
4 changes: 2 additions & 2 deletions x/ssi/tests/query_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestQuerySchema(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateEd25519KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
didId := rpcElements.DidDocument.GetId()
t.Logf("Registering DID with DID Id: %s", didId)

Expand Down Expand Up @@ -88,7 +88,7 @@ func TestQuerySchemas(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateEd25519KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
didId := rpcElements.DidDocument.GetId()
t.Logf("Registering DID with DID Id: %s", didId)

Expand Down
12 changes: 6 additions & 6 deletions x/ssi/tests/tx_create_did_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestCreateDIDUsingEd25519KeyPair(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateEd25519KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1,[]DidSigningElements{})
t.Logf("Registering DID with DID Id: %s", rpcElements.DidDocument.GetId())

msgCreateDID := &types.MsgCreateDID{
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestCreateDIDUsingSecp256k1KeyPair(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateSecp256k1KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})

msgCreateDID := &types.MsgCreateDID{
DidDocString: rpcElements.DidDocument,
Expand All @@ -75,7 +75,7 @@ func TestInvalidServiceType(t *testing.T) {
goCtx := sdk.WrapSDKContext(ctx)

keyPair1 := GenerateEd25519KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
// Set Namespace
k.SetChainNamespace(&ctx, "devnet")

Expand Down Expand Up @@ -134,7 +134,7 @@ func TestCheckValidMethodSpecificId(t *testing.T) {

t.Log("Registering DID Document with Valid Method Specific ID")

rpcElements = GenerateDidDocumentRPCElements(keyPair1)
rpcElements = GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
msgCreateDID = &types.MsgCreateDID{
DidDocString: rpcElements.DidDocument,
Signatures: rpcElements.Signatures,
Expand All @@ -151,7 +151,7 @@ func TestCheckValidMethodSpecificId(t *testing.T) {

t.Logf("Registering DID Document with Invalid Method Specific ID - %s", keyPair2.optionalID)

rpcElements = GenerateDidDocumentRPCElements(keyPair2)
rpcElements = GenerateDidDocumentRPCElements(keyPair2, []DidSigningElements{})
msgCreateDID = &types.MsgCreateDID{
DidDocString: rpcElements.DidDocument,
Signatures: rpcElements.Signatures,
Expand All @@ -169,7 +169,7 @@ func TestCheckValidMethodSpecificId(t *testing.T) {

t.Logf("Registering DID Document with Invalid Method Specific ID - %s", keyPair3.optionalID)

rpcElements = GenerateDidDocumentRPCElements(keyPair3)
rpcElements = GenerateDidDocumentRPCElements(keyPair3, []DidSigningElements{})
msgCreateDID = &types.MsgCreateDID{
DidDocString: rpcElements.DidDocument,
Signatures: rpcElements.Signatures,
Expand Down
100 changes: 99 additions & 1 deletion x/ssi/tests/tx_create_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCreateSchema(t *testing.T) {
k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateSecp256k1KeyPair()
rpcElements := GenerateDidDocumentRPCElements(keyPair1)
rpcElements := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
t.Logf("Registering DID with DID Id: %s", rpcElements.DidDocument.GetId())

msgCreateDID := &types.MsgCreateDID{
Expand Down Expand Up @@ -57,3 +57,101 @@ func TestCreateSchema(t *testing.T) {

t.Log("Create Schema Tx Test Completed")
}

func TestCreateSchemaWithMultiControllerDid(t *testing.T) {
t.Log("Running test for Valid Create Schmea Tx")
k, ctx := TestKeeper(t)
msgServer := keeper.NewMsgServerImpl(*k)
goCtx := sdk.WrapSDKContext(ctx)

k.SetChainNamespace(&ctx, "devnet")

keyPair1 := GenerateSecp256k1KeyPair()
rpcElements1 := GenerateDidDocumentRPCElements(keyPair1, []DidSigningElements{})
t.Logf("Registering Employee 1 with DID Id: %s", rpcElements1.DidDocument.GetId())

msgCreateDID := &types.MsgCreateDID{
DidDocString: rpcElements1.DidDocument,
Signatures: rpcElements1.Signatures,
Creator: rpcElements1.Creator,
}

_, err := msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
t.FailNow()
}
t.Log("Employee 1 DID Registered Successfully")

keyPair2 := GenerateSecp256k1KeyPair()
rpcElements2 := GenerateDidDocumentRPCElements(keyPair2, []DidSigningElements{})
t.Logf("Registering Employee 2 with DID Id: %s", rpcElements2.DidDocument.GetId())

msgCreateDID = &types.MsgCreateDID{
DidDocString: rpcElements2.DidDocument,
Signatures: rpcElements2.Signatures,
Creator: rpcElements2.Creator,
}

_, err = msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
t.FailNow()
}
t.Log("Employee 2 DID Registered Successfully")

keyPairOrg := GenerateSecp256k1KeyPair()
singingElements := []DidSigningElements{
DidSigningElements{
keyPair: keyPair1,
vmId: rpcElements1.DidDocument.VerificationMethod[0].Id,
},
DidSigningElements{
keyPair: keyPair2,
vmId: rpcElements2.DidDocument.VerificationMethod[0].Id,
},
}
rpcElementsOrg := GenerateDidDocumentRPCElements(keyPairOrg, singingElements)
t.Logf("Registering Org with DID Id: %s", rpcElementsOrg.DidDocument.GetId())

msgCreateDID = &types.MsgCreateDID{
DidDocString: rpcElementsOrg.DidDocument,
Signatures: rpcElementsOrg.Signatures,
Creator: rpcElementsOrg.Creator,
}

_, err = msgServer.CreateDID(goCtx, msgCreateDID)
if err != nil {
t.Error("DID Registeration Failed")
t.Error(err)
t.FailNow()
}
t.Log("Org DID Registered Successfully")



t.Log("Registering Schema")
schemaRpcElements := GenerateSchemaDocumentRPCElements(
keyPair1,
rpcElementsOrg.DidDocument.Id,
rpcElements1.DidDocument.AssertionMethod[0],
)

msgCreateSchema := &types.MsgCreateSchema{
SchemaDoc: schemaRpcElements.SchemaDocument,
SchemaProof: schemaRpcElements.SchemaProof,
Creator: schemaRpcElements.Creator,
}

_, errCreateSchema := msgServer.CreateSchema(goCtx, msgCreateSchema)
if errCreateSchema != nil {
t.Error("Schema Registeration Failed")
t.Error(errCreateSchema)
t.FailNow()
}
t.Log("Schema Registered Successfully")

t.Log("Create Schema Tx Test Completed")
}
Loading

0 comments on commit cd3789d

Please sign in to comment.