예제 #1
0
        public static Safe Load(string password, string walletFilePath)
        {
            if (!File.Exists(walletFilePath))
            {
                throw new ArgumentException($"No wallet file found at {walletFilePath}");
            }

            var walletFileRawContent = WalletFileSerializer.Deserialize(walletFilePath);

            var encryptedBitcoinPrivateKeyString = walletFileRawContent.EncryptedSeed;
            var chainCodeString = walletFileRawContent.ChainCode;

            var chainCode = Convert.FromBase64String(chainCodeString);

            Network network;
            var     networkString = walletFileRawContent.Network;

            network = networkString == Network.Main.ToString() ? Network.Main : Network.TestNet;

            DateTimeOffset creationTime = DateTimeOffset.ParseExact(walletFileRawContent.CreationTime, "yyyy-MM-dd", CultureInfo.InvariantCulture);

            var safe = new Safe(password, walletFilePath, network, creationTime);

            var privateKey = Key.Parse(encryptedBitcoinPrivateKeyString, password, safe.Network);
            var seedExtKey = new ExtKey(privateKey, chainCode);

            safe.SetSeed(seedExtKey);

            return(safe);
        }
예제 #2
0
        private void Save(string password, string walletFilePath, Network network, DateTimeOffset creationTime)
        {
            if (File.Exists(walletFilePath))
            {
                throw new NotSupportedException($"Wallet already exists at {walletFilePath}");
            }

            var directoryPath = Path.GetDirectoryName(Path.GetFullPath(walletFilePath));

            if (directoryPath != null)
            {
                Directory.CreateDirectory(directoryPath);
            }

            var privateKey = ExtKey.PrivateKey;
            var chainCode  = ExtKey.ChainCode;

            var encryptedBitcoinPrivateKeyString = privateKey.GetEncryptedBitcoinSecret(password, Network).ToWif();
            var chainCodeString = Convert.ToBase64String(chainCode);

            var networkString = network.ToString();

            var creationTimeString = creationTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

            WalletFileSerializer.Serialize(
                walletFilePath,
                encryptedBitcoinPrivateKeyString,
                chainCodeString,
                networkString,
                creationTimeString);
        }
예제 #3
0
        private void Save(string password, string walletFilePath, Network network)
        {
            if (File.Exists(walletFilePath))
            {
                throw new Exception("WalletFileAlreadyExists");
            }

            var directoryPath = Path.GetDirectoryName(Path.GetFullPath(walletFilePath));

            if (directoryPath != null)
            {
                Directory.CreateDirectory(directoryPath);
            }

            var privateKey = ExtKey.PrivateKey;
            var chainCode  = ExtKey.ChainCode;

            var encryptedBitcoinPrivateKeyString = privateKey.GetEncryptedBitcoinSecret(password, Network).ToWif();
            var chainCodeString = Convert.ToBase64String(chainCode);

            var networkString = network.ToString();

            WalletFileSerializer.Serialize(walletFilePath,
                                           encryptedBitcoinPrivateKeyString,
                                           chainCodeString,
                                           networkString);
        }
예제 #4
0
        public static Safe Load(string password, string walletFilePath)
        {
            if (!File.Exists(walletFilePath))
            {
                throw new Exception("WalletFileDoesNotExists");
            }

            var walletFileRawContent = WalletFileSerializer.Deserialize(walletFilePath);

            var encryptedBitcoinPrivateKeyString = walletFileRawContent.EncryptedSeed;
            var chainCodeString = walletFileRawContent.ChainCode;

            var chainCode = Convert.FromBase64String(chainCodeString);

            Network network;
            var     networkString = walletFileRawContent.Network;

            if (networkString == Network.Main.ToString())
            {
                network = Network.Main;
            }
            else if (networkString == Network.TestNet.ToString())
            {
                network = Network.TestNet;
            }
            else
            {
                throw new Exception("NotRecognizedNetworkInWalletFile");
            }

            var safe = new Safe(password, walletFilePath, network);

            var privateKey = Key.Parse(encryptedBitcoinPrivateKeyString, password, safe.Network);
            var seedExtKey = new ExtKey(privateKey, chainCode);

            safe.SetSeed(seedExtKey);

            return(safe);
        }