Live video on the AT Protocol
1package spkey
2
3import (
4 "crypto"
5 "fmt"
6
7 atcrypto "github.com/bluesky-social/indigo/atproto/crypto"
8 "github.com/decred/dcrd/dcrec/secp256k1"
9 "github.com/mr-tron/base58"
10)
11
12// returns private key, public key, error
13func GenerateStreamKey() (*atcrypto.PrivateKeyK256, *atcrypto.PublicKeyK256, error) {
14 priv, err := atcrypto.GeneratePrivateKeyK256()
15 if err != nil {
16 return nil, nil, err
17 }
18 pub, err := priv.PublicKey()
19 if err != nil {
20 return nil, nil, err
21 }
22
23 return priv, pub.(*atcrypto.PublicKeyK256), nil
24}
25
26func GenerateStreamKeyForDID(did string) (string, *atcrypto.PublicKeyK256, error) {
27 priv, pub, err := GenerateStreamKey()
28 if err != nil {
29 return "", nil, err
30 }
31 didBytes := []byte(did)
32 combinedBytes := append(priv.Bytes(), didBytes...)
33 multibaseKey := "z" + base58.Encode(combinedBytes)
34 return multibaseKey, pub, nil
35}
36
37func KeyToSigner(priv *atcrypto.PrivateKeyK256) (crypto.Signer, error) {
38 addrBytes := priv.Bytes()
39 key, _ := secp256k1.PrivKeyFromBytes(addrBytes)
40 if key == nil {
41 return nil, fmt.Errorf("invalid authorization key (not valid secp256k1)")
42 }
43 var signer crypto.Signer = key.ToECDSA()
44 return signer, nil
45}