forked from TrueCloudLab/neoneo-go
implemented smart contract utility function FromAddress (#88)
* implemented smart contract utility function FromAddress * bumped version
This commit is contained in:
parent
4bd4635e49
commit
311313f2ff
6 changed files with 35 additions and 7 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.44.5
|
0.44.6
|
||||||
|
|
|
@ -3,6 +3,7 @@ package tokensale
|
||||||
import (
|
import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/api/runtime"
|
"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/storage"
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/vm/api/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -10,7 +11,7 @@ const (
|
||||||
multiplier = decimals * 10
|
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.
|
// TokenConfig holds information about the token we want to use for the sale.
|
||||||
type TokenConfig struct {
|
type TokenConfig struct {
|
||||||
|
|
6
pkg/vm/api/util/util.go
Normal file
6
pkg/vm/api/util/util.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
// FromAddress returns the underlying bytes from the given address string.
|
||||||
|
func FromAddress(address string) []byte {
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -32,6 +32,9 @@ The neo-go compiler compiles Go programs to bytecode that the NEO virtual machin
|
||||||
- Hash256
|
- Hash256
|
||||||
- Hash160
|
- Hash160
|
||||||
|
|
||||||
|
### Custom utility functions
|
||||||
|
- `FromAddress(address string) []byte`
|
||||||
|
|
||||||
## Not yet implemented
|
## Not yet implemented
|
||||||
- range
|
- range
|
||||||
- some parts of the interop layer (VM API)
|
- some parts of the interop layer (VM API)
|
||||||
|
@ -109,9 +112,12 @@ Will output something like:
|
||||||
```Golang
|
```Golang
|
||||||
package mycontract
|
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 {
|
func Main() bool {
|
||||||
isOwner := runtime.CheckWitness(owner)
|
isOwner := runtime.CheckWitness(owner)
|
||||||
|
@ -135,7 +141,7 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/api/storage"
|
"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 {
|
type Token struct {
|
||||||
Name string
|
Name string
|
||||||
|
|
|
@ -14,7 +14,7 @@ var (
|
||||||
// Go language builtin functions and custom builtin utility functions.
|
// Go language builtin functions and custom builtin utility functions.
|
||||||
builtinFuncs = []string{
|
builtinFuncs = []string{
|
||||||
"len", "append", "SHA256",
|
"len", "append", "SHA256",
|
||||||
"SHA1", "Hash256", "Hash160",
|
"SHA1", "Hash256", "Hash160", "FromAddress",
|
||||||
}
|
}
|
||||||
|
|
||||||
// VM system calls that have no return value.
|
// VM system calls that have no return value.
|
||||||
|
|
|
@ -8,10 +8,13 @@ import (
|
||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/crypto"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm"
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// The identifier of the entry function. Default set to Main.
|
||||||
const mainIdent = "Main"
|
const mainIdent = "Main"
|
||||||
|
|
||||||
type codegen struct {
|
type codegen struct {
|
||||||
|
@ -188,7 +191,7 @@ func (c *codegen) convertFuncDecl(file *ast.File, decl *ast.FuncDecl) {
|
||||||
|
|
||||||
ast.Walk(c, decl.Body)
|
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) {
|
if !hasReturnStmt(decl) {
|
||||||
emitOpcode(c.prog, vm.Ofromaltstack)
|
emitOpcode(c.prog, vm.Ofromaltstack)
|
||||||
emitOpcode(c.prog, vm.Odrop)
|
emitOpcode(c.prog, vm.Odrop)
|
||||||
|
@ -564,6 +567,18 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
|
||||||
emitOpcode(c.prog, vm.Ohash256)
|
emitOpcode(c.prog, vm.Ohash256)
|
||||||
case "Hash160":
|
case "Hash160":
|
||||||
emitOpcode(c.prog, vm.Ohash160)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue