Ori Newman 4658f9d05c
Implement BIP 39 and HD wallet features (#1705)
* 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>
2021-05-19 10:03:23 +03:00

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)
}