mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-23 07:16:47 +00:00

* Naive bip39 with address reuse * Avoid address reuse in libkaspawallet * Add wallet daemon * Use daemon everywhere * Add forceOverride * Make CreateUnsignedTransaction endpoint receive amount in sompis * Collect close UTXOs * Filter out non-spendable UTXOs from selectUTXOs * Use different paths for multisig and non multisig * Fix tests to use non zero path * Fix multisig cosigner index detection * Add comments * Fix dump_unencrypted_data.go according to bip39 and bip32 * Fix wrong derivation path for multisig on wallet creation * Remove IsSynced endpoint and add validation if wallet is synced for the relevant endpoints * Rename server address to daemon address * Fix capacity for extendedPublicKeys * Use ReadBytes instead of ReadLine * Add validation when importing * Increment before using index value, and use it as is * Save keys file exactly where needed * Use %+v printErrorAndExit * Remove redundant consts * Rnemae collectCloseUTXOs and collectFarUTXOs * Move typedefs around * Add comment to addressesToQuery * Update collectUTXOsFromRecentAddresses comment about locks * Split collectUTXOs to small functions * Add sanity check * Add addEntryToUTXOSet function * Change validateIsSynced to isSynced * Simplify createKeyPairsFromFunction logic * Rename .Sync() to .Save() * Fix typo * Create bip39BitSize const * Add consts to purposes * Add multisig check for 'send' * Rename updatedPSTxBytes to partiallySignedTransaction * Change collectUTXOsFromFarAddresses's comment * Use setters for last used indexes * Don't use the pstx acronym * Fix SetPath * Remove spaces when reading lines * Fix walletserver to daemonaddress * Fix isUTXOSpendable to use DAA score Co-authored-by: Svarog <feanorr@gmail.com>
105 lines
2.5 KiB
Go
105 lines
2.5 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) {
|
|
descendantExtKey := extKey
|
|
for _, index := range path.indexes {
|
|
var err error
|
|
descendantExtKey, err = descendantExtKey.Child(index)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if path.isPublic {
|
|
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)
|
|
}
|