sign

package
v8.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2022 License: ISC Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RawTxInSignature

func RawTxInSignature(tx *wire.MsgTx, idx int, subScript []byte,
	hashType txscript.SigHashType, key []byte,
	sigType dcrec.SignatureType) ([]byte, error)

RawTxInSignature returns the serialized ECDSA signature for the input idx of the given transaction, with hashType appended to it.

NOTE: This function is only valid for version 0 scripts. Since the function does not accept a script version, the results are undefined for other script versions.

func SignTxOutput

func SignTxOutput(chainParams stdaddr.AddressParams, tx *wire.MsgTx, idx int,
	pkScript []byte, hashType txscript.SigHashType, kdb KeyDB, sdb ScriptDB,
	previousScript []byte, isTreasuryEnabled bool) ([]byte, error)

SignTxOutput signs output idx of the given tx to resolve the script given in pkScript with a signature type of hashType. Any keys required will be looked up by calling getKey() with the string of the given address. Any pay-to-script-hash signatures will be similarly looked up by calling getScript. If previousScript is provided then the results in previousScript will be merged in a type-dependent manner with the newly generated. signature script.

NOTE: This function is only valid for version 0 scripts. Since the function does not accept a script version, the results are undefined for other script versions.

Example

This example demonstrates manually creating and signing a redeem transaction.

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/Decred-Next/dcrnd/chaincfg/chainhash/v8"
	"github.com/Decred-Next/dcrnd/chaincfg/v8"
	secp256k1 "github.com/Decred-Next/dcrnd/dcrec/secp256k1/version4/v8"
	"github.com/Decred-Next/dcrnd/dcrec/v8"
	"github.com/Decred-Next/dcrnd/txscript/version4/v8"
	"github.com/Decred-Next/dcrnd/txscript/version4/v8/sign"
	"github.com/Decred-Next/dcrnd/txscript/version4/v8/stdaddr"
	"github.com/Decred-Next/dcrnd/wire/v8"
)

const (
	// noTreasury signifies the treasury agenda should be treated as though
	// it is inactive.  It is used to increase the readability of the
	// tests.
	noTreasury = false
)

// This example demonstrates manually creating and signing a redeem transaction.
func main() {
	// Ordinarily the private key would come from whatever storage mechanism
	// is being used, but for this example just hard code it.
	privKeyBytes, err := hex.DecodeString("22a47fa09a223f2aa079edf85a7c2" +
		"d4f8720ee63e502ee2869afab7de234b80c")
	if err != nil {
		fmt.Println(err)
		return
	}
	pubKey := secp256k1.PrivKeyFromBytes(privKeyBytes).PubKey()
	pubKeyHash := stdaddr.Hash160(pubKey.SerializeCompressed())
	mainNetParams := chaincfg.MainNetParams()
	addr, err := stdaddr.NewAddressPubKeyHashEcdsaSecp256k1V0(pubKeyHash,
		mainNetParams)
	if err != nil {
		fmt.Println(err)
		return
	}

	// For this example, create a fake transaction that represents what
	// would ordinarily be the real transaction that is being spent.  It
	// contains a single output that pays to address in the amount of 1 DCR.
	originTx := wire.NewMsgTx()
	prevOut := wire.NewOutPoint(&chainhash.Hash{}, ^uint32(0), wire.TxTreeRegular)
	txIn := wire.NewTxIn(prevOut, 100000000, []byte{txscript.OP_0, txscript.OP_0})
	originTx.AddTxIn(txIn)
	pkScriptVer, pkScript := addr.PaymentScript()
	txOut := wire.NewTxOut(100000000, pkScript)
	txOut.Version = pkScriptVer
	originTx.AddTxOut(txOut)
	originTxHash := originTx.TxHash()

	// Create the transaction to redeem the fake transaction.
	redeemTx := wire.NewMsgTx()

	// Add the input(s) the redeeming transaction will spend.  There is no
	// signature script at this point since it hasn't been created or signed
	// yet, hence nil is provided for it.
	prevOut = wire.NewOutPoint(&originTxHash, 0, wire.TxTreeRegular)
	txIn = wire.NewTxIn(prevOut, 100000000, nil)
	redeemTx.AddTxIn(txIn)

	// Ordinarily this would contain that actual destination of the funds,
	// but for this example don't bother.
	txOut = wire.NewTxOut(0, nil)
	redeemTx.AddTxOut(txOut)

	// Sign the redeeming transaction.
	sigType := dcrec.STEcdsaSecp256k1
	lookupKey := func(a stdaddr.Address) ([]byte, dcrec.SignatureType, bool, error) {
		// Ordinarily this function would involve looking up the private
		// key for the provided address, but since the only thing being
		// signed in this example uses the address associated with the
		// private key from above, simply return it with the compressed
		// flag set since the address is using the associated compressed
		// public key.
		//
		// NOTE: If you want to prove the code is actually signing the
		// transaction properly, uncomment the following line which
		// intentionally returns an invalid key to sign with, which in
		// turn will result in a failure during the script execution
		// when verifying the signature.
		//
		// privKey.D.SetInt64(12345)
		//
		return privKeyBytes, sigType, true, nil
	}
	// Notice that the script database parameter is nil here since it isn't
	// used.  It must be specified when pay-to-script-hash transactions are
	// being signed.
	sigScript, err := sign.SignTxOutput(mainNetParams, redeemTx, 0,
		originTx.TxOut[0].PkScript, txscript.SigHashAll,
		sign.KeyClosure(lookupKey), nil, nil, noTreasury)
	if err != nil {
		fmt.Println(err)
		return
	}
	redeemTx.TxIn[0].SignatureScript = sigScript

	// Prove that the transaction has been validly signed by executing the
	// script pair.

	flags := txscript.ScriptDiscourageUpgradableNops
	vm, err := txscript.NewEngine(originTx.TxOut[0].PkScript, redeemTx, 0,
		flags, 0, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	if err := vm.Execute(); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Transaction successfully signed")

}
Output:

Transaction successfully signed

func SignatureScript

func SignatureScript(tx *wire.MsgTx, idx int, subscript []byte,
	hashType txscript.SigHashType, privKey []byte,
	sigType dcrec.SignatureType, compress bool) ([]byte, error)

SignatureScript creates an input signature script for tx to spend coins sent from a previous output to the owner of privKey. tx must include all transaction inputs and outputs, however txin scripts are allowed to be filled or empty. The returned script is calculated to be used as the idx'th txin sigscript for tx. subscript is the PkScript of the previous output being used as the idx'th input. privKey is serialized in the respective format for the ECDSA type. This format must match the same format used to generate the payment address, or the script validation will fail.

func TSpendSignatureScript

func TSpendSignatureScript(msgTx *wire.MsgTx, privKey []byte) ([]byte, error)

TSpendSignatureScript creates an input signature for the provided tx, which is expected to be a treasury spend transaction, to authorize coins to be spent from the treasury. The private key must correspond to one of the valid public keys for a Pi instance recognized by consensus.

Types

type KeyClosure

type KeyClosure func(stdaddr.Address) ([]byte, dcrec.SignatureType, bool, error)

KeyClosure implements KeyDB with a closure.

func (KeyClosure) GetKey

func (kc KeyClosure) GetKey(address stdaddr.Address) ([]byte, dcrec.SignatureType, bool, error)

GetKey implements KeyDB by returning the result of calling the closure.

type KeyDB

type KeyDB interface {
	GetKey(stdaddr.Address) ([]byte, dcrec.SignatureType, bool, error)
}

KeyDB is an interface type provided to SignTxOutput, it encapsulates any user state required to get the private keys for an address.

type ScriptClosure

type ScriptClosure func(stdaddr.Address) ([]byte, error)

ScriptClosure implements ScriptDB with a closure.

func (ScriptClosure) GetScript

func (sc ScriptClosure) GetScript(address stdaddr.Address) ([]byte, error)

GetScript implements ScriptDB by returning the result of calling the closure.

type ScriptDB

type ScriptDB interface {
	GetScript(stdaddr.Address) ([]byte, error)
}

ScriptDB is an interface type provided to SignTxOutput, it encapsulates any user state required to get the scripts for a pay-to-script-hash address.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL