2018-02-09 16:08:50 +00:00
|
|
|
package smartcontract
|
|
|
|
|
|
|
|
import (
|
2018-03-05 08:53:09 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2018-03-05 08:53:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-08-25 14:40:15 +00:00
|
|
|
"os"
|
2020-12-03 11:57:45 +00:00
|
|
|
"path"
|
2018-08-25 14:40:15 +00:00
|
|
|
"path/filepath"
|
2020-03-11 18:11:51 +00:00
|
|
|
"strings"
|
2018-03-05 08:53:09 +00:00
|
|
|
|
2020-03-11 18:11:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
2020-08-31 09:22:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
2020-06-17 21:15:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2020-10-02 13:13:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/paramcontext"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2020-11-18 20:10:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-06-11 08:45:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-03-11 18:11:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-12-01 08:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-06-09 13:12:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-06-25 16:21:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2020-03-11 18:11:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2018-02-09 16:08:50 +00:00
|
|
|
"github.com/urfave/cli"
|
2020-08-12 13:31:06 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2018-02-09 16:08:50 +00:00
|
|
|
)
|
|
|
|
|
2018-08-25 14:40:15 +00:00
|
|
|
var (
|
2019-10-18 15:34:58 +00:00
|
|
|
errNoInput = errors.New("no input file was found, specify an input file with the '--in or -i' flag")
|
2019-11-20 13:07:43 +00:00
|
|
|
errNoConfFile = errors.New("no config file was found, specify a config file with the '--config' or '-c' flag")
|
2020-06-30 11:24:07 +00:00
|
|
|
errNoManifestFile = errors.New("no manifest file was found, specify a manifest file with the '--manifest' flag")
|
2020-04-13 08:19:37 +00:00
|
|
|
errNoMethod = errors.New("no method specified for function invocation command")
|
2020-03-11 18:11:51 +00:00
|
|
|
errNoWallet = errors.New("no wallet parameter found, specify it with the '--wallet or -w' flag")
|
2019-11-27 09:52:15 +00:00
|
|
|
errNoScriptHash = errors.New("no smart contract hash was provided, specify one as the first argument")
|
2019-10-18 15:34:58 +00:00
|
|
|
errNoSmartContractName = errors.New("no name was provided, specify the '--name or -n' flag")
|
2018-08-25 14:40:15 +00:00
|
|
|
errFileExist = errors.New("A file with given smart-contract name already exists")
|
2019-11-29 15:16:46 +00:00
|
|
|
|
2020-03-11 18:11:51 +00:00
|
|
|
walletFlag = cli.StringFlag{
|
|
|
|
Name: "wallet, w",
|
|
|
|
Usage: "wallet to use to get the key for transaction signing",
|
|
|
|
}
|
|
|
|
addressFlag = flags.AddressFlag{
|
|
|
|
Name: "address, a",
|
|
|
|
Usage: "address to use as transaction signee (and gas source)",
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
2020-03-12 16:41:54 +00:00
|
|
|
gasFlag = flags.Fixed8Flag{
|
2019-11-29 15:59:55 +00:00
|
|
|
Name: "gas, g",
|
2020-03-11 17:32:06 +00:00
|
|
|
Usage: "gas to add to the transaction",
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
2020-10-02 13:13:17 +00:00
|
|
|
outFlag = cli.StringFlag{
|
|
|
|
Name: "out",
|
|
|
|
Usage: "file to put JSON transaction to",
|
|
|
|
}
|
2020-12-04 07:59:46 +00:00
|
|
|
forceFlag = cli.BoolFlag{
|
2020-10-12 09:45:38 +00:00
|
|
|
Name: "force",
|
|
|
|
Usage: "force-push the transaction in case of bad VM state after test script invocation",
|
|
|
|
}
|
2018-08-25 14:40:15 +00:00
|
|
|
)
|
|
|
|
|
2019-11-29 15:11:49 +00:00
|
|
|
const (
|
2018-08-25 14:40:15 +00:00
|
|
|
// smartContractTmpl is written to a file when used with `init` command.
|
|
|
|
// %s is parsed to be the smartContractName
|
|
|
|
smartContractTmpl = `package %s
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
2018-08-25 14:40:15 +00:00
|
|
|
|
2020-08-10 13:01:56 +00:00
|
|
|
var notificationName string
|
|
|
|
|
|
|
|
// init initializes notificationName before calling any other smart-contract method
|
|
|
|
func init() {
|
|
|
|
notificationName = "Hello world!"
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuntimeNotify sends runtime notification with "Hello world!" name
|
|
|
|
func RuntimeNotify(args []interface{}) {
|
|
|
|
runtime.Notify(notificationName, args)
|
2018-08-25 14:40:15 +00:00
|
|
|
}`
|
2020-06-11 08:45:17 +00:00
|
|
|
// cosignersSeparator is a special value which is used to distinguish
|
|
|
|
// parameters and cosigners for invoke* commands
|
2020-08-26 09:08:17 +00:00
|
|
|
cosignersSeparator = "--"
|
|
|
|
arrayStartSeparator = "["
|
|
|
|
arrayEndSeparator = "]"
|
2018-02-24 09:10:45 +00:00
|
|
|
)
|
|
|
|
|
2019-10-19 20:58:45 +00:00
|
|
|
// NewCommands returns 'contract' command.
|
|
|
|
func NewCommands() []cli.Command {
|
2020-06-17 21:15:13 +00:00
|
|
|
testInvokeScriptFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
2020-06-25 16:21:49 +00:00
|
|
|
Usage: "Input location of the .nef file that needs to be invoked",
|
2020-06-17 21:15:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
testInvokeScriptFlags = append(testInvokeScriptFlags, options.RPC...)
|
|
|
|
deployFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
2020-06-25 16:21:49 +00:00
|
|
|
Usage: "Input file for the smart contract (*.nef)",
|
2020-06-17 21:15:13 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
2020-06-30 11:24:07 +00:00
|
|
|
Name: "manifest",
|
|
|
|
Usage: "Manifest input file (*.manifest.json)",
|
2020-06-17 21:15:13 +00:00
|
|
|
},
|
|
|
|
walletFlag,
|
|
|
|
addressFlag,
|
|
|
|
gasFlag,
|
|
|
|
}
|
|
|
|
deployFlags = append(deployFlags, options.RPC...)
|
|
|
|
invokeFunctionFlags := []cli.Flag{
|
|
|
|
walletFlag,
|
|
|
|
addressFlag,
|
|
|
|
gasFlag,
|
2020-10-02 13:13:17 +00:00
|
|
|
outFlag,
|
2020-10-12 09:45:38 +00:00
|
|
|
forceFlag,
|
2020-06-17 21:15:13 +00:00
|
|
|
}
|
|
|
|
invokeFunctionFlags = append(invokeFunctionFlags, options.RPC...)
|
2019-10-19 20:58:45 +00:00
|
|
|
return []cli.Command{{
|
2018-02-09 16:08:50 +00:00
|
|
|
Name: "contract",
|
|
|
|
Usage: "compile - debug - deploy smart contracts",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "compile",
|
2020-06-25 16:21:49 +00:00
|
|
|
Usage: "compile a smart contract to a .nef file",
|
2018-02-09 16:08:50 +00:00
|
|
|
Action: contractCompile,
|
2018-02-15 15:35:49 +00:00
|
|
|
Flags: []cli.Flag{
|
2018-02-24 09:10:45 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
|
|
|
Usage: "Input file for the smart contract to be compiled",
|
|
|
|
},
|
2018-02-15 15:35:49 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "out, o",
|
|
|
|
Usage: "Output of the compiled contract",
|
|
|
|
},
|
2018-03-25 16:21:00 +00:00
|
|
|
cli.BoolFlag{
|
2020-04-06 12:32:06 +00:00
|
|
|
Name: "verbose, v",
|
|
|
|
Usage: "Print out additional information after a compiling",
|
2018-03-25 16:21:00 +00:00
|
|
|
},
|
2020-04-02 12:38:53 +00:00
|
|
|
cli.StringFlag{
|
2020-04-08 10:11:44 +00:00
|
|
|
Name: "debug, d",
|
2020-04-02 12:38:53 +00:00
|
|
|
Usage: "Emit debug info in a separate file",
|
|
|
|
},
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
cli.StringFlag{
|
2020-06-25 13:10:08 +00:00
|
|
|
Name: "manifest, m",
|
|
|
|
Usage: "Emit contract manifest (*.manifest.json) file into separate file using configuration input file (*.yml)",
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Usage: "Configuration input file (*.yml)",
|
|
|
|
},
|
2020-11-24 10:38:24 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-standards",
|
|
|
|
Usage: "do not check compliance with supported standards",
|
|
|
|
},
|
2020-11-24 13:36:35 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-events",
|
|
|
|
Usage: "do not check emitted events with the manifest",
|
|
|
|
},
|
2018-02-15 15:35:49 +00:00
|
|
|
},
|
2018-02-09 16:08:50 +00:00
|
|
|
},
|
2019-11-20 13:07:43 +00:00
|
|
|
{
|
2020-03-11 17:32:06 +00:00
|
|
|
Name: "deploy",
|
2020-06-25 16:21:49 +00:00
|
|
|
Usage: "deploy a smart contract (.nef with description)",
|
2020-03-11 17:32:06 +00:00
|
|
|
Description: `Deploys given contract into the chain. The gas parameter is for additional
|
|
|
|
gas to be added as a network fee to prioritize the transaction. It may also
|
|
|
|
be required to add that to satisfy chain's policy regarding transaction size
|
|
|
|
and the minimum size fee (so if transaction send fails, try adding 0.001 GAS
|
|
|
|
to it).
|
|
|
|
`,
|
2019-11-20 13:07:43 +00:00
|
|
|
Action: contractDeploy,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: deployFlags,
|
2019-11-29 15:59:55 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "invokefunction",
|
|
|
|
Usage: "invoke deployed contract on the blockchain",
|
2020-10-02 13:13:17 +00:00
|
|
|
UsageText: "neo-go contract invokefunction -r endpoint -w wallet [-a address] [-g gas] [--out file] scripthash [method] [arguments...] [--] [signers...]",
|
2020-06-11 08:45:17 +00:00
|
|
|
Description: `Executes given (as a script hash) deployed script with the given method,
|
2020-07-29 16:57:38 +00:00
|
|
|
arguments and signers. Sender is included in the list of signers by default
|
2020-10-01 12:26:51 +00:00
|
|
|
with None witness scope. If you'd like to change default sender's scope,
|
2020-07-29 16:57:38 +00:00
|
|
|
specify it via signers parameter. See testinvokefunction documentation for
|
|
|
|
the details about parameters. It differs from testinvokefunction in that this
|
|
|
|
command sends an invocation transaction to the network.
|
2019-11-29 15:59:55 +00:00
|
|
|
`,
|
|
|
|
Action: invokeFunction,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: invokeFunctionFlags,
|
2019-11-20 13:07:43 +00:00
|
|
|
},
|
2019-11-27 09:52:15 +00:00
|
|
|
{
|
|
|
|
Name: "testinvokefunction",
|
|
|
|
Usage: "invoke deployed contract on the blockchain (test mode)",
|
2020-07-29 16:57:38 +00:00
|
|
|
UsageText: "neo-go contract testinvokefunction -r endpoint scripthash [method] [arguments...] [--] [signers...]",
|
2020-06-11 08:45:17 +00:00
|
|
|
Description: `Executes given (as a script hash) deployed script with the given method,
|
2020-07-29 16:57:38 +00:00
|
|
|
arguments and signers (sender is not included by default). If no method is given
|
|
|
|
"" is passed to the script, if no arguments are given, an empty array is
|
|
|
|
passed, if no signers are given no array is passed. If signers are specified,
|
|
|
|
the first one of them is treated as a sender. All of the given arguments are
|
|
|
|
encapsulated into array before invoking the script. The script thus should
|
|
|
|
follow the regular convention of smart contract arguments (method string and
|
|
|
|
an array of other arguments).
|
2019-11-27 09:52:15 +00:00
|
|
|
|
|
|
|
Arguments always do have regular Neo smart contract parameter types, either
|
|
|
|
specified explicitly or being inferred from the value. To specify the type
|
|
|
|
manually use "type:value" syntax where the type is one of the following:
|
|
|
|
'signature', 'bool', 'int', 'hash160', 'hash256', 'bytes', 'key' or 'string'.
|
2020-08-26 09:08:17 +00:00
|
|
|
Array types are also supported: use special space-separated '[' and ']'
|
|
|
|
symbols around array values to denote array bounds. Nested arrays are also
|
|
|
|
supported.
|
2019-11-27 09:52:15 +00:00
|
|
|
|
|
|
|
Given values are type-checked against given types with the following
|
|
|
|
restrictions applied:
|
|
|
|
* 'signature' type values should be hex-encoded and have a (decoded)
|
|
|
|
length of 64 bytes.
|
|
|
|
* 'bool' type values are 'true' and 'false'.
|
|
|
|
* 'int' values are decimal integers that can be successfully converted
|
|
|
|
from the string.
|
|
|
|
* 'hash160' values are Neo addresses and hex-encoded 20-bytes long (after
|
|
|
|
decoding) strings.
|
|
|
|
* 'hash256' type values should be hex-encoded and have a (decoded)
|
|
|
|
length of 32 bytes.
|
|
|
|
* 'bytes' type values are any hex-encoded things.
|
|
|
|
* 'key' type values are hex-encoded marshalled public keys.
|
|
|
|
* 'string' type values are any valid UTF-8 strings. In the value's part of
|
|
|
|
the string the colon looses it's special meaning as a separator between
|
|
|
|
type and value and is taken literally.
|
|
|
|
|
|
|
|
If no type is explicitly specified, it is inferred from the value using the
|
|
|
|
following logic:
|
|
|
|
- anything that can be interpreted as a decimal integer gets
|
|
|
|
an 'int' type
|
|
|
|
- 'true' and 'false' strings get 'bool' type
|
|
|
|
- valid Neo addresses and 20 bytes long hex-encoded strings get 'hash160'
|
|
|
|
type
|
|
|
|
- valid hex-encoded public keys get 'key' type
|
|
|
|
- 32 bytes long hex-encoded values get 'hash256' type
|
|
|
|
- 64 bytes long hex-encoded values get 'signature' type
|
|
|
|
- any other valid hex-encoded values get 'bytes' type
|
|
|
|
- anything else is a 'string'
|
|
|
|
|
|
|
|
Backslash character is used as an escape character and allows to use colon in
|
|
|
|
an implicitly typed string. For any other characters it has no special
|
|
|
|
meaning, to get a literal backslash in the string use the '\\' sequence.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
* 'int:42' is an integer with a value of 42
|
|
|
|
* '42' is an integer with a value of 42
|
|
|
|
* 'bad' is a string with a value of 'bad'
|
|
|
|
* 'dead' is a byte array with a value of 'dead'
|
|
|
|
* 'string:dead' is a string with a value of 'dead'
|
|
|
|
* 'AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y' is a hash160 with a value
|
|
|
|
of '23ba2703c53263e8d6e522dc32203339dcd8eee9'
|
|
|
|
* '\4\2' is an integer with a value of 42
|
|
|
|
* '\\4\2' is a string with a value of '\42'
|
|
|
|
* 'string:string' is a string with a value of 'string'
|
|
|
|
* 'string\:string' is a string with a value of 'string:string'
|
|
|
|
* '03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c' is a
|
|
|
|
key with a value of '03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c'
|
2020-08-26 09:08:17 +00:00
|
|
|
* '[ a b c ]' is an array with strings values 'a', 'b' and 'c'
|
|
|
|
* '[ a b [ c d ] e ]' is an array with 4 values: string 'a', string 'b',
|
|
|
|
array of two strings 'c' and 'd', string 'e'
|
|
|
|
* '[ ]' is an empty array
|
2020-06-11 08:45:17 +00:00
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
Signers represent a set of Uint160 hashes with witness scopes and are used
|
|
|
|
to verify hashes in System.Runtime.CheckWitness syscall. First signer is treated
|
|
|
|
as a sender. To specify signers use signer[:scope] syntax where
|
|
|
|
* 'signer' is hex-encoded 160 bit (20 byte) LE value of signer's address,
|
2020-06-11 08:45:17 +00:00
|
|
|
which could have '0x' prefix.
|
|
|
|
* 'scope' is a comma-separated set of cosigner's scopes, which could be:
|
2020-10-01 12:26:51 +00:00
|
|
|
- 'None' - default witness scope which may be used for the sender
|
|
|
|
to only pay fee for the transaction.
|
2020-06-11 08:45:17 +00:00
|
|
|
- 'Global' - allows this witness in all contexts. This cannot be combined
|
|
|
|
with other flags.
|
|
|
|
- 'CalledByEntry' - means that this condition must hold: EntryScriptHash
|
|
|
|
== CallingScriptHash. The witness/permission/signature
|
|
|
|
given on first invocation will automatically expire if
|
|
|
|
entering deeper internal invokes. This can be default
|
|
|
|
safe choice for native NEO/GAS.
|
|
|
|
- 'CustomContracts' - define valid custom contract hashes for witness check.
|
|
|
|
- 'CustomGroups' - define custom pubkey for group members.
|
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
If no scopes were specified, 'Global' used as default. If no signers were
|
|
|
|
specified, no array is passed. Note that scopes are properly handled by
|
2020-06-11 08:45:17 +00:00
|
|
|
neo-go RPC server only. C# implementation does not support scopes capability.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
* '0000000009070e030d0f0e020d0c06050e030c02'
|
|
|
|
* '0x0000000009070e030d0f0e020d0c06050e030c02'
|
|
|
|
* '0x0000000009070e030d0f0e020d0c06050e030c02:Global'
|
|
|
|
* '0000000009070e030d0f0e020d0c06050e030c02:CalledByEntry,CustomGroups'
|
2019-11-27 09:52:15 +00:00
|
|
|
`,
|
|
|
|
Action: testInvokeFunction,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: options.RPC,
|
2019-11-27 09:52:15 +00:00
|
|
|
},
|
2018-03-05 08:53:09 +00:00
|
|
|
{
|
2020-06-11 08:45:17 +00:00
|
|
|
Name: "testinvokescript",
|
2020-06-25 16:21:49 +00:00
|
|
|
Usage: "Invoke compiled AVM code in NEF format on the blockchain (test mode, not creating a transaction for it)",
|
2020-07-29 16:57:38 +00:00
|
|
|
UsageText: "neo-go contract testinvokescript -r endpoint -i input.nef [signers...]",
|
2020-06-25 16:21:49 +00:00
|
|
|
Description: `Executes given compiled AVM instructions in NEF format with the given set of
|
2020-07-29 16:57:38 +00:00
|
|
|
signers not included sender by default. See testinvokefunction documentation
|
|
|
|
for the details about parameters.
|
2020-06-11 08:45:17 +00:00
|
|
|
`,
|
2019-11-26 14:00:43 +00:00
|
|
|
Action: testInvokeScript,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: testInvokeScriptFlags,
|
2018-03-05 08:53:09 +00:00
|
|
|
},
|
2018-08-25 14:40:15 +00:00
|
|
|
{
|
|
|
|
Name: "init",
|
|
|
|
Usage: "initialize a new smart-contract in a directory with boiler plate code",
|
|
|
|
Action: initSmartContract,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name, n",
|
|
|
|
Usage: "name of the smart-contract to be initialized",
|
|
|
|
},
|
2018-10-16 06:33:29 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "skip-details, skip",
|
|
|
|
Usage: "skip filling in the projects and contract details",
|
|
|
|
},
|
2018-08-25 14:40:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-10-23 08:23:03 +00:00
|
|
|
{
|
|
|
|
Name: "inspect",
|
|
|
|
Usage: "creates a user readable dump of the program instructions",
|
|
|
|
Action: inspect,
|
|
|
|
Flags: []cli.Flag{
|
2019-10-29 09:56:44 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "compile, c",
|
|
|
|
Usage: "compile input file (it should be go code then)",
|
|
|
|
},
|
2018-10-23 08:23:03 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
2020-06-25 16:21:49 +00:00
|
|
|
Usage: "input file of the program (either .go or .nef)",
|
2018-10-23 08:23:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-02-09 16:08:50 +00:00
|
|
|
},
|
2019-10-19 20:58:45 +00:00
|
|
|
}}
|
2018-02-09 16:08:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 14:40:15 +00:00
|
|
|
// initSmartContract initializes a given directory with some boiler plate code.
|
|
|
|
func initSmartContract(ctx *cli.Context) error {
|
2018-10-16 06:33:29 +00:00
|
|
|
contractName := ctx.String("name")
|
|
|
|
if contractName == "" {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(errNoSmartContractName, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file already exists, if yes, exit
|
2018-10-16 06:33:29 +00:00
|
|
|
if _, err := os.Stat(contractName); err == nil {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(errFileExist, 1)
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:33:29 +00:00
|
|
|
basePath := contractName
|
2020-12-03 11:57:45 +00:00
|
|
|
contractName = path.Base(contractName)
|
2018-08-25 14:40:15 +00:00
|
|
|
fileName := "main.go"
|
|
|
|
|
|
|
|
// create base directory
|
2018-10-16 06:33:29 +00:00
|
|
|
if err := os.Mkdir(basePath, os.ModePerm); err != nil {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2020-08-04 09:55:36 +00:00
|
|
|
m := ProjectConfig{
|
2020-11-20 08:02:58 +00:00
|
|
|
Name: contractName,
|
2020-08-04 09:55:36 +00:00
|
|
|
SupportedStandards: []string{},
|
2020-12-10 14:55:52 +00:00
|
|
|
SafeMethods: []string{},
|
2020-08-10 13:01:56 +00:00
|
|
|
Events: []manifest.Event{
|
|
|
|
{
|
|
|
|
Name: "Hello world!",
|
|
|
|
Parameters: []manifest.Parameter{
|
|
|
|
{
|
|
|
|
Name: "args",
|
|
|
|
Type: smartcontract.ArrayType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-04 09:55:36 +00:00
|
|
|
}
|
2020-06-09 13:12:58 +00:00
|
|
|
b, err := yaml.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(basePath, "neo-go.yml"), b, 0644); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
2018-10-16 06:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data := []byte(fmt.Sprintf(smartContractTmpl, contractName))
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(basePath, fileName), data, 0644); err != nil {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2018-10-16 06:33:29 +00:00
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "Successfully initialized smart contract [%s]\n", contractName)
|
2018-10-16 06:33:29 +00:00
|
|
|
|
2018-08-25 14:40:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:08:50 +00:00
|
|
|
func contractCompile(ctx *cli.Context) error {
|
2018-02-24 09:10:45 +00:00
|
|
|
src := ctx.String("in")
|
|
|
|
if len(src) == 0 {
|
2018-03-05 08:53:09 +00:00
|
|
|
return cli.NewExitError(errNoInput, 1)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
2020-06-25 13:10:08 +00:00
|
|
|
manifestFile := ctx.String("manifest")
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
confFile := ctx.String("config")
|
2020-06-30 11:21:58 +00:00
|
|
|
debugFile := ctx.String("debug")
|
|
|
|
if len(confFile) == 0 && (len(manifestFile) != 0 || len(debugFile) != 0) {
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
return cli.NewExitError(errNoConfFile, 1)
|
|
|
|
}
|
2018-02-15 15:35:49 +00:00
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
o := &compiler.Options{
|
|
|
|
Outfile: ctx.String("out"),
|
2020-04-02 12:38:53 +00:00
|
|
|
|
2020-06-30 11:21:58 +00:00
|
|
|
DebugInfo: debugFile,
|
2020-06-25 13:10:08 +00:00
|
|
|
ManifestFile: manifestFile,
|
2020-11-24 10:38:24 +00:00
|
|
|
|
|
|
|
NoStandardCheck: ctx.Bool("no-standards"),
|
2020-11-24 13:36:35 +00:00
|
|
|
NoEventsCheck: ctx.Bool("no-events"),
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(confFile) != 0 {
|
|
|
|
conf, err := parseContractConfig(confFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-20 08:02:58 +00:00
|
|
|
o.Name = conf.Name
|
2020-08-11 08:21:54 +00:00
|
|
|
o.ContractEvents = conf.Events
|
2020-08-04 09:55:36 +00:00
|
|
|
o.ContractSupportedStandards = conf.SupportedStandards
|
2020-12-10 14:55:52 +00:00
|
|
|
o.SafeMethods = conf.SafeMethods
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 14:16:52 +00:00
|
|
|
result, err := compiler.CompileAndSave(src, o)
|
|
|
|
if err != nil {
|
2018-02-24 09:10:45 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-04-06 12:33:44 +00:00
|
|
|
if ctx.Bool("verbose") {
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, hex.EncodeToString(result))
|
2020-04-06 12:33:44 +00:00
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
|
2018-02-24 09:10:45 +00:00
|
|
|
return nil
|
2018-02-09 16:08:50 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:52:15 +00:00
|
|
|
func testInvokeFunction(ctx *cli.Context) error {
|
2020-06-10 08:31:01 +00:00
|
|
|
return invokeInternal(ctx, false)
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func invokeFunction(ctx *cli.Context) error {
|
2020-06-10 08:31:01 +00:00
|
|
|
return invokeInternal(ctx, true)
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 08:31:01 +00:00
|
|
|
func invokeInternal(ctx *cli.Context, signAndPush bool) error {
|
2019-11-29 15:59:55 +00:00
|
|
|
var (
|
2020-08-26 09:08:17 +00:00
|
|
|
err error
|
2020-12-01 08:40:58 +00:00
|
|
|
gas fixedn.Fixed8
|
2020-08-26 09:08:17 +00:00
|
|
|
operation string
|
|
|
|
params = make([]smartcontract.Parameter, 0)
|
|
|
|
paramsStart = 1
|
|
|
|
cosigners []transaction.Signer
|
|
|
|
cosignersOffset = 0
|
|
|
|
resp *result.Invoke
|
|
|
|
acc *wallet.Account
|
2019-11-29 15:59:55 +00:00
|
|
|
)
|
2019-11-28 16:40:13 +00:00
|
|
|
|
2019-11-27 09:52:15 +00:00
|
|
|
args := ctx.Args()
|
|
|
|
if !args.Present() {
|
|
|
|
return cli.NewExitError(errNoScriptHash, 1)
|
|
|
|
}
|
2020-07-02 13:38:33 +00:00
|
|
|
script, err := util.Uint160DecodeStringLE(args[0])
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("incorrect script hash: %w", err), 1)
|
2020-07-02 13:38:33 +00:00
|
|
|
}
|
2020-06-10 08:31:01 +00:00
|
|
|
if len(args) <= 1 {
|
|
|
|
return cli.NewExitError(errNoMethod, 1)
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
2020-06-10 08:31:01 +00:00
|
|
|
operation = args[1]
|
|
|
|
paramsStart++
|
|
|
|
|
2019-11-28 16:40:13 +00:00
|
|
|
if len(args) > paramsStart {
|
2020-08-26 09:08:17 +00:00
|
|
|
cosignersOffset, params, err = parseParams(args[paramsStart:], true)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:08:17 +00:00
|
|
|
cosignersStart := paramsStart + cosignersOffset
|
|
|
|
if len(args) > cosignersStart {
|
2020-06-11 08:45:17 +00:00
|
|
|
for i, c := range args[cosignersStart:] {
|
|
|
|
cosigner, err := parseCosigner(c)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to parse cosigner #%d: %w", i+1, err), 1)
|
2020-06-11 08:45:17 +00:00
|
|
|
}
|
|
|
|
cosigners = append(cosigners, cosigner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 15:59:55 +00:00
|
|
|
if signAndPush {
|
2020-03-12 16:41:54 +00:00
|
|
|
gas = flags.Fixed8FromContext(ctx, "gas")
|
2020-03-11 18:11:51 +00:00
|
|
|
acc, err = getAccFromContext(ctx)
|
2019-11-29 15:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2019-11-27 09:52:15 +00:00
|
|
|
if err != nil {
|
2020-06-17 21:15:13 +00:00
|
|
|
return err
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 08:45:17 +00:00
|
|
|
resp, err = c.InvokeFunction(script, operation, params, cosigners)
|
2019-11-27 09:52:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-10-12 09:45:38 +00:00
|
|
|
if signAndPush && resp.State != "HALT" {
|
|
|
|
errText := fmt.Sprintf("Warning: %s VM state returned from the RPC node: %s\n", resp.State, resp.FaultException)
|
2020-12-04 07:59:46 +00:00
|
|
|
if !ctx.Bool("force") {
|
2020-10-12 09:45:38 +00:00
|
|
|
return cli.NewExitError(errText+". Use --force flag to send the transaction anyway.", 1)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(ctx.App.Writer, errText+". Sending transaction...")
|
|
|
|
}
|
2020-10-02 13:13:17 +00:00
|
|
|
if out := ctx.String("out"); out != "" {
|
2020-10-14 13:46:06 +00:00
|
|
|
tx, err := c.CreateTxFromScript(resp.Script, acc, resp.GasConsumed, int64(gas), cosigners...)
|
2020-10-02 13:13:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to create tx: %w", err), 1)
|
|
|
|
}
|
|
|
|
if err := paramcontext.InitAndSave(tx, acc, out); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(ctx.App.Writer, tx.Hash().StringLE())
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-29 15:59:55 +00:00
|
|
|
if signAndPush {
|
2020-02-20 18:08:22 +00:00
|
|
|
if len(resp.Script) == 0 {
|
2019-11-29 15:59:55 +00:00
|
|
|
return cli.NewExitError(errors.New("no script returned from the RPC node"), 1)
|
|
|
|
}
|
2020-10-14 13:46:06 +00:00
|
|
|
txHash, err := c.SignAndPushInvocationTx(resp.Script, acc, resp.GasConsumed, gas, cosigners)
|
2019-11-29 15:59:55 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %w", err), 1)
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "Sent invocation transaction %s\n", txHash.StringLE())
|
2019-11-29 15:59:55 +00:00
|
|
|
} else {
|
2020-02-20 18:08:22 +00:00
|
|
|
b, err := json.MarshalIndent(resp, "", " ")
|
2019-11-29 15:59:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2019-11-27 09:52:15 +00:00
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, string(b))
|
2019-11-27 09:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:08:17 +00:00
|
|
|
// parseParams extracts array of smartcontract.Parameter from the given args and
|
|
|
|
// returns the number of handled words, the array itself and an error.
|
|
|
|
// `calledFromMain` denotes whether the method was called from the outside or
|
|
|
|
// recursively and used to check if cosignersSeparator and closing bracket are
|
|
|
|
// allowed to be in `args` sequence.
|
|
|
|
func parseParams(args []string, calledFromMain bool) (int, []smartcontract.Parameter, error) {
|
|
|
|
res := []smartcontract.Parameter{}
|
|
|
|
for k := 0; k < len(args); {
|
|
|
|
s := args[k]
|
|
|
|
switch s {
|
|
|
|
case cosignersSeparator:
|
|
|
|
if calledFromMain {
|
|
|
|
return k + 1, res, nil // `1` to convert index to numWordsRead
|
|
|
|
}
|
|
|
|
return 0, []smartcontract.Parameter{}, errors.New("invalid array syntax: missing closing bracket")
|
|
|
|
case arrayStartSeparator:
|
|
|
|
numWordsRead, array, err := parseParams(args[k+1:], false)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to parse array: %w", err)
|
|
|
|
}
|
|
|
|
res = append(res, smartcontract.Parameter{
|
|
|
|
Type: smartcontract.ArrayType,
|
|
|
|
Value: array,
|
|
|
|
})
|
|
|
|
k += 1 + numWordsRead // `1` for opening bracket
|
|
|
|
case arrayEndSeparator:
|
|
|
|
if calledFromMain {
|
|
|
|
return 0, nil, errors.New("invalid array syntax: missing opening bracket")
|
|
|
|
}
|
|
|
|
return k + 1, res, nil // `1`to convert index to numWordsRead
|
|
|
|
default:
|
|
|
|
param, err := smartcontract.NewParameterFromString(s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to parse argument #%d: %w", k+1, err)
|
|
|
|
}
|
|
|
|
res = append(res, *param)
|
|
|
|
k++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if calledFromMain {
|
|
|
|
return len(args), res, nil
|
|
|
|
}
|
|
|
|
return 0, []smartcontract.Parameter{}, errors.New("invalid array syntax: missing closing bracket")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:00:43 +00:00
|
|
|
func testInvokeScript(ctx *cli.Context) error {
|
2018-03-05 08:53:09 +00:00
|
|
|
src := ctx.String("in")
|
|
|
|
if len(src) == 0 {
|
|
|
|
return cli.NewExitError(errNoInput, 1)
|
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
|
2018-03-05 08:53:09 +00:00
|
|
|
b, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-06-25 16:21:49 +00:00
|
|
|
nefFile, err := nef.FileFromBytes(b)
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to restore .nef file: %w", err), 1)
|
2020-06-25 16:21:49 +00:00
|
|
|
}
|
2018-03-05 08:53:09 +00:00
|
|
|
|
2020-06-11 08:45:17 +00:00
|
|
|
args := ctx.Args()
|
2020-07-29 16:57:38 +00:00
|
|
|
var signers []transaction.Signer
|
2020-06-11 08:45:17 +00:00
|
|
|
if args.Present() {
|
|
|
|
for i, c := range args[:] {
|
|
|
|
cosigner, err := parseCosigner(c)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to parse signer #%d: %w", i+1, err), 1)
|
2020-06-11 08:45:17 +00:00
|
|
|
}
|
2020-07-29 16:57:38 +00:00
|
|
|
signers = append(signers, cosigner)
|
2020-06-11 08:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
2020-06-17 21:15:13 +00:00
|
|
|
return err
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
resp, err := c.InvokeScript(nefFile.Script, signers)
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2020-02-20 18:08:22 +00:00
|
|
|
b, err = json.MarshalIndent(resp, "", " ")
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, string(b))
|
2018-03-05 08:53:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-20 09:29:37 +00:00
|
|
|
// ProjectConfig contains project metadata.
|
|
|
|
type ProjectConfig struct {
|
2020-11-20 08:02:58 +00:00
|
|
|
Name string
|
2020-12-10 14:55:52 +00:00
|
|
|
SafeMethods []string
|
2020-08-04 09:55:36 +00:00
|
|
|
SupportedStandards []string
|
|
|
|
Events []manifest.Event
|
2018-10-16 06:33:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 08:23:03 +00:00
|
|
|
func inspect(ctx *cli.Context) error {
|
2019-10-29 09:56:44 +00:00
|
|
|
in := ctx.String("in")
|
|
|
|
compile := ctx.Bool("compile")
|
|
|
|
if len(in) == 0 {
|
2018-10-23 08:23:03 +00:00
|
|
|
return cli.NewExitError(errNoInput, 1)
|
|
|
|
}
|
2020-08-12 09:22:16 +00:00
|
|
|
var (
|
|
|
|
b []byte
|
|
|
|
err error
|
|
|
|
)
|
2019-10-29 09:56:44 +00:00
|
|
|
if compile {
|
2020-08-10 15:23:45 +00:00
|
|
|
b, err = compiler.Compile(in, nil)
|
2019-10-29 09:56:44 +00:00
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to compile: %w", err), 1)
|
2019-10-29 09:56:44 +00:00
|
|
|
}
|
2020-06-25 16:21:49 +00:00
|
|
|
} else {
|
2020-08-12 09:22:16 +00:00
|
|
|
f, err := ioutil.ReadFile(in)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to read .nef file: %w", err), 1)
|
|
|
|
}
|
|
|
|
nefFile, err := nef.FileFromBytes(f)
|
2020-06-25 16:21:49 +00:00
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to restore .nef file: %w", err), 1)
|
2020-06-25 16:21:49 +00:00
|
|
|
}
|
|
|
|
b = nefFile.Script
|
2019-10-29 09:56:44 +00:00
|
|
|
}
|
|
|
|
v := vm.New()
|
|
|
|
v.LoadScript(b)
|
2020-12-01 13:52:23 +00:00
|
|
|
v.PrintOps(ctx.App.Writer)
|
2019-10-29 09:56:44 +00:00
|
|
|
|
2018-10-23 08:23:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-20 13:07:43 +00:00
|
|
|
|
2020-03-11 18:11:51 +00:00
|
|
|
func getAccFromContext(ctx *cli.Context) (*wallet.Account, error) {
|
|
|
|
var addr util.Uint160
|
|
|
|
|
|
|
|
wPath := ctx.String("wallet")
|
|
|
|
if len(wPath) == 0 {
|
|
|
|
return nil, cli.NewExitError(errNoWallet, 1)
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 18:11:51 +00:00
|
|
|
wall, err := wallet.NewWalletFromFile(wPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
addrFlag := ctx.Generic("address").(*flags.Address)
|
|
|
|
if addrFlag.IsSet {
|
|
|
|
addr = addrFlag.Uint160()
|
|
|
|
} else {
|
|
|
|
addr = wall.GetChangeAddress()
|
|
|
|
}
|
|
|
|
acc := wall.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
return nil, cli.NewExitError(fmt.Errorf("wallet contains no account for '%s'", address.Uint160ToString(addr)), 1)
|
|
|
|
}
|
|
|
|
|
2020-08-31 09:22:09 +00:00
|
|
|
rawPass, err := input.ReadPassword(ctx.App.Writer,
|
|
|
|
fmt.Sprintf("Enter account %s password > ", address.Uint160ToString(addr)))
|
2020-03-11 18:11:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
pass := strings.TrimRight(string(rawPass), "\n")
|
|
|
|
err = acc.Decrypt(pass)
|
2019-11-29 15:59:55 +00:00
|
|
|
if err != nil {
|
2020-03-11 18:11:51 +00:00
|
|
|
return nil, cli.NewExitError(err, 1)
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
2020-03-11 18:11:51 +00:00
|
|
|
return acc, nil
|
2019-11-29 15:59:55 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:07:43 +00:00
|
|
|
// contractDeploy deploys contract.
|
|
|
|
func contractDeploy(ctx *cli.Context) error {
|
|
|
|
in := ctx.String("in")
|
|
|
|
if len(in) == 0 {
|
|
|
|
return cli.NewExitError(errNoInput, 1)
|
|
|
|
}
|
2020-06-30 11:24:07 +00:00
|
|
|
manifestFile := ctx.String("manifest")
|
|
|
|
if len(manifestFile) == 0 {
|
|
|
|
return cli.NewExitError(errNoManifestFile, 1)
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
2020-03-12 16:41:54 +00:00
|
|
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
2019-11-20 13:07:43 +00:00
|
|
|
|
2020-03-11 18:11:51 +00:00
|
|
|
acc, err := getAccFromContext(ctx)
|
2019-11-20 13:07:43 +00:00
|
|
|
if err != nil {
|
2019-11-29 15:59:55 +00:00
|
|
|
return err
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
sender, err := address.StringToUint160(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-06-25 16:21:49 +00:00
|
|
|
f, err := ioutil.ReadFile(in)
|
2019-11-20 13:07:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
// Check the file.
|
2020-06-25 16:21:49 +00:00
|
|
|
nefFile, err := nef.FileFromBytes(f)
|
|
|
|
if err != nil {
|
2020-11-18 20:10:48 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to read .nef file: %w", err), 1)
|
2020-06-25 16:21:49 +00:00
|
|
|
}
|
2020-06-30 11:24:07 +00:00
|
|
|
|
|
|
|
manifestBytes, err := ioutil.ReadFile(manifestFile)
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to read manifest file: %w", err), 1)
|
2020-06-30 11:24:07 +00:00
|
|
|
}
|
|
|
|
m := &manifest.Manifest{}
|
|
|
|
err = json.Unmarshal(manifestBytes, m)
|
2019-11-20 13:07:43 +00:00
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to restore manifest file: %w", err), 1)
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2019-11-20 13:07:43 +00:00
|
|
|
if err != nil {
|
2020-06-17 21:15:13 +00:00
|
|
|
return err
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 18:25:04 +00:00
|
|
|
mgmtHash, err := c.GetNativeContractHash(nativenames.Management)
|
2019-11-20 13:07:43 +00:00
|
|
|
if err != nil {
|
2020-12-13 15:26:35 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to get management contract's hash: %w", err), 1)
|
2019-11-29 14:59:07 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.AppCallWithOperationAndArgs(buf.BinWriter, mgmtHash, "deploy", f, manifestBytes)
|
|
|
|
if buf.Err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to create deployment script: %w", buf.Err), 1)
|
|
|
|
}
|
|
|
|
txScript := buf.Bytes()
|
2020-07-29 16:57:38 +00:00
|
|
|
// It doesn't require any signers.
|
2020-07-02 13:38:33 +00:00
|
|
|
invRes, err := c.InvokeScript(txScript, nil)
|
2020-12-10 11:34:46 +00:00
|
|
|
if err == nil && invRes.FaultException != "" {
|
|
|
|
err = errors.New(invRes.FaultException)
|
|
|
|
}
|
2020-07-02 13:22:49 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to test-invoke deployment script: %w", err), 1)
|
2020-07-02 13:22:49 +00:00
|
|
|
}
|
2019-11-29 14:59:07 +00:00
|
|
|
|
2020-07-06 07:01:14 +00:00
|
|
|
txHash, err := c.SignAndPushInvocationTx(txScript, acc, invRes.GasConsumed, gas, nil)
|
2019-11-29 14:59:07 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("failed to push invocation tx: %w", err), 1)
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
2020-11-18 20:10:48 +00:00
|
|
|
hash := state.CreateContractHash(sender, nefFile.Script)
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "Contract: %s\n", hash.StringLE())
|
2020-09-02 09:20:42 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, txHash.StringLE())
|
2019-11-20 13:07:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
|
|
|
|
func parseContractConfig(confFile string) (ProjectConfig, error) {
|
|
|
|
conf := ProjectConfig{}
|
|
|
|
confBytes, err := ioutil.ReadFile(confFile)
|
|
|
|
if err != nil {
|
|
|
|
return conf, cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(confBytes, &conf)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return conf, cli.NewExitError(fmt.Errorf("bad config: %w", err), 1)
|
compiler: add ability to generate .abi.json file
A part of integration with NEO Blockchain Toolkit (see #902). To be
able to deploy smart-contract compiled with neo-go compiler via NEO
Express, we have to generate additional .abi.json file. This file
contains the following information:
- hash of the compiled contract
- smart-contract metadata (title, description, version, author,
email, has-storage, has-dynamic-invoke, is-payable)
- smart-contract entry point
- functions
- events
However, this .abi.json file is slightly different from the one,
described in manifest.go, so we have to add auxilaury stractures for
json marshalling. The .abi.json format used by NEO-Express is described
[here](https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.MSIL/FuncExport.cs#L66).
2020-04-28 16:39:01 +00:00
|
|
|
}
|
|
|
|
return conf, nil
|
|
|
|
}
|
2020-06-11 08:45:17 +00:00
|
|
|
|
2020-07-29 16:57:38 +00:00
|
|
|
func parseCosigner(c string) (transaction.Signer, error) {
|
2020-06-11 08:45:17 +00:00
|
|
|
var (
|
|
|
|
err error
|
2020-07-29 16:57:38 +00:00
|
|
|
res = transaction.Signer{
|
2020-07-29 16:41:15 +00:00
|
|
|
Scopes: transaction.Global,
|
|
|
|
}
|
2020-06-11 08:45:17 +00:00
|
|
|
)
|
2020-07-03 20:35:22 +00:00
|
|
|
data := strings.SplitN(c, ":", 2)
|
2020-06-11 08:45:17 +00:00
|
|
|
s := data[0]
|
|
|
|
if len(s) == 2*util.Uint160Size+2 && s[0:2] == "0x" {
|
|
|
|
s = s[2:]
|
|
|
|
}
|
|
|
|
res.Account, err = util.Uint160DecodeStringLE(s)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if len(data) > 1 {
|
|
|
|
res.Scopes, err = transaction.ScopesFromString(data[1])
|
|
|
|
if err != nil {
|
2020-07-29 16:57:38 +00:00
|
|
|
return transaction.Signer{}, err
|
2020-06-11 08:45:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|