Ejemplo n.º 1
0
        /// <summary>
        /// Deploy Contract, return signed transaction
        /// </summary>
        /// <param name="contractScript">contract script</param>
        /// <param name="manifest">contract manifest</param>
        /// <param name="key">sender KeyPair</param>
        /// <param name="networkFee">transaction NetworkFee, set to be 0 if you don't need higher priority</param>
        /// <returns></returns>
        public Transaction DeployContract(byte[] contractScript, ContractManifest manifest, KeyPair key, long networkFee = 0)
        {
            byte[] script;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(InteropService.Neo_Contract_Create, contractScript, manifest.ToString());
                script = sb.ToArray();
            }

            Transaction tx = new TransactionManager(rpcClient, Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash())
                             .MakeTransaction(script, null, null, networkFee)
                             .AddSignature(key)
                             .Sign()
                             .Tx;

            return(tx);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deploy Contract, return signed transaction
        /// </summary>
        /// <param name="contractScript">contract script</param>
        /// <param name="manifest">contract manifest</param>
        /// <param name="key">sender KeyPair</param>
        /// <returns></returns>
        public Transaction CreateDeployContractTx(byte[] contractScript, ContractManifest manifest, KeyPair key)
        {
            byte[] script;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(InteropService.Contract.Create, contractScript, manifest.ToString());
                script = sb.ToArray();
            }

            UInt160     sender = Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash();
            Transaction tx     = new TransactionManager(rpcClient, sender)
                                 .MakeTransaction(script, null, null)
                                 .AddSignature(key)
                                 .Sign()
                                 .Tx;

            return(tx);
        }
Ejemplo n.º 3
0
        public void TestDeserializeAndSerialize()
        {
            var expected = TestUtils.CreateDefaultManifest();

            expected.Extra = JObject.Parse(@"{""a"":123}");

            var clone = new ContractManifest();

            ((IInteroperable)clone).FromStackItem(expected.ToStackItem(null));

            Assert.AreEqual(expected.Extra.ToString(), @"{""a"":123}");
            Assert.AreEqual(expected.ToString(), clone.ToString());

            expected.Extra = null;
            clone          = new ContractManifest();
            ((IInteroperable)clone).FromStackItem(expected.ToStackItem(null));

            Assert.AreEqual(expected.Extra, clone.Extra);
            Assert.AreEqual(expected.ToString(), clone.ToString());
        }
Ejemplo n.º 4
0
 public StackItem ToStackItem(ReferenceCounter referenceCounter)
 {
     return(new Array(referenceCounter, new StackItem[] { Id, (int)UpdateCounter, Hash.ToArray(), Script, Manifest.ToString() }));
 }
Ejemplo n.º 5
0
        public void GetContract()
        {
            var contract = new ContractState()
            {
                Hash = new byte[] { 0x01, 0x02, 0x03 }.ToScriptHash(),
                Nef = new NefFile()
                {
                    Script   = new byte[] { 0x01, 0x02, 0x03 },
                    Compiler = "neon-test",
                    Tokens   = System.Array.Empty <MethodToken>()
                },
                Manifest = new ContractManifest()
                {
                    Name = "Name",
                    SupportedStandards = System.Array.Empty <string>(),
                    Groups             = System.Array.Empty <ContractGroup>(),
                    Trusts             = WildcardContainer <ContractPermissionDescriptor> .Create(),
                    Permissions        = System.Array.Empty <ContractPermission>(),
                    Abi = new ContractAbi()
                    {
                        Methods = System.Array.Empty <ContractMethodDescriptor>(),
                        Events  = System.Array.Empty <ContractEventDescriptor>(),
                    },
                }
            };

            _engine.Snapshot.ContractAdd(contract);

            // Not found

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(UInt160.Zero.ToArray()), new ByteString(new byte[20]));

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Null));

            // Found + Manifest

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("Manifest")));
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Struct));
            var ritem = new ContractManifest();

            ((IInteroperable)ritem).FromStackItem(item);
            Assert.AreEqual(contract.Manifest.ToString(), ritem.ToString());

            // Found + UpdateCounter

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("UpdateCounter")));
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0, item.GetInteger());

            // Found + Id

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("Id")));
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0, item.GetInteger());

            // Found + Hash

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("Hash")));
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(ByteString));
            CollectionAssert.AreEqual(contract.Hash.ToArray(), item.GetSpan().ToArray());

            // Found + Uknown property

            _engine.Reset();
            _ = _engine.ExecuteTestCaseStandard("getContract", new ByteString(contract.Hash.ToArray()), new ByteString(Utility.StrictUTF8.GetBytes("ASD")));
            Assert.AreEqual(VMState.FAULT, _engine.State);
        }
Ejemplo n.º 6
0
        public void TestDeployContract()
        {
            byte[] script;
            var    manifest = new ContractManifest()
            {
                Permissions = new[] { ContractPermission.DefaultPermission },
                Abi         = new ContractAbi()
                {
                    Hash    = new byte[1].ToScriptHash(),
                    Events  = new ContractEventDescriptor[0],
                    Methods = new ContractMethodDescriptor[0]
                },
                Groups      = new ContractGroup[0],
                SafeMethods = WildcardContainer <string> .Create(),
                Trusts      = WildcardContainer <UInt160> .Create(),
                Extra       = null,
            };

            manifest.Features = ContractFeatures.HasStorage | ContractFeatures.Payable;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(ApplicationEngine.System_Contract_Create, new byte[1], manifest.ToString());
                script = sb.ToArray();
            }

            UT_TransactionManager.MockInvokeScript(rpcClientMock, script, new ContractParameter());

            ContractClient contractClient = new ContractClient(rpcClientMock.Object);
            var            result         = contractClient.CreateDeployContractTx(new byte[1], manifest, keyPair1);

            Assert.IsNotNull(result);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Deploy Contract, return signed transaction
        /// </summary>
        /// <param name="contractScript">contract script</param>
        /// <param name="manifest">contract manifest</param>
        /// <param name="key">sender KeyPair</param>
        /// <returns></returns>
        public async Task <Transaction> CreateDeployContractTxAsync(byte[] nefFile, ContractManifest manifest, KeyPair key)
        {
            byte[] script;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", true, nefFile, manifest.ToString());
                script = sb.ToArray();
            }
            UInt160 sender = Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash();

            Signer[] signers = new[] { new Signer {
                                           Scopes = WitnessScope.CalledByEntry, Account = sender
                                       } };

            TransactionManagerFactory factory = new TransactionManagerFactory(rpcClient, magic);
            TransactionManager        manager = await factory.MakeTransactionAsync(script, signers).ConfigureAwait(false);

            return(await manager
                   .AddSignature(key)
                   .SignAsync().ConfigureAwait(false));
        }
Ejemplo n.º 8
0
        public void Test_CreateCallDestroy()
        {
            // Create

            var entryScript = _engine.ScriptEntry;
            var script      = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest    = ContractManifest.FromJson(JObject.Parse(script.finalManifest));
            var nef         = new NefFile()
            {
                Script = script.finalNEFScript, Compiler = "unit-test-1.0", Tokens = System.Array.Empty <MethodToken>()
            };

            nef.CheckSum = NefFile.ComputeChecksum(nef);

            var hash = Helper.GetContractHash((_engine.ScriptContainer as Transaction).Sender, nef.CheckSum, manifest.Name);

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", nef.ToArray(), manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(1, itemArray[0].GetInteger());                                                                   // Id
            Assert.AreEqual(0, itemArray[1].GetInteger());                                                                   // UpdateCounter
            Assert.AreEqual(hash.ToArray(), itemArray[2]);                                                                   // Hash
            Assert.AreEqual(nef.ToJson().AsString(), itemArray[3].GetSpan().AsSerializable <NefFile>().ToJson().AsString()); // Nef
            var ritem = new ContractManifest();

            ((IInteroperable)ritem).FromStackItem(itemArray[4]);
            Assert.AreEqual(manifest.ToString(), ritem.ToString()); // Manifest

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray(), "oldContract", (byte)CallFlags.All, new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy", entryScript.nefFile);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
Ejemplo n.º 9
0
        public void Test_CreateCallDestroy()
        {
            // Create

            TestEngine engine = new();

            engine.AddEntryScript("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(engine.Manifest);
            var nef      = new NefFile()
            {
                Script = engine.Nef.Script, Compiler = engine.Nef.Compiler, Source = engine.Nef.Source, Tokens = engine.Nef.Tokens
            };

            nef.CheckSum = NefFile.ComputeChecksum(nef);

            var hash = Helper.GetContractHash((_engine.ScriptContainer as Transaction).Sender, nef.CheckSum, manifest.Name);

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", nef.ToArray(), manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(1, itemArray[0].GetInteger());                                                                             // Id
            Assert.AreEqual(0, itemArray[1].GetInteger());                                                                             // UpdateCounter
            Assert.AreEqual(hash.ToArray(), itemArray[2]);                                                                             // Hash
            Assert.AreEqual(nef.ToJson().AsString(), itemArray[3].GetSpan().ToArray().AsSerializable <NefFile>().ToJson().AsString()); // Nef
            var ritem = new ContractManifest();

            ((IInteroperable)ritem).FromStackItem(itemArray[4]);
            Assert.AreEqual(manifest.ToString(), ritem.ToString()); // Manifest

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray(), "oldContract", (byte)CallFlags.All, new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(ByteString));
            Assert.AreEqual(manifest.Name, item.GetString());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy", _engine.Nef);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
Ejemplo n.º 10
0
 public StackItem ToStackItem(ReferenceCounter referenceCounter)
 {
     return(new Array(referenceCounter, new StackItem[] { Script, Manifest.ToString() }));
 }
        public async Task TestDeployContract()
        {
            byte[] script;
            var    manifest = new ContractManifest()
            {
                Permissions = new[] { ContractPermission.DefaultPermission },
                Abi         = new ContractAbi()
                {
                    Events  = new ContractEventDescriptor[0],
                    Methods = new ContractMethodDescriptor[0]
                },
                Groups             = new ContractGroup[0],
                Trusts             = WildcardContainer <UInt160> .Create(),
                SupportedStandards = new string[]
                {
                    "NEP-10"
                },
                Extra = null,
            };

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitDynamicCall(NativeContract.ContractManagement.Hash, "deploy", true, new byte[1], manifest.ToString());
                script = sb.ToArray();
            }

            UT_TransactionManager.MockInvokeScript(rpcClientMock, script, new ContractParameter());

            ContractClient contractClient = new ContractClient(rpcClientMock.Object);
            var            result         = await contractClient.CreateDeployContractTxAsync(new byte[1], manifest, keyPair1);

            Assert.IsNotNull(result);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Deploy Contract, return signed transaction
        /// </summary>
        /// <param name="contractScript">contract script</param>
        /// <param name="manifest">contract manifest</param>
        /// <param name="key">sender KeyPair</param>
        /// <returns></returns>
        public Transaction CreateDeployContractTx(byte[] contractScript, ContractManifest manifest, KeyPair key)
        {
            byte[] script;
            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall(ApplicationEngine.System_Contract_Create, contractScript, manifest.ToString());
                script = sb.ToArray();
            }
            UInt160 sender = Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash();

            Signer[] signers = new[] { new Signer {
                                           Scopes = WitnessScope.CalledByEntry, Account = sender
                                       } };

            Transaction tx = new TransactionManager(rpcClient)
                             .MakeTransaction(script, signers)
                             .AddSignature(key)
                             .Sign()
                             .Tx;

            return(tx);
        }