2014-04-15 23:16:38 +03:00
|
|
|
package ethchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Disassemble(script []byte) (asm []string) {
|
|
|
|
pc := new(big.Int)
|
|
|
|
for {
|
|
|
|
if pc.Cmp(big.NewInt(int64(len(script)))) >= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the memory location of pc
|
|
|
|
val := script[pc.Int64()]
|
|
|
|
// Get the opcode (it must be an opcode!)
|
|
|
|
op := OpCode(val)
|
|
|
|
|
|
|
|
asm = append(asm, fmt.Sprintf("%v", op))
|
|
|
|
|
|
|
|
switch op {
|
2014-05-06 18:43:27 +03:00
|
|
|
case oPUSH32: // Push PC+1 on to the stack
|
2014-04-15 23:16:38 +03:00
|
|
|
pc.Add(pc, ethutil.Big1)
|
|
|
|
data := script[pc.Int64() : pc.Int64()+32]
|
|
|
|
val := ethutil.BigD(data)
|
|
|
|
|
|
|
|
var b []byte
|
|
|
|
if val.Int64() == 0 {
|
|
|
|
b = []byte{0}
|
|
|
|
} else {
|
|
|
|
b = val.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
asm = append(asm, fmt.Sprintf("0x%x", b))
|
|
|
|
|
|
|
|
pc.Add(pc, big.NewInt(31))
|
|
|
|
}
|
|
|
|
|
|
|
|
pc.Add(pc, ethutil.Big1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|