Beispiel #1
0
 private void AddAccount(BRC6Account account, bool is_import)
 {
     lock (accounts)
     {
         if (accounts.TryGetValue(account.ScriptHash, out BRC6Account account_old))
         {
             account.Label     = account_old.Label;
             account.IsDefault = account_old.IsDefault;
             account.Lock      = account_old.Lock;
             if (account.Contract == null)
             {
                 account.Contract = account_old.Contract;
             }
             else
             {
                 BRC6Contract contract_old = (BRC6Contract)account_old.Contract;
                 if (contract_old != null)
                 {
                     BRC6Contract contract = (BRC6Contract)account.Contract;
                     contract.ParameterNames = contract_old.ParameterNames;
                     contract.Deployed       = contract_old.Deployed;
                 }
             }
             account.Extra = account_old.Extra;
         }
         else
         {
             indexer.RegisterAccounts(new[] { account.ScriptHash }, is_import ? 0 : Blockchain.Singleton.Height);
         }
         accounts[account.ScriptHash] = account;
     }
 }
Beispiel #2
0
 public BRC6Wallet(WalletIndexer indexer, string path, string name = null)
 {
     this.indexer = indexer;
     this.path    = path;
     if (File.Exists(path))
     {
         JObject wallet;
         using (StreamReader reader = new StreamReader(path))
         {
             wallet = JObject.Parse(reader);
         }
         this.name     = wallet["name"]?.AsString();
         this.version  = Version.Parse(wallet["version"].AsString());
         this.Scrypt   = ScryptParameters.FromJson(wallet["scrypt"]);
         this.accounts = ((JArray)wallet["accounts"]).Select(p => BRC6Account.FromJson(p, this)).ToDictionary(p => p.ScriptHash);
         this.extra    = wallet["extra"];
         indexer.RegisterAccounts(accounts.Keys);
     }
     else
     {
         this.name     = name;
         this.version  = Version.Parse("1.0");
         this.Scrypt   = ScryptParameters.Default;
         this.accounts = new Dictionary <UInt160, BRC6Account>();
         this.extra    = JObject.Null;
     }
     indexer.WalletTransaction += WalletIndexer_WalletTransaction;
 }
Beispiel #3
0
 public override bool VerifyPassword(string password)
 {
     lock (accounts)
     {
         BRC6Account account = accounts.Values.FirstOrDefault(p => !p.Decrypted);
         if (account == null)
         {
             account = accounts.Values.FirstOrDefault(p => p.HasKey);
         }
         if (account == null)
         {
             return(true);
         }
         if (account.Decrypted)
         {
             return(account.VerifyPassword(password));
         }
         else
         {
             try
             {
                 account.GetKey(password);
                 return(true);
             }
             catch (FormatException)
             {
                 return(false);
             }
         }
     }
 }
Beispiel #4
0
        public override WalletAccount CreateAccount(UInt160 scriptHash)
        {
            BRC6Account account = new BRC6Account(this, scriptHash);

            AddAccount(account, true);
            return(account);
        }
Beispiel #5
0
        public override WalletAccount CreateAccount(Contract contract, KeyPair key = null)
        {
            BRC6Contract BRC6contract = contract as BRC6Contract;

            if (BRC6contract == null)
            {
                BRC6contract = new BRC6Contract
                {
                    Script         = contract.Script,
                    ParameterList  = contract.ParameterList,
                    ParameterNames = contract.ParameterList.Select((p, i) => $"parameter{i}").ToArray(),
                    Deployed       = false
                };
            }
            BRC6Account account;

            if (key == null)
            {
                account = new BRC6Account(this, BRC6contract.ScriptHash);
            }
            else
            {
                account = new BRC6Account(this, BRC6contract.ScriptHash, key, password);
            }
            account.Contract = BRC6contract;
            AddAccount(account, false);
            return(account);
        }
Beispiel #6
0
        public override WalletAccount Import(string wif)
        {
            KeyPair      key      = new KeyPair(GetPrivateKeyFromWIF(wif));
            BRC6Contract contract = new BRC6Contract
            {
                Script         = Contract.CreateSignatureRedeemScript(key.PublicKey),
                ParameterList  = new[] { ContractParameterType.Signature },
                ParameterNames = new[] { "signature" },
                Deployed       = false
            };
            BRC6Account account = new BRC6Account(this, contract.ScriptHash, key, password)
            {
                Contract = contract
            };

            AddAccount(account, true);
            return(account);
        }
Beispiel #7
0
        public override WalletAccount CreateAccount(byte[] privateKey)
        {
            KeyPair      key      = new KeyPair(privateKey);
            BRC6Contract contract = new BRC6Contract
            {
                Script         = Contract.CreateSignatureRedeemScript(key.PublicKey),
                ParameterList  = new[] { ContractParameterType.Signature },
                ParameterNames = new[] { "signature" },
                Deployed       = false
            };
            BRC6Account account = new BRC6Account(this, contract.ScriptHash, key, password)
            {
                Contract = contract
            };

            AddAccount(account, false);
            return(account);
        }
Beispiel #8
0
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="Oldpassword">老密码</param>
        /// <param name="Newpassword">新密码</param>
        /// <param name="wallet">修改密码的钱包</param>
        /// <returns></returns>
        public IDisposable ChangeBrc6WalletPassword(string Oldpassword, string Newpassword, BRC6Wallet wallet)
        {
            if (!VerifyPassword(Oldpassword))
            {
                throw new CryptographicException();
            }

            for (int i = 0; i < wallet.GetAccounts().ToArray().Length; i++)
            {
                byte[]      privatekey_1 = wallet.GetAccount(wallet.GetAccounts().ToArray()[i].Address.ToScriptHash()).GetKey().PrivateKey.ToHexString().HexToBytes();
                KeyPair     key          = new KeyPair(privatekey_1);
                BRC6Account account      = new BRC6Account(this, wallet.GetAccounts().ToArray()[i].Contract.ScriptHash, key, Newpassword)
                {
                    Contract = wallet.GetAccounts().ToArray()[i].Contract
                };
                accounts[wallet.GetAccounts().ToArray()[i].Address.ToScriptHash()] = account;
            }
            this.password = Newpassword;
            return(new WalletLocker(this));
        }
Beispiel #9
0
        public override WalletAccount Import(X509Certificate2 cert)
        {
            KeyPair key;

            using (ECDsa ecdsa = cert.GetECDsaPrivateKey())
            {
                key = new KeyPair(ecdsa.ExportParameters(true).D);
            }
            BRC6Contract contract = new BRC6Contract
            {
                Script         = Contract.CreateSignatureRedeemScript(key.PublicKey),
                ParameterList  = new[] { ContractParameterType.Signature },
                ParameterNames = new[] { "signature" },
                Deployed       = false
            };
            BRC6Account account = new BRC6Account(this, contract.ScriptHash, key, password)
            {
                Contract = contract
            };

            AddAccount(account, true);
            return(account);
        }
Beispiel #10
0
        public override WalletAccount Import(string brc2, string passphrase)
        {
            KeyPair      key      = new KeyPair(GetPrivateKeyFromBRC2(brc2, passphrase));
            BRC6Contract contract = new BRC6Contract
            {
                Script         = Contract.CreateSignatureRedeemScript(key.PublicKey),
                ParameterList  = new[] { ContractParameterType.Signature },
                ParameterNames = new[] { "signature" },
                Deployed       = false
            };
            BRC6Account account;

            if (Scrypt.N == 16384 && Scrypt.R == 8 && Scrypt.P == 8)
            {
                account = new BRC6Account(this, contract.ScriptHash, brc2);
            }
            else
            {
                account = new BRC6Account(this, contract.ScriptHash, key, passphrase);
            }
            account.Contract = contract;
            AddAccount(account, true);
            return(account);
        }