Esempio n. 1
0
        /// <summary>	Decrypts./ </summary>
        ///
        ///		auto secret = priv.get_shared_secret(pub);
        ///      auto nonce_plus_secret = fc::sha512::hash(fc::to_string(nonce) + secret.str());
        ///      auto plain_text = fc::aes_decrypt( nonce_plus_secret, message );
        ///      auto result = memo_message::deserialize(string(plain_text.begin(), plain_text.end()));
        ///      FC_ASSERT( result.checksum == uint32_t(digest_type::hash(result.text)._hash[0]) );
        ///      return result.text;
        ///
        /// <remarks>	Paul, 26/10/2015. </remarks>
        ///
        /// <param name="memo">				    The memo. </param>
        /// <param name="receiverPrivateKey">	The receiver private key. </param>
        /// <param name="senderPublicKey">      The sender public key. </param>
        ///
        /// <returns>	A string. </returns>
        static public string Decrypt(GrapheneMemo memo, KeyPair receiverKeyPair)
        {
            PublicKey pub = new BitsharesPubKey(memo.from).GetBitcoinPubKey();

            byte[] sharedSecret = GetSharedSecret(receiverKeyPair, pub);

            string ss = StringExtensions.ByteArrayToHexString(sharedSecret, true);

            string ps = memo.nonce.ToString() + StringExtensions.ByteArrayToHexString(Crypto.ComputeSha512(sharedSecret), true);

            byte[] seed = ASCIIEncoding.ASCII.GetBytes(ps);

            string hex = StringExtensions.ByteArrayToHexString(Crypto.ComputeSha512(seed), true);

            string key = hex.Substring(0, 64);
            string iv  = hex.Substring(64, 32);

            byte[] data = Util.HexStringToBytes(memo.message);

            int len = AES.AesDecrypt(data, Util.HexStringToBytes(key), Util.HexStringToBytes(iv));

            byte[] result = new byte[len - 4];
            Buffer.BlockCopy(data, 4, result, 0, result.Length);

            string message = UTF8Encoding.UTF8.GetString(result);

            return(message);
        }
Esempio n. 2
0
        /// <summary>	Bitshares account to bitcoin address. </summary>
        ///
        /// <remarks>	Paul, 15/01/2015. </remarks>
        ///
        /// <param name="account">	The account. </param>
        ///
        /// <returns>	A string. </returns>
        protected string BitsharesAccountToBitcoinAddress(BitsharesAccount account)
        {
            // turn that into a BTC address
            BitsharesPubKey pubKey = new BitsharesPubKey(account.active_key_history.Last().Values.Last());

            return(pubKey.ToBitcoinAddress(true, m_addressByteType));
        }
Esempio n. 3
0
        public void CheckBitcoinAddressFromBitsharesPublicKey()
        {
            for (int i = 0; i < m_btsPubKeys.Count; i++)
            {
                string btsPubKey      = m_btsPubKeys[i];
                string bitcoinAddress = m_bitcoinAddresses[i];

                BitsharesPubKey key = new BitsharesPubKey(btsPubKey);

                Assert.AreEqual(bitcoinAddress, key.ToBitcoinAddress(false, 0));
            }
        }
Esempio n. 4
0
        public void CheckBtsAddressFromBitcoinPubKeyHex()
        {
            for (int i = 0; i < m_bitcoinPrivKeys.Count; i++)
            {
                string bitcoinHex = m_bitcoinPubKeys[i];
                string btsAddress = m_btsAddresses[i];

                BitsharesPubKey key     = BitsharesPubKey.FromBitcoinHex(bitcoinHex);
                string          compare = key.m_Address;

                Assert.AreEqual(btsAddress, compare);
            }
        }
Esempio n. 5
0
        /// <summary>	Take the given transaction, pull out the first input and get the public key,
        ///             turn that into a bitshares address </summary>
        ///
        /// <remarks>	Paul, 22/12/2014. </remarks>
        ///
        /// <param name="t">	The TransactionSinceBlock to process. </param>
        ///
        /// <returns>	The bitshares address from bitcoin deposit. </returns>
        protected virtual AddressOrAccountName GetBitsharesAddressFromBitcoinDeposit(TransactionSinceBlock t)
        {
            IEnumerable <string> allPubKeys = GetAllPubkeysFromBitcoinTransaction(t.TxId);

            // this is probably ok to leave out because if the user imports their whole wallet, they will likely
            // have all the PKs they need since bitcoin pregenerates 100 or so of them. worst case the user
            // can re-import their wallet into bitshares to get access to missing transcations

            /*if (allPubKeys.Distinct().Count() > 1)
             * {
             *      // can't handle more than one sender case
             *      throw new MultiplePublicKeysException();
             * }*/

            string          publicKey = allPubKeys.First();
            BitsharesPubKey btsPk     = BitsharesPubKey.FromBitcoinHex(publicKey, m_addressByteType);

            return(new AddressOrAccountName {
                m_text = btsPk.m_Address, m_isAddress = true
            });
        }
Esempio n. 6
0
        /// <summary>	Executes the submit address action. </summary>
        ///
        /// <remarks>	Paul, 05/02/2015. </remarks>
        ///
        /// <exception cref="ApiExceptionMessage">	    Thrown when an API exception message error
        ///                                             condition occurs. </exception>
        /// <exception cref="UnexpectedCaseException">	Thrown when an Unexpected Case error condition
        ///                                             occurs. </exception>
        ///
        /// <param name="receivingAddress">	The receiving address. </param>
        /// <param name="orderType">	    Type of the order. </param>
        ///
        /// <returns>	A SubmitAddressResponse. </returns>
        public override SubmitAddressResponse OnSubmitAddress(string receivingAddress, MetaOrderType orderType, uint referralUser)
        {
            SubmitAddressResponse response;

            if (orderType == MetaOrderType.buy)
            {
                string accountName = receivingAddress;
                bool   isPublicKey = BitsharesPubKey.IsValidPublicKey(accountName);

                // check for theoretical validity

                if (!isPublicKey && !BitsharesWallet.IsValidAccountName(accountName))
                {
                    throw new ApiExceptionInvalidAccount(accountName);
                }

                // try and retrieve a previous entry
                SenderToDepositRow senderToDeposit = m_daemon.GetSenderDepositFromReceiver(accountName, m_market.symbol_pair, referralUser);
                if (senderToDeposit == null)
                {
                    // no dice, create a new entry

                    // check for actual validity
                    string rcA;

                    if (!isPublicKey)
                    {
                        BitsharesAccount account = m_bitshares.GetAccount(accountName);
                        if (account == null)
                        {
                            throw new ApiExceptionInvalidAccount(accountName);
                        }

                        rcA = account.name;
                    }
                    else
                    {
                        rcA = accountName;
                    }

                    // generate a new bitcoin address and tie it to this account
                    string depositAdress = m_bitcoin.GetNewAddress();
                    senderToDeposit = m_daemon.InsertSenderToDeposit(rcA, depositAdress, m_market.symbol_pair, referralUser);
                }

                response = new SubmitAddressResponse
                {
                    deposit_address   = senderToDeposit.deposit_address,
                    receiving_address = senderToDeposit.receiving_address
                };
            }
            else if (orderType == MetaOrderType.sell)
            {
                string bitcoinAddress = receivingAddress;

                // validate bitcoin address
                byte[] check = Util.Base58CheckToByteArray(bitcoinAddress);
                if (check == null)
                {
                    throw new ApiExceptionInvalidAddress(bitcoinAddress);
                }

                // try and retrieve a previous entry
                SenderToDepositRow senderToDeposit = m_daemon.GetSenderDepositFromReceiver(bitcoinAddress, m_market.symbol_pair, referralUser);
                if (senderToDeposit == null)
                {
                    // generate a memo field to use instead
                    senderToDeposit = m_daemon.InsertSenderToDeposit(bitcoinAddress, MarketBase.CreateMemo(bitcoinAddress, m_market.symbol_pair, referralUser), m_market.symbol_pair, referralUser);
                }

                response = new SubmitAddressResponse
                {
                    deposit_address   = m_bitsharesAccount,
                    receiving_address = senderToDeposit.receiving_address,
                    memo = senderToDeposit.deposit_address
                };
            }
            else
            {
                throw new UnexpectedCaseException();
            }

            return(response);
        }