示例#1
0
        public void FromString()
        {
            Assert.Throws <ArgumentOutOfRangeException>(() => PrivateKey.FromString(string.Empty));
            Assert.Throws <ArgumentOutOfRangeException>(() => PrivateKey.FromString("a"));
            Assert.Throws <ArgumentOutOfRangeException>(() => PrivateKey.FromString("870912"));
            Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                        PrivateKey.FromString(
                                                            "00000000000000000000000000000000000000000000000000000000870912"
                                                            )
                                                        );
            Assert.Throws <ArgumentOutOfRangeException>(() =>
                                                        PrivateKey.FromString(
                                                            "000000000000000000000000000000000000000000000000000000000000870912"
                                                            )
                                                        );
            Assert.Throws <FormatException>(() => PrivateKey.FromString("zz"));
            PrivateKey actual = PrivateKey.FromString(
                "e07107ca4b0d19147fa1152a0f2c7884705d59cbb6318e2f901bd28dd9ff78e3"
                );

            AssertBytesEqual(
                new byte[]
            {
                0xe0, 0x71, 0x07, 0xca, 0x4b, 0x0d, 0x19, 0x14, 0x7f, 0xa1, 0x15,
                0x2a, 0x0f, 0x2c, 0x78, 0x84, 0x70, 0x5d, 0x59, 0xcb, 0xb6, 0x31,
                0x8e, 0x2f, 0x90, 0x1b, 0xd2, 0x8d, 0xd9, 0xff, 0x78, 0xe3,
            },
                actual.ToByteArray()
                );
        }
示例#2
0
        public void Transactions()
        {
            var key = PrivateKey.FromString(
                "ea0493b0ed67fc97b2e5e85a1d145adea294112f09df15398cb10f2ed5ad1a83"
                );
            var tx2 = new Transaction <Arithmetic>(
                nonce: 0L,
                signer: key.ToAddress(),
                publicKey: key.PublicKey,
                genesisHash: GenesisHash,
                updatedAddresses: ImmutableHashSet <Address> .Empty,
                timestamp: new DateTimeOffset(2021, 9, 7, 10, 23, 12, 345, TimeSpan.FromHours(9)),
                actions: Array.Empty <Arithmetic>(),
                signature: ByteUtil.ParseHex(
                    "304402202a8324c83390b1fe0fdd4014056a049bc02ca059369ef62145fe574cb31224f" +
                    "d022073bf8a48403cf46f5fa63f26f3e8ef4db8ef1d841684856da63d9b7eeb91759a"
                    )
                );

            Block1.Transactions = new[] { tx2, Tx0InBlock1, Tx1InBlock1 };
            Assert.Equal(
                new[] { Tx1InBlock1.Id, Tx0InBlock1.Id, tx2.Id },
                Block1.Transactions.Select(tx => tx.Id).ToArray()
                );
        }
示例#3
0
        public void TransactionsWithInconsistentGenesisHashes()
        {
            var key = PrivateKey.FromString(
                "2ed05de0b35d93e4ae801ae40c8bb4257a771ff67c1e5d1754562e4191953710"
                );
            var differentGenesisHash = BlockHash.FromString(
                "76942b42f99c28da02ed916ebd2fadb189415e8288a4bd87f9ae3594127b79e6"
                );
            var txWithDifferentGenesis = new Transaction <Arithmetic>(
                nonce: 0L,
                signer: key.ToAddress(),
                publicKey: key.PublicKey,
                genesisHash: differentGenesisHash,
                updatedAddresses: ImmutableHashSet <Address> .Empty,
                timestamp: new DateTimeOffset(2021, 9, 7, 12, 1, 12, 345, TimeSpan.FromHours(9)),
                actions: Array.Empty <Arithmetic>(),
                signature: ByteUtil.ParseHex(
                    "304402202027a31e4298c685daaa944b1d120b4e6894f3bfffa13563331c0a7071a04b" +
                    "310220167507575e982d47d7c6753b782a5f1beb6415af96e7db3ccaf83b516d5133d1"
                    )
                );
            BlockContent <Arithmetic> block = Block1.Copy();

            Transaction <Arithmetic>[] inconsistentTxs =
                block.Transactions.Append(txWithDifferentGenesis).ToArray();
            InvalidTxGenesisHashException e = Assert.Throws <InvalidTxGenesisHashException>(
                () => block.Transactions = inconsistentTxs
                );

            Assert.Equal(Block1.Transactions[0].GenesisHash, e.ExpectedGenesisHash);
            Assert.Equal(differentGenesisHash, e.ImproperGenesisHash);
            Assert.Equal(Block1.Transactions, block.Transactions);
        }
示例#4
0
        public void ShouldBeAbleToSerializeAndRecoverIdentities()
        {
            PrivateKey id   = PrivateKey.FromRandom();
            string     str  = id.ToString();
            PrivateKey back = PrivateKey.FromString(str);

            Assert.Equal(id.GetHashCode(), back.GetHashCode());
        }
示例#5
0
        public void Import(
            [Argument(
                 "PRIVATE-KEY",
                 Description = "A raw private key to import in hexadecimal string."
                 )]
            string rawKeyHex,
            [Option(
                 'p',
                 ValueName = "PASSPHRASE",
                 Description = "Take passphrase through this option instead of prompt."
                 )]
            string?passphrase = null,
            [Option(
                 Description = "Print the created private key as Web3 Secret Storage format."
                 )]
            bool json = false,
            [Option(Description = "Do not add to the key store, but only show the created key.")]
            bool dryRun = false
            )
        {
            PrivateKey key;

            try
            {
                key = PrivateKey.FromString(rawKeyHex);
            }
            catch (FormatException)
            {
                throw Utils.Error("A raw private key should be hexadecimal.");
            }
            catch (ArgumentOutOfRangeException)
            {
                throw Utils.Error("Hexadecimal characters should be even (not odd).");
            }
            catch (Exception)
            {
                throw Utils.Error("Invalid private key.");
            }

            Add(key, passphrase, json, dryRun);
        }
示例#6
0
        public void ExchangeTest()
        {
            PrivateKey prvKey = PrivateKey.FromString(
                "82fc9947e878fc7ed01c6c310688603f0a41c8e8704e5b990e8388343b0fd465"
                );

            byte[] pubkeyBytes = ByteUtil.ParseHex(
                "5f706787ac72c1080275c1f398640fb07e9da0b124ae9734b28b8d0f01eda586"
                );
            var pubKey = new PrivateKey(pubkeyBytes).PublicKey;

            var expected = new SymmetricKey(
                new byte[]
            {
                0x59, 0x35, 0xd0, 0x47, 0x6a, 0xf9, 0xdf, 0x29, 0x98, 0xef,
                0xb6, 0x03, 0x83, 0xad, 0xf2, 0xff, 0x23, 0xbc, 0x92, 0x83,
                0x22, 0xcf, 0xbb, 0x73, 0x8f, 0xca, 0x88, 0xe4, 0x9d, 0x55,
                0x7d, 0x7e,
            }
                );
            SymmetricKey actual = prvKey.ExchangeKey(pubKey);

            Assert.Equal(expected, actual);
        }
示例#7
0
        public async Task CanBroadcastBlock()
        {
            // If the bucket stored peers are the same, the block may not propagate,
            // so specify private keys to make the buckets different.
            PrivateKey keyA = PrivateKey.FromString(
                "8568eb6f287afedece2c7b918471183db0451e1a61535bb0381cfdf95b85df20");
            PrivateKey keyB = PrivateKey.FromString(
                "c34f7498befcc39a14f03b37833f6c7bb78310f1243616524eda70e078b8313c");
            PrivateKey keyC = PrivateKey.FromString(
                "941bc2edfab840d79914d80fe3b30840628ac37a5d812d7f922b5d2405a223d3");

            var swarmA = CreateSwarm(keyA);
            var swarmB = CreateSwarm(keyB);
            var swarmC = CreateSwarm(keyC);

            BlockChain <DumbAction> chainA = swarmA.BlockChain;
            BlockChain <DumbAction> chainB = swarmB.BlockChain;
            BlockChain <DumbAction> chainC = swarmC.BlockChain;

            foreach (int i in Enumerable.Range(0, 10))
            {
                await chainA.MineBlock(keyA);
            }

            foreach (int i in Enumerable.Range(0, 3))
            {
                await chainB.MineBlock(keyB);
            }

            try
            {
                await StartAsync(swarmA);
                await StartAsync(swarmB);
                await StartAsync(swarmC);

                await BootstrapAsync(swarmB, swarmA.AsPeer);
                await BootstrapAsync(swarmC, swarmA.AsPeer);

                swarmB.BroadcastBlock(chainB[-1]);

                // chainA ignores block header received because its index is shorter.
                await swarmA.BlockHeaderReceived.WaitAsync();

                await swarmC.BlockAppended.WaitAsync();

                Assert.False(swarmA.BlockAppended.IsSet);

                // chainB doesn't applied to chainA since chainB is shorter
                // than chainA
                Assert.NotEqual(chainB, chainA);

                swarmA.BroadcastBlock(chainA[-1]);

                await swarmB.BlockAppended.WaitAsync();

                await swarmC.BlockAppended.WaitAsync();

                Log.Debug("Compare chainA and chainB");
                Assert.Equal(chainA.BlockHashes, chainB.BlockHashes);
                Log.Debug("Compare chainA and chainC");
                Assert.Equal(chainA.BlockHashes, chainC.BlockHashes);
            }
            finally
            {
                await StopAsync(swarmA);
                await StopAsync(swarmB);
                await StopAsync(swarmC);

                swarmA.Dispose();
                swarmB.Dispose();
                swarmC.Dispose();
            }
        }
示例#8
0
        public async Task DoNotRebroadcastTxsWithLowerNonce()
        {
            // If the bucket stored peers are the same, the block may not propagate,
            // so specify private keys to make the buckets different.
            PrivateKey keyA = PrivateKey.FromString(
                "8568eb6f287afedece2c7b918471183db0451e1a61535bb0381cfdf95b85df20");
            PrivateKey keyB = PrivateKey.FromString(
                "c34f7498befcc39a14f03b37833f6c7bb78310f1243616524eda70e078b8313c");
            PrivateKey keyC = PrivateKey.FromString(
                "941bc2edfab840d79914d80fe3b30840628ac37a5d812d7f922b5d2405a223d3");

            var swarmA = CreateSwarm(keyA);
            var swarmB = CreateSwarm(keyB);
            var swarmC = CreateSwarm(keyC);

            BlockChain <DumbAction> chainA = swarmA.BlockChain;
            BlockChain <DumbAction> chainB = swarmB.BlockChain;
            BlockChain <DumbAction> chainC = swarmC.BlockChain;

            var privateKey = new PrivateKey();

            try
            {
                var tx1 = swarmA.BlockChain.MakeTransaction(
                    privateKey: privateKey,
                    actions: new DumbAction[] { });
                var tx2 = swarmA.BlockChain.MakeTransaction(
                    privateKey: privateKey,
                    actions: new DumbAction[] { });
                Assert.Equal(0, tx1.Nonce);
                Assert.Equal(1, tx2.Nonce);
                await StartAsync(swarmA);
                await StartAsync(swarmB);

                await swarmA.AddPeersAsync(new[] { swarmB.AsPeer }, null);

                swarmA.BroadcastTxs(new[] { tx1, tx2 });
                await swarmB.TxReceived.WaitAsync();

                Assert.Equal(
                    new HashSet <TxId> {
                    tx1.Id, tx2.Id
                },
                    chainB.GetStagedTransactionIds().ToHashSet());

                chainA.UnstageTransaction(tx2);
                Assert.Equal(1, chainA.GetNextTxNonce(privateKey.ToAddress()));
                swarmA.RoutingTable.RemovePeer((BoundPeer)swarmB.AsPeer);
                swarmB.RoutingTable.RemovePeer((BoundPeer)swarmA.AsPeer);
                Assert.Empty(swarmA.Peers);
                Assert.Empty(swarmB.Peers);
                await chainB.MineBlock(keyB);

                var tx3 = chainA.MakeTransaction(
                    privateKey: privateKey,
                    actions: new DumbAction[] { });
                var tx4 = chainA.MakeTransaction(
                    privateKey: privateKey,
                    actions: new DumbAction[] { });
                Assert.Equal(1, tx3.Nonce);
                Assert.Equal(2, tx4.Nonce);

                await StartAsync(swarmC);

                await swarmA.AddPeersAsync(new[] { swarmB.AsPeer }, null);

                await swarmB.AddPeersAsync(new[] { swarmC.AsPeer }, null);

                swarmA.BroadcastTxs(new[] { tx3, tx4 });
                await swarmC.TxReceived.WaitAsync();

                Assert.DoesNotContain(tx3.Id, chainB.GetStagedTransactionIds());
                Assert.Contains(tx4.Id, chainB.GetStagedTransactionIds());
                // SwarmC can not receive tx3 because SwarmB does not rebroadcast it.
                Assert.DoesNotContain(tx3.Id, chainC.GetStagedTransactionIds());
                Assert.Contains(tx4.Id, chainC.GetStagedTransactionIds());
            }
            finally
            {
                await StopAsync(swarmA);
                await StopAsync(swarmB);
                await StopAsync(swarmC);

                swarmA.Dispose();
                swarmB.Dispose();
                swarmC.Dispose();
            }
        }
示例#9
0
        public BlockContentFixture()
        {
            TimeSpan kst = TimeSpan.FromHours(9);

            GenesisKey = new PrivateKey(new byte[]
            {
                0x9b, 0xf4, 0x66, 0x4b, 0xa0, 0x9a, 0x89, 0xfa, 0xeb, 0x68, 0x4b,
                0x94, 0xe6, 0x9f, 0xfd, 0xe0, 0x1d, 0x26, 0xae, 0x14, 0xb5, 0x56,
                0x20, 0x4d, 0x3f, 0x6a, 0xb5, 0x8f, 0x61, 0xf7, 0x84, 0x18,
            });
            GenesisMetadata = new BlockMetadata
            {
                Index        = 0,
                Timestamp    = new DateTimeOffset(2021, 9, 6, 13, 46, 39, 123, kst),
                PublicKey    = GenesisKey.PublicKey,
                Difficulty   = 0,
                PreviousHash = null,
                TxHash       = null,
            };
            Genesis     = new BlockContent <Arithmetic>(GenesisMetadata);
            GenesisHash = BlockHash.FromString(
                "341e8f360597d5bc45ab96aabc5f1b0608063f30af7bd4153556c9536a07693a"
                );
            Block1Key = new PrivateKey(new byte[]
            {
                0xfc, 0xf3, 0x0b, 0x33, 0x3d, 0x04, 0xcc, 0xfe, 0xb5, 0x62, 0xf0,
                0x00, 0xa3, 0x2d, 0xf4, 0x88, 0xe7, 0x15, 0x49, 0x49, 0xd3, 0x1d,
                0xdc, 0xac, 0x3c, 0xf9, 0x27, 0x8a, 0xcb, 0x57, 0x86, 0xc7,
            });
            BlockMetadata1 = new BlockMetadata
            {
                Index        = 1,
                Timestamp    = new DateTimeOffset(2021, 9, 6, 17, 1, 9, 45, kst),
                PublicKey    = Block1Key.PublicKey,
                Difficulty   = 123,
                PreviousHash = GenesisHash,
                TxHash       = HashDigest <SHA256> .FromString(
                    "654698d34b6d9a55b0c93e4ffb2639278324868c91965bc5f96cb3071d6903a0"
                    ),
            };
            var block1Tx0Key = PrivateKey.FromString(
                "2d5c20079bc4b2e6eab9ecbb405da8ba6590c436edfb07b7d4466563d7dac096"
                );

            Tx0InBlock1 = new Transaction <Arithmetic>(
                nonce: 0L,
                signer: block1Tx0Key.ToAddress(),
                publicKey: block1Tx0Key.PublicKey,
                genesisHash: GenesisHash,
                updatedAddresses: ImmutableHashSet <Address> .Empty.Add(block1Tx0Key.ToAddress()),
                timestamp: new DateTimeOffset(2021, 9, 6, 17, 0, 1, 1, kst),
                actions: new[]
            {
                Arithmetic.Add(10), Arithmetic.Add(50), Arithmetic.Sub(25),
            },
                signature: ByteUtil.ParseHex(
                    "30440220422c85ea44845a56253654d95595ad06d6f09f862ca71b97e986ecbb453eac" +
                    "ae0220606e76276e40fa8f0795b880f712531fd6bd9db253bd8ab9c86aa4ab7d791d37"
                    )
                );
            Tx0InBlock1.Validate(block1Tx0Key);
            var block1Tx1Key = PrivateKey.FromString(
                "105341c78dfb0dd313b961081630444c2586a1f01fb0c625368ffdc9136cfa30"
                );

            Tx1InBlock1 = new Transaction <Arithmetic>(
                nonce: 1L,
                signer: block1Tx1Key.ToAddress(),
                publicKey: block1Tx1Key.PublicKey,
                genesisHash: GenesisHash,
                updatedAddresses: ImmutableHashSet <Address> .Empty.Add(block1Tx1Key.ToAddress()),
                timestamp: new DateTimeOffset(2021, 9, 6, 17, 0, 1, 1, kst),
                actions: new[] { Arithmetic.Add(30) },
                signature: ByteUtil.ParseHex(
                    "3045022100abe3caabf2a46a297f2e4496f2c46d7e2f723e75fc42025d19f3ed7fce382" +
                    "d4e02200ffd36f7bef759b6c7ab43bc0f8959a0c463f88fd0f1faeaa209a8661506c4f0"
                    )
                );
            Tx1InBlock1.Validate(block1Tx1Key);
            Block1 = new BlockContent <Arithmetic>(
                BlockMetadata1,
                new[]
            {
                Tx0InBlock1,
                Tx1InBlock1,
            }
                );
            BlockMetadataPv0 = new BlockMetadata
            {
                ProtocolVersion = 0,
                Index           = 0,
                Timestamp       = new DateTimeOffset(2021, 9, 6, 13, 46, 39, 123, kst),
                Miner           = GenesisKey.ToAddress(),
                Difficulty      = 0,
                PreviousHash    = null,
                TxHash          = null,
            };
            BlockPv0 = new BlockContent <Arithmetic>(BlockMetadataPv0);
            BlockPv1 = new BlockContent <Arithmetic>(Block1)
            {
                ProtocolVersion = 1,
                PublicKey       = null,
            };
            BlockMetadataPv1 = new BlockMetadata(BlockPv1);
        }