Exemplo n.º 1
0
        public Account ImportAccount(string privateKeyText)
        {
            var dac = new AccountDac();

            byte[] privateKey = Base16.Decode(privateKeyText);
            byte[] publicKey;
            using (var dsa = ECDsa.ImportPrivateKey(privateKey))
            {
                publicKey = dsa.PublicKey;
            }

            var     id      = AccountIdHelper.CreateAccountAddress(publicKey);
            Account account = dac.SelectById(id);

            if (account == null)
            {
                account             = new Account();
                account.Id          = AccountIdHelper.CreateAccountAddress(publicKey);
                account.PrivateKey  = Base16.Encode(privateKey);
                account.PublicKey   = Base16.Encode(publicKey);
                account.Balance     = 0;
                account.IsDefault   = false;
                account.WatchedOnly = false;

                dac.Insert(account);
                UtxoSet.Instance.AddAccountId(account.Id);
            }

            return(account);
        }
Exemplo n.º 2
0
        public Account ImportObservedAccount(string publicKeyText)
        {
            var dac = new AccountDac();

            var publicKey = Base16.Decode(publicKeyText);
            var id        = AccountIdHelper.CreateAccountAddress(publicKey);

            Account account = dac.SelectById(id);

            if (account == null)
            {
                account             = new Account();
                account.Id          = AccountIdHelper.CreateAccountAddress(publicKey);
                account.PrivateKey  = null;
                account.PublicKey   = Base16.Encode(publicKey);
                account.Balance     = 0;
                account.IsDefault   = false;
                account.WatchedOnly = true;

                dac.Insert(account);
                UtxoSet.Instance.AddAccountId(account.Id);
            }

            return(account);
        }
Exemplo n.º 3
0
        public Account ImportPublicKeyAndAddress(string filePath, string salt)
        {
            Account result = null;

            string extensionName = Path.GetExtension(filePath).ToLower();

            if (extensionName != encryptExtensionName && extensionName != noEncryptExtensionName)
            {
                throw new CommonException(ErrorCode.Engine.Wallet.IO.EXTENSION_NAME_NOT_SUPPORT);
            }
            WatchAccountBackup backup = null;

            try
            {
                if (extensionName == noEncryptExtensionName)
                {
                    backup = LoadFile <WatchAccountBackup>(filePath, null);
                }
                else
                {
                    backup = LoadFile <WatchAccountBackup>(filePath, salt);
                }
                if (backup != null)
                {
                    AccountDac dac = AccountDac.Default;
                    dac.Insert(new Account {
                        Balance = 0, Id = backup.Address, IsDefault = false, PrivateKey = null, PublicKey = backup.PublicKey, Tag = "", Timestamp = Time.EpochTime, WatchedOnly = true
                    });
                    result = dac.SelectById(backup.Address);
                }
            }
            catch (Exception ex)
            {
                throw new CommonException(ErrorCode.Engine.Wallet.DB.EXECUTE_SQL_ERROR, ex);
            }
            return(result);
        }
Exemplo n.º 4
0
        public Account GenerateNewAccount()
        {
            var dac = new AccountDac();

            byte[] privateKey;
            byte[] publicKey;
            using (var dsa = ECDsa.GenerateNewKeyPair())
            {
                privateKey = dsa.PrivateKey;
                publicKey  = dsa.PublicKey;
            }

            var id = AccountIdHelper.CreateAccountAddress(publicKey);

            if (dac.IsExisted(id))
            {
                throw new Exception("Account id is existed");
            }

            Account account = new Account();

            account.Id          = id;
            account.PrivateKey  = Base16.Encode(privateKey);
            account.PublicKey   = Base16.Encode(publicKey);
            account.Balance     = 0;
            account.IsDefault   = false;
            account.WatchedOnly = false;

            dac.Insert(account);

            if (UtxoSet.Instance != null)
            {
                UtxoSet.Instance.AddAccountId(account.Id);
            }

            return(account);
        }