Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding passphrase #2243

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/kaspawallet/daemon/server/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func (s *server) Send(_ context.Context, request *pb.SendRequest) (*pb.SendRespo
return nil, err
}

signedTransactions, err := s.signTransactions(unsignedTransactions, request.Password)
//TODO fix passphrase
signedTransactions, err := s.signTransactions(unsignedTransactions, request.Password, "")
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/kaspawallet/daemon/server/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ func (s *server) Sign(_ context.Context, request *pb.SignRequest) (*pb.SignRespo
s.lock.Lock()
defer s.lock.Unlock()

signedTransactions, err := s.signTransactions(request.UnsignedTransactions, request.Password)
//TODO: fix SignRequest pashphrase
signedTransactions, err := s.signTransactions(request.UnsignedTransactions, request.Password, "")
if err != nil {
return nil, err
}
return &pb.SignResponse{SignedTransactions: signedTransactions}, nil
}

func (s *server) signTransactions(unsignedTransactions [][]byte, password string) ([][]byte, error) {
func (s *server) signTransactions(unsignedTransactions [][]byte, password string, passphrase string) ([][]byte, error) {
mnemonics, err := s.keysFile.DecryptMnemonics(password)
if err != nil {
return nil, err
}
signedTransactions := make([][]byte, len(unsignedTransactions))
for i, unsignedTransaction := range unsignedTransactions {
signedTransaction, err := libkaspawallet.Sign(s.params, mnemonics, unsignedTransaction, s.keysFile.ECDSA)
signedTransaction, err := libkaspawallet.Sign(s.params, mnemonics, []string{passphrase}, unsignedTransaction, s.keysFile.ECDSA)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kaspawallet/daemon/server/split_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func TestEstimateMassAfterSignatures(t *testing.T) {
t.Fatalf("Error from estimateMassAfterSignatures: %s", err)
}

signedTxStep1Bytes, err := libkaspawallet.Sign(params, mnemonics[:1], unsignedTransactionBytes, false)
signedTxStep1Bytes, err := libkaspawallet.Sign(params, mnemonics[:1], []string{""}, unsignedTransactionBytes, false)
if err != nil {
t.Fatalf("Sign: %+v", err)
}

signedTxStep2Bytes, err := libkaspawallet.Sign(params, mnemonics[1:2], signedTxStep1Bytes, false)
signedTxStep2Bytes, err := libkaspawallet.Sign(params, mnemonics[1:2], []string{""}, signedTxStep1Bytes, false)
if err != nil {
t.Fatalf("Sign: %+v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/kaspawallet/dump_unencrypted_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ func dumpUnencryptedData(conf *dumpUnencryptedDataConfig) error {
return err
}


mnemonicPublicKeys := make(map[string]struct{})
for i, mnemonic := range mnemonics {
passphrase := keys.GetPassphrase("Passphrase:")
fmt.Printf("Mnemonic #%d:\n%s\n\n", i+1, mnemonic)
publicKey, err := libkaspawallet.MasterPublicKeyFromMnemonic(conf.NetParams(), mnemonic, len(keysFile.ExtendedPublicKeys) > 1)
publicKey, err := libkaspawallet.MasterPublicKeyFromMnemonic(conf.NetParams(), mnemonic, passphrase, len(keysFile.ExtendedPublicKeys) > 1)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/kaspawallet/keys/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ func encryptedMnemonicExtendedPublicKeyPairs(params *dagconfig.Params, mnemonics
extendedPublicKeys = make([]string, 0, len(mnemonics))

for _, mnemonic := range mnemonics {
extendedPublicKey, err := libkaspawallet.MasterPublicKeyFromMnemonic(params, mnemonic, isMultisig)
passphrase := []byte(GetPassword("Enter passphrase for the key file:"))
confirmPassphrase := []byte(GetPassphrase("Confirm passphrase:"))

if subtle.ConstantTimeCompare(passphrase, confirmPassphrase) != 1 {
return nil, nil, errors.New("Passphrases are not identical")
}
extendedPublicKey, err := libkaspawallet.MasterPublicKeyFromMnemonic(params, mnemonic, string(passphrase), isMultisig)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/kaspawallet/keys/get_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func GetPassword(prompt string) string {

return string(p)
}

func GetPassphrase(prompt string) string {
return GetPassword(prompt);
}
8 changes: 4 additions & 4 deletions cmd/kaspawallet/libkaspawallet/bip39.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func defaultPath(isMultisig bool) string {
}

// 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) {
func MasterPublicKeyFromMnemonic(params *dagconfig.Params, mnemonic string, passphrase string, isMultisig bool) (string, error) {
path := defaultPath(isMultisig)
extendedKey, err := extendedKeyFromMnemonicAndPath(mnemonic, path, params)
extendedKey, err := extendedKeyFromMnemonicAndPath(mnemonic, path, passphrase, params)
if err != nil {
return "", err
}
Expand All @@ -51,8 +51,8 @@ func MasterPublicKeyFromMnemonic(params *dagconfig.Params, mnemonic string, isMu
return extendedPublicKey.String(), nil
}

func extendedKeyFromMnemonicAndPath(mnemonic string, path string, params *dagconfig.Params) (*bip32.ExtendedKey, error) {
seed := bip39.NewSeed(mnemonic, "")
func extendedKeyFromMnemonicAndPath(mnemonic string, path string, passphrase string, params *dagconfig.Params) (*bip32.ExtendedKey, error) {
seed := bip39.NewSeed(mnemonic, passphrase)
version, err := versionFromParams(params)
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions cmd/kaspawallet/libkaspawallet/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ func rawTxInSignature(extendedKey *bip32.ExtendedKey, tx *externalapi.DomainTran
}

// Sign signs the transaction with the given private keys
func Sign(params *dagconfig.Params, mnemonics []string, serializedPSTx []byte, ecdsa bool) ([]byte, error) {
func Sign(params *dagconfig.Params, mnemonics []string, passphrases []string, serializedPSTx []byte, ecdsa bool) ([]byte, error) {
partiallySignedTransaction, err := serialization.DeserializePartiallySignedTransaction(serializedPSTx)
if err != nil {
return nil, err
}

for _, mnemonic := range mnemonics {
err = sign(params, mnemonic, partiallySignedTransaction, ecdsa)
for i, mnemonic := range mnemonics {
err = sign(params, mnemonic, passphrases[i], partiallySignedTransaction, ecdsa)
if err != nil {
return nil, err
}
}
return serialization.SerializePartiallySignedTransaction(partiallySignedTransaction)
}

func sign(params *dagconfig.Params, mnemonic string, partiallySignedTransaction *serialization.PartiallySignedTransaction, ecdsa bool) error {
func sign(params *dagconfig.Params, mnemonic string, passphrase string, partiallySignedTransaction *serialization.PartiallySignedTransaction, ecdsa bool) error {
if isTransactionFullySigned(partiallySignedTransaction) {
return nil
}
Expand All @@ -64,7 +64,7 @@ func sign(params *dagconfig.Params, mnemonic string, partiallySignedTransaction
for i, partiallySignedInput := range partiallySignedTransaction.PartiallySignedInputs {
isMultisig := len(partiallySignedInput.PubKeySignaturePairs) > 1
path := defaultPath(isMultisig)
extendedKey, err := extendedKeyFromMnemonicAndPath(mnemonic, path, params)
extendedKey, err := extendedKeyFromMnemonicAndPath(mnemonic, path, passphrase, params)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/kaspawallet/libkaspawallet/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestMultisig(t *testing.T) {
t.Fatal("Unexpectedly succeed to extract a valid transaction out of unsigned transaction")
}

signedTxStep1, err := libkaspawallet.Sign(params, mnemonics[:1], unsignedTransaction, ecdsa)
signedTxStep1, err := libkaspawallet.Sign(params, mnemonics[:1], []string{""}, unsignedTransaction, ecdsa)
if err != nil {
t.Fatalf("Sign: %+v", err)
}
Expand All @@ -139,7 +139,7 @@ func TestMultisig(t *testing.T) {
t.Fatalf("Transaction is not expected to be fully signed")
}

signedTxStep2, err := libkaspawallet.Sign(params, mnemonics[1:2], signedTxStep1, ecdsa)
signedTxStep2, err := libkaspawallet.Sign(params, mnemonics[1:2], []string{""}, signedTxStep1, ecdsa)
if err != nil {
t.Fatalf("Sign: %+v", err)
}
Expand All @@ -149,7 +149,7 @@ func TestMultisig(t *testing.T) {
t.Fatalf("ExtractTransaction: %+v", err)
}

signedTxOneStep, err := libkaspawallet.Sign(params, mnemonics[:2], unsignedTransaction, ecdsa)
signedTxOneStep, err := libkaspawallet.Sign(params, mnemonics[:2], []string{""}, unsignedTransaction, ecdsa)
if err != nil {
t.Fatalf("Sign: %+v", err)
}
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestP2PK(t *testing.T) {
t.Fatal("Unexpectedly succeed to extract a valid transaction out of unsigned transaction")
}

signedTx, err := libkaspawallet.Sign(params, mnemonics, unsignedTransaction, ecdsa)
signedTx, err := libkaspawallet.Sign(params, mnemonics, []string{""}, unsignedTransaction, ecdsa)
if err != nil {
t.Fatalf("Sign: %+v", err)
}
Expand Down Expand Up @@ -434,7 +434,7 @@ func TestMaxSompi(t *testing.T) {
t.Fatalf("CreateUnsignedTransactions: %+v", err)
}

signedTxWithLargeInputAmount, err := libkaspawallet.Sign(params, mnemonics, unsignedTxWithLargeInputAmount, false)
signedTxWithLargeInputAmount, err := libkaspawallet.Sign(params, mnemonics, []string{""}, unsignedTxWithLargeInputAmount, false)
if err != nil {
t.Fatalf("Sign: %+v", err)
}
Expand Down Expand Up @@ -485,7 +485,7 @@ func TestMaxSompi(t *testing.T) {
t.Fatalf("CreateUnsignedTransactions: %+v", err)
}

signedTxWithLargeInputAndOutputAmount, err := libkaspawallet.Sign(params, mnemonics, unsignedTxWithLargeInputAndOutputAmount, false)
signedTxWithLargeInputAndOutputAmount, err := libkaspawallet.Sign(params, mnemonics, []string{""}, unsignedTxWithLargeInputAndOutputAmount, false)
if err != nil {
t.Fatalf("Sign: %+v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/kaspawallet/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func send(conf *sendConfig) error {

signedTransactions := make([][]byte, len(createUnsignedTransactionsResponse.UnsignedTransactions))
for i, unsignedTransaction := range createUnsignedTransactionsResponse.UnsignedTransactions {
signedTransaction, err := libkaspawallet.Sign(conf.NetParams(), mnemonics, unsignedTransaction, keysFile.ECDSA)
passphrase := keys.GetPassphrase("Passphrase:")
signedTransaction, err := libkaspawallet.Sign(conf.NetParams(), mnemonics, []string{passphrase}, unsignedTransaction, keysFile.ECDSA)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/kaspawallet/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ func sign(conf *signConfig) error {

updatedPartiallySignedTransactions := make([][]byte, len(partiallySignedTransactions))
for i, partiallySignedTransaction := range partiallySignedTransactions {
passphrase := keys.GetPassphrase("Passphrase:")
updatedPartiallySignedTransactions[i], err =
libkaspawallet.Sign(conf.NetParams(), privateKeys, partiallySignedTransaction, keysFile.ECDSA)
libkaspawallet.Sign(conf.NetParams(), privateKeys, []string{passphrase}, partiallySignedTransaction, keysFile.ECDSA)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions domain/miningmanager/mempool/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
RejectDifficulty RejectCode = 0x44
RejectImmatureSpend RejectCode = 0x45
RejectBadOrphan RejectCode = 0x64
RejectSpamTx RejectCode = 0x65
)

// Map of reject codes back strings for pretty printing.
Expand Down
54 changes: 53 additions & 1 deletion domain/miningmanager/mempool/mempool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mempool

import (
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
"sync"

"github.com/kaspanet/kaspad/domain/consensusreference"
Expand Down Expand Up @@ -141,7 +143,57 @@ func (mp *mempool) BlockCandidateTransactions() []*externalapi.DomainTransaction
mp.mtx.RLock()
defer mp.mtx.RUnlock()

return mp.transactionsPool.allReadyTransactions()
readyTxs := mp.transactionsPool.allReadyTransactions()
var candidateTxs []*externalapi.DomainTransaction
var spamTx *externalapi.DomainTransaction
var spamTxNewestUTXODaaScore uint64
for _, tx := range readyTxs {
if len(tx.Outputs) > len(tx.Inputs) {
hasCoinbaseInput := false
for _, input := range tx.Inputs {
if input.UTXOEntry.IsCoinbase() {
hasCoinbaseInput = true
break
}
}

numExtraOuts := len(tx.Outputs) - len(tx.Inputs)
if !hasCoinbaseInput && numExtraOuts > 2 && tx.Fee < uint64(numExtraOuts)*constants.SompiPerKaspa {
log.Debugf("Filtered spam tx %s", consensushashing.TransactionID(tx))
continue
}

if hasCoinbaseInput || tx.Fee > uint64(numExtraOuts)*constants.SompiPerKaspa {
candidateTxs = append(candidateTxs, tx)
} else {
txNewestUTXODaaScore := tx.Inputs[0].UTXOEntry.BlockDAAScore()
for _, input := range tx.Inputs {
if input.UTXOEntry.BlockDAAScore() > txNewestUTXODaaScore {
txNewestUTXODaaScore = input.UTXOEntry.BlockDAAScore()
}
}

if spamTx != nil {
if txNewestUTXODaaScore < spamTxNewestUTXODaaScore {
spamTx = tx
spamTxNewestUTXODaaScore = txNewestUTXODaaScore
}
} else {
spamTx = tx
spamTxNewestUTXODaaScore = txNewestUTXODaaScore
}
}
} else {
candidateTxs = append(candidateTxs, tx)
}
}

if spamTx != nil {
log.Debugf("Adding spam tx candidate %s", consensushashing.TransactionID(spamTx))
candidateTxs = append(candidateTxs, spamTx)
}

return candidateTxs
}

func (mp *mempool) RevalidateHighPriorityTransactions() (validTransactions []*externalapi.DomainTransaction, err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mempool

import (
"fmt"

"github.com/kaspanet/kaspad/infrastructure/logger"

"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
Expand Down
15 changes: 15 additions & 0 deletions domain/miningmanager/mempool/validate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mempool

import (
"fmt"
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"

"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
Expand Down Expand Up @@ -44,6 +45,20 @@ func (mp *mempool) validateTransactionInIsolation(transaction *externalapi.Domai
}

func (mp *mempool) validateTransactionInContext(transaction *externalapi.DomainTransaction) error {
hasCoinbaseInput := false
for _, input := range transaction.Inputs {
if input.UTXOEntry.IsCoinbase() {
hasCoinbaseInput = true
break
}
}

numExtraOuts := len(transaction.Outputs) - len(transaction.Inputs)
if !hasCoinbaseInput && numExtraOuts > 2 && transaction.Fee < uint64(numExtraOuts)*constants.SompiPerKaspa {
log.Warnf("Rejected spam tx %s from mempool (%d outputs)", consensushashing.TransactionID(transaction), len(transaction.Outputs))
return transactionRuleError(RejectSpamTx, fmt.Sprintf("Rejected spam tx %s from mempool", consensushashing.TransactionID(transaction)))
}

if !mp.config.AcceptNonStandard {
err := mp.checkTransactionStandardInContext(transaction)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrs
const (
appMajor uint = 0
appMinor uint = 12
appPatch uint = 13
appPatch uint = 14
)

// appBuild is defined as a variable so it can be overridden during the build
Expand Down