diff --git a/VERSION b/VERSION index 23453d568..616c0bf9c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.44.5 +0.44.6 diff --git a/examples/token-sale/token_sale.go b/examples/token-sale/token_sale.go index 0e150925f..c6f399b18 100644 --- a/examples/token-sale/token_sale.go +++ b/examples/token-sale/token_sale.go @@ -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 { diff --git a/pkg/vm/api/util/util.go b/pkg/vm/api/util/util.go new file mode 100644 index 000000000..fee3d9b05 --- /dev/null +++ b/pkg/vm/api/util/util.go @@ -0,0 +1,6 @@ +package util + +// FromAddress returns the underlying bytes from the given address string. +func FromAddress(address string) []byte { + return nil +} diff --git a/pkg/vm/compiler/README.md b/pkg/vm/compiler/README.md index b97069731..588630308 100644 --- a/pkg/vm/compiler/README.md +++ b/pkg/vm/compiler/README.md @@ -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 diff --git a/pkg/vm/compiler/analysis.go b/pkg/vm/compiler/analysis.go index 05cd1e5fc..aacadd5ef 100644 --- a/pkg/vm/compiler/analysis.go +++ b/pkg/vm/compiler/analysis.go @@ -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. diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index f1a90aba3..c8affaf04 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -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) } }