-
Notifications
You must be signed in to change notification settings - Fork 2
/
bob.go
224 lines (186 loc) · 5.48 KB
/
bob.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Package boost provides a way to parse boostpow data from a bpu.Tape
package boost
import (
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"github.com/bitcoinschema/go-bpu"
)
// FromTape builds a boost object from a *bpu.Tape
func (b *Boost) FromTape(tape *bpu.Tape) (err error) {
if (len(tape.Cell) < 8 || tape.Cell[1].Ops == nil) && !isBoostInput(tape) {
err = fmt.Errorf("invalid %s record len %d, %+v", b.Spend.Hash, len(tape.Cell), tape.Cell)
return
}
var isContract = false
if tape.Cell[0].S != nil && *tape.Cell[0].S == Prefix {
// Boostpow output found
if *tape.Cell[1].Ops != "OP_DROP" {
err = fmt.Errorf("no OP_DROP %s", *tape.Cell[1].Ops)
return
}
var data []byte
data, err = base64.StdEncoding.DecodeString(*tape.Cell[2].B)
if err != nil {
return err
}
var baseIdx uint8
if len(data) == 4 {
// Bounty
b.Spend.Category = binary.LittleEndian.Uint32(data)
baseIdx = 3
} else if len(data) == 20 {
// isContract = true
// Contract
baseIdx = 4
// Miner address
// If the miner pubkey hash is present, then the redeeming tx needs to have a signature
// that matches the pubkey hash. only the miner who has the private key can mine the boost output
dataStr := PubkeyHashToAddress(data)
b.Spend.MinerAddress = &dataStr
// Set the category
var dataCat []byte
dataCat, err = base64.StdEncoding.DecodeString(*tape.Cell[3].B)
if err != nil {
return err
}
b.Spend.Category = binary.LittleEndian.Uint32(dataCat)
}
if tape.Cell[baseIdx].S == nil {
err = fmt.Errorf("no content")
return
}
b.Spend.Content = *tape.Cell[baseIdx].S
// Calculate the difficulty from target
dataTarget, errTarget := base64.StdEncoding.DecodeString(*tape.Cell[baseIdx+1].B)
if errTarget != nil {
err = errTarget
return
}
ib := binary.BigEndian.Uint32(Reverse(dataTarget))
// to compact size
t := big.NewInt(int64(ib % 0x01000000))
t.Mul(t, big.NewInt(2).Exp(big.NewInt(2), big.NewInt(8*(int64(ib/0x01000000)-3)), nil))
// get difficulty
flt, errDifficulty := targetToDifficulty(*tape.Cell[baseIdx+1].B)
if errDifficulty != nil {
err = errDifficulty
return
}
// set difficulty
b.Spend.Difficulty = *flt
if len(tape.Cell) > 20 && (tape.Cell[18].Ops != nil && *tape.Cell[18].Ops == "OP_5") ||
(tape.Cell[19].Ops != nil && *tape.Cell[19].Ops == "OP_5") {
b.Spend.Version = 1
} else if len(tape.Cell) > 20 && tape.Cell[18].Ops != nil && (*tape.Cell[18].Ops == "OP_6" ||
*tape.Cell[19].Ops == "OP_6") {
b.Spend.Version = 2
}
// Set topic / tag
if tape.Cell[baseIdx+2].B != nil {
dataTagB, errDataTag := base64.StdEncoding.DecodeString(*tape.Cell[baseIdx+2].B)
if errDataTag != nil {
err = errDataTag
return
}
if len(dataTagB) > 0 {
topic := string(dataTagB)
b.Spend.Topic = &topic
}
}
if tape.Cell[baseIdx+3].B != nil {
// Set nonce
dataNonce, errNonce := base64.StdEncoding.DecodeString(*tape.Cell[baseIdx+3].B)
if errNonce != nil {
err = errNonce
return
}
b.Spend.Nonce = binary.LittleEndian.Uint32(dataNonce)
}
// Set additional data
b.Spend.AdditionalData = tape.Cell[baseIdx+4].S
return
} else if isBoostInput(tape) {
b.Redeem.Signature = *tape.Cell[0].H
b.Redeem.PubKey = *tape.Cell[1].H
// Set nonce
dataNonce, errNonce := base64.StdEncoding.DecodeString(*tape.Cell[2].B)
if errNonce != nil {
err = errNonce
return
}
b.Redeem.Nonce = binary.LittleEndian.Uint32(dataNonce)
// Set timestamp
dataTimestamp, errTimestamp := base64.StdEncoding.DecodeString(*tape.Cell[3].B)
if errTimestamp != nil {
err = errNonce
return
}
b.Redeem.Timestamp = binary.LittleEndian.Uint32(dataTimestamp)
// Set extra nonce 2
b.Redeem.ExtraNonce2 = *tape.Cell[4].H
// Set extra nonce 1
dataNonce1, errNonce1 := base64.StdEncoding.DecodeString(*tape.Cell[5].B)
if errNonce1 != nil {
err = errNonce1
return
}
b.Redeem.ExtraNonce1 = binary.BigEndian.Uint32(dataNonce1)
// Set miner pubkey hash
var dataMinerPkh []byte
var errMinerPkh error
if len(*tape.Cell[6].H) == 40 {
b.Redeem.Version = 1
// V1 bounty redeem
dataMinerPkh, errMinerPkh = base64.StdEncoding.DecodeString(*tape.Cell[6].B)
} else if len(tape.Cell) == 8 && len(*tape.Cell[6].H) == 8 {
b.Redeem.Version = 2
// V2 bounty redeem
dataMinerPkh, errMinerPkh = base64.StdEncoding.DecodeString(*tape.Cell[7].B)
} else if len(tape.Cell) == 7 && len(*tape.Cell[6].H) == 8 {
b.Redeem.Version = 2
// V2 contract redeem
dataMinerPkh, errMinerPkh = base64.StdEncoding.DecodeString(*tape.Cell[6].B)
isContract = true
}
if errMinerPkh != nil {
err = errMinerPkh
return
}
b.Redeem.MinerPubKeyHash = hex.EncodeToString(Reverse(dataMinerPkh[:]))
}
if b.Redeem.Version == 0 {
b.Redeem.Version = redeemVersion(tape, isContract)
}
return
}
func isBoostInput(tape *bpu.Tape) bool {
// v1 bounty redeem, v2 contract redeem = 7
// v2 bounty redeem = 8
return len(tape.Cell) == 7 || len(tape.Cell) == 8
}
func redeemVersion(tape *bpu.Tape, contract bool) int32 {
// v1 bounty redeem, v2 contract redeem = 7
// v2 bounty redeem = 8
if len(tape.Cell) == 7 {
if contract {
return 2
}
return 1
} else if len(tape.Cell) == 8 {
return 2
}
return 0
}
// NewFromTape takes a bob.Tape and returns a BAP data structure
func NewFromTape(tape *bpu.Tape) (b *Boost, err error) {
b = new(Boost)
if tape == nil {
err = fmt.Errorf("tape is nil %x", tape)
return
}
err = b.FromTape(tape)
return
}