MCPcopy Index your code
hub / github.com/btcsuite/btcd / TestScriptBuilderAddData

Function TestScriptBuilderAddData

txscript/scriptbuilder_test.go:182–314  ·  view source on GitHub ↗

TestScriptBuilderAddData tests that pushing data to a script via the ScriptBuilder API works as expected and conforms to BIP0062.

(t *testing.T)

Source from the content-addressed store, hash-verified

180// TestScriptBuilderAddData tests that pushing data to a script via the
181// ScriptBuilder API works as expected and conforms to BIP0062.
182func TestScriptBuilderAddData(t *testing.T) {
183 t.Parallel()
184
185 tests := []struct {
186 name string
187 data []byte
188 expected []byte
189 useFull bool // use AddFullData instead of AddData.
190 }{
191 // BIP0062: Pushing an empty byte sequence must use OP_0.
192 {name: "push empty byte sequence", data: nil, expected: []byte{OP_0}},
193 {name: "push 1 byte 0x00", data: []byte{0x00}, expected: []byte{OP_0}},
194
195 // BIP0062: Pushing a 1-byte sequence of byte 0x01 through 0x10 must use OP_n.
196 {name: "push 1 byte 0x01", data: []byte{0x01}, expected: []byte{OP_1}},
197 {name: "push 1 byte 0x02", data: []byte{0x02}, expected: []byte{OP_2}},
198 {name: "push 1 byte 0x03", data: []byte{0x03}, expected: []byte{OP_3}},
199 {name: "push 1 byte 0x04", data: []byte{0x04}, expected: []byte{OP_4}},
200 {name: "push 1 byte 0x05", data: []byte{0x05}, expected: []byte{OP_5}},
201 {name: "push 1 byte 0x06", data: []byte{0x06}, expected: []byte{OP_6}},
202 {name: "push 1 byte 0x07", data: []byte{0x07}, expected: []byte{OP_7}},
203 {name: "push 1 byte 0x08", data: []byte{0x08}, expected: []byte{OP_8}},
204 {name: "push 1 byte 0x09", data: []byte{0x09}, expected: []byte{OP_9}},
205 {name: "push 1 byte 0x0a", data: []byte{0x0a}, expected: []byte{OP_10}},
206 {name: "push 1 byte 0x0b", data: []byte{0x0b}, expected: []byte{OP_11}},
207 {name: "push 1 byte 0x0c", data: []byte{0x0c}, expected: []byte{OP_12}},
208 {name: "push 1 byte 0x0d", data: []byte{0x0d}, expected: []byte{OP_13}},
209 {name: "push 1 byte 0x0e", data: []byte{0x0e}, expected: []byte{OP_14}},
210 {name: "push 1 byte 0x0f", data: []byte{0x0f}, expected: []byte{OP_15}},
211 {name: "push 1 byte 0x10", data: []byte{0x10}, expected: []byte{OP_16}},
212
213 // BIP0062: Pushing the byte 0x81 must use OP_1NEGATE.
214 {name: "push 1 byte 0x81", data: []byte{0x81}, expected: []byte{OP_1NEGATE}},
215
216 // BIP0062: Pushing any other byte sequence up to 75 bytes must
217 // use the normal data push (opcode byte n, with n the number of
218 // bytes, followed n bytes of data being pushed).
219 {name: "push 1 byte 0x11", data: []byte{0x11}, expected: []byte{OP_DATA_1, 0x11}},
220 {name: "push 1 byte 0x80", data: []byte{0x80}, expected: []byte{OP_DATA_1, 0x80}},
221 {name: "push 1 byte 0x82", data: []byte{0x82}, expected: []byte{OP_DATA_1, 0x82}},
222 {name: "push 1 byte 0xff", data: []byte{0xff}, expected: []byte{OP_DATA_1, 0xff}},
223 {
224 name: "push data len 17",
225 data: bytes.Repeat([]byte{0x49}, 17),
226 expected: append([]byte{OP_DATA_17}, bytes.Repeat([]byte{0x49}, 17)...),
227 },
228 {
229 name: "push data len 75",
230 data: bytes.Repeat([]byte{0x49}, 75),
231 expected: append([]byte{OP_DATA_75}, bytes.Repeat([]byte{0x49}, 75)...),
232 },
233
234 // BIP0062: Pushing 76 to 255 bytes must use OP_PUSHDATA1.
235 {
236 name: "push data len 76",
237 data: bytes.Repeat([]byte{0x49}, 76),
238 expected: append([]byte{OP_PUSHDATA1, 76}, bytes.Repeat([]byte{0x49}, 76)...),
239 },

Callers

nothing calls this directly

Calls 5

ResetMethod · 0.95
ScriptMethod · 0.95
NewScriptBuilderFunction · 0.85
AddDataMethod · 0.80
AddFullDataMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…