Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }