spend from wpkh works

This commit is contained in:
Tadge Dryja 2016-02-22 01:41:40 -08:00
parent 57a34f0753
commit 2b808724fb
6 changed files with 49 additions and 26 deletions

@ -57,7 +57,7 @@ func shell() {
log.Fatal(err)
}
if tip == 0 { // DB has never been used, set to birthday
tip = 20000 // hardcoded; later base on keyfile date?
tip = 21900 // hardcoded; later base on keyfile date?
err = SCon.TS.SetDBSyncHeight(tip)
if err != nil {
log.Fatal(err)

@ -31,11 +31,13 @@ const (
// 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.
func calcWitnessSignatureHash(
func calcWitnessSignatureHash(sigscript []byte,
hashType txscript.SigHashType, tx *wire.MsgTx, idx int, amt int64) []byte {
// in the script.go calcSignatureHash(), idx is assumed safe, so I guess
// that's OK here too...? Nah I'm gonna check
if idx > len(tx.TxIn)-1 {
fmt.Printf("calcWitnessSignatureHash error: idx %d but %d txins",
idx, len(tx.TxIn))
return nil
}
@ -62,23 +64,22 @@ func calcWitnessSignatureHash(
// scriptCode which is some new thing
// detect wpkh mode
if len(tx.TxIn[idx].SignatureScript) == 22 &&
tx.TxIn[idx].SignatureScript[0] == 0x00 &&
tx.TxIn[idx].SignatureScript[1] == 0x14 {
fmt.Printf("txin %d sscript len %d\n", idx, len(tx.TxIn[idx].SignatureScript))
if len(sigscript) == 22 && sigscript[0] == 0x00 && sigscript[1] == 0x14 {
// wpkh mode .... recreate op_dup codes here
sCode := []byte{0x19, 0x76, 0xa9, 0x14}
sCode = append(sCode, tx.TxIn[idx].SignatureScript[2:22]...)
sCode = append(sCode, sigscript[2:22]...)
sCode = append(sCode, []byte{0x88, 0xac}...)
pre = append(pre, sCode...)
} else if len(tx.TxIn[idx].SignatureScript) == 34 &&
tx.TxIn[idx].SignatureScript[0] == 0x00 &&
tx.TxIn[idx].SignatureScript[1] == 0x20 {
} else if len(sigscript) == 34 &&
sigscript[0] == 0x00 && sigscript[1] == 0x20 {
// whs mode- need to remove codeseparators. this doesn't yet.
var buf bytes.Buffer
writeVarBytes(&buf, 0, tx.TxIn[idx].Witness[0])
writeVarBytes(&buf, 0, sigscript)
pre = append(pre, buf.Bytes()...)
} else {
// ?? this is not witness tx! fail
fmt.Printf("Non witness error ")
return nil
}
@ -204,13 +205,13 @@ func main() {
// make tx
ttx := wire.NewMsgTx()
// deserialize into tx
buf := bytes.NewBuffer(txbytes)
ttx.Deserialize(buf)
fmt.Printf("=====tx locktime: %x\n", ttx.LockTime)
ttx.TxIn[0].SignatureScript = in0spk
ttx.TxIn[1].SignatureScript = in1spk
// ttx.TxIn[1].SignatureScript = in1spk
fmt.Printf(uspv.TxToString(ttx))
@ -218,15 +219,36 @@ func main() {
if err != nil {
log.Fatal(err)
}
sig, err := txscript.WitnessScript(ttx, 1, inamt1, in1spk,
// assert flag before writing witness
ttx.Flags = 0x01
ttx.TxIn[1].Witness, err = txscript.WitnessScript(ttx, 1, inamt1, in1spk,
txscript.SigHashAll, priv, true)
// ttx.TxIn[1].SignatureScript = nil
fmt.Printf("got sig %x\n", sig)
hxh := calcWitnessSignatureHash(txscript.SigHashAll, ttx, 1, inamt1)
hxh := calcWitnessSignatureHash(in1spk, txscript.SigHashAll, ttx, 1, inamt1)
fmt.Printf("got sigHash %x\n", hxh)
fmt.Printf("got sigHash %x NON LIB\n", hxh)
fmt.Printf("expect hash %x\n ", xpkt)
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))
}
// pver can be 0, doesn't do anything in these. Same for msg.Version

@ -189,6 +189,7 @@ func (s *SPVCon) GetDataHandler(m *wire.MsgGetData) {
log.Printf("error getting tx %s: %s",
thing.Hash.String(), err.Error())
}
fmt.Printf(TxToString(tx))
s.outMsgQueue <- tx
sent++
continue
@ -199,6 +200,7 @@ func (s *SPVCon) GetDataHandler(m *wire.MsgGetData) {
log.Printf("error getting tx %s: %s",
thing.Hash.String(), err.Error())
}
tx.Flags = 0x00 // dewitnessify
s.outMsgQueue <- tx
sent++
continue

@ -234,7 +234,7 @@ func (s *SPVCon) SendCoins(adr btcutil.Address, sendAmt int64) error {
fmt.Printf("tx: %s", TxToString(tx))
buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
tx.Serialize(buf)
tx.SerializeWitness(buf)
fmt.Printf("tx: %x\n", buf.Bytes())
// send it out on the wire. hope it gets there.

@ -137,20 +137,19 @@ func CheckDoubleSpends(
// TxToString prints out some info about a transaction. for testing / debugging
func TxToString(tx *wire.MsgTx) string {
str := fmt.Sprintf("\t size %d vsize %d wsize %d Tx %s\n",
str := fmt.Sprintf("size %d vsize %d wsize %d locktime %d flag %x txid %s\n",
tx.SerializeSize(), tx.VirtualSize(), tx.SerializeSizeWitness(),
tx.TxSha().String())
tx.LockTime, tx.Flags, tx.TxSha().String())
for i, in := range tx.TxIn {
str += fmt.Sprintf("Input %d: %s\n", i, in.PreviousOutPoint.String())
str += fmt.Sprintf("SigScript for input %d: %x\n", i, in.SignatureScript)
str += fmt.Sprintf("Input %d spends %s\n", i, in.PreviousOutPoint.String())
str += fmt.Sprintf("\tSigScript: %x\n", in.SignatureScript)
for j, wit := range in.Witness {
str += fmt.Sprintf("witness %d: %x\t", j, wit)
str += fmt.Sprintf("\twitness %d: %x\n", j, wit)
}
str += fmt.Sprintf("\n")
}
for i, out := range tx.TxOut {
if out != nil {
str += fmt.Sprintf("\toutput %d script: %x amt: %d\n",
str += fmt.Sprintf("output %d script: %x amt: %d\n",
i, out.PkScript, out.Value)
} else {
str += fmt.Sprintf("output %d nil (WARNING)\n", i)

@ -470,7 +470,7 @@ func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) {
// store this relevant tx
sha := tx.TxSha()
var buf bytes.Buffer
tx.Serialize(&buf)
tx.SerializeWitness(&buf) // always store witness version
err = txns.Put(sha.Bytes(), buf.Bytes())
if err != nil {
return err