This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updates * cid * consistent api * typo * cycles
- Loading branch information
1 parent
8f8ea12
commit 487d633
Showing
9 changed files
with
165 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Controlled Identifier Documents | ||
|
||
This directory contains an implementation of the [Controlled Identifier Document 1.0](https://w3c.github.io/cid/) | ||
specification. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package vc_jose_cose_go | ||
|
||
import ( | ||
"fmt" | ||
"github.com/TBD54566975/vc-jose-cose-go/cid" | ||
"github.com/TBD54566975/vc-jose-cose-go/util" | ||
"github.com/goccy/go-json" | ||
"github.com/lestrrat-go/jwx/v2/jwa" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestGenerateKeys is used to generate sample cid document verification methods | ||
func TestGenerateKeys(t *testing.T) { | ||
t.Skip("skipping test as it is not needed except for local testing") | ||
|
||
tests := []struct { | ||
name string | ||
curve jwa.EllipticCurveAlgorithm | ||
kid string | ||
}{ | ||
{"EC P-256", jwa.P256, "key-1"}, | ||
{"EC P-384", jwa.P384, "key-2"}, | ||
{"EC P-521", jwa.P521, "key-3"}, | ||
{"OKP EdDSA", jwa.Ed25519, "key-4"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
key, err := util.GenerateJWK(tt.curve) | ||
require.NoError(t, err) | ||
assert.NotNil(t, key) | ||
|
||
pubKey, err := key.PublicKey() | ||
require.NoError(t, err) | ||
assert.NotNil(t, pubKey) | ||
|
||
id := "https://example.issuer/6c427e8392ab4057b93356fbb9022ecb" | ||
vm := cid.VerificationMethod{ | ||
ID: fmt.Sprintf("%s#%s", id, tt.kid), | ||
Type: cid.TypeJSONWebKey, | ||
Controller: util.SingleOrArray[string]{id}, | ||
PublicKeyJWK: pubKey, | ||
SecretKeyJWK: key, | ||
} | ||
|
||
vmJSONBytes, err := json.Marshal(vm) | ||
require.NoError(t, err) | ||
t.Logf("\n%s\n", string(vmJSONBytes)) | ||
}) | ||
} | ||
} | ||
|
||
func TestKeyToBytes(t *testing.T) { | ||
for _, keyType := range util.GetSupportedKeyTypes() { | ||
t.Run(string(keyType), func(t *testing.T) { | ||
pub, priv, err := util.GenerateKeyByKeyType(keyType) | ||
|
||
assert.NoError(t, err) | ||
assert.NotEmpty(t, pub) | ||
assert.NotEmpty(t, priv) | ||
|
||
pubKeyBytes, err := util.PubKeyToBytes(pub) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, pubKeyBytes) | ||
|
||
reconstructedPub, err := util.BytesToPubKey(pubKeyBytes, keyType) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, reconstructedPub) | ||
assert.EqualValues(t, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := util.PrivKeyToBytes(priv) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, privKeyBytes) | ||
|
||
reconstructedPriv, err := util.BytesToPrivKey(privKeyBytes, keyType) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, reconstructedPriv) | ||
assert.EqualValues(t, priv, reconstructedPriv) | ||
|
||
kt, err := util.GetKeyTypeFromPrivateKey(priv) | ||
assert.NoError(t, err) | ||
assert.Equal(t, keyType, kt) | ||
}) | ||
} | ||
|
||
for _, keyType := range util.GetSupportedKeyTypes() { | ||
t.Run(string(keyType)+" with pointers", func(t *testing.T) { | ||
pub, priv, err := util.GenerateKeyByKeyType(keyType) | ||
|
||
assert.NoError(t, err) | ||
assert.NotEmpty(t, pub) | ||
assert.NotEmpty(t, priv) | ||
|
||
pubKeyBytes, err := util.PubKeyToBytes(&pub) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, pubKeyBytes) | ||
|
||
reconstructedPub, err := util.BytesToPubKey(pubKeyBytes, keyType) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, reconstructedPub) | ||
assert.EqualValues(t, pub, reconstructedPub) | ||
|
||
privKeyBytes, err := util.PrivKeyToBytes(&priv) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, privKeyBytes) | ||
|
||
reconstructedPriv, err := util.BytesToPrivKey(privKeyBytes, keyType) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, reconstructedPriv) | ||
assert.EqualValues(t, priv, reconstructedPriv) | ||
|
||
kt, err := util.GetKeyTypeFromPrivateKey(&priv) | ||
assert.NoError(t, err) | ||
assert.Equal(t, keyType, kt) | ||
}) | ||
} | ||
} | ||
|
||
func TestSECP256k1Conversions(t *testing.T) { | ||
pk, sk, err := util.GenerateSECP256k1Key() | ||
assert.NoError(t, err) | ||
|
||
ecdsaPK := pk.ToECDSA() | ||
ecdsaSK := sk.ToECDSA() | ||
|
||
gotPK := util.SECP256k1ECDSAPubKeyToSECP256k1(*ecdsaPK) | ||
gotSK := util.SECP256k1ECDSASPrivKeyToSECP256k1(*ecdsaSK) | ||
|
||
assert.Equal(t, pk, gotPK) | ||
assert.Equal(t, sk, gotSK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.