mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-19 21:36:43 +00:00

* Update constants * Add to transaction SigOpCount * Update mass calculation, and move it from InContext to InIsolation * Update block validation accordingly * Add SigOpCount validation during TransactionInContext * Remove checking of mass vs maxMassAcceptedByBlock from consensusStateManager * Update mining manager with latest changes * Add SigOpCount to MsgTx.Copy() * Fix initTestTransactionAcceptanceDataForClone * Fix all tests in transaction_equal_clone_test.go * Fix TestBlockMass * Fix tests in transactionvalidator package * Add SigOpCount to sighash * Fix TestPruningDepth * Fix problems in libkaspawalelt * Fix integration tests * Fix CalculateSignatureHash tests * Remove remaining places talking about block size * Add sanity check to checkBlockMass to make sure all transactions have their mass filled * always add own sigOpCount to sigHash * Update protowire/rpc.md * Start working on removing any remaining reference to block/tx size * Update rpc transaction verbose data to include mass rather then size * Convert verboseData and block size check to mass * Remove remaining usages of tx size in mempool * Move transactionEstimatedSerializedSize to transactionvalidator * Add PopulateMass to fakeRelayInvsContext * Move PopulateMass to beggining of ValidateAndInsertTransaction + fix in it * Assign mass a new number for backward-compatibility
101 lines
3.3 KiB
Go
101 lines
3.3 KiB
Go
package libkaspawallet
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet/bip32"
|
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet/serialization"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/txscript"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/utxo"
|
|
"github.com/kaspanet/kaspad/domain/dagconfig"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func rawTxInSignature(extendedKey *bip32.ExtendedKey, tx *externalapi.DomainTransaction, idx int, hashType consensushashing.SigHashType,
|
|
sighashReusedValues *consensushashing.SighashReusedValues, ecdsa bool) ([]byte, error) {
|
|
|
|
privateKey := extendedKey.PrivateKey()
|
|
if ecdsa {
|
|
return txscript.RawTxInSignatureECDSA(tx, idx, hashType, privateKey, sighashReusedValues)
|
|
}
|
|
|
|
schnorrKeyPair, err := privateKey.ToSchnorr()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return txscript.RawTxInSignature(tx, idx, hashType, schnorrKeyPair, sighashReusedValues)
|
|
}
|
|
|
|
// Sign signs the transaction with the given private keys
|
|
func Sign(params *dagconfig.Params, mnemonics []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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return serialization.SerializePartiallySignedTransaction(partiallySignedTransaction)
|
|
}
|
|
|
|
func sign(params *dagconfig.Params, mnemonic string, partiallySignedTransaction *serialization.PartiallySignedTransaction, ecdsa bool) error {
|
|
if isTransactionFullySigned(partiallySignedTransaction) {
|
|
return nil
|
|
}
|
|
|
|
sighashReusedValues := &consensushashing.SighashReusedValues{}
|
|
for i, partiallySignedInput := range partiallySignedTransaction.PartiallySignedInputs {
|
|
prevOut := partiallySignedInput.PrevOutput
|
|
partiallySignedTransaction.Tx.Inputs[i].UTXOEntry = utxo.NewUTXOEntry(
|
|
prevOut.Value,
|
|
prevOut.ScriptPublicKey,
|
|
false, // This is a fake value, because it's irrelevant for the signature
|
|
0, // This is a fake value, because it's irrelevant for the signature
|
|
)
|
|
partiallySignedTransaction.Tx.Inputs[i].SigOpCount = byte(len(partiallySignedInput.PubKeySignaturePairs))
|
|
}
|
|
|
|
signed := false
|
|
for i, partiallySignedInput := range partiallySignedTransaction.PartiallySignedInputs {
|
|
isMultisig := len(partiallySignedInput.PubKeySignaturePairs) > 1
|
|
path := defaultPath(isMultisig)
|
|
extendedKey, err := extendedKeyFromMnemonicAndPath(mnemonic, path, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
derivedKey, err := extendedKey.DeriveFromPath(partiallySignedInput.DerivationPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
derivedPublicKey, err := derivedKey.Public()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, pair := range partiallySignedInput.PubKeySignaturePairs {
|
|
if pair.ExtendedPublicKey == derivedPublicKey.String() {
|
|
pair.Signature, err = rawTxInSignature(derivedKey, partiallySignedTransaction.Tx, i, consensushashing.SigHashAll, sighashReusedValues, ecdsa)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
signed = true
|
|
}
|
|
}
|
|
}
|
|
|
|
if !signed {
|
|
return errors.Errorf("Public key doesn't match any of the transaction public keys")
|
|
}
|
|
|
|
return nil
|
|
}
|