github.com/trustbloc/kms-go@v1.1.2/doc/util/fingerprint/parse.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  package fingerprint
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  // MethodIDFromDIDKey parses the did:key DID and returns it's specific Method ID.
    14  func MethodIDFromDIDKey(didKey string) (string, error) {
    15  	msID, err := getMethodSpecificID(didKey)
    16  	if err != nil {
    17  		return "", err
    18  	}
    19  
    20  	// did:key is hard-coded to base58btc:
    21  	// - https://w3c-ccg.github.io/did-method-key/
    22  	// - https://github.com/multiformats/multibase#multibase-table
    23  	if !strings.HasPrefix(msID, "z") {
    24  		return "", fmt.Errorf("not a valid did:key identifier (not a base58btc multicodec): %s", didKey)
    25  	}
    26  
    27  	return msID, nil
    28  }
    29  
    30  func getMethodSpecificID(did string) (string, error) {
    31  	parts := strings.SplitN(did, ":", 3)
    32  
    33  	if len(parts) < 3 {
    34  		return "", fmt.Errorf("invalid did")
    35  	}
    36  
    37  	return parts[2], nil
    38  }