MCPcopy Create free account
hub / github.com/btcsuite/btcd / addData

Method addData

txscript/scriptbuilder.go:159–200  ·  view source on GitHub ↗

addData is the internal function that actually pushes the passed data to the end of the script. It automatically chooses canonical opcodes depending on the length of the data. A zero length buffer will lead to a push of empty data onto the stack (OP_0). No data limits are enforced with this funct

(data []byte)

Source from the content-addressed store, hash-verified

157// the length of the data. A zero length buffer will lead to a push of empty
158// data onto the stack (OP_0). No data limits are enforced with this function.
159func (b *ScriptBuilder) addData(data []byte) *ScriptBuilder {
160 dataLen := len(data)
161
162 // When the data consists of a single number that can be represented
163 // by one of the "small integer" opcodes, use that opcode instead of
164 // a data push opcode followed by the number.
165 if dataLen == 0 || dataLen == 1 && data[0] == 0 {
166 b.script = append(b.script, OP_0)
167 return b
168 } else if dataLen == 1 && data[0] <= 16 {
169 b.script = append(b.script, (OP_1-1)+data[0])
170 return b
171 } else if dataLen == 1 && data[0] == 0x81 {
172 b.script = append(b.script, byte(OP_1NEGATE))
173 return b
174 }
175
176 // Use one of the OP_DATA_# opcodes if the length of the data is small
177 // enough so the data push instruction is only a single byte.
178 // Otherwise, choose the smallest possible OP_PUSHDATA# opcode that
179 // can represent the length of the data.
180 if dataLen < OP_PUSHDATA1 {
181 b.script = append(b.script, byte((OP_DATA_1-1)+dataLen))
182 } else if dataLen <= 0xff {
183 b.script = append(b.script, OP_PUSHDATA1, byte(dataLen))
184 } else if dataLen <= 0xffff {
185 buf := make([]byte, 2)
186 binary.LittleEndian.PutUint16(buf, uint16(dataLen))
187 b.script = append(b.script, OP_PUSHDATA2)
188 b.script = append(b.script, buf...)
189 } else {
190 buf := make([]byte, 4)
191 binary.LittleEndian.PutUint32(buf, uint32(dataLen))
192 b.script = append(b.script, OP_PUSHDATA4)
193 b.script = append(b.script, buf...)
194 }
195
196 // Append the actual data.
197 b.script = append(b.script, data...)
198
199 return b
200}
201
202// AddFullData should not typically be used by ordinary users as it does not
203// include the checks which prevent data pushes larger than the maximum allowed

Callers 2

AddFullDataMethod · 0.95
AddDataMethod · 0.95

Calls 2

PutUint16Method · 0.80
PutUint32Method · 0.80

Tested by

no test coverage detected