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

82 lines
2.2 KiB
Go

package libkaspawallet
import (
"fmt"
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet/bip32"
"github.com/kaspanet/kaspad/domain/dagconfig"
"github.com/pkg/errors"
"github.com/tyler-smith/go-bip39"
)
// CreateMnemonic creates a new bip-39 compatible mnemonic
func CreateMnemonic() (string, error) {
const bip39BitSize = 256
entropy, _ := bip39.NewEntropy(bip39BitSize)
return bip39.NewMnemonic(entropy)
}
func defaultPath(isMultisig bool) string {
const (
singleSignerPurpose = 44
// Note: this is not entirely compatible to BIP 45 since
// BIP 45 doesn't have a coin type in its derivation path.
multiSigPurpose = 45
)
purpose := singleSignerPurpose
if isMultisig {
purpose = multiSigPurpose
}
// TODO: Register the coin type in https://github.com/satoshilabs/slips/blob/master/slip-0044.md
const coinType = 111111
return fmt.Sprintf("m/%d'/%d'/0'", coinType, purpose)
}
// MasterPublicKeyFromMnemonic returns the master public key with the correct derivation for the given mnemonic.
func MasterPublicKeyFromMnemonic(params *dagconfig.Params, mnemonic string, isMultisig bool) (string, error) {
path := defaultPath(isMultisig)
extendedKey, err := extendedKeyFromMnemonicAndPath(mnemonic, path, params)
if err != nil {
return "", err
}
extendedPublicKey, err := extendedKey.Public()
if err != nil {
return "", err
}
return extendedPublicKey.String(), nil
}
func extendedKeyFromMnemonicAndPath(mnemonic string, path string, params *dagconfig.Params) (*bip32.ExtendedKey, error) {
seed := bip39.NewSeed(mnemonic, "")
version, err := versionFromParams(params)
if err != nil {
return nil, err
}
master, err := bip32.NewMasterWithPath(seed, version, path)
if err != nil {
return nil, err
}
return master, nil
}
func versionFromParams(params *dagconfig.Params) ([4]byte, error) {
switch params.Name {
case dagconfig.MainnetParams.Name:
return bip32.KaspaMainnetPrivate, nil
case dagconfig.TestnetParams.Name:
return bip32.KaspaTestnetPrivate, nil
case dagconfig.DevnetParams.Name:
return bip32.KaspaDevnetPrivate, nil
case dagconfig.SimnetParams.Name:
return bip32.KaspaSimnetPrivate, nil
}
return [4]byte{}, errors.Errorf("unknown network %s", params.Name)
}