Beispiel #1
0
        public string ToJson()
        {
            var wallet = new WalletJson
            {
                Version       = this.version,
                MasterKey     = this.masterKey,
                Iv            = this.ivHex,
                SeedEncrypted = this.seedEncrypted,
                Address       = this.Address,
            };

            if (this.scryptParams != null)
            {
                wallet.Scrypt = new ScryptParams
                {
                    Salt = this.scryptParams.Salt,
                    N    = this.scryptParams.N,
                    R    = this.scryptParams.R,
                    P    = this.scryptParams.P,
                };
            }

            var result = JsonSerializer.ToJsonString(wallet);

            return(result);
        }
Beispiel #2
0
        public static Wallet Decrypt(WalletJson wallet, WalletOptions options)
        {
            options.Iv        = wallet.Iv;
            options.MasterKey = Aes.Decrypt(wallet.MasterKey, options.PasswordKey, options.Iv.FromHexString());
            options.SeedHex   = Aes.Decrypt(wallet.SeedEncrypted, options.MasterKey, options.Iv.FromHexString());
            options.PasswordKeys.Add(wallet.Version.Value, options.PasswordKey);

            switch (wallet.Version)
            {
            case 2:

                options.Scrypt = new ScryptParams
                {
                    Salt = wallet.Scrypt.Salt,
                    N    = wallet.Scrypt.N,
                    P    = wallet.Scrypt.P,
                    R    = wallet.Scrypt.R
                };

                break;

            default:
                break;
            }

            var account = new Account(options.SeedHex);

            if (account.Address != wallet.Address)
            {
                throw new WrongPasswordException();
            }

            return(new Wallet(options));
        }
Beispiel #3
0
        static public void Import(string privatekey, string walletFile = "./Data/walletImport.json")
        {
            var walletKey = new WalletKey();

            walletKey.random = privatekey.HexToBytes();
            ed25519.ed25519_create_keypair(walletKey.publickey, walletKey.privatekey, walletKey.random);

            var keys = new List <WalletKey>();

            keys.Add(walletKey);

            var walletJson = new WalletJson();

            walletJson.curIndex = 0;
            walletJson.accounts = new List <WalletJsonAddress>();

            var aes256 = new AesEverywhere.AES256();

            for (int i = 0; i < keys.Count; i++)
            {
                var walletJsonAddress = new WalletJsonAddress();

                walletJsonAddress.address   = keys[i].ToAddress();
                walletJsonAddress.encrypted = aes256.Encrypt(keys[i].random.ToHexString(), "smartx123");

                walletJson.accounts.Add(walletJsonAddress);
            }

            File.WriteAllText(walletFile, JsonHelper.ToJson(walletJson), System.Text.Encoding.UTF8);
        }
Beispiel #4
0
        public WalletOptions AssignFrom(WalletJson wallet)
        {
            this.Iv        = wallet.Iv;
            this.MasterKey = wallet.MasterKey;
            this.Version   = wallet.Version;
            this.Scrypt    = wallet.Scrypt;

            return(this);
        }
Beispiel #5
0
 public static WalletOptions FromWalletJson(WalletJson wallet)
 {
     return(new WalletOptions
     {
         Iv = wallet.Iv,
         MasterKey = wallet.MasterKey,
         Version = wallet.Version,
         Scrypt = wallet.Scrypt,
     });
 }
Beispiel #6
0
        public static Wallet FromJson(string json, WalletOptions options = null)
        {
            options = options ?? new WalletOptions();

            var wallet = WalletJson.FromJson(json);

            options.AssignFrom(wallet);

            var computeOptions = WalletOptions.FromWalletJson(wallet);

            computeOptions.Password = options.Password;

            var passwordKey = Wallet.ComputePasswordKey(computeOptions);

            options.PasswordKey = passwordKey;

            return(Wallet.Decrypt(wallet, options));
        }
Beispiel #7
0
        public void SaveWallet()
        {
            var walletJson = new WalletJson();

            walletJson.curIndex = curIndex;
            walletJson.accounts = new List <WalletJsonAddress>();

            var aes256 = new AesEverywhere.AES256();

            for (int i = 0; i < keys.Count; i++)
            {
                var walletJsonAddress = new WalletJsonAddress();

                walletJsonAddress.address   = keys[i].ToAddress();
                walletJsonAddress.encrypted = aes256.Encrypt(keys[i].random.ToHexString(), passwords);

                walletJson.accounts.Add(walletJsonAddress);
            }

            File.WriteAllText(walletFile, JsonHelper.ToJson(walletJson), System.Text.Encoding.UTF8);
        }