implemented smart contract utility function FromAddress (#88)

* implemented smart contract utility function FromAddress

* bumped version
This commit is contained in:
Anthony De Meulemeester 2018-07-02 15:02:00 +02:00 committed by GitHub
parent 4bd4635e49
commit 311313f2ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 7 deletions

View file

@ -1 +1 @@
0.44.5
0.44.6

View file

@ -3,6 +3,7 @@ package tokensale
import (
"github.com/CityOfZion/neo-go/pkg/vm/api/runtime"
"github.com/CityOfZion/neo-go/pkg/vm/api/storage"
"github.com/CityOfZion/neo-go/pkg/vm/api/util"
)
const (
@ -10,7 +11,7 @@ const (
multiplier = decimals * 10
)
var owner = []byte{0xaf, 0x12, 0xa8, 0x68, 0x7b, 0x14, 0x94, 0x8b, 0xc4, 0xa0, 0x08, 0x12, 0x8a, 0x55, 0x0a, 0x63, 0x69, 0x5b, 0xc1, 0xa5}
var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg")
// TokenConfig holds information about the token we want to use for the sale.
type TokenConfig struct {

6
pkg/vm/api/util/util.go Normal file
View file

@ -0,0 +1,6 @@
package util
// FromAddress returns the underlying bytes from the given address string.
func FromAddress(address string) []byte {
return nil
}

View file

@ -32,6 +32,9 @@ The neo-go compiler compiles Go programs to bytecode that the NEO virtual machin
- Hash256
- Hash160
### Custom utility functions
- `FromAddress(address string) []byte`
## Not yet implemented
- range
- some parts of the interop layer (VM API)
@ -109,9 +112,12 @@ Will output something like:
```Golang
package mycontract
import "github.com/CityOfZion/neo-go/pkg/vm/api/runtime"
import (
"github.com/CityOfZion/neo-go/pkg/vm/api/runtime"
"github.com/CityOfZion/neo-go/pkg/vm/api/util"
)
var owner = []byte{0xaf, 0x12, 0xa8, 0x68, 0x7b, 0x14, 0x94, 0x8b, 0xc4, 0xa0, 0x08, 0x12, 0x8a, 0x55, 0x0a, 0x63, 0x69, 0x5b, 0xc1, 0xa5}
var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg")
func Main() bool {
isOwner := runtime.CheckWitness(owner)
@ -135,7 +141,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/vm/api/storage"
)
var owner = []byte{0xaf, 0x12, 0xa8, 0x68, 0x7b, 0x14, 0x94, 0x8b, 0xc4, 0xa0, 0x08, 0x12, 0x8a, 0x55, 0x0a, 0x63, 0x69, 0x5b, 0xc1, 0xa5}
var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg")
type Token struct {
Name string

View file

@ -14,7 +14,7 @@ var (
// Go language builtin functions and custom builtin utility functions.
builtinFuncs = []string{
"len", "append", "SHA256",
"SHA1", "Hash256", "Hash160",
"SHA1", "Hash256", "Hash160", "FromAddress",
}
// VM system calls that have no return value.

View file

@ -8,10 +8,13 @@ import (
"go/token"
"go/types"
"log"
"strings"
"github.com/CityOfZion/neo-go/pkg/crypto"
"github.com/CityOfZion/neo-go/pkg/vm"
)
// The identifier of the entry function. Default set to Main.
const mainIdent = "Main"
type codegen struct {
@ -188,7 +191,7 @@ func (c *codegen) convertFuncDecl(file *ast.File, decl *ast.FuncDecl) {
ast.Walk(c, decl.Body)
// If this function returs the void (no return stmt) we will cleanup its junk on the stack.
// If this function returns the void (no return stmt) we will cleanup its junk on the stack.
if !hasReturnStmt(decl) {
emitOpcode(c.prog, vm.Ofromaltstack)
emitOpcode(c.prog, vm.Odrop)
@ -564,6 +567,18 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
emitOpcode(c.prog, vm.Ohash256)
case "Hash160":
emitOpcode(c.prog, vm.Ohash160)
case "FromAddress":
// We can be sure that this is a ast.BasicLit just containing a simple
// address string. Note that the string returned from callin Value will
// contain double qoutes that need to be stripped.
addressStr := expr.Args[0].(*ast.BasicLit).Value
addressStr = strings.Replace(addressStr, "\"", "", 2)
uint160, err := crypto.Uint160DecodeAddress(addressStr)
if err != nil {
log.Fatal(err)
}
bytes := uint160.Bytes()
emitBytes(c.prog, bytes)
}
}