コード例 #1
0
ファイル: DefaultCrypto.cs プロジェクト: LAToken/lachain
 public byte[] SignHashed(byte[] messageHash, byte[] privateKey, bool useNewChainId)
 {
     if (privateKey.Length != 32)
     {
         throw new ArgumentException(nameof(privateKey));
     }
     if (messageHash.Length != 32)
     {
         throw new ArgumentException(nameof(messageHash));
     }
     return(EcSign.Benchmark(() =>
     {
         var sig = new byte[65];
         if (!Secp256K1.SignRecoverable(sig, messageHash, privateKey))
         {
             throw new Exception("secp256k1.sign_recoverable failed");
         }
         var serialized = new byte[64];
         if (!Secp256K1.RecoverableSignatureSerializeCompact(serialized, out var recId, sig))
         {
             throw new Exception("Cannot serialize recoverable signature: how did it happen?");
         }
         recId = TransactionUtils.ChainId(useNewChainId) * 2 + 35 + recId;
         var recIdBytes = new byte[useNewChainId ? 2 : 1];
         var fullBin = recId.ToBytes().ToArray();
         if (useNewChainId)
         {
             recIdBytes[0] = fullBin[1];
             recIdBytes[1] = fullBin[0];
         }
         else
         {
             recIdBytes[0] = fullBin[0];
         }
         return serialized.Concat(recIdBytes).ToArray();
     }));
 }
コード例 #2
0
ファイル: DefaultCrypto.cs プロジェクト: LAToken/lachain
 public byte[] RecoverSignatureHashed(byte[] messageHash, byte[] signature, bool useNewChainId)
 {
     if (messageHash.Length != 32)
     {
         throw new ArgumentException(nameof(messageHash));
     }
     if (signature.Length != SignatureSize(useNewChainId))
     {
         throw new ArgumentException(nameof(signature));
     }
     return(EcRecover.Benchmark(() =>
     {
         var parsedSig = new byte[65];
         var pk = new byte[64];
         var encodedRecId = RestoreEncodedRecIdFromSignatureBuffer(signature);
         var recId = (encodedRecId - 36) / 2 / TransactionUtils.ChainId(useNewChainId);
         if (recId < 0 || recId > 3)
         {
             throw new Exception($"Invalid recId={recId}: : recId >= 0 && recId <= 3 ");
         }
         if (!Secp256K1.RecoverableSignatureParseCompact(parsedSig, signature.Take(64).ToArray(), recId))
         {
             throw new ArgumentException(nameof(signature));
         }
         if (!Secp256K1.Recover(pk, parsedSig, messageHash))
         {
             throw new ArgumentException("Bad signature");
         }
         var result = new byte[33];
         if (!Secp256K1.PublicKeySerialize(result, pk, Flags.SECP256K1_EC_COMPRESSED))
         {
             throw new Exception("Cannot serialize recovered public key: how did it happen?");
         }
         return result;
     }));
 }
コード例 #3
0
ファイル: DefaultCrypto.cs プロジェクト: LAToken/lachain
        public bool VerifySignatureHashed(byte[] messageHash, byte[] signature, byte[] publicKey, bool useNewChainId)
        {
            if (messageHash.Length != 32 || signature.Length != SignatureSize(useNewChainId))
            {
                return(false);
            }
            return(EcVerify.Benchmark(() =>
            {
                var pk = new byte[64];
                if (!Secp256K1.PublicKeyParse(pk, publicKey))
                {
                    return false;
                }

                var publicKeySerialized = new byte[33];
                if (!Secp256K1.PublicKeySerialize(publicKeySerialized, pk, Flags.SECP256K1_EC_COMPRESSED))
                {
                    throw new Exception("Cannot serialize parsed key: how did it happen?");
                }

                var parsedSig = new byte[65];
                var recId = (RestoreEncodedRecIdFromSignatureBuffer(signature) - 36) / 2 / TransactionUtils.ChainId(useNewChainId);
                if (recId < 0 || recId > 3)
                {
                    throw new Exception($"Invalid recId={recId}: : recId >= 0 && recId <= 3 ");
                }
                if (!Secp256K1.RecoverableSignatureParseCompact(parsedSig, signature.Take(64).ToArray(), recId))
                {
                    return false;
                }

                return Secp256K1.Verify(parsedSig.Take(64).ToArray(), messageHash, pk);
            }));
        }
コード例 #4
0
        public void Start(RunOptions options)
        {
            _blockManager       = _container.Resolve <IBlockManager>();
            _configManager      = _container.Resolve <IConfigManager>();
            _stateManager       = _container.Resolve <IStateManager>();
            _transactionBuilder = _container.Resolve <ITransactionBuilder>();
            _transactionPool    = _container.Resolve <ITransactionPool>();
            _transactionSigner  = _container.Resolve <ITransactionSigner>();
            _wallet             = _container.Resolve <IPrivateWallet>();

            var keyPair = _wallet.EcdsaKeyPair;

            Console.WriteLine("-------------------------------");
            Console.WriteLine("Private Key: " + keyPair.PrivateKey.Encode().ToHex());
            Console.WriteLine("Public Key: " + keyPair.PublicKey.EncodeCompressed().ToHex());
            Console.WriteLine("Address: " + Crypto.ComputeAddress(keyPair.PublicKey.EncodeCompressed()).ToHex());
            Console.WriteLine("-------------------------------");

            if (_blockManager.TryBuildGenesisBlock())
            {
                Console.WriteLine("Generated genesis block");
            }

            var genesisBlock = _stateManager.LastApprovedSnapshot.Blocks.GetBlockByHeight(0);

            Console.WriteLine("Genesis Block: " + genesisBlock !.Hash.ToHex());
            Console.WriteLine($" + prevBlockHash: {genesisBlock.Header.PrevBlockHash.ToHex()}");
            Console.WriteLine($" + merkleRoot: {genesisBlock.Header.MerkleRoot.ToHex()}");
            Console.WriteLine($" + nonce: {genesisBlock.Header.Nonce}");
            Console.WriteLine($" + transactionHashes: {genesisBlock.TransactionHashes.Count}");
            foreach (var s in genesisBlock.TransactionHashes)
            {
                Console.WriteLine($" + - {s.ToHex()}");
            }
            Console.WriteLine($" + hash: {genesisBlock.Hash.ToHex()}");

            var address1 = "0xe3c7a20ee19c0107b9121087bcba18eb4dcb8576".HexToUInt160();
            var address2 = "0x6bc32575acb8754886dc283c2c8ac54b1bd93195".HexToUInt160();

            Console.WriteLine("-------------------------------");
            // Console.WriteLine("Current block header height: " + blockchainContext.CurrentBlockHeight);
            Console.WriteLine("-------------------------------");
            Console.WriteLine("Balance of LA 0x3e: " +
                              _stateManager.LastApprovedSnapshot.Balances.GetBalance(address1));
            Console.WriteLine("Balance of LA 0x6b: " +
                              _stateManager.LastApprovedSnapshot.Balances.GetBalance(address2));
            Console.WriteLine("-------------------------------");

            Logger.LogInformation($"Setting chainId");
            var chainId    = _configManager.GetConfig <NetworkConfig>("network")?.ChainId ?? 42;
            var newChainId = _configManager.GetConfig <NetworkConfig>("network")?.NewChainId ?? chainId;

            TransactionUtils.SetChainId((int)chainId, newChainId);

            // _BenchTxProcessing(_transactionBuilder, _transactionSigner, keyPair);
            // _BenchOneTxInBlock(_transactionBuilder, _transactionSigner, keyPair);

            Console.WriteLine("---------------START - TX POOL BENCHMARK----------------");
            _Bench_Tx_Pool(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - TX POOL BENCHMARK----------------");

            Console.WriteLine("---------------START - BLOCK EMULATE BENCHMARK----------------");
            _Bench_Emulate_Block(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - BLOCK EMULATE BENCHMARK----------------");

            Console.WriteLine("---------------START - BLOCK EMULATE + EXECUTE BENCHMARK----------------");
            _Bench_Emulate_Execute_Tx(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - BLOCK EMULATE + EXECUTE BENCHMARK----------------");

            Console.WriteLine("---------------START - MULTIPLE BLOCKS EMULATE + EXECUTE BENCHMARK----------------");
            _Bench_Execute_Blocks(_transactionBuilder, _transactionSigner, keyPair);
            Console.WriteLine("---------------END - MULTIPLE BLOCKS EMULATE + EXECUTE BENCHMARK----------------");

            Environment.Exit(0);
        }