spend from wpkh works
This commit is contained in:
1 parent
57a34f0753
commit
2b808724fb
6 files changed
+49
-26
No files matched your search
@@ -57,7 +57,7 @@ func shell() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if tip == 0 { // DB has never been used, set to birthday
|
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)
|
err = SCon.TS.SetDBSyncHeight(tip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
+38
-16
@@ -31,11 +31,13 @@ const (
|
|||||||
// put your pkscripts in the sigscript slot before handing the tx to this
|
// 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
|
// 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.
|
// 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 {
|
hashType txscript.SigHashType, tx *wire.MsgTx, idx int, amt int64) []byte {
|
||||||
// in the script.go calcSignatureHash(), idx is assumed safe, so I guess
|
// in the script.go calcSignatureHash(), idx is assumed safe, so I guess
|
||||||
// that's OK here too...? Nah I'm gonna check
|
// that's OK here too...? Nah I'm gonna check
|
||||||
if idx > len(tx.TxIn)-1 {
|
if idx > len(tx.TxIn)-1 {
|
||||||
|
fmt.Printf("calcWitnessSignatureHash error: idx %d but %d txins",
|
||||||
|
idx, len(tx.TxIn))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,23 +64,22 @@ func calcWitnessSignatureHash(
|
|||||||
// scriptCode which is some new thing
|
// scriptCode which is some new thing
|
||||||
|
|
||||||
// detect wpkh mode
|
// detect wpkh mode
|
||||||
if len(tx.TxIn[idx].SignatureScript) == 22 &&
|
fmt.Printf("txin %d sscript len %d\n", idx, len(tx.TxIn[idx].SignatureScript))
|
||||||
tx.TxIn[idx].SignatureScript[0] == 0x00 &&
|
if len(sigscript) == 22 && sigscript[0] == 0x00 && sigscript[1] == 0x14 {
|
||||||
tx.TxIn[idx].SignatureScript[1] == 0x14 {
|
|
||||||
// wpkh mode .... recreate op_dup codes here
|
// wpkh mode .... recreate op_dup codes here
|
||||||
sCode := []byte{0x19, 0x76, 0xa9, 0x14}
|
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}...)
|
sCode = append(sCode, []byte{0x88, 0xac}...)
|
||||||
pre = append(pre, sCode...)
|
pre = append(pre, sCode...)
|
||||||
} else if len(tx.TxIn[idx].SignatureScript) == 34 &&
|
} else if len(sigscript) == 34 &&
|
||||||
tx.TxIn[idx].SignatureScript[0] == 0x00 &&
|
sigscript[0] == 0x00 && sigscript[1] == 0x20 {
|
||||||
tx.TxIn[idx].SignatureScript[1] == 0x20 {
|
|
||||||
// whs mode- need to remove codeseparators. this doesn't yet.
|
// whs mode- need to remove codeseparators. this doesn't yet.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
writeVarBytes(&buf, 0, tx.TxIn[idx].Witness[0])
|
writeVarBytes(&buf, 0, sigscript)
|
||||||
pre = append(pre, buf.Bytes()...)
|
pre = append(pre, buf.Bytes()...)
|
||||||
} else {
|
} else {
|
||||||
// ?? this is not witness tx! fail
|
// ?? this is not witness tx! fail
|
||||||
|
fmt.Printf("Non witness error ")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,13 +205,13 @@ func main() {
|
|||||||
|
|
||||||
// make tx
|
// make tx
|
||||||
ttx := wire.NewMsgTx()
|
ttx := wire.NewMsgTx()
|
||||||
|
|
||||||
// deserialize into tx
|
// deserialize into tx
|
||||||
buf := bytes.NewBuffer(txbytes)
|
buf := bytes.NewBuffer(txbytes)
|
||||||
ttx.Deserialize(buf)
|
ttx.Deserialize(buf)
|
||||||
|
|
||||||
|
fmt.Printf("=====tx locktime: %x\n", ttx.LockTime)
|
||||||
ttx.TxIn[0].SignatureScript = in0spk
|
ttx.TxIn[0].SignatureScript = in0spk
|
||||||
ttx.TxIn[1].SignatureScript = in1spk
|
// ttx.TxIn[1].SignatureScript = in1spk
|
||||||
|
|
||||||
fmt.Printf(uspv.TxToString(ttx))
|
fmt.Printf(uspv.TxToString(ttx))
|
||||||
|
|
||||||
@@ -218,15 +219,36 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
// assert flag before writing witness
|
||||||
sig, err := txscript.WitnessScript(ttx, 1, inamt1, in1spk,
|
ttx.Flags = 0x01
|
||||||
|
ttx.TxIn[1].Witness, err = txscript.WitnessScript(ttx, 1, inamt1, in1spk,
|
||||||
txscript.SigHashAll, priv, true)
|
txscript.SigHashAll, priv, true)
|
||||||
|
// ttx.TxIn[1].SignatureScript = nil
|
||||||
|
|
||||||
fmt.Printf("got sig %x\n", sig)
|
hxh := calcWitnessSignatureHash(in1spk, txscript.SigHashAll, ttx, 1, inamt1)
|
||||||
hxh := calcWitnessSignatureHash(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("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
|
// 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",
|
log.Printf("error getting tx %s: %s",
|
||||||
thing.Hash.String(), err.Error())
|
thing.Hash.String(), err.Error())
|
||||||
}
|
}
|
||||||
|
fmt.Printf(TxToString(tx))
|
||||||
s.outMsgQueue <- tx
|
s.outMsgQueue <- tx
|
||||||
sent++
|
sent++
|
||||||
continue
|
continue
|
||||||
@@ -199,6 +200,7 @@ func (s *SPVCon) GetDataHandler(m *wire.MsgGetData) {
|
|||||||
log.Printf("error getting tx %s: %s",
|
log.Printf("error getting tx %s: %s",
|
||||||
thing.Hash.String(), err.Error())
|
thing.Hash.String(), err.Error())
|
||||||
}
|
}
|
||||||
|
tx.Flags = 0x00 // dewitnessify
|
||||||
s.outMsgQueue <- tx
|
s.outMsgQueue <- tx
|
||||||
sent++
|
sent++
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ func (s *SPVCon) SendCoins(adr btcutil.Address, sendAmt int64) error {
|
|||||||
|
|
||||||
fmt.Printf("tx: %s", TxToString(tx))
|
fmt.Printf("tx: %s", TxToString(tx))
|
||||||
buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
|
buf := bytes.NewBuffer(make([]byte, 0, tx.SerializeSize()))
|
||||||
tx.Serialize(buf)
|
tx.SerializeWitness(buf)
|
||||||
fmt.Printf("tx: %x\n", buf.Bytes())
|
fmt.Printf("tx: %x\n", buf.Bytes())
|
||||||
|
|
||||||
// send it out on the wire. hope it gets there.
|
// send it out on the wire. hope it gets there.
|
||||||
|
|||||||
+6
-7
@@ -137,20 +137,19 @@ func CheckDoubleSpends(
|
|||||||
|
|
||||||
// TxToString prints out some info about a transaction. for testing / debugging
|
// TxToString prints out some info about a transaction. for testing / debugging
|
||||||
func TxToString(tx *wire.MsgTx) string {
|
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.SerializeSize(), tx.VirtualSize(), tx.SerializeSizeWitness(),
|
||||||
tx.TxSha().String())
|
tx.LockTime, tx.Flags, tx.TxSha().String())
|
||||||
for i, in := range tx.TxIn {
|
for i, in := range tx.TxIn {
|
||||||
str += fmt.Sprintf("Input %d: %s\n", i, in.PreviousOutPoint.String())
|
str += fmt.Sprintf("Input %d spends %s\n", i, in.PreviousOutPoint.String())
|
||||||
str += fmt.Sprintf("SigScript for input %d: %x\n", i, in.SignatureScript)
|
str += fmt.Sprintf("\tSigScript: %x\n", in.SignatureScript)
|
||||||
for j, wit := range in.Witness {
|
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 {
|
for i, out := range tx.TxOut {
|
||||||
if out != nil {
|
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)
|
i, out.PkScript, out.Value)
|
||||||
} else {
|
} else {
|
||||||
str += fmt.Sprintf("output %d nil (WARNING)\n", i)
|
str += fmt.Sprintf("output %d nil (WARNING)\n", i)
|
||||||
|
|||||||
+1
-1
@@ -470,7 +470,7 @@ func (ts *TxStore) Ingest(tx *wire.MsgTx, height int32) (uint32, error) {
|
|||||||
// store this relevant tx
|
// store this relevant tx
|
||||||
sha := tx.TxSha()
|
sha := tx.TxSha()
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
tx.Serialize(&buf)
|
tx.SerializeWitness(&buf) // always store witness version
|
||||||
err = txns.Put(sha.Bytes(), buf.Bytes())
|
err = txns.Put(sha.Bytes(), buf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in new issue
Block a user