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

Function MakeScriptNum

txscript/scriptnum.go:198–238  ·  view source on GitHub ↗

MakeScriptNum interprets the passed serialized bytes as an encoded integer and returns the result as a script number. Since the consensus rules dictate that serialized bytes interpreted as ints are only allowed to be in the range determined by a maximum number of bytes, on a per opcode basis, an er

(v []byte, requireMinimal bool, scriptNumLen int)

Source from the content-addressed store, hash-verified

196//
197// See the Bytes function documentation for example encodings.
198func MakeScriptNum(v []byte, requireMinimal bool, scriptNumLen int) (scriptNum, error) {
199 // Interpreting data requires that it is not larger than
200 // the passed scriptNumLen value.
201 if len(v) > scriptNumLen {
202 str := fmt.Sprintf("numeric value encoded as %x is %d bytes "+
203 "which exceeds the max allowed of %d", v, len(v),
204 scriptNumLen)
205 return 0, scriptError(ErrNumberTooBig, str)
206 }
207
208 // Enforce minimal encoded if requested.
209 if requireMinimal {
210 if err := checkMinimalDataEncoding(v); err != nil {
211 return 0, err
212 }
213 }
214
215 // Zero is encoded as an empty byte slice.
216 if len(v) == 0 {
217 return 0, nil
218 }
219
220 // Decode from little endian.
221 var result int64
222 for i, val := range v {
223 result |= int64(val) << uint8(8*i)
224 }
225
226 // When the most significant byte of the input bytes has the sign bit
227 // set, the result is negative. So, remove the sign bit from the result
228 // and make it negative.
229 if v[len(v)-1]&0x80 != 0 {
230 // The maximum length of v has already been determined to be 4
231 // above, so uint8 is enough to cover the max possible shift
232 // value of 24.
233 result &= ^(int64(0x80) << uint8(8*(len(v)-1)))
234 return scriptNum(-result), nil
235 }
236
237 return scriptNum(result), nil
238}

Callers 6

TestMakeScriptNumFunction · 0.85
PopIntMethod · 0.85
PeekIntMethod · 0.85

Calls 3

scriptErrorFunction · 0.85
checkMinimalDataEncodingFunction · 0.85
scriptNumTypeAlias · 0.85

Tested by 1

TestMakeScriptNumFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…