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
/
crypto_test.go
134 lines (108 loc) · 3.6 KB
/
crypto_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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)
}