package tx import ( "github.com/btcsuite/btcd/btcec/v2" btcecdsa "github.com/btcsuite/btcd/btcec/v2/ecdsa" ) // SignDigest signs one BIP143 digest and returns the DER signature with its sighash byte and // the compressed public key that verifies it. // // It is the piece SignP2WPKH and a PSBT signer share: a P2WPKH witness and a partial-signature // record carry the same bytes, and two signers that computed them differently would be two // answers to one question. func SignDigest(secret []byte, digest [32]byte) ([]byte, []byte, error) { return SignDigestWithType(secret, digest, byte(SighashAll)) } // SignDigestWithType signs one digest and appends the signature hash byte the // verifier reads. The legacy wallet uses SIGHASH_ALL; the XBT wallet uses // SIGHASH_ALL|SIGHASH_UNIFIED, where the byte is 0x21 and the digest is the // unified one, not BIP143. func SignDigestWithType(secret []byte, digest [32]byte, sighashType byte) ([]byte, []byte, error) { if len(secret) != 32 { return nil, nil, ErrKey } key, _ := btcec.PrivKeyFromBytes(secret) if key == nil { return nil, nil, ErrKey } signature := btcecdsa.Sign(key, digest[:]) der := append(signature.Serialize(), sighashType) return der, key.PubKey().SerializeCompressed(), nil }