Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("----==== Norn Wallet ====----");
            Console.WriteLine();
            Console.WriteLine("Norn Wallet created by Norn Community: https://t.me/invest_flood");
            Console.WriteLine();
            Console.WriteLine("Choose action:");
            Console.WriteLine("1 - Import mnemonic");
            Console.WriteLine("2 - Import private key");
            Console.WriteLine("Esc - exit");
            var key = Console.ReadKey().Key;

            Console.WriteLine();
            switch (key)
            {
            case ConsoleKey.D1:
                ImportSeed();
                break;

            case ConsoleKey.D2:
                ImportSk();
                break;

            default:
                return;
            }
            Console.WriteLine();
            Console.Write("Enter current block hash: ");
            hash = Console.ReadLine();
            Console.Write("Enter account counter: ");
            counter = Console.ReadLine();
            int cnt = int.Parse(counter);

            Console.Write("Enter destination address: ");
            string to = Console.ReadLine();

            Console.Write("Enter amount: ");
            string amount = Console.ReadLine();

            Console.Write("Enter fee: ");
            string fee = Console.ReadLine();

            var hashBytesString = CryptoServices.DecodePrefixed(HashType.Block, hash).ToHexString();
            var toBytesString   = CryptoServices.DecodePrefixed(HashType.PublicKeyHash, to).ToHexString();

            Console.WriteLine();
            cnt++;
            string transferOp = hashBytesString + "080000" + CryptoServices.DecodePrefixed(HashType.PublicKeyHash, tz1).ToHexString() + int.Parse(fee).EncodeInt32() + cnt.EncodeInt32() + 400.EncodeInt32() + 0.EncodeInt32() +
                                ((int)(decimal.Parse(amount, CultureInfo.InvariantCulture) * 1000000)).EncodeInt32() + "01" + toBytesString + "0000";
            var opBytes    = ("03" + transferOp).HexToByteArray();
            var hashedData = CryptoServices.Hash(opBytes, 32);
            var bsign      = CryptoServices.CreateSignature(privateKey, hashedData);
            var inject     = transferOp + bsign.ToHexString();

            Console.WriteLine("Operation hex:");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(inject);
            Console.ForegroundColor = ConsoleColor.Gray;
        }
Exemplo n.º 2
0
        // The signing function _____________________________________________________________________

        internal async Task <bool> Sign(ProtectedTask task, Identity signingIdentity)
        {
            Approval approval = null;

            // Find signing provider
            var provider = signProviders
                           .FirstOrDefault(p => p.Contains(signingIdentity.AccountID));

            if (provider == null)
            {
                Trace($"No provider found for {signingIdentity}");
                return(false);
            }

            Trace($"Operation: {task.Operation}");

            // Fetch operation data to sign later.
            // This prevents tampering with the operation during approval
            var operationData = task.Operation.HexToByteArray();

            // Check validity of operation data
            // This protects against forgery due to a compromised node
            ValidateOperation(task, operationData);

            // Approval mechanism registered?
            if (ApprovalRequired != null)
            {
                approval = new Approval
                {
                    Task    = task,
                    Signer  = signingIdentity,
                    Timeout = ApprovalTimeout,
                };

                // Send approval to UI and wait for completion
                await ExecuteSynchronized(() =>
                {
                    ApprovalRequired(approval);
                });

WaitForUser:
                Trace("Wait for user decision");

                var userResult = await approval.GetUserResult();

                Trace($"Approval result: {userResult}");

                if (userResult != SigningResult.Approved)
                {
                    // Cancel or timeout
                    approval.Close(userResult);
                    return(false);
                }

                // Unlock identity if needed
                if (signingIdentity.IsLocked)
                {
                    if (!signingIdentity.Unlock(approval.Passphrase))
                    {
                        // Wrong credentials
                        approval.Retry(SigningResult.InvalidCredentials);

                        goto WaitForUser;
                    }
                }
            }

            // Sign
            Trace("Sign CreateRequest");

            var dataToHash = new byte[1 + operationData.Length];

            dataToHash[0] = 3;
            operationData.CopyTo(dataToHash, 1);

            var hashedData = CryptoServices.Hash(dataToHash, 32);

            if (await provider.Sign(signingIdentity.AccountID, hashedData, out byte[] signature))
            {
                // Success
                task.Signature = CryptoServices.EncodePrefixed(HashType.Signature, signature);

                using (var buffer = new MemoryStream())
                {
                    buffer.Write(operationData, 0, operationData.Length);
                    buffer.Write(signature, 0, signature.Length);

                    task.SignedOperation = buffer.ToArray().ToHexString();
                }

                approval?.Close(SigningResult.Signed);
                return(true);
            }