Пример #1
0
        public void TestCreateAccountBySmartContract()
        {
            byte[] privateKey = new byte[32];
            using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(privateKey);
            }
            KeyPair key = new KeyPair(privateKey);
            VerificationContract contract = new VerificationContract
            {
                Script        = Contract.CreateSignatureRedeemScript(key.PublicKey),
                ParameterList = new[] { ContractParameterType.Signature }
            };
            var account   = wallet.CreateAccount(contract, key);
            var dbAccount = wallet.GetAccount(account.ScriptHash);

            account.Should().Be(dbAccount);

            byte[] privateKey2 = new byte[32];
            using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(privateKey2);
            }
            KeyPair  key2      = new KeyPair(privateKey2);
            Contract contract2 = new Contract
            {
                Script        = Contract.CreateSignatureRedeemScript(key2.PublicKey),
                ParameterList = new[] { ContractParameterType.Signature }
            };
            var account2   = wallet.CreateAccount(contract2, key2);
            var dbAccount2 = wallet.GetAccount(account2.ScriptHash);

            account2.Should().Be(dbAccount2);
        }
Пример #2
0
 public static void ClassInit(TestContext context)
 {
     byte[] privateKey = new byte[32];
     using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
     {
         rng.GetBytes(privateKey);
     }
     keyPair        = new KeyPair(privateKey);
     testScriptHash = Contract.CreateSignatureContract(keyPair.PublicKey).ScriptHash;
     nep2key        = keyPair.Export("123", ProtocolSettings.Default.AddressVersion, 2, 1, 1);
 }
Пример #3
0
        public void TestGetAccount()
        {
            bool result = uut.Contains(testScriptHash);

            Assert.AreEqual(false, result);
            uut.CreateAccount(keyPair.PrivateKey);
            result = uut.Contains(testScriptHash);
            Assert.AreEqual(true, result);
            WalletAccount account = uut.GetAccount(testScriptHash);

            Assert.AreEqual(Contract.CreateSignatureRedeemScript(keyPair.PublicKey).ToScriptHash().ToAddress(ProtocolSettings.Default.AddressVersion), account.Address);
        }
Пример #4
0
        public void TestGetAccounts()
        {
            Dictionary <UInt160, KeyPair> keys = new();

            byte[] privateKey = new byte[32];
            using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(privateKey);
            }
            KeyPair key = new(privateKey);

            keys.Add(Contract.CreateSignatureRedeemScript(key.PublicKey).ToScriptHash(), key);
            keys.Add(Contract.CreateSignatureRedeemScript(keyPair.PublicKey).ToScriptHash(), keyPair);
            uut.CreateAccount(key.PrivateKey);
            uut.CreateAccount(keyPair.PrivateKey);
            foreach (var account in uut.GetAccounts())
            {
                if (!keys.ContainsKey(account.ScriptHash))
                {
                    Assert.Fail();
                }
            }
        }
Пример #5
0
        public bool AddSignature(Contract contract, ECPoint pubkey, byte[] signature)
        {
            if (contract.Script.IsMultiSigContract())
            {
                if (!ScriptHash.Equals(contract.ScriptHash))
                {
                    return(false);
                }
                if (Parameters is null)
                {
                    Script     = contract.Script;
                    Parameters = contract.ParameterList.Select(p => new ContractParameter {
                        Type = p
                    }).ToArray();
                }
                if (Parameters.All(p => p.Value != null))
                {
                    return(false);
                }
                if (Signatures == null)
                {
                    Signatures = new Dictionary <ECPoint, byte[]>();
                }
                else if (Signatures.ContainsKey(pubkey))
                {
                    return(false);
                }
                List <ECPoint> points = new List <ECPoint>();
                {
                    int i = 0;
                    switch (contract.Script[i++])
                    {
                    case 1:
                        ++i;
                        break;

                    case 2:
                        i += 2;
                        break;
                    }
                    while (contract.Script[i++] == 33)
                    {
                        points.Add(ECPoint.DecodePoint(contract.Script.Skip(i).Take(33).ToArray(), ECCurve.Secp256r1));
                        i += 33;
                    }
                }
                if (!points.Contains(pubkey))
                {
                    return(false);
                }
                Signatures.Add(pubkey, signature);
                if (Signatures.Count == contract.ParameterList.Length)
                {
                    Dictionary <ECPoint, int> dic = points.Select((p, i) => new
                    {
                        PublicKey = p,
                        Index     = i
                    }).ToDictionary(p => p.PublicKey, p => p.Index);
                    byte[][] sigs = Signatures.Select(p => new
                    {
                        Signature = p.Value,
                        Index     = dic[p.Key]
                    }).OrderByDescending(p => p.Index).Select(p => p.Signature).ToArray();
                    for (int i = 0; i < sigs.Length; i++)
                    {
                        if (!Add(contract, i, sigs[i]))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    Signatures = null;
                }
                return(true);
            }
            else
            {
                int index = -1;
                for (int i = 0; i < contract.ParameterList.Length; i++)
                {
                    if (contract.ParameterList[i] == ContractParameterType.Signature)
                    {
                        if (index >= 0)
                        {
                            throw new NotSupportedException();
                        }
                        else
                        {
                            index = i;
                        }
                    }
                }

                if (index == -1)
                {
                    // unable to find ContractParameterType.Signature in contract.ParameterList
                    // return now to prevent array index out of bounds exception
                    return(false);
                }
                return(Add(contract, index, signature));
            }
        }