mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-25 08:16:46 +00:00

* Implement bip32 * Unite private and public extended keys * Change variable names * Change test name and add comment * Rename var name * Rename ckd.go to child_key_derivation.go * Rename ser32 -> serializeUint32 * Add PrivateKey method * Rename Path -> DeriveFromPath * Add comment to validateChecksum * Remove redundant condition from parsePath * Rename Fingerprint->ParentFingerprint * Merge hardened and non-hardened paths in calcI * Change fingerPrintFromPoint to method * Move hash160 to hash.go * Fix a bug in calcI * Simplify doubleSha256 * Remove slice end bound * Split long line * Change KaspaMainnetPrivate/public to represent kprv/kpub * Add comments * Fix comment * Copy base58 library to kaspad * Add versions for all networks * Change versions to hex * Add comments Co-authored-by: Svarog <feanorr@gmail.com> Co-authored-by: Elichai Turkel <elichai.turkel@gmail.com>
109 lines
2.6 KiB
Go
109 lines
2.6 KiB
Go
package bip32
|
|
|
|
import (
|
|
"github.com/btcsuite/btcutil/base58"
|
|
"github.com/kaspanet/go-secp256k1"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// ExtendedKey is a bip32 extended key
|
|
type ExtendedKey struct {
|
|
privateKey *secp256k1.ECDSAPrivateKey
|
|
publicKey *secp256k1.ECDSAPublicKey
|
|
Version [4]byte
|
|
Depth uint8
|
|
ParentFingerprint [4]byte
|
|
ChildNumber uint32
|
|
ChainCode [32]byte
|
|
}
|
|
|
|
// PrivateKey returns the ECDSA private key associated with the extended key
|
|
func (extKey *ExtendedKey) PrivateKey() *secp256k1.ECDSAPrivateKey {
|
|
return extKey.privateKey
|
|
}
|
|
|
|
// PublicKey returns the ECDSA public key associated with the extended key
|
|
func (extKey *ExtendedKey) PublicKey() (*secp256k1.ECDSAPublicKey, error) {
|
|
if extKey.publicKey != nil {
|
|
return extKey.publicKey, nil
|
|
}
|
|
|
|
publicKey, err := extKey.privateKey.ECDSAPublicKey()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
extKey.publicKey = publicKey
|
|
return publicKey, nil
|
|
}
|
|
|
|
// IsPrivate returns whether the extended key is private
|
|
func (extKey *ExtendedKey) IsPrivate() bool {
|
|
return extKey.privateKey != nil
|
|
}
|
|
|
|
// Public returns public version of the extended key
|
|
func (extKey *ExtendedKey) Public() (*ExtendedKey, error) {
|
|
if !extKey.IsPrivate() {
|
|
return extKey, nil
|
|
}
|
|
|
|
publicKey, err := extKey.PublicKey()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "error calculating publicKey")
|
|
}
|
|
|
|
version, err := toPublicVersion(extKey.Version)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &ExtendedKey{
|
|
publicKey: publicKey,
|
|
Version: version,
|
|
Depth: extKey.Depth,
|
|
ParentFingerprint: extKey.ParentFingerprint,
|
|
ChildNumber: extKey.ChildNumber,
|
|
ChainCode: extKey.ChainCode,
|
|
}, nil
|
|
}
|
|
|
|
// DeriveFromPath returns the extended key derived from the given path
|
|
func (extKey *ExtendedKey) DeriveFromPath(pathString string) (*ExtendedKey, error) {
|
|
path, err := parsePath(pathString)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return extKey.path(path)
|
|
}
|
|
|
|
func (extKey *ExtendedKey) path(path *path) (*ExtendedKey, error) {
|
|
if path.isPrivate && !extKey.IsPrivate() {
|
|
return nil, errors.Errorf("extended public keys cannot handle a private path")
|
|
}
|
|
|
|
descendantExtKey := extKey
|
|
for _, index := range path.indexes {
|
|
var err error
|
|
descendantExtKey, err = descendantExtKey.Child(index)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if !path.isPrivate {
|
|
return descendantExtKey.Public()
|
|
}
|
|
|
|
return descendantExtKey, nil
|
|
}
|
|
|
|
func (extKey *ExtendedKey) String() string {
|
|
serialized, err := extKey.serialize()
|
|
if err != nil {
|
|
panic(errors.Wrap(err, "error serializing key"))
|
|
}
|
|
return base58.Encode(serialized)
|
|
}
|