Esempio n. 1
0
        public async Task ShouldRemoveAccessKeyNoLongerWorks()
        {
            var keypair   = KeyPairEd25519.FromRandom();
            var publicKey = keypair.GetPublicKey();
            await _workingAccount.AddKeyAsync(publicKey.ToString(), new UInt128(400000), "", _contractId);

            await _workingAccount.DeleteKeyAsync(publicKey : publicKey.ToString());

            var signer = (InMemorySigner)this._near.Connection.Signer;
            await signer.KeyStore.SetKeyAsync(TestUtils.NetworkId, _workingAccount.AccountId, keypair);

            dynamic args = new ExpandoObject();

            args.value = "test";
            try
            {
                var changeResult = await _contract.Change("setValue", args, null, new UInt128(0));

                Assert.Fail("should throw an error");
            }
            catch (Exception e)
            {
                Assert.Pass("pass with exception", e);
            }
        }
Esempio n. 2
0
        public void ItShouldConvertToString()
        {
            var keypair           = KeyPairEd25519.FromRandom();
            var keypairFromString = (KeyPairEd25519)KeyPair.FromString(keypair.ToString());

            Assert.AreEqual(keypairFromString.GetSecretKey(), keypair.GetSecretKey());
            var key = "ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw";
            var keypairFromKeyString = KeyPair.FromString(key);

            Assert.AreEqual(keypairFromKeyString.ToString(), key);
        }
Esempio n. 3
0
        public void ItShouldSignAndVerifyWithRandom()
        {
            var keypair = KeyPairEd25519.FromRandom();

            byte[] messageBytes = Encoding.Default.GetBytes("message");
            byte[] messageSha256;
            using (var sha256 = SHA256.Create())
            {
                messageSha256 = sha256.ComputeHash(messageBytes);
            }
            var signature = keypair.Sign(messageSha256);

            Assert.AreEqual(keypair.Verify(messageSha256, signature.SignatureBytes), true);
        }
Esempio n. 4
0
        public async Task ShouldMakeFunctionCallUsingAccessKey()
        {
            var keypair   = KeyPairEd25519.FromRandom();
            var publicKey = keypair.GetPublicKey();
            await _workingAccount.AddKeyAsync(publicKey.ToString(), new UInt128(10000000), "", _contractId);

            var signer = (InMemorySigner)this._near.Connection.Signer;
            await signer.KeyStore.SetKeyAsync(TestUtils.NetworkId, _workingAccount.AccountId, keypair);

            var     setCallValue = TestUtils.GenerateUniqueString(prefix: "setCallPrefix");
            dynamic args         = new ExpandoObject();

            args.value = setCallValue;
            await _contract.Change("setValue", args, null, new UInt128(0));

            var viewArgs  = new ExpandoObject();
            var testValue = await _contract.View("getValue", viewArgs);

            Assert.AreEqual(testValue.result, setCallValue);
        }
Esempio n. 5
0
        public async Task ShouldLoadingAccountAfterAddingAFullKey()
        {
            var keypair   = KeyPairEd25519.FromRandom();
            var publicKey = keypair.GetPublicKey();
            await _workingAccount.AddKeyAsync(publicKey.ToString(), null, "", "");

            var rawAccessKeys = await _workingAccount.GetAccessKeysAsync();

            var accessKeys = new List <dynamic>();

            foreach (dynamic accessKey in rawAccessKeys)
            {
                accessKeys.Add(accessKey);
            }
            Assert.That(accessKeys, Has.Exactly(2).Items);
            var addedKey = accessKeys.First(obj => ((string)obj.public_key).Equals(publicKey.ToString()));

            Assert.IsNotNull(addedKey);
            var permission = (string)addedKey.access_key.permission;

            Assert.AreEqual(permission, "FullAccess");
        }
Esempio n. 6
0
        public async Task ShouldViewAccountDetailsAfterAddingAccessKeys()
        {
            var keypair   = KeyPairEd25519.FromRandom();
            var publicKey = keypair.GetPublicKey();
            await _workingAccount.AddKeyAsync(publicKey.ToString(), new UInt128(1000000000), "", _contractId);

            var contractId2 = "test_contract2_" + (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            var contract2   = await TestUtils.DeployContract(_workingAccount, contractId2, new UInt128(10000000));

            var keypair2   = KeyPairEd25519.FromRandom();
            var publicKey2 = keypair2.GetPublicKey();
            await _workingAccount.AddKeyAsync(publicKey2.ToString(), new UInt128(2000000000), "", contractId2);

            var details = await _workingAccount.GetAccountDetailsAsync();

            var     expectedResult = new List <dynamic>();
            dynamic authorizedApp1 = new ExpandoObject();

            authorizedApp1.ContractId = _contractId;
            authorizedApp1.Amount     = new UInt128(1000000000);
            authorizedApp1.PublicKey  = publicKey.ToString();
            expectedResult.Add(authorizedApp1);

            dynamic authorizedApp2 = new ExpandoObject();

            authorizedApp2.ContractId = contractId2;
            authorizedApp2.Amount     = new UInt128(2000000000);
            authorizedApp2.PublicKey  = publicKey2.ToString();
            expectedResult.Add(authorizedApp2);

            IEnumerable <string> expected = expectedResult.Select(x => ((object)x.ContractId).ToString()).ToList().OrderBy(s => s);

            dynamic[]            apps = details.AuthorizedApps;
            IEnumerable <string> real = apps.Select(x => ((object)x.ContractId).ToString()).ToList().OrderBy(s => s);

            Assert.IsTrue(real.SequenceEqual(expected));
        }