OSDN Git Service

a464c96f130eefff63a48482c933185b679dafee
[bytom/vapor.git] / crypto / csp / util.go
1 // csp is a package of cipher service provider
2 package csp
3
4 import (
5         "crypto"
6         "io"
7
8         vcrypto "github.com/vapor/crypto"
9         edchainkd "github.com/vapor/crypto/ed25519/chainkd"
10 )
11
12 // Utility functions
13
14 func NewXKeys(r io.Reader) (xprv vcrypto.XPrvKeyer, xpub vcrypto.XPubKeyer, err error) {
15         // TODO: if ... create sm2 xprv and xpub
16         // return .....
17
18         // if ... create ed25519 xprv and xpub
19         return edchainkd.NewXKeys(r)
20 }
21
22 func XPubKeys(xpubs []vcrypto.XPubKeyer) []crypto.PublicKey {
23         res := make([]crypto.PublicKey, 0, len(xpubs))
24         for _, xpub := range xpubs {
25                 switch xpb := xpub.(type) {
26                 case edchainkd.XPub:
27                         res = append(res, xpb.PublicKey())
28                 }
29         }
30         return res
31 }
32
33 func DeriveXPubs(xpubs []vcrypto.XPubKeyer, path [][]byte) []vcrypto.XPubKeyer {
34         res := make([]vcrypto.XPubKeyer, 0, len(xpubs))
35         for _, xpub := range xpubs {
36                 switch xpb := xpub.(type) {
37                 case edchainkd.XPub:
38                         d := xpb.Derive(path)
39                         res = append(res, d)
40                 }
41         }
42         return res
43 }