Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
refactor: Support JwsVerificationKey2020 for DID public key
Browse files Browse the repository at this point in the history
closes #1527, #1513

Signed-off-by: Dmitriy Kinoshenko <dkinoshenko@gmail.com>
  • Loading branch information
kdimak committed Apr 2, 2020
1 parent e17df7e commit c251104
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 150 deletions.
1 change: 1 addition & 0 deletions cmd/aries-agent-rest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/aws/aws-sdk-go v1.25.39/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/btcsuite/btcd v0.20.1-beta h1:Ik4hyJqN8Jfyv3S4AGBOmyouMsYE3EdYODkMbQjwPGw=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module github.com/hyperledger/aries-framework-go

require (
github.com/VictoriaMetrics/fastcache v1.5.7
github.com/btcsuite/btcd v0.20.1-beta
github.com/btcsuite/btcutil v1.0.1
github.com/decred/dcrd/dcrec/secp256k1/v2 v2.0.0
github.com/golang/mock v1.4.0
github.com/golang/protobuf v1.3.3
github.com/google/tink v1.3.0
Expand Down
95 changes: 66 additions & 29 deletions pkg/doc/did/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ type PublicKey struct {
ID string
Type string
Controller string
Value []byte

Value []byte

algorithm string
curve string
keyType string
}

// Service DID doc service
Expand Down Expand Up @@ -351,61 +356,90 @@ func populatePublicKeys(context string, rawPKs []map[string]interface{}) ([]Publ
var publicKeys []PublicKey

for _, rawPK := range rawPKs {
decodeValue, err := decodePK(rawPK)
if err != nil {
return nil, err
}

controllerKey := jsonldController

if context == contextV011 {
controllerKey = jsonldOwner
}

publicKeys = append(publicKeys, PublicKey{ID: stringEntry(rawPK[jsonldID]), Type: stringEntry(rawPK[jsonldType]),
Controller: stringEntry(rawPK[controllerKey]), Value: decodeValue})
publicKey := PublicKey{ID: stringEntry(rawPK[jsonldID]), Type: stringEntry(rawPK[jsonldType]),
Controller: stringEntry(rawPK[controllerKey])}

err := decodePK(&publicKey, rawPK)
if err != nil {
return nil, err
}

publicKeys = append(publicKeys, publicKey)
}

return publicKeys, nil
}

func decodePK(rawPK map[string]interface{}) ([]byte, error) {
func decodePK(publicKey *PublicKey, rawPK map[string]interface{}) error {
if stringEntry(rawPK[jsonldPublicKeyBase58]) != "" {
return base58.Decode(stringEntry(rawPK[jsonldPublicKeyBase58])), nil
publicKey.Value = base58.Decode(stringEntry(rawPK[jsonldPublicKeyBase58]))
return nil
}

if stringEntry(rawPK[jsonldPublicKeyHex]) != "" {
value, err := hex.DecodeString(stringEntry(rawPK[jsonldPublicKeyHex]))
if err != nil {
return nil, fmt.Errorf("decode public key hex failed: %w", err)
return fmt.Errorf("decode public key hex failed: %w", err)
}

return value, nil
publicKey.Value = value

return nil
}

if stringEntry(rawPK[jsonldPublicKeyPem]) != "" {
block, _ := pem.Decode([]byte(stringEntry(rawPK[jsonldPublicKeyPem])))
if block == nil {
return nil, errors.New("failed to decode PEM block containing public key")
return errors.New("failed to decode PEM block containing public key")
}

return block.Bytes, nil
publicKey.Value = block.Bytes

return nil
}

if jwkMap := mapEntry(rawPK[jsonldPublicKeyjwk]); jwkMap != nil {
jwkBytes, err := json.Marshal(jwkMap)
if err != nil {
return nil, fmt.Errorf("failed to marshal '%s', cause: %w ", jsonldPublicKeyjwk, err)
}
return decodePublicKeyJwk(jwkMap, publicKey)
}

if string(jwkBytes) == "{}" {
return []byte(""), nil
}
return errors.New("public key encoding not supported")
}

return jose.DecodePublicKey(jwkBytes)
func decodePublicKeyJwk(jwkMap map[string]interface{}, publicKey *PublicKey) error {
jwkBytes, err := json.Marshal(jwkMap)
if err != nil {
return fmt.Errorf("failed to marshal '%s', cause: %w ", jsonldPublicKeyjwk, err)
}

return nil, errors.New("public key encoding not supported")
if string(jwkBytes) == "{}" {
publicKey.Value = []byte("")
return nil
}

var jwk jose.JWK

err = json.Unmarshal(jwkBytes, &jwk)
if err != nil {
return fmt.Errorf("unmarshal JWK: %w", err)
}

pkBytes, err := jwk.PublicKeyBytes()
if err != nil {
return fmt.Errorf("failed to decode public key from JWK: %w", err)
}

publicKey.Value = pkBytes
publicKey.algorithm = jwk.Algorithm
publicKey.keyType = jwk.Kty
publicKey.curve = jwk.Crv

return nil
}

func (r *rawDoc) ParseContext() []string {
Expand Down Expand Up @@ -591,8 +625,11 @@ func (r *didKeyResolver) Resolve(id string) (*verifier.PublicKey, error) {
for _, key := range r.PubKeys {
if key.ID == id {
return &verifier.PublicKey{
Type: key.Type,
Value: key.Value,
Type: key.Type,
Value: key.Value,
Curve: key.curve,
Alg: key.algorithm,
KeyType: key.keyType,
}, nil
}
}
Expand Down Expand Up @@ -628,14 +665,14 @@ func populateRawServices(services []Service) []map[string]interface{} {

func populateRawPublicKeys(context string, pks []PublicKey) []map[string]interface{} {
var rawPKs []map[string]interface{}
for _, pk := range pks {
rawPKs = append(rawPKs, populateRawPublicKey(context, pk))
for i := range pks {
rawPKs = append(rawPKs, populateRawPublicKey(context, &pks[i]))
}

return rawPKs
}

func populateRawPublicKey(context string, pk PublicKey) map[string]interface{} {
func populateRawPublicKey(context string, pk *PublicKey) map[string]interface{} {
rawPK := make(map[string]interface{})
rawPK[jsonldID] = pk.ID
rawPK[jsonldType] = pk.Type
Expand All @@ -657,7 +694,7 @@ func populateRawAuthentications(context string, vms []VerificationMethod) []inte
var rawAuthentications []interface{}

for _, vm := range vms {
rawAuthentications = append(rawAuthentications, populateRawPublicKey(context, vm.PublicKey))
rawAuthentications = append(rawAuthentications, populateRawPublicKey(context, &vm.PublicKey))
}

return rawAuthentications
Expand Down

0 comments on commit c251104

Please sign in to comment.