public static ExpressChain CreateBlockchain(int count)
        {
            var wallets = new List <(DevWallet wallet, Neo.Wallets.WalletAccount account)>(count);

            ushort GetPortNumber(int index, ushort portNumber) => (ushort)((49000 + (index * 1000)) + portNumber);

            try
            {
                for (var i = 1; i <= count; i++)
                {
                    var wallet  = new DevWallet($"node{i}");
                    var account = wallet.CreateAccount();
                    account.IsDefault = true;
                    wallets.Add((wallet, account));
                }

                var keys = wallets.Select(t => t.account.GetKey().PublicKey).ToArray();

                var contract = Neo.SmartContract.Contract.CreateMultiSigContract((keys.Length * 2 / 3) + 1, keys);

                foreach (var(wallet, account) in wallets)
                {
                    var multiSigContractAccount = wallet.CreateAccount(contract, account.GetKey());
                    multiSigContractAccount.Label = "MultiSigContract";
                }

                // 49152 is the first port in the "Dynamic and/or Private" range as specified by IANA
                // http://www.iana.org/assignments/port-numbers
                var nodes = new List <ExpressConsensusNode>(count);
                for (var i = 0; i < count; i++)
                {
                    nodes.Add(new ExpressConsensusNode()
                    {
                        TcpPort       = GetPortNumber(i, 333),
                        WebSocketPort = GetPortNumber(i, 334),
                        RpcPort       = GetPortNumber(i, 332),
                        Wallet        = wallets[i].wallet.ToExpressWallet()
                    });
                }

                return(new ExpressChain()
                {
                    Magic = ExpressChain.GenerateMagicValue(),
                    ConsensusNodes = nodes,
                });
            }
            finally
            {
                foreach (var(wallet, _) in wallets)
                {
                    wallet.Dispose();
                }
            }
        }
Esempio n. 2
0
 static bool TryGetNEP6Wallet(string path, string password, ProtocolSettings settings, [MaybeNullWhen(false)] out Wallet wallet, [MaybeNullWhen(false)] out UInt160 accountHash)
 {
     try
     {
         var nep6wallet = new Neo.Wallets.NEP6.NEP6Wallet(path, settings);
         using var unlock = nep6wallet.Unlock(password);
         var nep6account = nep6wallet.GetAccounts().SingleOrDefault(a => a.IsDefault)
                           ?? nep6wallet.GetAccounts().SingleOrDefault()
                           ?? throw new InvalidOperationException("Neo-express only supports NEP-6 wallets with a single default account or a single account");
         if (nep6account.IsMultiSigContract())
         {
             throw new Exception("Neo-express doesn't supports multi-sig NEP-6 accounts");
         }
         var keyPair = nep6account.GetKey() ?? throw new Exception("account.GetKey() returned null");
         wallet = new DevWallet(settings, string.Empty);
         var account = wallet.CreateAccount(keyPair.PrivateKey);
         accountHash = account.ScriptHash;
         return(true);
     }
     catch
     {
         wallet      = null;
         accountHash = null;
         return(false);
     }
 }
Esempio n. 3
0
        internal static ExpressChain CreateChain(int nodeCount)
        {
            if (nodeCount != 1 && nodeCount != 4 && nodeCount != 7)
            {
                throw new ArgumentException("invalid blockchain node count", nameof(nodeCount));
            }

            var wallets = new List <(DevWallet wallet, Neo.Wallets.WalletAccount account)>(nodeCount);

            for (var i = 1; i <= nodeCount; i++)
            {
                var wallet  = new DevWallet($"node{i}");
                var account = wallet.CreateAccount();
                account.IsDefault = true;
                wallets.Add((wallet, account));
            }

            var keys = wallets.Select(t => t.account.GetKey().PublicKey).ToArray();

            var contract = Neo.SmartContract.Contract.CreateMultiSigContract((keys.Length * 2 / 3) + 1, keys);

            foreach (var(wallet, account) in wallets)
            {
                var multiSigContractAccount = wallet.CreateAccount(contract, account.GetKey());
                multiSigContractAccount.Label = "MultiSigContract";
            }

            // 49152 is the first port in the "Dynamic and/or Private" range as specified by IANA
            // http://www.iana.org/assignments/port-numbers
            var nodes = new List <ExpressConsensusNode>(nodeCount);

            for (var i = 0; i < nodeCount; i++)
            {
                nodes.Add(new ExpressConsensusNode()
                {
                    TcpPort       = GetPortNumber(i, 3),
                    WebSocketPort = GetPortNumber(i, 4),
                    RpcPort       = GetPortNumber(i, 2),
                    Wallet        = wallets[i].wallet.ToExpressWallet()
                });
            }

            return(new ExpressChain()
            {
                Magic = ExpressChain.GenerateMagicValue(),
                ConsensusNodes = nodes,
            });
Esempio n. 4
0
        internal static ExpressChain CreateChain(int nodeCount, byte?addressVersion)
        {
            if (nodeCount != 1 && nodeCount != 4 && nodeCount != 7)
            {
                throw new ArgumentException("invalid blockchain node count", nameof(nodeCount));
            }

            var settings = ProtocolSettings.Default with
            {
                Network        = ExpressChain.GenerateNetworkValue(),
                AddressVersion = addressVersion ?? ProtocolSettings.Default.AddressVersion
            };

            var wallets = new List <(DevWallet wallet, WalletAccount account)>(nodeCount);

            for (var i = 1; i <= nodeCount; i++)
            {
                var wallet  = new DevWallet(settings, $"node{i}");
                var account = wallet.CreateAccount();
                account.IsDefault = true;
                wallets.Add((wallet, account));
            }

            var keys     = wallets.Select(t => t.account.GetKey().PublicKey).ToArray();
            var contract = Contract.CreateMultiSigContract((keys.Length * 2 / 3) + 1, keys);

            foreach (var(wallet, account) in wallets)
            {
                var multiSigContractAccount = wallet.CreateAccount(contract, account.GetKey());
                multiSigContractAccount.Label = "Consensus MultiSigContract";
            }

            var nodes = wallets.Select((w, i) => new ExpressConsensusNode
            {
                TcpPort       = GetPortNumber(i, 3),
                WebSocketPort = GetPortNumber(i, 4),
                RpcPort       = GetPortNumber(i, 2),
                Wallet        = w.wallet.ToExpressWallet()
            });

            return(new ExpressChain()
            {
                Network = settings.Network,
                AddressVersion = settings.AddressVersion,
                ConsensusNodes = nodes.ToList(),
            });
 public static ExpressWallet CreateWallet(string name)
 {
     using (var wallet = new DevWallet(name))
     {
         var account = wallet.CreateAccount();
         account.IsDefault = true;
         return(wallet.ToExpressWallet());
     }
 }
Esempio n. 6
0
 static bool TryGetNEP2Wallet(string nep2, string password, ProtocolSettings settings, [MaybeNullWhen(false)] out Wallet wallet, [MaybeNullWhen(false)] out UInt160 accountHash)
 {
     try
     {
         var privateKey = Wallet.GetPrivateKeyFromNEP2(nep2, password, settings.AddressVersion);
         wallet = new DevWallet(settings, string.Empty);
         var account = wallet.CreateAccount(privateKey);
         accountHash = account.ScriptHash;
         return(true);
     }
     catch
     {
         wallet      = null;
         accountHash = null;
         return(false);
     }
 }