github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/subkey/scheme.go (about)

     1  package subkey
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Scheme represents a cryptography scheme.
     8  type Scheme interface {
     9  	fmt.Stringer
    10  	Generate() (KeyPair, error)
    11  	FromSeed(seed []byte) (KeyPair, error)
    12  	FromPhrase(phrase, password string) (KeyPair, error)
    13  	Derive(pair KeyPair, djs []DeriveJunction) (KeyPair, error)
    14  	FromPublicKey([]byte) (PublicKey, error)
    15  }
    16  
    17  // DeriveKeyPair derives the Keypair from the URI using the provided cryptography scheme.
    18  func DeriveKeyPair(scheme Scheme, uri string) (kp KeyPair, err error) {
    19  	phrase, path, pwd, err := splitURI(uri)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	if b, ok := DecodeHex(phrase); ok {
    25  		kp, err = scheme.FromSeed(b)
    26  	} else {
    27  		kp, err = scheme.FromPhrase(phrase, pwd)
    28  	}
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	djs, err := deriveJunctions(derivePath(path))
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	return scheme.Derive(kp, djs)
    39  }