2015-12-23 11:08:34 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
2015-12-30 16:38:57 +03:00
|
|
|
"bytes"
|
2015-12-23 11:08:34 +03:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"io"
|
2015-12-27 10:52:20 +03:00
|
|
|
"io/ioutil"
|
2015-12-23 11:08:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
//Actual pkScript, not redeemScript
|
|
|
|
type PkScript []byte
|
|
|
|
|
2015-12-27 05:20:25 +03:00
|
|
|
//Subsatoshi amount
|
|
|
|
type MicroSatoshi int32
|
|
|
|
|
2015-12-23 11:08:34 +03:00
|
|
|
//Writes the big endian representation of element
|
|
|
|
//Unified function to call when writing different types
|
|
|
|
//Pre-allocate a byte-array of the correct size for cargo-cult security
|
|
|
|
//More copies but whatever...
|
2015-12-31 10:34:40 +03:00
|
|
|
func writeElement(w io.Writer, element interface{}) error {
|
2015-12-23 11:08:34 +03:00
|
|
|
var err error
|
|
|
|
switch e := element.(type) {
|
|
|
|
case uint8:
|
|
|
|
var b [1]byte
|
|
|
|
b[0] = byte(e)
|
|
|
|
_, err = w.Write(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-23 11:08:34 +03:00
|
|
|
case uint32:
|
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], uint32(e))
|
|
|
|
_, err = w.Write(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-30 16:38:57 +03:00
|
|
|
case uint64:
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], uint64(e))
|
|
|
|
_, err = w.Write(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-12-23 11:08:34 +03:00
|
|
|
case btcutil.Amount:
|
2015-12-27 10:52:20 +03:00
|
|
|
err = binary.Write(w, binary.BigEndian, int64(e))
|
2015-12-23 11:08:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-23 11:08:34 +03:00
|
|
|
case *btcec.PublicKey:
|
|
|
|
var b [33]byte
|
|
|
|
serializedPubkey := e.SerializeCompressed()
|
|
|
|
if len(serializedPubkey) != 33 {
|
|
|
|
return fmt.Errorf("Wrong size pubkey")
|
|
|
|
}
|
|
|
|
copy(b[:], serializedPubkey)
|
|
|
|
_, err = w.Write(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-31 11:25:00 +03:00
|
|
|
case []*btcec.Signature:
|
|
|
|
numSigs := len(e)
|
2015-12-30 16:38:57 +03:00
|
|
|
if numSigs > 127 {
|
|
|
|
return fmt.Errorf("Too many signatures!")
|
|
|
|
}
|
|
|
|
//Write the size
|
2015-12-31 10:34:40 +03:00
|
|
|
err = writeElement(w, uint8(numSigs))
|
2015-12-30 16:38:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//Write the data
|
|
|
|
for i := 0; i < numSigs; i++ {
|
2015-12-31 11:25:00 +03:00
|
|
|
err = writeElement(w, e[i])
|
2015-12-30 16:38:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case *btcec.Signature:
|
|
|
|
sig := e.Serialize()
|
|
|
|
sigLength := len(sig)
|
|
|
|
if sigLength > 73 {
|
|
|
|
return fmt.Errorf("Signature too long!")
|
|
|
|
}
|
|
|
|
//Write the size
|
2015-12-31 10:34:40 +03:00
|
|
|
err = writeElement(w, uint8(sigLength))
|
2015-12-30 16:38:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//Write the data
|
|
|
|
_, err = w.Write(sig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case *wire.ShaHash:
|
|
|
|
_, err = w.Write(e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-12-23 11:08:34 +03:00
|
|
|
case [20]byte:
|
|
|
|
_, err = w.Write(e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
|
|
|
case wire.BitcoinNet:
|
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], uint32(e))
|
|
|
|
_, err := w.Write(b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-12-23 11:08:34 +03:00
|
|
|
case PkScript:
|
|
|
|
scriptLength := len(e)
|
|
|
|
//Make sure it's P2PKH or P2SH size or less
|
|
|
|
if scriptLength > 25 {
|
|
|
|
return fmt.Errorf("PkScript too long!")
|
|
|
|
}
|
|
|
|
//Write the size (1-byte)
|
2015-12-31 10:34:40 +03:00
|
|
|
err = writeElement(w, uint8(scriptLength))
|
2015-12-23 11:08:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//Write the data
|
|
|
|
_, err = w.Write(e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-23 11:08:34 +03:00
|
|
|
case []*wire.TxIn:
|
|
|
|
//Append the unsigned(!!!) txins
|
|
|
|
//Write the size (1-byte)
|
|
|
|
if len(e) > 127 {
|
|
|
|
return fmt.Errorf("Too many txins")
|
|
|
|
}
|
2015-12-31 10:34:40 +03:00
|
|
|
err = writeElement(w, uint8(len(e)))
|
2015-12-23 11:08:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//Append the actual TxIns (Size: NumOfTxins * 36)
|
|
|
|
//Do not include the sequence number to eliminate funny business
|
|
|
|
for _, in := range e {
|
2015-12-31 11:25:00 +03:00
|
|
|
err = writeElement(w, in)
|
2015-12-23 11:08:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-31 11:25:00 +03:00
|
|
|
case *wire.TxIn:
|
|
|
|
//Hash
|
|
|
|
var h [32]byte
|
|
|
|
copy(h[:], e.PreviousOutPoint.Hash.Bytes())
|
|
|
|
_, err = w.Write(h[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//Index
|
|
|
|
var idx [4]byte
|
|
|
|
binary.BigEndian.PutUint32(idx[:], e.PreviousOutPoint.Index)
|
|
|
|
_, err = w.Write(idx[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2015-12-23 11:08:34 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type in writeElement: %T", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-31 10:34:40 +03:00
|
|
|
func writeElements(w io.Writer, elements ...interface{}) error {
|
2015-12-28 14:24:16 +03:00
|
|
|
for _, element := range elements {
|
2015-12-31 10:34:40 +03:00
|
|
|
err := writeElement(w, element)
|
2015-12-28 14:24:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-31 10:34:40 +03:00
|
|
|
func readElement(r io.Reader, element interface{}) error {
|
2015-12-23 11:08:34 +03:00
|
|
|
var err error
|
|
|
|
switch e := element.(type) {
|
2015-12-27 05:20:25 +03:00
|
|
|
case *uint8:
|
2015-12-27 10:52:20 +03:00
|
|
|
var b [1]uint8
|
|
|
|
_, err = r.Read(b[:])
|
2015-12-27 05:20:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-27 10:52:20 +03:00
|
|
|
*e = b[0]
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-23 11:08:34 +03:00
|
|
|
case *uint32:
|
|
|
|
var b [4]byte
|
|
|
|
_, err = io.ReadFull(r, b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = binary.BigEndian.Uint32(b[:])
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-30 16:38:57 +03:00
|
|
|
case *uint64:
|
|
|
|
var b [8]byte
|
|
|
|
_, err = io.ReadFull(r, b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = binary.BigEndian.Uint64(b[:])
|
|
|
|
return nil
|
2015-12-27 05:20:25 +03:00
|
|
|
case *btcutil.Amount:
|
|
|
|
var b [8]byte
|
|
|
|
_, err = io.ReadFull(r, b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-27 10:52:20 +03:00
|
|
|
*e = btcutil.Amount(int64(binary.BigEndian.Uint64(b[:])))
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-30 16:38:57 +03:00
|
|
|
case **wire.ShaHash:
|
|
|
|
var b wire.ShaHash
|
|
|
|
_, err = io.ReadFull(r, b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = &b
|
|
|
|
return nil
|
2015-12-27 10:52:20 +03:00
|
|
|
case **btcec.PublicKey:
|
2015-12-27 05:20:25 +03:00
|
|
|
var b [33]byte
|
|
|
|
_, err = io.ReadFull(r, b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-27 10:52:20 +03:00
|
|
|
x, err := btcec.ParsePubKey(b[:], btcec.S256())
|
2015-12-30 16:38:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-27 10:52:20 +03:00
|
|
|
*e = &*x
|
2015-12-30 16:38:57 +03:00
|
|
|
return nil
|
2015-12-31 11:25:00 +03:00
|
|
|
case *[]*btcec.Signature:
|
2015-12-30 16:38:57 +03:00
|
|
|
var numSigs uint8
|
2015-12-31 10:34:40 +03:00
|
|
|
err = readElement(r, &numSigs)
|
2015-12-27 05:20:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-30 16:38:57 +03:00
|
|
|
if numSigs > 127 {
|
|
|
|
return fmt.Errorf("Too many signatures!")
|
|
|
|
}
|
|
|
|
|
|
|
|
//Read that number of signatures
|
2015-12-31 11:25:00 +03:00
|
|
|
var sigs []*btcec.Signature
|
2015-12-30 16:38:57 +03:00
|
|
|
for i := uint8(0); i < numSigs; i++ {
|
|
|
|
sig := new(btcec.Signature)
|
2015-12-31 10:34:40 +03:00
|
|
|
err = readElement(r, &sig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-31 11:25:00 +03:00
|
|
|
sigs = append(sigs, sig)
|
2015-12-30 16:38:57 +03:00
|
|
|
}
|
2015-12-31 11:25:00 +03:00
|
|
|
*e = *&sigs
|
2015-12-30 16:38:57 +03:00
|
|
|
return nil
|
|
|
|
case **btcec.Signature:
|
|
|
|
var sigLength uint8
|
2015-12-31 10:34:40 +03:00
|
|
|
err = readElement(r, &sigLength)
|
2015-12-30 16:38:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sigLength > 73 {
|
|
|
|
return fmt.Errorf("Signature too long!")
|
|
|
|
}
|
|
|
|
|
|
|
|
//Read the sig length
|
|
|
|
l := io.LimitReader(r, int64(sigLength))
|
|
|
|
sig, err := ioutil.ReadAll(l)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(sig) != int(sigLength) {
|
|
|
|
return fmt.Errorf("EOF: Signature length mismatch.")
|
|
|
|
}
|
|
|
|
btcecSig, err := btcec.ParseSignature(sig, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = &*btcecSig
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-27 05:20:25 +03:00
|
|
|
case *[20]byte:
|
|
|
|
_, err = io.ReadFull(r, e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
|
|
|
case *wire.BitcoinNet:
|
|
|
|
var b [4]byte
|
|
|
|
_, err := io.ReadFull(r, b[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = wire.BitcoinNet(binary.BigEndian.Uint32(b[:]))
|
|
|
|
return nil
|
2015-12-27 05:20:25 +03:00
|
|
|
case *PkScript:
|
|
|
|
//Get the script length first
|
2015-12-30 16:38:57 +03:00
|
|
|
var scriptLength uint8
|
2015-12-31 10:34:40 +03:00
|
|
|
err = readElement(r, &scriptLength)
|
2015-12-27 05:20:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-27 10:52:20 +03:00
|
|
|
|
2015-12-30 16:38:57 +03:00
|
|
|
if scriptLength > 25 {
|
2015-12-27 05:20:25 +03:00
|
|
|
return fmt.Errorf("PkScript too long!")
|
|
|
|
}
|
|
|
|
|
|
|
|
//Read the script length
|
2015-12-30 16:38:57 +03:00
|
|
|
l := io.LimitReader(r, int64(scriptLength))
|
|
|
|
*e, err = ioutil.ReadAll(l)
|
|
|
|
if len(*e) != int(scriptLength) {
|
|
|
|
return fmt.Errorf("EOF: Signature length mismatch.")
|
|
|
|
}
|
2015-12-27 05:20:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-27 10:52:20 +03:00
|
|
|
case *[]*wire.TxIn:
|
2015-12-27 05:20:25 +03:00
|
|
|
//Read the size (1-byte number of txins)
|
2015-12-30 16:38:57 +03:00
|
|
|
var numScripts uint8
|
2015-12-31 10:34:40 +03:00
|
|
|
err = readElement(r, &numScripts)
|
2015-12-27 05:20:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-30 16:38:57 +03:00
|
|
|
if numScripts > 127 {
|
2015-12-27 05:20:25 +03:00
|
|
|
return fmt.Errorf("Too many txins")
|
|
|
|
}
|
|
|
|
|
|
|
|
//Append the actual TxIns
|
|
|
|
var txins []*wire.TxIn
|
2015-12-30 16:38:57 +03:00
|
|
|
for i := uint8(0); i < numScripts; i++ {
|
2015-12-31 11:25:00 +03:00
|
|
|
outpoint := new(wire.OutPoint)
|
|
|
|
txin := wire.NewTxIn(outpoint, nil)
|
|
|
|
err = readElement(r, &txin)
|
2015-12-27 05:20:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-31 11:25:00 +03:00
|
|
|
txins = append(txins, txin)
|
2015-12-27 05:20:25 +03:00
|
|
|
}
|
2015-12-27 10:52:20 +03:00
|
|
|
*e = *&txins
|
2015-12-28 14:24:16 +03:00
|
|
|
return nil
|
2015-12-31 11:25:00 +03:00
|
|
|
case **wire.TxIn:
|
|
|
|
//Hash
|
|
|
|
var h [32]byte
|
|
|
|
_, err = io.ReadFull(r, h[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hash, err := wire.NewShaHash(h[:])
|
|
|
|
(*e).PreviousOutPoint.Hash = *hash
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
//Index
|
|
|
|
var idxBytes [4]byte
|
|
|
|
_, err = io.ReadFull(r, idxBytes[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
(*e).PreviousOutPoint.Index = binary.BigEndian.Uint32(idxBytes[:])
|
|
|
|
return nil
|
2015-12-27 05:20:25 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type in readElement: %T", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-31 10:34:40 +03:00
|
|
|
func readElements(r io.Reader, elements ...interface{}) error {
|
2015-12-28 14:24:16 +03:00
|
|
|
for _, element := range elements {
|
2015-12-31 10:34:40 +03:00
|
|
|
err := readElement(r, element)
|
2015-12-28 14:24:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-23 11:08:34 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-30 16:38:57 +03:00
|
|
|
|
|
|
|
//Validates whether a PkScript byte array is P2SH or P2PKH
|
|
|
|
func ValidatePkScript(pkScript PkScript) error {
|
2015-12-31 10:34:40 +03:00
|
|
|
if &pkScript == nil {
|
|
|
|
return fmt.Errorf("PkScript should not be empty!")
|
|
|
|
}
|
2015-12-30 16:38:57 +03:00
|
|
|
if len(pkScript) == 25 {
|
|
|
|
//P2PKH
|
|
|
|
//Begins with OP_DUP OP_HASH160 PUSHDATA(20)
|
|
|
|
if !bytes.Equal(pkScript[0:3], []byte{118, 169, 20}) ||
|
|
|
|
//Ends with OP_EQUALVERIFY OP_CHECKSIG
|
|
|
|
!bytes.Equal(pkScript[23:25], []byte{136, 172}) {
|
|
|
|
//If it's not correct, return error
|
|
|
|
return fmt.Errorf("PkScript only allows P2SH or P2PKH")
|
|
|
|
}
|
|
|
|
} else if len(pkScript) == 23 {
|
|
|
|
//P2SH
|
|
|
|
//Begins with OP_HASH160 PUSHDATA(20)
|
|
|
|
if !bytes.Equal(pkScript[0:2], []byte{169, 20}) ||
|
|
|
|
//Ends with OP_EQUAL
|
|
|
|
!bytes.Equal(pkScript[22:23], []byte{135}) {
|
|
|
|
//If it's not correct, return error
|
|
|
|
return fmt.Errorf("PkScript only allows P2SH or P2PKH")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Length not 23 or 25
|
|
|
|
return fmt.Errorf("PkScript only allows P2SH or P2PKH")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|