2017-09-09 22:54:58 -05:00
|
|
|
package main
|
|
|
|
|
2017-09-16 22:15:58 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"log"
|
|
|
|
)
|
2017-09-09 22:54:58 -05:00
|
|
|
|
|
|
|
// TXOutput represents a transaction output
|
|
|
|
type TXOutput struct {
|
2017-09-10 02:05:23 -05:00
|
|
|
Value int
|
|
|
|
PubKeyHash []byte
|
2017-09-09 22:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock signs the output
|
|
|
|
func (out *TXOutput) Lock(address []byte) {
|
|
|
|
pubKeyHash := Base58Decode(address)
|
|
|
|
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-4]
|
2017-09-10 02:05:23 -05:00
|
|
|
out.PubKeyHash = pubKeyHash
|
2017-09-09 22:54:58 -05:00
|
|
|
}
|
|
|
|
|
2017-09-10 00:45:15 -05:00
|
|
|
// IsLockedWithKey checks if the output can be used by the owner of the pubkey
|
|
|
|
func (out *TXOutput) IsLockedWithKey(pubKeyHash []byte) bool {
|
2017-09-10 02:05:23 -05:00
|
|
|
return bytes.Compare(out.PubKeyHash, pubKeyHash) == 0
|
2017-09-09 22:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTXOutput create a new TXOutput
|
|
|
|
func NewTXOutput(value int, address string) *TXOutput {
|
|
|
|
txo := &TXOutput{value, nil}
|
|
|
|
txo.Lock([]byte(address))
|
|
|
|
|
|
|
|
return txo
|
|
|
|
}
|
2017-09-16 22:15:58 -05:00
|
|
|
|
|
|
|
// TXOutputs collects TXOutput
|
|
|
|
type TXOutputs struct {
|
|
|
|
Outputs []TXOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize serializes TXOutputs
|
|
|
|
func (outs TXOutputs) Serialize() []byte {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
|
|
|
|
enc := gob.NewEncoder(&buff)
|
|
|
|
err := enc.Encode(outs)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buff.Bytes()
|
|
|
|
}
|