Skip to content

Commit

Permalink
feat: add proof.expires to did doc (#45)
Browse files Browse the repository at this point in the history
Signed-off-by: Misha Sizov <[email protected]>
  • Loading branch information
mishasizov-SK authored Jan 10, 2025
1 parent 793b311 commit 76c309e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 42 deletions.
21 changes: 17 additions & 4 deletions doc/did/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (

jsonldCreator = "creator"
jsonldCreated = "created"
jsonldExpires = "expires"
jsonldProofValue = "proofValue"
jsonldSignatureValue = "signatureValue"
jsonldDomain = "domain"
Expand Down Expand Up @@ -493,6 +494,7 @@ func (r *rawDoc) UnmarshalJSON(data []byte) error {
type Proof struct {
Type string
Created *time.Time
Expires *time.Time
Creator string
ProofValue []byte
Domain string
Expand Down Expand Up @@ -632,7 +634,7 @@ func populateVerificationRelationships(doc *Doc, raw *rawDoc) error {
return nil
}

func populateProofs(context, didID, baseURI string, rawProofs []interface{}) ([]Proof, error) { // nolint:funlen
func populateProofs(context, didID, baseURI string, rawProofs []interface{}) ([]Proof, error) { // nolint:funlen,gocyclo
proofs := make([]Proof, 0, len(rawProofs))

for _, rawProof := range rawProofs {
Expand Down Expand Up @@ -680,17 +682,24 @@ func populateProofs(context, didID, baseURI string, rawProofs []interface{}) ([]
relativeURL: isRelative,
}

created := stringEntry(emap[jsonldCreated])
if created != "" {
if created := stringEntry(emap[jsonldCreated]); created != "" {
timeValue, errTime := time.Parse(time.RFC3339, created)

if errTime != nil {
return nil, errTime
}

proof.Created = &timeValue
}

if expires := stringEntry(emap[jsonldExpires]); expires != "" {
timeValue, errTime := time.Parse(time.RFC3339, expires)
if errTime != nil {
return nil, errTime
}

proof.Expires = &timeValue
}

proofs = append(proofs, proof)
}

Expand Down Expand Up @@ -1606,6 +1615,10 @@ func populateRawProofs(context, didID, baseURI string, proofs []Proof) []interfa
rawProof[jsonldVerificationMethod] = p.VerificationMethod
}

if p.Expires != nil {
rawProof[jsonldExpires] = p.Expires
}

rawProofs = append(rawProofs, rawProof)
}

Expand Down
108 changes: 70 additions & 38 deletions doc/did/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,54 +458,69 @@ func TestInvalid(t *testing.T) {
}

func TestValidWithProof(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
doc, err := ParseDocument([]byte(d))
require.NoError(t, err)
require.NotNil(t, doc)
t.Run("Success", func(t *testing.T) {
docs := []string{validDocWithProof, validDocV011WithProof}
for _, d := range docs {
doc, err := ParseDocument([]byte(d))
require.NoError(t, err)
require.NotNil(t, doc)

// test proof
created, err := time.Parse(time.RFC3339, "2019-09-23T14:16:59.484733-04:00")
require.NoError(t, err)
// test proof
created, err := time.Parse(time.RFC3339, "2019-09-23T14:16:59.484733-04:00")
require.NoError(t, err)
expires, err := time.Parse(time.RFC3339, "2019-09-23T15:16:59.484733-04:00")
require.NoError(t, err)

const encProofValue = "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA"
proofValue, err := base64.RawURLEncoding.DecodeString(encProofValue)
require.NoError(t, err)
const encProofValue = "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA"
proofValue, err := base64.RawURLEncoding.DecodeString(encProofValue)
require.NoError(t, err)

nonce, err := base64.RawURLEncoding.DecodeString("")
require.NoError(t, err)
nonce, err := base64.RawURLEncoding.DecodeString("")
require.NoError(t, err)

eProof := Proof{
Type: "Ed25519Signature2018",
Created: &created,
Creator: "did:method:abc#key-1",
ProofValue: proofValue,
Domain: "",
Nonce: nonce,
ProofPurpose: "assertionMethod",
CryptoSuite: "cryptosuite1",
Challenge: "challenge1",
VerificationMethod: "verificationMethod1",
JWS: "jws1",
relativeURL: false,
eProof := Proof{
Type: "Ed25519Signature2018",
Created: &created,
Expires: &expires,
Creator: "did:method:abc#key-1",
ProofValue: proofValue,
Domain: "",
Nonce: nonce,
ProofPurpose: "assertionMethod",
CryptoSuite: "cryptosuite1",
Challenge: "challenge1",
VerificationMethod: "verificationMethod1",
JWS: "jws1",
relativeURL: false,
}
require.Equal(t, []Proof{eProof}, doc.Proof)

byteDoc, err := doc.JSONBytes()
require.NoError(t, err)
require.NotNil(t, byteDoc)
}
require.Equal(t, []Proof{eProof}, doc.Proof)
})

byteDoc, err := doc.JSONBytes()
require.NoError(t, err)
require.NotNil(t, byteDoc)
t.Run("Invalid created", func(t *testing.T) {
invalid, err := ParseDocument([]byte(docWithInvalidCreatedInProof))
require.NotNil(t, err)
require.Nil(t, invalid)
require.Contains(t, err.Error(), "populate proofs failed")
})

// test invalid created
docWithInvalid := docWithInvalidCreatedInProof
if d == validDocV011WithProof {
docWithInvalid = docV011WithInvalidCreatedInProof
}
t.Run("Invalid created V011", func(t *testing.T) {
invalid, err := ParseDocument([]byte(docV011WithInvalidCreatedInProof))
require.NotNil(t, err)
require.Nil(t, invalid)
require.Contains(t, err.Error(), "populate proofs failed")
})

invalidDoc, err := ParseDocument([]byte(docWithInvalid))
t.Run("Invalid expires", func(t *testing.T) {
invalid, err := ParseDocument([]byte(docWithInvalidExpiresInProof))
require.NotNil(t, err)
require.Nil(t, invalidDoc)
require.Nil(t, invalid)
require.Contains(t, err.Error(), "populate proofs failed")
}
})
}

func TestInvalidEncodingInProof(t *testing.T) {
Expand Down Expand Up @@ -2009,6 +2024,7 @@ const validDocWithProof = `{
"id": "did:method:abc",
"proof": [{
"created": "2019-09-23T14:16:59.484733-04:00",
"expires": "2019-09-23T15:16:59.484733-04:00",
"creator": "did:method:abc#key-1",
"domain": "",
"nonce": "",
Expand Down Expand Up @@ -2069,6 +2085,7 @@ const validDocV011WithProof = `{
"id": "did:method:abc",
"proof": [{
"created": "2019-09-23T14:16:59.484733-04:00",
"expires": "2019-09-23T15:16:59.484733-04:00",
"creator": "did:method:abc#key-1",
"domain": "",
"nonce": "",
Expand Down Expand Up @@ -2103,6 +2120,21 @@ const docWithInvalidCreatedInProof = `{
}]
}`

const docWithInvalidExpiresInProof = `{
"@context": ["https://w3id.org/did/v1"],
"created": "2019-09-23T14:16:59.261024-04:00",
"id": "did:method:abc",
"proof": [{
"created": "2019-09-23T14:16:59.484733-04:00",
"expires": "2019-9-23T14:16:59",
"creator": "did:method:abc#key-1",
"domain": "",
"nonce": "",
"proofValue": "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA",
"type": "Ed25519Signature2018"
}]
}`

const docV011WithInvalidCreatedInProof = `{
"@context": ["https://w3id.org/did/v0.11"],
"created": "2019-09-23T14:16:59.261024-04:00",
Expand Down

0 comments on commit 76c309e

Please sign in to comment.