Skip to content

Commit ec9356e

Browse files
committed
feat: add data integrity proof
Signed-off-by: Misha Sizov <[email protected]>
1 parent 6f83cdc commit ec9356e

File tree

5 files changed

+68
-43
lines changed

5 files changed

+68
-43
lines changed

doc/did/doc.go

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ const (
4848
jsonldController = "controller"
4949
jsonldOwner = "owner"
5050

51-
jsonldCreator = "creator"
52-
jsonldCreated = "created"
53-
jsonldProofValue = "proofValue"
54-
jsonldSignatureValue = "signatureValue"
55-
jsonldDomain = "domain"
56-
jsonldNonce = "nonce"
57-
jsonldProofPurpose = "proofPurpose"
51+
jsonldCreator = "creator"
52+
jsonldCreated = "created"
53+
jsonldProofValue = "proofValue"
54+
jsonldSignatureValue = "signatureValue"
55+
jsonldDomain = "domain"
56+
jsonldNonce = "nonce"
57+
jsonldProofPurpose = "proofPurpose"
58+
jsonldChallenge = "challenge"
59+
jsonldCryptoSuite = "cryptosuite"
60+
jsonldVerificationMethod = "verificationMethod"
5861

5962
// various public key encodings.
6063
jsonldPublicKeyBase58 = "publicKeyBase58"
@@ -487,14 +490,17 @@ func (r *rawDoc) UnmarshalJSON(data []byte) error {
487490

488491
// Proof is cryptographic proof of the integrity of the DID Document.
489492
type Proof struct {
490-
Type string
491-
Created *time.Time
492-
Creator string
493-
ProofValue []byte
494-
Domain string
495-
Nonce []byte
496-
ProofPurpose string
497-
relativeURL bool
493+
Type string
494+
Created *time.Time
495+
Creator string
496+
ProofValue []byte
497+
Domain string
498+
Nonce []byte
499+
ProofPurpose string
500+
CryptoSuite string
501+
Challenge string
502+
VerificationMethod string
503+
relativeURL bool
498504
}
499505

500506
// UnmarshalJSON unmarshals a DID Document.
@@ -659,13 +665,16 @@ func populateProofs(context, didID, baseURI string, rawProofs []interface{}) ([]
659665
}
660666

661667
proof := Proof{
662-
Type: stringEntry(emap[jsonldType]),
663-
Creator: creator,
664-
ProofValue: proofValue,
665-
ProofPurpose: stringEntry(emap[jsonldProofPurpose]),
666-
Domain: stringEntry(emap[jsonldDomain]),
667-
Nonce: nonce,
668-
relativeURL: isRelative,
668+
Type: stringEntry(emap[jsonldType]),
669+
Creator: creator,
670+
ProofValue: proofValue,
671+
ProofPurpose: stringEntry(emap[jsonldProofPurpose]),
672+
Domain: stringEntry(emap[jsonldDomain]),
673+
VerificationMethod: stringEntry(emap[jsonldVerificationMethod]),
674+
CryptoSuite: stringEntry(emap[jsonldCryptoSuite]),
675+
Challenge: stringEntry(emap[jsonldChallenge]),
676+
Nonce: nonce,
677+
relativeURL: isRelative,
669678
}
670679

671680
created := stringEntry(emap[jsonldCreated])
@@ -1246,7 +1255,8 @@ func (doc *Doc) MarshalJSON() ([]byte, error) {
12461255
}
12471256

12481257
// VerifyProof verifies document proofs.
1249-
func (doc *Doc) VerifyProof(suites []api.VerifierSuite, jsonldOpts ...processor.Opts) error {
1258+
// Deprecated. Please use vc-go/verifiable.VerifyDIDProof().
1259+
func (doc *Doc) VerifyProof(suites []api.VerifierSuite, opts ...processor.Opts) error {
12501260
if len(doc.Proof) == 0 {
12511261
return ErrProofNotFound
12521262
}
@@ -1261,7 +1271,7 @@ func (doc *Doc) VerifyProof(suites []api.VerifierSuite, jsonldOpts ...processor.
12611271
return fmt.Errorf("create verifier: %w", err)
12621272
}
12631273

1264-
return v.Verify(docBytes, jsonldOpts...)
1274+
return v.Verify(docBytes, opts...)
12651275
}
12661276

12671277
// VerificationMethods returns verification methods of DID Doc of certain relationship.
@@ -1565,13 +1575,16 @@ func populateRawProofs(context, didID, baseURI string, proofs []Proof) []interfa
15651575
}
15661576

15671577
rawProofs = append(rawProofs, map[string]interface{}{
1568-
jsonldType: p.Type,
1569-
jsonldCreated: p.Created,
1570-
jsonldCreator: creator,
1571-
k: sigproof.EncodeProofValue(p.ProofValue, p.Type),
1572-
jsonldDomain: p.Domain,
1573-
jsonldNonce: base64.RawURLEncoding.EncodeToString(p.Nonce),
1574-
jsonldProofPurpose: p.ProofPurpose,
1578+
jsonldType: p.Type,
1579+
jsonldCreated: p.Created,
1580+
jsonldCreator: creator,
1581+
k: sigproof.EncodeProofValue(p.ProofValue, p.Type),
1582+
jsonldDomain: p.Domain,
1583+
jsonldNonce: base64.RawURLEncoding.EncodeToString(p.Nonce),
1584+
jsonldProofPurpose: p.ProofPurpose,
1585+
jsonldVerificationMethod: p.VerificationMethod,
1586+
jsonldCryptoSuite: p.CryptoSuite,
1587+
jsonldChallenge: p.Challenge,
15751588
})
15761589
}
15771590

doc/ld/proof/proof.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/base64"
1010
"errors"
1111
"fmt"
12-
"strings"
1312

1413
"github.com/multiformats/go-multibase"
1514

@@ -41,6 +40,7 @@ const (
4140
jsonldCapabilityChain = "capabilityChain"
4241

4342
ed25519Signature2020 = "Ed25519Signature2020"
43+
dataIntegrityProof = "DataIntegrityProof"
4444
)
4545

4646
// Proof is cryptographic proof of the integrity of the DID Document.
@@ -150,23 +150,21 @@ func decodeBase64(s string) ([]byte, error) {
150150

151151
// DecodeProofValue decodes proofValue basing on proof type.
152152
func DecodeProofValue(s, proofType string) ([]byte, error) {
153-
if proofType == ed25519Signature2020 {
153+
switch proofType {
154+
case ed25519Signature2020:
154155
_, value, err := multibase.Decode(s)
155156
if err == nil {
156157
return value, nil
157158
}
158159

159160
return nil, errors.New("unsupported encoding")
161+
case dataIntegrityProof:
162+
// No need to decode Data integrity proof as encoding/decoding logic for this proof type
163+
// is managed by vc-go/dataintegrity package.
164+
return []byte(s), nil
165+
default:
166+
return decodeBase64(s)
160167
}
161-
162-
if strings.HasPrefix(s, "z") { // maybe base58
163-
_, value, err := multibase.Decode(s)
164-
if err == nil {
165-
return value, nil
166-
}
167-
}
168-
169-
return decodeBase64(s)
170168
}
171169

172170
// stringEntry.
@@ -232,9 +230,14 @@ func (p *Proof) JSONLdObject() map[string]interface{} { // nolint:gocyclo
232230

233231
// EncodeProofValue decodes proofValue basing on proof type.
234232
func EncodeProofValue(proofValue []byte, proofType string) string {
235-
if proofType == ed25519Signature2020 {
233+
switch proofType {
234+
case ed25519Signature2020:
236235
encoded, _ := multibase.Encode(multibase.Base58BTC, proofValue) //nolint: errcheck
237236
return encoded
237+
case dataIntegrityProof:
238+
// No need to encode Data integrity proof as encoding/decoding logic for this proof type
239+
// is managed by vc-go/dataintegrity package.
240+
return string(proofValue)
238241
}
239242

240243
return base64.RawURLEncoding.EncodeToString(proofValue)

doc/ld/proof/proof_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestProof(t *testing.T) {
3030
"domain": "abc.com",
3131
"nonce": "",
3232
"proofValue": proofValueBase64,
33+
"cryptosuite": "ecdsa-rdfc-2019",
3334
})
3435
require.NoError(t, err)
3536

@@ -47,6 +48,7 @@ func TestProof(t *testing.T) {
4748
require.Equal(t, "abc.com", p.Domain)
4849
require.Equal(t, []byte(""), p.Nonce)
4950
require.Equal(t, proofValueBytes, p.ProofValue)
51+
//require.Equal(t, "ecdsa-rdfc-2019", p.CryptoSuite)
5052

5153
// test proof with multibase encoding
5254
p, err = NewProof(map[string]interface{}{
@@ -57,6 +59,7 @@ func TestProof(t *testing.T) {
5759
"domain": "abc.com",
5860
"nonce": "",
5961
"proofValue": proofValueMultibase,
62+
//"cryptosuite": "eddsa-rdfc-2022",
6063
})
6164
require.NoError(t, err)
6265

@@ -74,6 +77,7 @@ func TestProof(t *testing.T) {
7477
require.Equal(t, "abc.com", p.Domain)
7578
require.Equal(t, []byte(""), p.Nonce)
7679
require.Equal(t, proofValueBytes, p.ProofValue)
80+
//require.Equal(t, "eddsa-rdfc-2022", p.CryptoSuite)
7781

7882
// test created time with milliseconds section
7983
p, err = NewProof(map[string]interface{}{
@@ -391,6 +395,7 @@ func TestProof_JSONLdObject(t *testing.T) {
391395
Domain: "internal",
392396
Nonce: nonceBase64,
393397
Challenge: "sample-challenge-xyz",
398+
//CryptoSuite: "eddsa-rdfc-2022",
394399
}
395400

396401
pJSONLd := p.JSONLdObject()
@@ -403,6 +408,7 @@ func TestProof_JSONLdObject(t *testing.T) {
403408
r.Equal("internal", pJSONLd["domain"])
404409
r.Equal("abc", pJSONLd["nonce"])
405410
r.Equal("sample-challenge-xyz", pJSONLd["challenge"])
411+
r.Equal("eddsa-rdfc-2022", pJSONLd["cryptosuite"])
406412

407413
// test created time with milliseconds section
408414
created, err = time.Parse(time.RFC3339Nano, "2018-03-15T00:00:00.972Z")

doc/signature/signer/signer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func New(signatureSuites ...SignatureSuite) *DocumentSigner {
3636
}
3737

3838
// Sign will sign JSON LD document.
39+
// Deprecated. Please use vc-go/verifiable.AddDIDLinkedDataProof().
3940
func (signer *DocumentSigner) Sign(
4041
context *Context,
4142
jsonLdDoc []byte,

doc/signature/verifier/verifier.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func New(resolver keyResolver, suites ...api.VerifierSuite) (*DocumentVerifier,
4040
}
4141

4242
// Verify will verify document proofs.
43+
// Deprecated. Please use vc-go/verifiable.VerifyDIDProof().
4344
func (dv *DocumentVerifier) Verify(jsonLdDoc []byte, opts ...processor.Opts) error {
4445
var jsonLdObject map[string]interface{}
4546

@@ -52,6 +53,7 @@ func (dv *DocumentVerifier) Verify(jsonLdDoc []byte, opts ...processor.Opts) err
5253
}
5354

5455
// VerifyObject will verify document proofs for JSON LD object.
56+
// Deprecated. Please use vc-go/verifiable.VerifyDIDProof().
5557
func (dv *DocumentVerifier) VerifyObject(jsonLdObject map[string]interface{}, opts ...processor.Opts) error {
5658
proofs, err := proof.GetProofs(jsonLdObject)
5759
if err != nil {

0 commit comments

Comments
 (0)