public void GetHashData() { var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot); Assert.ThrowsException <ArgumentException>(() => new MethodCallback(engine, UInt160.Zero, "_test")); var contract = new ContractState() { Manifest = new ContractManifest() { Permissions = new ContractPermission[0], Groups = new ContractGroup[0], Trusts = WildcardContainer <UInt160> .Create(), Abi = new ContractAbi() { Methods = new ContractMethodDescriptor[] { new ContractMethodDescriptor() { Name = "test", Parameters = new ContractParameterDefinition[0] } }, Events = new ContractEventDescriptor[0], }, }, Script = new byte[] { 1, 2, 3 }, Hash = new byte[] { 1, 2, 3 }.ToScriptHash() }; engine.LoadScript(contract.Script); engine.Snapshot.AddContract(contract.Hash, contract); Assert.ThrowsException <InvalidOperationException>(() => new MethodCallback(engine, contract.Hash, "test")); contract.Manifest.Permissions = new ContractPermission[] { new ContractPermission() { Contract = ContractPermissionDescriptor.Create(contract.Hash), Methods = WildcardContainer <string> .Create("test") } }; var data = new MethodCallback(engine, contract.Hash, "test"); Assert.AreEqual(0, engine.CurrentContext.EvaluationStack.Count); var array = new VM.Types.Array(); data.LoadContext(engine, array); Assert.AreEqual(3, engine.CurrentContext.EvaluationStack.Count); Assert.AreEqual("9bc4860bb936abf262d7a51f74b4304833fee3b2", engine.Pop <VM.Types.ByteString>().GetSpan().ToHexString()); Assert.AreEqual("test", engine.Pop <VM.Types.ByteString>().GetString()); Assert.IsTrue(engine.Pop() == array); }
public void TestIsAllowed() { ContractManifest contractManifest1 = ContractManifest.CreateDefault(UInt160.Zero); ContractPermission contractPermission1 = ContractPermission.DefaultPermission; contractPermission1.Contract = ContractPermissionDescriptor.Create(UInt160.Zero); Assert.AreEqual(true, contractPermission1.IsAllowed(contractManifest1, "AAA")); contractPermission1.Contract = ContractPermissionDescriptor.CreateWildcard(); ContractManifest contractManifest2 = ContractManifest.CreateDefault(UInt160.Zero); ContractPermission contractPermission2 = ContractPermission.DefaultPermission; contractPermission2.Contract = ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001")); Assert.AreEqual(false, contractPermission2.IsAllowed(contractManifest2, "AAA")); contractPermission2.Contract = ContractPermissionDescriptor.CreateWildcard(); Random random3 = new Random(); byte[] privateKey3 = new byte[32]; random3.NextBytes(privateKey3); ECPoint publicKey3 = ECCurve.Secp256r1.G * privateKey3; ContractManifest contractManifest3 = ContractManifest.CreateDefault(UInt160.Zero); contractManifest3.Groups = new ContractGroup[] { new ContractGroup() { PubKey = publicKey3 } }; ContractPermission contractPermission3 = ContractPermission.DefaultPermission; contractPermission3.Contract = ContractPermissionDescriptor.Create(publicKey3); Assert.AreEqual(true, contractPermission3.IsAllowed(contractManifest3, "AAA")); contractPermission3.Contract = ContractPermissionDescriptor.CreateWildcard(); Random random4 = new Random(); byte[] privateKey41 = new byte[32]; random4.NextBytes(privateKey41); ECPoint publicKey41 = ECCurve.Secp256r1.G * privateKey41; byte[] privateKey42 = new byte[32]; random4.NextBytes(privateKey42); ECPoint publicKey42 = ECCurve.Secp256r1.G * privateKey42; ContractManifest contractManifest4 = ContractManifest.CreateDefault(UInt160.Zero); contractManifest4.Groups = new ContractGroup[] { new ContractGroup() { PubKey = publicKey42 } }; ContractPermission contractPermission4 = ContractPermission.DefaultPermission; contractPermission4.Contract = ContractPermissionDescriptor.Create(publicKey41); Assert.AreEqual(false, contractPermission4.IsAllowed(contractManifest4, "AAA")); contractPermission4.Contract = ContractPermissionDescriptor.CreateWildcard(); }
public void ParseFromJson_Trust() { var json = @"{""name"":""testManifest"",""groups"":[],""supportedstandards"":[],""abi"":{""methods"":[{""name"":""testMethod"",""parameters"":[],""returntype"":""Void"",""offset"":0,""safe"":true}],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToJson().ToString(), json); var check = TestUtils.CreateDefaultManifest(); check.Trusts = WildcardContainer <ContractPermissionDescriptor> .Create(ContractPermissionDescriptor.Create(UInt160.Parse("0x0000000000000000000000000000000000000001"))); Assert.AreEqual(manifest.ToJson().ToString(), check.ToJson().ToString()); }
public void TestFromAndToJson() { byte[] privateKey = new byte[32]; RandomNumberGenerator rng = RandomNumberGenerator.Create(); rng.GetBytes(privateKey); KeyPair key = new KeyPair(privateKey); ContractPermissionDescriptor temp = ContractPermissionDescriptor.Create(key.PublicKey); ContractPermissionDescriptor result = ContractPermissionDescriptor.FromJson(temp.ToJson()); Assert.AreEqual(null, result.Hash); Assert.AreEqual(result.Group, result.Group); }
public void TestCreateByECPointAndIsWildcard() { byte[] privateKey = new byte[32]; RandomNumberGenerator rng = RandomNumberGenerator.Create(); rng.GetBytes(privateKey); KeyPair key = new KeyPair(privateKey); ContractPermissionDescriptor contractPermissionDescriptor = ContractPermissionDescriptor.Create(key.PublicKey); Assert.IsNotNull(contractPermissionDescriptor); Assert.AreEqual(key.PublicKey, contractPermissionDescriptor.Group); Assert.AreEqual(false, contractPermissionDescriptor.IsWildcard); }
public void ParseFromJson_Permissions() { var json = @"{""name"":""testManifest"",""groups"":[],""supportedstandards"":[],""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safemethods"":[],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = TestUtils.CreateDefaultManifest(UInt160.Zero); check.Permissions = new[] { new ContractPermission() { Contract = ContractPermissionDescriptor.Create(UInt160.Zero), Methods = WildcardContainer <string> .Create("method1", "method2") } }; Assert.AreEqual(manifest.ToString(), check.ToString()); }
public void ParseFromJson_Permissions() { var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""0x0000000000000000000000000000000000000000"",""methods"":[""method1"",""method2""]}],""trusts"":[],""safeMethods"":[],""extra"":null}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = ContractManifest.CreateDefault(UInt160.Zero); check.Permissions = new[] { new ContractPermission() { Contract = ContractPermissionDescriptor.Create(UInt160.Zero), Methods = WildcardContainer <string> .Create("method1", "method2") } }; Assert.AreEqual(manifest.ToString(), check.ToString()); }