Exemplo n.º 1
0
        private void ParseReveal()
        {
            var header = ParseHeader();

            var what = TakeOne();

            var publicKey = CryptoServices.EncodePrefixed(HashType.Public, Take(32));
        }
Exemplo n.º 2
0
        internal static void ApplySignature(ProtectedTask task, byte[] data, byte[] signature)
        {
            task.Signature = CryptoServices.EncodePrefixed(HashType.Signature, signature);

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

                task.SignedOperation = buffer.ToArray().ToHexString();
            }
        }
Exemplo n.º 3
0
        public ParsedOperation(byte[] operationData)
        {
            this.operationData = operationData;

            // Branch
            Branch = CryptoServices.EncodePrefixed(HashType.Block, Take(32));

            // Items
            while (!EndOfData)
            {
                ParseItem();
            }
        }
Exemplo n.º 4
0
        private static void ImportSk()
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("Import private key");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("Enter encoded private key: ");
            edsk = Console.ReadLine();
            var keys = CryptoServices.ImportEd25519(edsk);

            publicKey  = keys.Item1;
            privateKey = keys.Item2;
            tz1        = CryptoServices.CreatePrefixedHash(HashType.PublicKeyHash, publicKey);
            edsk       = CryptoServices.EncodePrefixed(HashType.Private, privateKey);
        }
Exemplo n.º 5
0
        private string ParseAccountID()
        {
            switch (TakeOne())
            {
            case 0:                     // Identity
                return(ParseIdentityID());

            case 1:                     // Account
                var data = Take(20);
                TakeOne();              // Padding
                return(CryptoServices.EncodePrefixed(HashType.Account, data));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 6
0
        private static void ImportSeed()
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("Import mnemonic");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("Enter words: ");
            mnemonic = Console.ReadLine();
            Console.Write("Enter password or press Enter: ");
            string pass = Console.ReadLine();
            var    bip  = new BIP39(mnemonic, pass);
            var    seed = new ArraySegment <byte>(bip.SeedBytes, 0, Ed25519.PrivateKeySeedSizeInBytes);

            Ed25519.KeyPairFromSeed(out publicKey, out privateKey, seed.ToArray());
            tz1  = CryptoServices.CreatePrefixedHash(HashType.PublicKeyHash, publicKey);
            edsk = CryptoServices.EncodePrefixed(HashType.Private, privateKey);
        }
Exemplo n.º 7
0
        private string ParseIdentityID()
        {
            HashType hashType;

            switch (TakeOne())
            {
            case 0:
                hashType = HashType.PublicKeyHash;
                break;

            case 1:
                hashType = HashType.PublicKeyHash2;
                break;

            case 2:
                hashType = HashType.PublicKeyHash3;
                break;

            default:
                throw new NotImplementedException();
            }

            return(CryptoServices.EncodePrefixed(hashType, Take(20)));
        }
Exemplo n.º 8
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);
            }