From 233ec9538c765b284ef826a61942250ec8ffbf24 Mon Sep 17 00:00:00 2001 From: Tadge Dryja Date: Fri, 19 Feb 2016 14:49:04 -0800 Subject: [PATCH] add testing sighash folder --- .gitignore | 3 ++ sighash143/sighash143.go | 91 ++++++++++++++++++++++++++++++++++++++++ uspv/sortsignsend.go | 1 + uspv/txstore.go | 25 +++++++++++ 4 files changed, 120 insertions(+) create mode 100644 sighash143/sighash143.go diff --git a/.gitignore b/.gitignore index b55095c7..2d4d343a 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,9 @@ cmd/cmd cmd/lncli/lncli cmd/lnshell/lnshell +sighash143/sighash143 +sighash143/*.hex + test_wal/* # vim diff --git a/sighash143/sighash143.go b/sighash143/sighash143.go new file mode 100644 index 00000000..7d93206a --- /dev/null +++ b/sighash143/sighash143.go @@ -0,0 +1,91 @@ +package main + +import ( + "bytes" + "encoding/binary" + "encoding/hex" + "fmt" + "io/ioutil" + "log" + "strings" + + "github.com/btcsuite/btcd/txscript" + "github.com/btcsuite/btcd/wire" + "github.com/lightningnetwork/lnd/uspv" +) + +const ( + inspk0 = "2103c9f4836b9a4f77fc0d81f7bcb01b7f1b35916864b9476c241ce9fc198bd25432ac" + inamt0 = uint64(625000000) + + inspk1 = "00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1" + inamt1 = uint64(600000000) +) + +func outpointBytesLil(op wire.OutPoint) []byte { + var buf bytes.Buffer + // ignore errors because.. whatever + _ = binary.Write(&buf, binary.LittleEndian, op.Index) + + b := op.Hash[:] + return append(b, buf.Bytes()...) +} + +func calcSignatureHash( + hashType txscript.SigHashType, tx *wire.MsgTx, idx int) []byte { + + return nil +} + +// if sighash_ALL, hash of all txin outpoints, sequentially. +// if other sighash type, 0x00 * 32 +func calcHashPrevOuts(tx *wire.MsgTx) [32]byte { + + for _, in := range tx.TxIn { + // in.PreviousOutPoint.Hash + } + + return wire.DoubleSha256SH(tx.TxIn[0].SignatureScript) +} + +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) + } + + // 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) + + ttx.TxIn[0].SignatureScript = in0spk + ttx.TxIn[1].SignatureScript = in1spk + + fmt.Printf(uspv.TxToString(ttx)) + +} diff --git a/uspv/sortsignsend.go b/uspv/sortsignsend.go index 93813a23..f1ef3647 100644 --- a/uspv/sortsignsend.go +++ b/uspv/sortsignsend.go @@ -226,6 +226,7 @@ func (s *SPVCon) SendCoins(adr btcutil.Address, sendAmt int64) error { return nil } +// SignThis isn't used anymore... func (t *TxStore) SignThis(tx *wire.MsgTx) error { fmt.Printf("-= SignThis =-\n") diff --git a/uspv/txstore.go b/uspv/txstore.go index 89b83d5f..2fe582f3 100644 --- a/uspv/txstore.go +++ b/uspv/txstore.go @@ -185,6 +185,20 @@ func outPointToBytes(op *wire.OutPoint) ([]byte, error) { return buf.Bytes(), nil } +/*----- serialization for utxos ------- */ +/* Utxos serialization: +byte length desc at offset + +32 txid 0 +4 idx 32 +4 height 36 +4 keyidx 40 +8 amt 44 +1 flag 52 + +end len 53 +*/ + // ToBytes turns a Utxo into some bytes. // note that the txid is the first 36 bytes and in our use cases will be stripped // off, but is left here for other applications @@ -278,6 +292,17 @@ func UtxoFromBytes(b []byte) (Utxo, error) { return u, nil } +/*----- serialization for stxos ------- */ +/* Stxo serialization: +byte length desc at offset + +53 utxo 0 +4 sheight 53 +32 stxid 57 + +end len 89 +*/ + // ToBytes turns an Stxo into some bytes. // prevUtxo serialization, then spendheight [4], spendtxid [32] func (s *Stxo) ToBytes() ([]byte, error) {