2016-02-20 01:49:04 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2016-02-20 11:42:07 +03:00
|
|
|
"io"
|
2016-02-20 01:49:04 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2016-02-20 11:42:07 +03:00
|
|
|
"math"
|
2016-02-20 01:49:04 +03:00
|
|
|
"strings"
|
|
|
|
|
2016-02-22 04:42:30 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2016-02-20 01:49:04 +03:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/lightningnetwork/lnd/uspv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
inspk0 = "2103c9f4836b9a4f77fc0d81f7bcb01b7f1b35916864b9476c241ce9fc198bd25432ac"
|
2016-02-20 11:42:07 +03:00
|
|
|
inamt0 = int64(625000000)
|
2016-02-20 01:49:04 +03:00
|
|
|
|
|
|
|
inspk1 = "00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1"
|
2016-02-20 11:42:07 +03:00
|
|
|
inamt1 = int64(600000000)
|
2016-02-22 04:42:30 +03:00
|
|
|
|
|
|
|
xpecthash = "c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670"
|
2016-02-20 01:49:04 +03:00
|
|
|
)
|
|
|
|
|
2016-02-20 11:42:07 +03:00
|
|
|
// calcWitnessSignatureHash is the witnessified version of calcSignatureHash
|
2016-02-22 00:49:04 +03:00
|
|
|
// put your pkscripts in the sigscript slot before handing the tx to this
|
|
|
|
// function. Also you're clearly supposed to cache the 3 sub-hashes generated
|
|
|
|
// here, because they apply to the tx, not a txin. But this doesn't yet.
|
2016-02-22 12:41:40 +03:00
|
|
|
func calcWitnessSignatureHash(sigscript []byte,
|
2016-02-20 11:42:07 +03:00
|
|
|
hashType txscript.SigHashType, tx *wire.MsgTx, idx int, amt int64) []byte {
|
|
|
|
// in the script.go calcSignatureHash(), idx is assumed safe, so I guess
|
2016-02-22 00:49:04 +03:00
|
|
|
// that's OK here too...? Nah I'm gonna check
|
|
|
|
if idx > len(tx.TxIn)-1 {
|
2016-02-22 12:41:40 +03:00
|
|
|
fmt.Printf("calcWitnessSignatureHash error: idx %d but %d txins",
|
|
|
|
idx, len(tx.TxIn))
|
2016-02-22 00:49:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-20 01:49:04 +03:00
|
|
|
|
2016-02-20 11:42:07 +03:00
|
|
|
// first get hashPrevOuts, hashSequence, and hashOutputs
|
|
|
|
hashPrevOuts := calcHashPrevOuts(tx, hashType)
|
|
|
|
hashSequence := calcHashSequence(tx, hashType)
|
|
|
|
hashOutputs := calcHashOutputs(tx, idx, hashType)
|
|
|
|
|
|
|
|
var buf4 [4]byte // buffer for 4-byte stuff
|
|
|
|
var buf8 [8]byte // buffer for 8-byte stuff
|
|
|
|
var pre []byte // the pre-image we're generating
|
|
|
|
|
|
|
|
binary.LittleEndian.PutUint32(buf4[:], uint32(tx.Version))
|
|
|
|
pre = append(pre, buf4[:]...)
|
|
|
|
|
|
|
|
pre = append(pre, hashPrevOuts.Bytes()...)
|
|
|
|
pre = append(pre, hashSequence.Bytes()...)
|
|
|
|
|
|
|
|
// outpoint being spent
|
|
|
|
pre = append(pre, tx.TxIn[idx].PreviousOutPoint.Hash.Bytes()...)
|
|
|
|
binary.LittleEndian.PutUint32(buf4[:], tx.TxIn[idx].PreviousOutPoint.Index)
|
|
|
|
pre = append(pre, buf4[:]...)
|
|
|
|
|
|
|
|
// scriptCode which is some new thing
|
2016-02-22 00:49:04 +03:00
|
|
|
|
|
|
|
// detect wpkh mode
|
2016-02-22 12:41:40 +03:00
|
|
|
fmt.Printf("txin %d sscript len %d\n", idx, len(tx.TxIn[idx].SignatureScript))
|
|
|
|
if len(sigscript) == 22 && sigscript[0] == 0x00 && sigscript[1] == 0x14 {
|
2016-02-22 00:49:04 +03:00
|
|
|
// wpkh mode .... recreate op_dup codes here
|
|
|
|
sCode := []byte{0x19, 0x76, 0xa9, 0x14}
|
2016-02-22 12:41:40 +03:00
|
|
|
sCode = append(sCode, sigscript[2:22]...)
|
2016-02-22 00:49:04 +03:00
|
|
|
sCode = append(sCode, []byte{0x88, 0xac}...)
|
|
|
|
pre = append(pre, sCode...)
|
2016-02-22 12:41:40 +03:00
|
|
|
} else if len(sigscript) == 34 &&
|
|
|
|
sigscript[0] == 0x00 && sigscript[1] == 0x20 {
|
2016-02-22 00:49:04 +03:00
|
|
|
// whs mode- need to remove codeseparators. this doesn't yet.
|
|
|
|
var buf bytes.Buffer
|
2016-02-22 12:41:40 +03:00
|
|
|
writeVarBytes(&buf, 0, sigscript)
|
2016-02-22 00:49:04 +03:00
|
|
|
pre = append(pre, buf.Bytes()...)
|
|
|
|
} else {
|
|
|
|
// ?? this is not witness tx! fail
|
2016-02-22 12:41:40 +03:00
|
|
|
fmt.Printf("Non witness error ")
|
2016-02-22 00:49:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-20 11:42:07 +03:00
|
|
|
|
|
|
|
// amount being signed off
|
|
|
|
binary.LittleEndian.PutUint64(buf8[:], uint64(amt))
|
|
|
|
pre = append(pre, buf8[:]...)
|
|
|
|
|
|
|
|
// nsequence of input
|
|
|
|
binary.LittleEndian.PutUint32(buf4[:], tx.TxIn[idx].Sequence)
|
|
|
|
pre = append(pre, buf4[:]...)
|
|
|
|
|
|
|
|
pre = append(pre, hashOutputs.Bytes()...)
|
|
|
|
|
|
|
|
// locktime
|
|
|
|
binary.LittleEndian.PutUint32(buf4[:], tx.LockTime)
|
|
|
|
pre = append(pre, buf4[:]...)
|
|
|
|
|
|
|
|
// hashType... in 4 bytes, instead of 1, because reasons.
|
|
|
|
binary.LittleEndian.PutUint32(buf4[:], uint32(hashType))
|
|
|
|
pre = append(pre, buf4[:]...)
|
|
|
|
|
2016-02-22 04:42:30 +03:00
|
|
|
fmt.Printf("calcWitnessSignatureHash pre: %x\n", pre)
|
2016-02-20 11:42:07 +03:00
|
|
|
hsh := wire.DoubleSha256SH(pre)
|
|
|
|
return hsh.Bytes()
|
2016-02-20 01:49:04 +03:00
|
|
|
}
|
|
|
|
|
2016-02-20 11:42:07 +03:00
|
|
|
// calcHashPrevOuts makes a single hash of all previous outputs in the tx
|
|
|
|
func calcHashPrevOuts(tx *wire.MsgTx, hType txscript.SigHashType) wire.ShaHash {
|
|
|
|
// skip this (0x00) for anyonecanpay
|
|
|
|
if hType == txscript.SigHashAnyOneCanPay {
|
|
|
|
var empty [32]byte
|
|
|
|
return empty
|
|
|
|
}
|
2016-02-20 01:49:04 +03:00
|
|
|
|
2016-02-20 11:42:07 +03:00
|
|
|
var pre []byte
|
|
|
|
for _, in := range tx.TxIn {
|
|
|
|
// first append 32 byte hash
|
|
|
|
pre = append(pre, in.PreviousOutPoint.Hash.Bytes()...)
|
|
|
|
// then make a buffer, put 4 byte index in lil' endian and append that
|
|
|
|
var buf [4]byte
|
|
|
|
binary.LittleEndian.PutUint32(buf[:], in.PreviousOutPoint.Index)
|
|
|
|
pre = append(pre, buf[:]...)
|
|
|
|
}
|
2016-02-22 04:42:30 +03:00
|
|
|
fmt.Printf("calcHashPrevOuts pre: %x\n", pre)
|
2016-02-20 11:42:07 +03:00
|
|
|
return wire.DoubleSha256SH(pre)
|
2016-02-20 01:49:04 +03:00
|
|
|
}
|
|
|
|
|
2016-02-20 11:42:07 +03:00
|
|
|
// calcHashSequence is hash of txins' seq numbers, lil' endian, stuck together
|
|
|
|
func calcHashSequence(tx *wire.MsgTx, hType txscript.SigHashType) wire.ShaHash {
|
|
|
|
// skip (0x00) for single, none, anyonecanpay
|
|
|
|
if hType == txscript.SigHashSingle || hType == txscript.SigHashNone ||
|
|
|
|
hType == txscript.SigHashAnyOneCanPay {
|
|
|
|
var empty [32]byte
|
|
|
|
return empty
|
|
|
|
}
|
2016-02-20 01:49:04 +03:00
|
|
|
|
2016-02-20 11:42:07 +03:00
|
|
|
var pre []byte
|
2016-02-20 01:49:04 +03:00
|
|
|
for _, in := range tx.TxIn {
|
2016-02-20 11:42:07 +03:00
|
|
|
var buf [4]byte
|
|
|
|
binary.LittleEndian.PutUint32(buf[:], in.Sequence)
|
|
|
|
pre = append(pre, buf[:]...)
|
2016-02-20 01:49:04 +03:00
|
|
|
}
|
2016-02-22 04:42:30 +03:00
|
|
|
fmt.Printf("calcHashSequence pre: %x\n", pre)
|
2016-02-20 11:42:07 +03:00
|
|
|
return wire.DoubleSha256SH(pre)
|
|
|
|
}
|
2016-02-20 01:49:04 +03:00
|
|
|
|
2016-02-20 11:42:07 +03:00
|
|
|
// calcHashOutputs also wants a input index, which it only uses for
|
|
|
|
// sighash single. If it's not sighash single, just put a 0 or whatever.
|
|
|
|
func calcHashOutputs(
|
|
|
|
tx *wire.MsgTx, inIndex int, hType txscript.SigHashType) wire.ShaHash {
|
|
|
|
if hType == txscript.SigHashNone ||
|
|
|
|
(hType == txscript.SigHashSingle && inIndex <= len(tx.TxOut)) {
|
|
|
|
var empty [32]byte
|
|
|
|
return empty
|
|
|
|
}
|
|
|
|
if hType == txscript.SigHashSingle {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
writeTxOut(&buf, 0, 0, tx.TxOut[inIndex])
|
|
|
|
return wire.DoubleSha256SH(buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
var pre []byte
|
|
|
|
for _, out := range tx.TxOut {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
writeTxOut(&buf, 0, 0, out)
|
|
|
|
pre = append(pre, buf.Bytes()...)
|
|
|
|
}
|
2016-02-22 04:42:30 +03:00
|
|
|
fmt.Printf("calcHashOutputs pre: %x\n", pre)
|
2016-02-20 11:42:07 +03:00
|
|
|
return wire.DoubleSha256SH(pre)
|
2016-02-20 01:49:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Printf("sighash 143\n")
|
|
|
|
|
|
|
|
// get previous pkscripts for inputs 0 and 1 from hex
|
|
|
|
in0spk, err := hex.DecodeString(string(inspk0))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
in1spk, err := hex.DecodeString(string(inspk1))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-02-22 04:42:30 +03:00
|
|
|
xpkt, err := hex.DecodeString(string(xpecthash))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-02-20 01:49:04 +03:00
|
|
|
|
|
|
|
// load tx skeleton from local file
|
|
|
|
fmt.Printf("loading tx from file tx.hex\n")
|
|
|
|
txhex, err := ioutil.ReadFile("tx.hex")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txhex = []byte(strings.TrimSpace(string(txhex)))
|
|
|
|
txbytes, err := hex.DecodeString(string(txhex))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("loaded %d byte tx %x\n", len(txbytes), txbytes)
|
|
|
|
|
|
|
|
// make tx
|
|
|
|
ttx := wire.NewMsgTx()
|
|
|
|
// deserialize into tx
|
|
|
|
buf := bytes.NewBuffer(txbytes)
|
|
|
|
ttx.Deserialize(buf)
|
|
|
|
|
2016-02-22 12:41:40 +03:00
|
|
|
fmt.Printf("=====tx locktime: %x\n", ttx.LockTime)
|
2016-02-20 01:49:04 +03:00
|
|
|
ttx.TxIn[0].SignatureScript = in0spk
|
2016-02-22 12:41:40 +03:00
|
|
|
// ttx.TxIn[1].SignatureScript = in1spk
|
2016-02-20 01:49:04 +03:00
|
|
|
|
|
|
|
fmt.Printf(uspv.TxToString(ttx))
|
|
|
|
|
2016-02-22 04:42:30 +03:00
|
|
|
priv, err := btcec.NewPrivateKey(btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-02-22 12:41:40 +03:00
|
|
|
// assert flag before writing witness
|
|
|
|
ttx.Flags = 0x01
|
|
|
|
ttx.TxIn[1].Witness, err = txscript.WitnessScript(ttx, 1, inamt1, in1spk,
|
2016-02-22 04:42:30 +03:00
|
|
|
txscript.SigHashAll, priv, true)
|
2016-02-22 12:41:40 +03:00
|
|
|
// ttx.TxIn[1].SignatureScript = nil
|
2016-02-22 04:42:30 +03:00
|
|
|
|
2016-02-22 12:41:40 +03:00
|
|
|
hxh := calcWitnessSignatureHash(in1spk, txscript.SigHashAll, ttx, 1, inamt1)
|
2016-02-20 11:42:07 +03:00
|
|
|
|
2016-02-22 12:41:40 +03:00
|
|
|
fmt.Printf("got sigHash %x NON LIB\n", hxh)
|
2016-02-22 04:42:30 +03:00
|
|
|
fmt.Printf("expect hash %x\n ", xpkt)
|
2016-02-22 12:41:40 +03:00
|
|
|
fmt.Printf("\n%s\n", uspv.TxToString(ttx))
|
|
|
|
|
|
|
|
fmt.Printf("loading tx from file xtx.hex\n")
|
|
|
|
txhex, err = ioutil.ReadFile("xtx.hex")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
txhex = []byte(strings.TrimSpace(string(txhex)))
|
|
|
|
txbytes, err = hex.DecodeString(string(txhex))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// make tx
|
|
|
|
xtx := wire.NewMsgTx()
|
|
|
|
// deserialize into tx
|
|
|
|
buf = bytes.NewBuffer(txbytes)
|
|
|
|
xtx.Deserialize(buf)
|
|
|
|
|
|
|
|
// ttx.Flags = 0x01
|
|
|
|
fmt.Printf("\n%s\n", uspv.TxToString(xtx))
|
2016-02-20 11:42:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pver can be 0, doesn't do anything in these. Same for msg.Version
|
|
|
|
// writeVarInt serializes val to w using a variable number of bytes depending
|
|
|
|
// on its value.
|
|
|
|
func writeVarInt(w io.Writer, pver uint32, val uint64) error {
|
|
|
|
if val < 0xfd {
|
|
|
|
_, err := w.Write([]byte{uint8(val)})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if val <= math.MaxUint16 {
|
|
|
|
var buf [3]byte
|
|
|
|
buf[0] = 0xfd
|
|
|
|
binary.LittleEndian.PutUint16(buf[1:], uint16(val))
|
|
|
|
_, err := w.Write(buf[:])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if val <= math.MaxUint32 {
|
|
|
|
var buf [5]byte
|
|
|
|
buf[0] = 0xfe
|
|
|
|
binary.LittleEndian.PutUint32(buf[1:], uint32(val))
|
|
|
|
_, err := w.Write(buf[:])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf [9]byte
|
|
|
|
buf[0] = 0xff
|
|
|
|
binary.LittleEndian.PutUint64(buf[1:], val)
|
|
|
|
_, err := w.Write(buf[:])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeVarBytes serializes a variable length byte array to w as a varInt
|
|
|
|
// containing the number of bytes, followed by the bytes themselves.
|
|
|
|
func writeVarBytes(w io.Writer, pver uint32, bytes []byte) error {
|
|
|
|
slen := uint64(len(bytes))
|
|
|
|
err := writeVarInt(w, pver, slen)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeTxOut encodes to into the bitcoin protocol encoding for a transaction
|
|
|
|
// output (TxOut) to w.
|
|
|
|
func writeTxOut(w io.Writer, pver uint32, version int32, to *wire.TxOut) error {
|
|
|
|
var buf [8]byte
|
|
|
|
binary.LittleEndian.PutUint64(buf[:], uint64(to.Value))
|
|
|
|
_, err := w.Write(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeVarBytes(w, pver, to.PkScript)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-02-20 01:49:04 +03:00
|
|
|
}
|