Skip to content

Commit

Permalink
fix: add public key fetcher to pd match (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHansLee committed Mar 2, 2023
1 parent 24e18b9 commit e5c210b
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 49 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ go 1.19

require (
github.com/btcsuite/btcd v0.22.2
github.com/cosmos/cosmos-sdk v0.45.12
github.com/cosmos/go-bip39 v1.0.0
github.com/gogo/protobuf v1.3.3
github.com/hyperledger/aries-framework-go v0.1.9-0.20230222063211-02f80847168a
github.com/hyperledger/aries-framework-go/component/storageutil v0.0.0-20220322085443-50e8f9bd208b
github.com/medibloc/panacea-core/v2 v2.0.5
github.com/mr-tron/base58 v1.2.0
github.com/piprate/json-gold v0.4.2
github.com/stretchr/testify v1.8.1
github.com/tendermint/tendermint v0.34.24
)

require github.com/google/uuid v1.3.0 // indirect
Expand All @@ -27,7 +31,6 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/cosmos-sdk v0.45.9 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
Expand Down Expand Up @@ -62,7 +65,6 @@ require (
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/multiformats/go-multibase v0.1.1 // indirect
Expand All @@ -89,7 +91,6 @@ require (
github.com/subosito/gotenv v1.4.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/tendermint v0.34.24 // indirect
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8 // indirect
github.com/tidwall/gjson v1.6.7 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/cosmos/btcutil v1.0.4 h1:n7C2ngKXo7UC9gNyMNLbzqz7Asuf+7Qv4gnX/rOdQ44=
github.com/cosmos/btcutil v1.0.4/go.mod h1:Ffqc8Hn6TJUdDgHBwIZLtrLQC1KdJ9jGJl/TvgUaxbU=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
github.com/cosmos/gorocksdb v1.2.0 h1:d0l3jJG8M4hBouIZq0mDUHZ+zjOx044J3nGRskwTb4Y=
github.com/cosmos/gorocksdb v1.2.0/go.mod h1:aaKvKItm514hKfNJpUJXnnOWeBnk2GL4+Qw9NHizILw=
github.com/cosmos/iavl v0.19.4 h1:t82sN+Y0WeqxDLJRSpNd8YFX5URIrT+p8n6oJbJ2Dok=
Expand Down Expand Up @@ -522,6 +523,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=
Expand Down Expand Up @@ -625,6 +627,7 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
20 changes: 7 additions & 13 deletions pkg/vc/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,18 @@ func (f *Framework) VerifyPresentation(vp []byte, opts ...VerificationOption) (*

// TODO: For now, check of constraints in presentation definition is not supported
// https://github.com/hyperledger/aries-framework-go/issues/2108
_, err = pd.Match(presentation, f.loader, presexch.WithCredentialOptions(verifiable.WithJSONLDDocumentLoader(f.loader)))
_, err = pd.Match(presentation,
f.loader,
presexch.WithCredentialOptions(
verifiable.WithJSONLDDocumentLoader(f.loader),
verifiable.WithPublicKeyFetcher(f.resolver.PublicKeyFetcher()),
),
)
if err != nil {
return nil, fmt.Errorf("is not matched with presentation definition: %w", err)
}
}

// verify VCs
for _, cred := range presentation.Credentials() {
vc, err := json.Marshal(cred)
if err != nil {
return nil, fmt.Errorf("failed to read credentials from presentation: %w", err)
}

if err = f.VerifyCredential(vc); err != nil {
return nil, fmt.Errorf("failed to verify credential: %w", err)
}
}

return presentation, nil
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/vc/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vc

import (
"github.com/hyperledger/aries-framework-go/component/storageutil/mem"
"github.com/hyperledger/aries-framework-go/pkg/doc/did"
"github.com/hyperledger/aries-framework-go/pkg/doc/ld"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
Expand All @@ -16,7 +17,11 @@ type Framework struct {
resolver *verifiable.VDRKeyResolver
}

func NewFramework(vdr vdr.Registry) (*Framework, error) {
type didResolver interface {
Resolve(did string, opts ...vdr.DIDMethodOption) (*did.DocResolution, error)
}

func NewFramework(vdr didResolver) (*Framework, error) {
storeProvider := mem.NewProvider()
contextStore, err := ldstore.NewContextStore(storeProvider)
if err != nil {
Expand Down
43 changes: 23 additions & 20 deletions pkg/vdr/panacea_vdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package vdr
import (
"bytes"
"context"
"errors"
"fmt"

"github.com/btcsuite/btcd/btcec"
"github.com/gogo/protobuf/jsonpb"
"github.com/hyperledger/aries-framework-go/pkg/doc/did"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
didtypes "github.com/medibloc/panacea-core/v2/x/did/types"
"github.com/mr-tron/base58"
)

var _ vdr.Registry = &PanaceaVDR{}

type didClient interface {
GetDID(context.Context, string) (*didtypes.DIDDocumentWithSeq, error)
}
Expand All @@ -34,6 +32,27 @@ func (r *PanaceaVDR) Resolve(didID string, _ ...vdr.DIDMethodOption) (*did.DocRe
return nil, fmt.Errorf("failed to get DID document: %w", err)
}

var vms []*didtypes.VerificationMethod
for _, vm := range didDocWithSeq.Document.VerificationMethods {
pubKeyBz, err := base58.Decode(vm.PublicKeyBase58)
if err != nil {
return nil, fmt.Errorf("invalid base58 encoded public key: %w", err)
}

if btcec.IsCompressedPubKey(pubKeyBz) {
pubKey, err := btcec.ParsePubKey(pubKeyBz, btcec.S256())
if err != nil {
return nil, fmt.Errorf("invalid secp256k1 public key of verification method: %w", err)
}

pubKeyStr := base58.Encode(pubKey.SerializeUncompressed())
vm.PublicKeyBase58 = pubKeyStr
}
vms = append(vms, vm)
}

didDocWithSeq.Document.VerificationMethods = vms

docBuf := new(bytes.Buffer)
if err := new(jsonpb.Marshaler).Marshal(docBuf, didDocWithSeq.Document); err != nil {
return nil, fmt.Errorf("failed to marshal DID document: %w", err)
Expand All @@ -48,19 +67,3 @@ func (r *PanaceaVDR) Resolve(didID string, _ ...vdr.DIDMethodOption) (*did.DocRe
DIDDocument: doc,
}, nil
}

func (r *PanaceaVDR) Create(_ string, _ *did.Doc, _ ...vdr.DIDMethodOption) (*did.DocResolution, error) {
return nil, errors.New("not implemented")
}

func (r *PanaceaVDR) Update(_ *did.Doc, _ ...vdr.DIDMethodOption) error {
return errors.New("not implemented")
}

func (r *PanaceaVDR) Deactivate(_ string, _ ...vdr.DIDMethodOption) error {
return errors.New("not implemented")
}

func (r *PanaceaVDR) Close() error {
return errors.New("not implemented")
}
Loading

0 comments on commit e5c210b

Please sign in to comment.