Skip to content

Commit 7fc3b2c

Browse files
authored
Fix DID verification method ID encoding and add controller (#48)
## Summary Fixed the DID document generation to correctly encode verification method IDs and set the controller field. ## Key Changes - **Fixed fragment encoding**: Changed `doc.Fragment("#key-0")` to `doc.Fragment("key-0")` to prevent double-encoding of the `#` character. The `Fragment()` method automatically prepends `#`, so passing `#key-0` resulted in `#%23key-0` in the serialized output. - **Set verification method controller**: Added `vm.Controller = doc.ID` to properly associate the verification method with the DID document. - **Added test coverage**: Created comprehensive test (`TestDIDDocument`) that validates: - DID document generation succeeds - Document ID matches the service DID - Verification method ID is correctly formatted as `did:web:example.com#key-0` - Verification method ID does not contain percent-encoded characters - Verification method controller is set to the document ID ## Implementation Details The fix ensures that DID documents conform to the W3C DID specification by properly formatting the verification method ID and establishing the correct controller relationship. https://claude.ai/code/session_01YAjDX5F44mcX8ojb1ENjY ## References - fil-forge/smelt#11
2 parents aba2bd2 + f06be16 commit 7fc3b2c

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

identity/identity.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ func (i Identity) DIDDocument() (did.Document, error) {
6363
if !ok {
6464
return did.Document{}, fmt.Errorf("identity does not have a multikey verifier")
6565
}
66-
vm := multikey.DeriveVerificationMethod(doc.Fragment("#key-0"), mkVerifier)
66+
// Fragment expects a bare name and prepends the "#" itself; passing "#key-0"
67+
// would serialize as "#%23key-0".
68+
vm := multikey.DeriveVerificationMethod(doc.Fragment("key-0"), mkVerifier)
69+
vm.Controller = doc.ID
6770

6871
if err := doc.VerificationMethods.Add(vm); err != nil {
6972
return did.Document{}, err

identity/identity_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package identity_test
2+
3+
import (
4+
"encoding/json"
5+
"strings"
6+
"testing"
7+
8+
"github.com/fil-forge/libforge/identity"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestDIDDocument(t *testing.T) {
13+
const serviceDID = "did:web:example.com"
14+
15+
id, err := identity.New("", serviceDID)
16+
require.NoError(t, err)
17+
18+
doc, err := id.DIDDocument()
19+
require.NoError(t, err)
20+
21+
docJSON, err := json.Marshal(doc)
22+
require.NoError(t, err)
23+
24+
var parsed struct {
25+
ID string `json:"id"`
26+
VerificationMethod []struct {
27+
ID string `json:"id"`
28+
Controller string `json:"controller"`
29+
} `json:"verificationMethod"`
30+
}
31+
require.NoError(t, json.Unmarshal(docJSON, &parsed))
32+
33+
require.Equal(t, serviceDID, parsed.ID)
34+
require.Len(t, parsed.VerificationMethod, 1)
35+
36+
vm := parsed.VerificationMethod[0]
37+
require.Equal(t, serviceDID+"#key-0", vm.ID)
38+
require.False(t, strings.Contains(vm.ID, "%23"), "verification method ID must not contain a percent-encoded '#'")
39+
require.Equal(t, serviceDID, vm.Controller)
40+
}

0 commit comments

Comments
 (0)