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

Commit

Permalink
feat: add support of multibase encoding for proofValue.
Browse files Browse the repository at this point in the history
Signed-off-by: Volodymyr Kubiv <volodymyr.kubiv@euristiq.com>
  • Loading branch information
vkubiv committed May 24, 2022
1 parent 0ba3492 commit d1c9773
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 13 deletions.
7 changes: 4 additions & 3 deletions pkg/doc/did/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/hyperledger/aries-framework-go/pkg/common/model"
"github.com/hyperledger/aries-framework-go/pkg/doc/jose/jwk"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
sigproof "github.com/hyperledger/aries-framework-go/pkg/doc/signature/proof"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/verifier"
)

Expand Down Expand Up @@ -597,9 +598,9 @@ func populateProofs(context, didID, baseURI string, rawProofs []interface{}) ([]
proofKey = jsonldSignatureValue
}

proofValue, err := base64.RawURLEncoding.DecodeString(stringEntry(emap[proofKey]))
proofValue, err := sigproof.DecodeProofValue(stringEntry(emap[proofKey]), stringEntry(emap[jsonldType]))
if err != nil {
return nil, err
return nil, errors.New("unsupported encoding")
}

nonce, err := base64.RawURLEncoding.DecodeString(stringEntry(emap[jsonldNonce]))
Expand Down Expand Up @@ -1438,7 +1439,7 @@ func populateRawProofs(context, didID, baseURI string, proofs []Proof) []interfa
jsonldType: p.Type,
jsonldCreated: p.Created,
jsonldCreator: creator,
k: base64.RawURLEncoding.EncodeToString(p.ProofValue),
k: sigproof.EncodeProofValue(p.ProofValue, p.Type),
jsonldDomain: p.Domain,
jsonldNonce: base64.RawURLEncoding.EncodeToString(p.Nonce),
jsonldProofPurpose: p.ProofPurpose,
Expand Down
2 changes: 1 addition & 1 deletion pkg/doc/did/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func TestInvalidEncodingInProof(t *testing.T) {
doc, err = populateProofs(c, "", "", rawProofs)
require.NotNil(t, err)
require.Nil(t, doc)
require.Contains(t, err.Error(), "illegal base64 data")
require.Contains(t, err.Error(), "unsupported encoding")
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/doc/did/testdata/valid_doc_with_base.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"creator": "#key-5",
"domain": "",
"nonce": "",
"proofValue": "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA",
"proofValue": "u6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA",
"type": "Ed25519Signature2018"
}],
"created": "2002-10-10T17:00:00Z"
Expand Down
32 changes: 30 additions & 2 deletions pkg/doc/signature/proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"errors"
"fmt"

"github.com/multiformats/go-multibase"

"github.com/hyperledger/aries-framework-go/pkg/doc/util"
)

Expand All @@ -36,6 +38,8 @@ const (
jsonldChallenge = "challenge"
// jsonldCapabilityChain is a key for capabilityChain.
jsonldCapabilityChain = "capabilityChain"

ed25519Signature2020 = "Ed25519Signature2020"
)

// Proof is cryptographic proof of the integrity of the DID Document.
Expand Down Expand Up @@ -71,7 +75,7 @@ func NewProof(emap map[string]interface{}) (*Proof, error) {
)

if generalProof, ok := emap[jsonldProofValue]; ok {
proofValue, err = decodeBase64(stringEntry(generalProof))
proofValue, err = DecodeProofValue(stringEntry(generalProof), stringEntry(emap[jsonldType]))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -143,6 +147,20 @@ func decodeBase64(s string) ([]byte, error) {
return nil, errors.New("unsupported encoding")
}

// DecodeProofValue decodes proofValue basing on proof type.
func DecodeProofValue(s, proofType string) ([]byte, error) {
if proofType == ed25519Signature2020 {
_, value, err := multibase.Decode(s)
if err == nil {
return value, nil
}

return nil, errors.New("unsupported encoding")
}

return decodeBase64(s)
}

// stringEntry.
func stringEntry(entry interface{}) string {
if entry == nil {
Expand Down Expand Up @@ -170,7 +188,7 @@ func (p *Proof) JSONLdObject() map[string]interface{} { // nolint:gocyclo
}

if len(p.ProofValue) > 0 {
emap[jsonldProofValue] = base64.RawURLEncoding.EncodeToString(p.ProofValue)
emap[jsonldProofValue] = EncodeProofValue(p.ProofValue, p.Type)
}

if len(p.JWS) > 0 {
Expand Down Expand Up @@ -200,6 +218,16 @@ func (p *Proof) JSONLdObject() map[string]interface{} { // nolint:gocyclo
return emap
}

// EncodeProofValue decodes proofValue basing on proof type.
func EncodeProofValue(proofValue []byte, proofType string) string {
if proofType == ed25519Signature2020 {
encoded, _ := multibase.Encode(multibase.Base58BTC, proofValue) //nolint: errcheck
return encoded
}

return base64.RawURLEncoding.EncodeToString(proofValue)
}

// PublicKeyID provides ID of public key to be used to independently verify the proof.
// "verificationMethod" field is checked first. If not empty, its value is returned.
// Otherwise, "creator" field is returned if not empty. Otherwise, error is returned.
Expand Down
41 changes: 36 additions & 5 deletions pkg/doc/signature/proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
"testing"
"time"

"github.com/multiformats/go-multibase"
"github.com/stretchr/testify/require"

"github.com/hyperledger/aries-framework-go/pkg/doc/util"
)

const proofValueBase64 = "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA"
const (
proofValueBase64 = "6mdES87erjP5r1qCSRW__otj-A_Rj0YgRO7XU_0Amhwdfa7AAmtGUSFGflR_fZqPYrY9ceLRVQCJ49s0q7-LBA"
proofValueMultibase = "z5gpJQZoaLUXevXk2mYYbQE9krfaJYBBwQcJhhAvX3zs6daJ2Eb6VJoU46WkUYN8R1vgX7o8ktuUkzpRJS5aJRQyh"
)

func TestProof(t *testing.T) {
p, err := NewProof(map[string]interface{}{
Expand Down Expand Up @@ -44,6 +48,33 @@ func TestProof(t *testing.T) {
require.Equal(t, []byte(""), p.Nonce)
require.Equal(t, proofValueBytes, p.ProofValue)

// test proof with multibase encoding
p, err = NewProof(map[string]interface{}{
"type": "Ed25519Signature2020",
"creator": "didID",
"verificationMethod": "did:example:123456#key1",
"created": "2018-03-15T00:00:00Z",
"domain": "abc.com",
"nonce": "",
"proofValue": proofValueMultibase,
})
require.NoError(t, err)

// test proof
created, err = time.Parse(time.RFC3339, "2018-03-15T00:00:00Z")
require.NoError(t, err)

_, proofValueBytes, err = multibase.Decode(proofValueMultibase)
require.NoError(t, err)

require.Equal(t, "Ed25519Signature2020", p.Type)
require.Equal(t, "didID", p.Creator)
require.Equal(t, "did:example:123456#key1", p.VerificationMethod)
require.Equal(t, created, p.Created.Time)
require.Equal(t, "abc.com", p.Domain)
require.Equal(t, []byte(""), p.Nonce)
require.Equal(t, proofValueBytes, p.ProofValue)

// test created time with milliseconds section
p, err = NewProof(map[string]interface{}{
"type": "type",
Expand Down Expand Up @@ -164,7 +195,7 @@ func TestInvalidNonce(t *testing.T) {
func TestProof_JSONLdObject(t *testing.T) {
r := require.New(t)

proofValueBytes, err := base64.RawURLEncoding.DecodeString(proofValueBase64)
_, proofValueBytes, err := multibase.Decode(proofValueMultibase)
r.NoError(err)

nonceBase64, err := base64.RawURLEncoding.DecodeString("abc")
Expand All @@ -174,7 +205,7 @@ func TestProof_JSONLdObject(t *testing.T) {
r.NoError(err)

p := &Proof{
Type: "Ed25519Signature2018",
Type: "Ed25519Signature2020",
Created: util.NewTime(created),
Creator: "creator",
ProofValue: proofValueBytes,
Expand All @@ -186,10 +217,10 @@ func TestProof_JSONLdObject(t *testing.T) {
}

pJSONLd := p.JSONLdObject()
r.Equal("Ed25519Signature2018", pJSONLd["type"])
r.Equal("Ed25519Signature2020", pJSONLd["type"])
r.Equal("2018-03-15T00:00:00Z", pJSONLd["created"])
r.Equal("creator", pJSONLd["creator"])
r.Equal(proofValueBase64, pJSONLd["proofValue"])
r.Equal(proofValueMultibase, pJSONLd["proofValue"])
r.Equal("test.jws.value", pJSONLd["jws"])
r.Equal("assertionMethod", pJSONLd["proofPurpose"])
r.Equal("internal", pJSONLd["domain"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"type": "BbsBlsSignature2020",
"created": "2020-12-06T19:23:10Z",
"proofPurpose": "assertionMethod",
"proofValue": "jj3Xd3+KxmbQo85PFDjQJ7dAZlhj8A8W1Um8Vk7Xoiv6+jWRx5d8s0rgPk5dAXy6HwaJ4fQOde/MBb7E4QaGMlfK6y5eEKDUYzoGG0DScWIvaGcSZug6DwvWVXi+214P5MtlKnNwO6gJdemEgj8T/A==",
"proofValue": "mjj3Xd3+KxmbQo85PFDjQJ7dAZlhj8A8W1Um8Vk7Xoiv6+jWRx5d8s0rgPk5dAXy6HwaJ4fQOde/MBb7E4QaGMlfK6y5eEKDUYzoGG0DScWIvaGcSZug6DwvWVXi+214P5MtlKnNwO6gJdemEgj8T/A==",
"verificationMethod": "did:example:489398593#test"
},
{
Expand Down

0 comments on commit d1c9773

Please sign in to comment.