2024-09-09 08:50:15 -04:00
|
|
|
package did_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-09-17 11:17:24 -04:00
|
|
|
"github.com/ucan-wg/go-ucan/did"
|
2024-09-09 08:50:15 -04:00
|
|
|
)
|
|
|
|
|
|
2024-09-11 07:12:54 -04:00
|
|
|
const (
|
|
|
|
|
exampleDIDStr = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
|
|
|
|
|
examplePubKeyStr = "Lm/M42cB3HkUiODQsXRcweM6TByfzEHGO9ND274JcOY="
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-09 08:50:15 -04:00
|
|
|
func TestFromPubKey(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
2024-09-11 07:12:54 -04:00
|
|
|
id, err := did.FromPubKey(examplePubKey(t))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, exampleDID(t), id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestToPubKey(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
pubKey, err := did.ToPubKey(exampleDIDStr)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, examplePubKey(t), pubKey)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exampleDID(t *testing.T) did.DID {
|
|
|
|
|
t.Helper()
|
2024-09-09 08:50:15 -04:00
|
|
|
|
2024-09-11 07:12:54 -04:00
|
|
|
id, err := did.Parse(exampleDIDStr)
|
2024-09-09 08:50:15 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2024-09-11 07:12:54 -04:00
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func examplePubKey(t *testing.T) crypto.PubKey {
|
|
|
|
|
t.Helper()
|
2024-09-09 08:50:15 -04:00
|
|
|
|
2024-09-11 07:12:54 -04:00
|
|
|
pubKeyCfg, err := crypto.ConfigDecodeKey(examplePubKeyStr)
|
2024-09-09 08:50:15 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2024-09-11 07:12:54 -04:00
|
|
|
pubKey, err := crypto.UnmarshalEd25519PublicKey(pubKeyCfg)
|
2024-09-09 08:50:15 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2024-09-11 07:12:54 -04:00
|
|
|
return pubKey
|
2024-09-09 08:50:15 -04:00
|
|
|
}
|