-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from iden3/feat/did-pkh
add PKH resolver
- Loading branch information
Showing
7 changed files
with
170 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
name: e2e driver-did-iden3 | ||
|
||
on: | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package pkh | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/iden3/driver-did-iden3/pkg/document" | ||
"github.com/iden3/go-iden3-core/v2/w3c" | ||
"github.com/iden3/go-schema-processor/v2/verifiable" | ||
) | ||
|
||
const ( | ||
NamespaceTezos = "tezos" | ||
NamespaceEIP155 = "eip155" | ||
NamespaceBIP122 = "bip122" | ||
) | ||
|
||
type Resolver struct { | ||
} | ||
|
||
type ResolverOption func(*Resolver) | ||
|
||
// NewResolver create new pkh resolver. | ||
func NewResolver() (*Resolver, error) { | ||
return &Resolver{}, nil | ||
} | ||
|
||
func (r *Resolver) Resolve( | ||
ctx context.Context, | ||
did w3c.DID, | ||
) (*document.DidResolution, error) { | ||
didString := did.String() | ||
parts := strings.Split(didString, ":") | ||
namespace := parts[2] | ||
vmID := didString + "#blockchainAccountId" | ||
|
||
didResolution := document.NewDidResolution() | ||
authentication := verifiable.Authentication{} | ||
authentication.ID = vmID | ||
authentication.Type = document.EcdsaSecp256k1RecoveryMethod2020Type | ||
authentication.Controller = didString | ||
blockchainAccountID, err := getBlockchainAccountID(didString) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
assertionMethod := verifiable.Authentication{} | ||
err = assertionMethod.UnmarshalJSON([]byte(fmt.Sprintf("%q", vmID))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
didResolution.DidDocument = &verifiable.DIDDocument{ | ||
Context: []interface{}{ | ||
document.DefaultDidDocContext, | ||
map[string]string{ | ||
"blockchainAccountId": document.BlockchainAccountIDContext, | ||
document.EcdsaSecp256k1RecoveryMethod2020Type: document.EcdsaSecp256k1RecoveryMethod2020Context, | ||
}, | ||
}, | ||
ID: didString, | ||
VerificationMethod: []verifiable.CommonVerificationMethod{}, | ||
Authentication: []verifiable.Authentication{authentication}, | ||
AssertionMethod: []verifiable.Authentication{assertionMethod}, | ||
} | ||
|
||
didResolution.DidDocument.VerificationMethod = append( | ||
didResolution.DidDocument.VerificationMethod, | ||
verifiable.CommonVerificationMethod{ | ||
ID: vmID, | ||
Type: document.EcdsaSecp256k1RecoveryMethod2020Type, | ||
Controller: didString, | ||
BlockchainAccountID: blockchainAccountID, | ||
}, | ||
) | ||
|
||
switch namespace { | ||
case NamespaceEIP155: | ||
case NamespaceBIP122: | ||
break | ||
case NamespaceTezos: | ||
didResolution.DidDocument.Context = []interface{}{ | ||
document.DefaultDidDocContext, | ||
map[string]string{ | ||
"blockchainAccountId": document.BlockchainAccountIDContext, | ||
document.EcdsaSecp256k1RecoveryMethod2020Type: document.EcdsaSecp256k1RecoveryMethod2020Context, | ||
document.TezosMethod2021Type: document.TezosMethod2021Context, | ||
}, | ||
} | ||
|
||
tzID := fmt.Sprintf("%s#%s", didString, document.TezosMethod2021Type) | ||
didResolution.DidDocument.VerificationMethod = append( | ||
didResolution.DidDocument.VerificationMethod, | ||
verifiable.CommonVerificationMethod{ | ||
ID: tzID, | ||
Type: document.TezosMethod2021Type, | ||
Controller: didString, | ||
BlockchainAccountID: blockchainAccountID, | ||
}, | ||
) | ||
tzAuthentication := verifiable.Authentication{} | ||
tzAuthentication.ID = tzID | ||
tzAuthentication.Type = document.TezosMethod2021Type | ||
tzAuthentication.Controller = didString | ||
|
||
tzAssertionMethod := verifiable.Authentication{} | ||
err = tzAssertionMethod.UnmarshalJSON([]byte(fmt.Sprintf("%q", tzID))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
didResolution.DidDocument.Authentication = append(didResolution.DidDocument.Authentication, tzAuthentication) | ||
didResolution.DidDocument.AssertionMethod = append(didResolution.DidDocument.AssertionMethod, tzAssertionMethod) | ||
default: | ||
return nil, fmt.Errorf("chain namespace not supported: %s", namespace) | ||
} | ||
return didResolution, nil | ||
} | ||
|
||
func getBlockchainAccountID(did string) (string, error) { | ||
prefix := "did:pkh:" | ||
if !strings.HasPrefix(did, prefix) { | ||
return "", errors.New("invalid DID format: must start with 'did:pkh:'") | ||
} | ||
blockchainAccountID := strings.TrimPrefix(did, prefix) | ||
if blockchainAccountID == "" { | ||
return "", errors.New("invalid DID format: missing blockchainAccountId") | ||
} | ||
return blockchainAccountID, nil | ||
} |