-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
101 lines (85 loc) · 2.01 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ombwire
import (
"bytes"
"errors"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/golang/protobuf/proto"
)
func EncodeWireType(m proto.Message) ([]byte, error) {
empt := []byte{}
b := make([]byte, 0, MaxRecordLength)
buf := bytes.NewBuffer(b)
buf.Write(Magic[:])
// Write the type byte
switch m.(type) {
case *Bulletin:
buf.WriteByte(BulletinMagic)
case *Endorsement:
buf.WriteByte(EndorsementMagic)
default:
return empt, errors.New("unsupported type")
}
// Write the length of the protocol buf
s := uint64(proto.Size(m))
err := writeVarInt(buf, s)
if err != nil {
return empt, err
}
if 6+4+s > MaxRecordLength {
return empt, ErrRecordTooBig
}
mb, err := proto.Marshal(m)
if err != nil {
return empt, err
}
// Write the protobuf Message to the buf
buf.Write(mb)
return buf.Bytes(), nil
}
// Converts a bulletin into public key scripts for encoding
func (bltn *Bulletin) TxOuts(toBurn int64, net *chaincfg.Params) ([]*wire.TxOut, error) {
empt := []*wire.TxOut{}
rawbytes, err := EncodeWireType(bltn)
if err != nil {
return empt, err
}
numcuts := numOuts(len(rawbytes))
cuts := make([][]byte, numcuts, numcuts)
for i := 0; i < numcuts; i++ {
sliceb := make([]byte, 20, 20)
copy(sliceb, rawbytes)
cuts[i] = sliceb
if len(rawbytes) >= 20 {
rawbytes = rawbytes[20:]
}
}
// Convert raw data into txouts
txouts := make([]*wire.TxOut, 0)
for _, cut := range cuts {
fakeaddr, err := btcutil.NewAddressPubKeyHash(cut, net)
if err != nil {
return empt, err
}
pkscript, err := txscript.PayToAddrScript(fakeaddr)
if err != nil {
return empt, err
}
txout := &wire.TxOut{
PkScript: pkscript,
Value: toBurn,
}
txouts = append(txouts, txout)
}
return txouts, nil
}
// numOuts returns the number of P2PKH outs needed to encode this bulletin
func numOuts(length int) int {
numouts := length / 20
if length%20 != 0 {
numouts += 1
}
return numouts
}