-
Notifications
You must be signed in to change notification settings - Fork 9
/
pushdata.go
64 lines (57 loc) · 1.26 KB
/
pushdata.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
package vm
import "encoding/binary"
func opFalse(vm *virtualMachine) error {
err := vm.applyCost(1)
if err != nil {
return err
}
return vm.pushBool(false, false)
}
func opPushdata(vm *virtualMachine) error {
err := vm.applyCost(1)
if err != nil {
return err
}
d := make([]byte, len(vm.data))
copy(d, vm.data)
return vm.push(d, false)
}
func op1Negate(vm *virtualMachine) error {
err := vm.applyCost(1)
if err != nil {
return err
}
return vm.pushInt64(-1, false)
}
func opNop(vm *virtualMachine) error {
return vm.applyCost(1)
}
func PushdataBytes(in []byte) []byte {
l := len(in)
if l == 0 {
return []byte{byte(OP_0)}
}
if l <= 75 {
return append([]byte{byte(OP_DATA_1) + uint8(l) - 1}, in...)
}
if l < 1<<8 {
return append([]byte{byte(OP_PUSHDATA1), uint8(l)}, in...)
}
if l < 1<<16 {
var b [2]byte
binary.LittleEndian.PutUint16(b[:], uint16(l))
return append([]byte{byte(OP_PUSHDATA2), b[0], b[1]}, in...)
}
var b [4]byte
binary.LittleEndian.PutUint32(b[:], uint32(l))
return append([]byte{byte(OP_PUSHDATA4), b[0], b[1], b[2], b[3]}, in...)
}
func PushdataInt64(n int64) []byte {
if n == 0 {
return []byte{byte(OP_0)}
}
if n >= 1 && n <= 16 {
return []byte{uint8(OP_1) + uint8(n) - 1}
}
return PushdataBytes(Int64Bytes(n))
}