2024-10-24 12:03:23 +02:00
|
|
|
//go:build jwx_es256k
|
2024-10-17 15:19:49 -04:00
|
|
|
|
|
|
|
|
package did_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-10-24 12:03:23 +02:00
|
|
|
|
2024-10-17 15:19:49 -04:00
|
|
|
"github.com/ucan-wg/go-ucan/did"
|
2024-10-24 12:03:23 +02:00
|
|
|
"github.com/ucan-wg/go-ucan/did/testvectors"
|
2024-10-17 15:19:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TestDidKeyVectors executes tests read from the [test vector files] provided
|
|
|
|
|
// as part of the DID Key method's [specification].
|
|
|
|
|
//
|
|
|
|
|
// [test vector files]: https://github.com/w3c-ccg/did-method-key/tree/main/test-vectors
|
|
|
|
|
// [specification]: https://w3c-ccg.github.io/did-method-key
|
|
|
|
|
func TestDidKeyVectors(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
for _, f := range []string{
|
|
|
|
|
// TODO: These test vectors are not supported by go-libp2p/core/crypto
|
|
|
|
|
// "bls12381.json",
|
|
|
|
|
"ed25519-x25519.json",
|
|
|
|
|
"nist-curves.json",
|
|
|
|
|
"rsa.json",
|
|
|
|
|
"secp256k1.json",
|
|
|
|
|
// This test vector only contains a DID Document
|
|
|
|
|
// "x25519.json",
|
|
|
|
|
} {
|
2024-10-24 12:51:21 +02:00
|
|
|
vectors := loadTestVectors(t, f)
|
2024-10-17 15:19:49 -04:00
|
|
|
|
|
|
|
|
t.Run(f, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
2024-10-24 12:51:21 +02:00
|
|
|
for k, vector := range vectors {
|
2024-10-17 15:19:49 -04:00
|
|
|
t.Run(k, func(t *testing.T) {
|
2024-10-24 12:51:21 +02:00
|
|
|
// round-trip pubkey-->did-->pubkey, verified against the test vectors.
|
2024-10-17 15:19:49 -04:00
|
|
|
|
2024-10-24 12:51:21 +02:00
|
|
|
exp := vectorPubKey(t, vector)
|
2024-10-17 15:19:49 -04:00
|
|
|
|
|
|
|
|
id, err := did.FromPubKey(exp)
|
|
|
|
|
require.NoError(t, err, f, k)
|
|
|
|
|
act, err := id.PubKey()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, k, id.String(), f, k)
|
|
|
|
|
assert.Equal(t, exp, act)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-24 12:03:23 +02:00
|
|
|
func loadTestVectors(t *testing.T, filename string) testvectors.Vectors {
|
2024-10-17 15:19:49 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2024-10-24 12:03:23 +02:00
|
|
|
data, err := os.ReadFile(filepath.Join("testvectors", filename))
|
2024-10-17 15:19:49 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2024-10-24 12:03:23 +02:00
|
|
|
var vs testvectors.Vectors
|
2024-10-17 15:19:49 -04:00
|
|
|
|
|
|
|
|
require.NoError(t, json.Unmarshal(data, &vs))
|
|
|
|
|
|
|
|
|
|
return vs
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-24 12:03:23 +02:00
|
|
|
func vectorPubKey(t *testing.T, v testvectors.Vector) crypto.PubKey {
|
2024-10-17 15:19:49 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2024-10-24 12:03:23 +02:00
|
|
|
pubKey, err := v.PubKey()
|
2024-10-17 15:19:49 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotZero(t, pubKey)
|
|
|
|
|
|
|
|
|
|
return pubKey
|
|
|
|
|
}
|