Beispiel #1
0
// NewMasterHDKey computes the root HD key from the given seed, key and private
// version.
func NewMaster(seed, key []byte, version uint16) (*HDKey, error) {
	if len(seed) < MinSeedSize || len(seed) > MaxSeedSize {
		return nil, ErrInvalidSeedLength
	}

	// il, ir = HMAC-512(key, seed)
	il, chainCode := util.HMAC512Split(key, seed)
	defer func() { util.Zero(il); util.Zero(chainCode) }()

	// Left 32 bytes becomes master secret key, clean up temporary SecretKey
	sk, err := eckey.NewSecretKey(il)
	if err != nil {
		return nil, ErrUnusableSeed
	}
	defer sk.Zero()

	return newHDSecretKey(version, 0, 0, 0, chainCode, sk), nil
}
Beispiel #2
0
// validPublicOrSecretBytes takes a byte slice, assumed to be childKeySize
// bytes in length, and determines whether the associated public or private key
// corresponds to a valid point on secp256k1 curve.
func validPublicOrSecretBytes(b []byte) error {
	// Assume derived key is a CompressedPublicKey if first byte is not 0.
	if b[0] != 0 {
		cpk, err := eckey.NewCompressedPublicKey(b)
		if err != nil {
			return nil
		}

		_, err = cpk.Uncompress()

		return err
	}

	// Otherwise verify SecretKey
	if _, err := eckey.NewSecretKey(b[1:]); err != nil {
		return err
	}

	return nil
}