2024-09-09 08:50:15 -04:00
|
|
|
package did
|
|
|
|
|
|
|
|
|
|
import (
|
2024-10-14 15:16:38 +02:00
|
|
|
"crypto/rand"
|
2024-09-09 08:50:15 -04:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
crypto "github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/crypto/pb"
|
|
|
|
|
"github.com/multiformats/go-multicodec"
|
|
|
|
|
"github.com/multiformats/go-varint"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-14 15:16:38 +02:00
|
|
|
// GenerateEd25519 generates an Ed25519 private key and the matching DID.
|
|
|
|
|
// This is the RECOMMENDED algorithm.
|
|
|
|
|
func GenerateEd25519() (crypto.PrivKey, DID, error) {
|
|
|
|
|
priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, Undef, nil
|
|
|
|
|
}
|
|
|
|
|
did, err := FromPubKey(pub)
|
|
|
|
|
return priv, did, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateRSA generates a RSA private key and the matching DID.
|
|
|
|
|
func GenerateSecp256k1() (crypto.PrivKey, DID, error) {
|
|
|
|
|
// NIST Special Publication 800-57 Part 1 Revision 5
|
|
|
|
|
// Section 5.6.1.1 (Table 2)
|
|
|
|
|
// Paraphrased: 2048-bit RSA keys are secure until 2030 and 3072-bit keys are recommended for longer-term security.
|
|
|
|
|
const keyLength = 3072
|
|
|
|
|
|
|
|
|
|
priv, pub, err := crypto.GenerateRSAKeyPair(keyLength, rand.Reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, Undef, nil
|
|
|
|
|
}
|
|
|
|
|
did, err := FromPubKey(pub)
|
|
|
|
|
return priv, did, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GenerateEd25519 generates a Secp256k1 private key and the matching DID.
|
|
|
|
|
func GenerateRSA() (crypto.PrivKey, DID, error) {
|
|
|
|
|
priv, pub, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, Undef, nil
|
|
|
|
|
}
|
|
|
|
|
did, err := FromPubKey(pub)
|
|
|
|
|
return priv, did, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 08:50:15 -04:00
|
|
|
func FromPrivKey(privKey crypto.PrivKey) (DID, error) {
|
|
|
|
|
return FromPubKey(privKey.GetPublic())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FromPubKey(pubKey crypto.PubKey) (DID, error) {
|
|
|
|
|
code, ok := map[pb.KeyType]multicodec.Code{
|
|
|
|
|
pb.KeyType_Ed25519: multicodec.Ed25519Pub,
|
|
|
|
|
pb.KeyType_RSA: multicodec.RsaPub,
|
|
|
|
|
pb.KeyType_Secp256k1: multicodec.Secp256k1Pub,
|
|
|
|
|
pb.KeyType_ECDSA: multicodec.Es256,
|
|
|
|
|
}[pubKey.Type()]
|
|
|
|
|
if !ok {
|
2024-10-14 15:16:38 +02:00
|
|
|
return Undef, errors.New("unsupported key type")
|
2024-09-09 08:50:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pubBytes, err := pubKey.Raw()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return Undef, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DID{
|
2024-10-14 15:16:38 +02:00
|
|
|
code: code,
|
|
|
|
|
bytes: string(append(varint.ToUvarint(uint64(code)), pubBytes...)),
|
2024-09-09 08:50:15 -04:00
|
|
|
}, nil
|
|
|
|
|
}
|
2024-09-11 07:12:54 -04:00
|
|
|
|
|
|
|
|
func ToPubKey(s string) (crypto.PubKey, error) {
|
|
|
|
|
id, err := Parse(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id.PubKey()
|
|
|
|
|
}
|