Skip to content

Commit 1f14678

Browse files
committed
add support for the Multikey verification method
1 parent 4503b2e commit 1f14678

File tree

17 files changed

+207
-65
lines changed

17 files changed

+207
-65
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package helpers
1+
package testsuite
22

33
import (
44
"fmt"

crypto/ed25519/key_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package ed25519
33
import (
44
"testing"
55

6-
"github.com/INFURA/go-did/crypto/internal"
6+
"github.com/INFURA/go-did/crypto/_testsuite"
77
)
88

9-
var harness = helpers.TestHarness[PublicKey, PrivateKey]{
9+
var harness = testsuite.TestHarness[PublicKey, PrivateKey]{
1010
Name: "ed25519",
1111
GenerateKeyPair: GenerateKeyPair,
1212
PublicKeyFromBytes: PublicKeyFromBytes,
@@ -23,9 +23,9 @@ var harness = helpers.TestHarness[PublicKey, PrivateKey]{
2323
}
2424

2525
func TestSuite(t *testing.T) {
26-
helpers.TestSuite(t, harness)
26+
testsuite.TestSuite(t, harness)
2727
}
2828

2929
func BenchmarkSuite(b *testing.B) {
30-
helpers.BenchSuite(b, harness)
30+
testsuite.BenchSuite(b, harness)
3131
}

crypto/ed25519/public.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"golang.org/x/crypto/cryptobyte"
1111

1212
"github.com/INFURA/go-did/crypto"
13-
"github.com/INFURA/go-did/crypto/internal"
13+
"github.com/INFURA/go-did/crypto/_helpers"
1414
)
1515

1616
var _ crypto.SigningPublicKey = &PublicKey{}

crypto/p256/key_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package p256
33
import (
44
"testing"
55

6-
"github.com/INFURA/go-did/crypto/internal"
6+
"github.com/INFURA/go-did/crypto/_testsuite"
77
)
88

9-
var harness = helpers.TestHarness[*PublicKey, *PrivateKey]{
9+
var harness = testsuite.TestHarness[*PublicKey, *PrivateKey]{
1010
Name: "p256",
1111
GenerateKeyPair: GenerateKeyPair,
1212
PublicKeyFromBytes: PublicKeyFromBytes,
@@ -23,9 +23,9 @@ var harness = helpers.TestHarness[*PublicKey, *PrivateKey]{
2323
}
2424

2525
func TestSuite(t *testing.T) {
26-
helpers.TestSuite(t, harness)
26+
testsuite.TestSuite(t, harness)
2727
}
2828

2929
func BenchmarkSuite(b *testing.B) {
30-
helpers.BenchSuite(b, harness)
30+
testsuite.BenchSuite(b, harness)
3131
}

crypto/p256/public.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"math/big"
1111

1212
"github.com/INFURA/go-did/crypto"
13-
helpers "github.com/INFURA/go-did/crypto/internal"
13+
helpers "github.com/INFURA/go-did/crypto/_helpers"
1414
)
1515

1616
var _ crypto.SigningPublicKey = (*PublicKey)(nil)

crypto/x25519/key_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55

66
"github.com/stretchr/testify/require"
77

8+
"github.com/INFURA/go-did/crypto/_testsuite"
89
"github.com/INFURA/go-did/crypto/ed25519"
9-
"github.com/INFURA/go-did/crypto/internal"
1010
)
1111

12-
var harness = helpers.TestHarness[*PublicKey, *PrivateKey]{
12+
var harness = testsuite.TestHarness[*PublicKey, *PrivateKey]{
1313
Name: "x25519",
1414
GenerateKeyPair: GenerateKeyPair,
1515
PublicKeyFromBytes: PublicKeyFromBytes,
@@ -26,11 +26,11 @@ var harness = helpers.TestHarness[*PublicKey, *PrivateKey]{
2626
}
2727

2828
func TestSuite(t *testing.T) {
29-
helpers.TestSuite(t, harness)
29+
testsuite.TestSuite(t, harness)
3030
}
3131

3232
func BenchmarkSuite(b *testing.B) {
33-
helpers.BenchSuite(b, harness)
33+
testsuite.BenchSuite(b, harness)
3434
}
3535

3636
func TestEd25519ToX25519(t *testing.T) {

crypto/x25519/public.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"math/big"
99

1010
"github.com/INFURA/go-did/crypto"
11+
helpers "github.com/INFURA/go-did/crypto/_helpers"
1112
"github.com/INFURA/go-did/crypto/ed25519"
12-
helpers "github.com/INFURA/go-did/crypto/internal"
1313
)
1414

1515
var _ crypto.PublicKey = (*PublicKey)(nil)

interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type VerificationMethodSignature interface {
102102
VerificationMethod
103103

104104
// Verify checks that 'sig' is a valid signature of 'data'.
105-
Verify(data []byte, sig []byte) bool
105+
Verify(data []byte, sig []byte) (bool, error)
106106
}
107107

108108
// VerificationMethodKeyAgreement is a VerificationMethod implementing a shared key agreement.

methods/did-key/key.go

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import (
44
"fmt"
55
"strings"
66

7-
mbase "github.com/multiformats/go-multibase"
8-
"github.com/multiformats/go-varint"
9-
107
"github.com/INFURA/go-did"
118
"github.com/INFURA/go-did/crypto"
9+
"github.com/INFURA/go-did/crypto/_helpers"
1210
"github.com/INFURA/go-did/crypto/ed25519"
1311
"github.com/INFURA/go-did/crypto/p256"
1412
"github.com/INFURA/go-did/crypto/x25519"
1513
"github.com/INFURA/go-did/verifications/ed25519"
14+
"github.com/INFURA/go-did/verifications/multikey"
1615
"github.com/INFURA/go-did/verifications/x25519"
1716
)
1817

@@ -39,61 +38,45 @@ func Decode(identifier string) (did.DID, error) {
3938

4039
msi := identifier[len(keyPrefix):]
4140

42-
baseCodec, bytes, err := mbase.Decode(msi)
41+
code, bytes, err := helpers.PublicKeyMultibaseDecode(msi)
4342
if err != nil {
44-
return nil, fmt.Errorf("%w: %w", did.ErrInvalidDid, err)
43+
return nil, err
4544
}
46-
// the specification enforces that encoding
47-
if baseCodec != mbase.Base58BTC {
48-
return nil, fmt.Errorf("%w: not Base58BTC encoded", did.ErrInvalidDid)
45+
46+
decoder, ok := map[uint64]func(b []byte) (crypto.PublicKey, error){
47+
ed25519.MultibaseCode: func(b []byte) (crypto.PublicKey, error) { return ed25519.PublicKeyFromBytes(b) },
48+
p256.MultibaseCode: func(b []byte) (crypto.PublicKey, error) { return p256.PublicKeyFromBytes(b) },
49+
x25519.MultibaseCode: func(b []byte) (crypto.PublicKey, error) { return x25519.PublicKeyFromBytes(b) },
50+
}[code]
51+
if !ok {
52+
return nil, fmt.Errorf("%w: unsupported did:key multicodec: 0x%x", did.ErrInvalidDid, code)
4953
}
50-
code, read, err := varint.FromUvarint(bytes)
54+
55+
pub, err := decoder(bytes)
5156
if err != nil {
5257
return nil, fmt.Errorf("%w: %w", did.ErrInvalidDid, err)
5358
}
54-
55-
switch code {
56-
case ed25519.MultibaseCode:
57-
pub, err := ed25519.PublicKeyFromBytes(bytes[read:])
58-
if err != nil {
59-
return nil, fmt.Errorf("%w: %w", did.ErrInvalidDid, err)
60-
}
61-
return FromPublicKey(pub)
62-
case p256.MultibaseCode:
63-
pub, err := p256.PublicKeyFromBytes(bytes[read:])
64-
if err != nil {
65-
return nil, fmt.Errorf("%w: %w", did.ErrInvalidDid, err)
66-
}
67-
return FromPublicKey(pub)
68-
69-
// case Secp256k1: // TODO
70-
// case RSA: // TODO
71-
}
72-
73-
return nil, fmt.Errorf("%w: unsupported did:key multicodec: 0x%x", did.ErrInvalidDid, code)
59+
return FromPublicKey(pub)
7460
}
7561

7662
func FromPublicKey(pub crypto.PublicKey) (did.DID, error) {
77-
var err error
7863
switch pub := pub.(type) {
7964
case ed25519.PublicKey:
8065
d := DidKey{msi: pub.ToPublicKeyMultibase()}
81-
d.signature, err = ed25519vm.NewVerificationKey2020(fmt.Sprintf("did:key:%s#%s", d.msi, d.msi), pub, d)
82-
if err != nil {
83-
return nil, err
84-
}
66+
d.signature = ed25519vm.NewVerificationKey2020(fmt.Sprintf("did:key:%s#%s", d.msi, d.msi), pub, d)
8567
xpub, err := x25519.PublicKeyFromEd25519(pub)
8668
if err != nil {
8769
return nil, fmt.Errorf("%w: %w", did.ErrInvalidDid, err)
8870
}
8971
xmsi := xpub.ToPublicKeyMultibase()
90-
d.keyAgreement, err = x25519vm.NewKeyAgreementKey2020(fmt.Sprintf("did:key:%s#%s", d.msi, xmsi), xpub, d)
91-
if err != nil {
92-
return nil, fmt.Errorf("%w: %w", did.ErrInvalidDid, err)
93-
}
72+
d.keyAgreement = x25519vm.NewKeyAgreementKey2020(fmt.Sprintf("did:key:%s#%s", d.msi, xmsi), xpub, d)
73+
return d, nil
74+
case *p256.PublicKey:
75+
d := DidKey{msi: pub.ToPublicKeyMultibase()}
76+
mk := multikey.NewMultiKey(fmt.Sprintf("did:key:%s#%s", d.msi, d.msi), pub, d)
77+
d.signature = mk
78+
d.keyAgreement = mk
9479
return d, nil
95-
// case *p256.PublicKey:
96-
// d := DidKey{msi: pub.ToPublicKeyMultibase()}
9780

9881
default:
9982
return nil, fmt.Errorf("unsupported public key: %T", pub)

0 commit comments

Comments
 (0)