Exemplo n.º 1
0
        public void Signature(byte[] privatekey)
        {
            ECKey          ec_key    = ECKey.FromPrivateKey(privatekey);
            ECDSASignature signature = ec_key.Sign(GetRawHash().Hash);

            this.transaction.Signature.Add(ByteString.CopyFrom(signature.ToByteArray()));
        }
Exemplo n.º 2
0
        public void AddSignature(byte[] privatekey, AccountStore account_store)
        {
            Transaction.Types.Contract contract = this.transaction.RawData.Contract[0];

            byte[] owner         = GetOwner(contract);
            int    permission_id = contract.PermissionId;

            AccountCapsule account = account_store.Get(owner);

            if (account == null)
            {
                throw new PermissionException("Account is not exist.");
            }

            Permission permission = account.GetPermissionById(permission_id);

            if (permission == null)
            {
                throw new PermissionException("Permission is not exist");
            }

            if (permission_id != 0)
            {
                if (permission.Type != Permission.Types.PermissionType.Active)
                {
                    throw new PermissionException("Permission type is error");
                }
                if (Wallet.CheckPermissionOperations(permission, contract))
                {
                    throw new PermissionException("Invalid permission");
                }
            }

            List <ByteString> approves = new List <ByteString>();
            ECKey             ec_key   = ECKey.FromPrivateKey(privatekey);

            byte[] address = Wallet.PublickKeyToAddress(ec_key.PublicKey);

            if (this.transaction.Signature.Count > 0)
            {
                CheckWeight(permission, new List <ByteString>(this.transaction.Signature), this.GetRawHash().Hash, approves);
                if (approves.Contains(ByteString.CopyFrom(address)))
                {
                    throw new PermissionException(Wallet.AddressToBase58(address) + "had signed!");
                }
            }

            long weight = GetWeight(permission, address);

            if (weight == 0)
            {
                throw new PermissionException(
                          privatekey.ToHexString() + " address is " +
                          Wallet.AddressToBase58(address) + "but it is not contained of permission.");
            }

            ECDSASignature signature = ec_key.Sign(this.GetRawHash().Hash);

            this.transaction.Signature.Add(ByteString.CopyFrom(signature.ToByteArray()));
        }
Exemplo n.º 3
0
        public void IsValidPrivateKey()
        {
            ECKey key = ECKey.FromPrivateKey(privatekey);

            this.privatekey.SequenceEqual(key.PrivateKey).Should().BeTrue();
            this.publickey.SequenceEqual(key.PublicKey).Should().BeTrue();
        }
Exemplo n.º 4
0
        public void Init()
        {
            if (Args.Instance.LocalWitness.GetPrivateKey().IsNullOrEmpty())
            {
                return;
            }

            byte[] privatekey         = Args.Instance.LocalWitness.GetPrivateKey();
            byte[] witness_address    = Args.Instance.LocalWitness.GetWitnessAccountAddress();
            byte[] privatekey_address = Wallet.PublickKeyToAddress(ECKey.FromPrivateKey(privatekey).PublicKey);

            WitnessCapsule witness = Manager.Instance.DBManager.Witness.Get(witness_address);

            if (witness == null)
            {
                Logger.Warning(
                    string.Format("WitnessCapsule[{0}] is not in witnessStore",
                                  witness_address));

                witness = new WitnessCapsule(ByteString.CopyFrom(witness_address));
            }

            this.privatekeys.Add(witness.Address, privatekey);
            this.local_witness_states.Add(witness.Address, witness);
            this.privatekey_addresses.Add(privatekey, privatekey_address);
        }
Exemplo n.º 5
0
        public void Sign(byte[] privatekey)
        {
            ECKey          ec_key    = ECKey.FromPrivateKey(privatekey);
            ECDSASignature signature = ec_key.Sign(this.GetRawHash().Hash);

            this.block.BlockHeader.WitnessSignature = ByteString.CopyFrom(signature.ToByteArray());
        }
Exemplo n.º 6
0
 public void InitWitnessAccountAddress()
 {
     if (this.witness_account_address == null)
     {
         ECKey key = ECKey.FromPrivateKey(GetPrivateKey());
         this.witness_account_address = Wallet.PublickKeyToAddress(key.PublicKey);
     }
 }
Exemplo n.º 7
0
        public void Signature()
        {
            ECKey key = ECKey.FromPrivateKey(this.privatekey);

            ECDSASignature signature = key.Sign(signature_message);

            signature.Should().NotBeNull();

            key.Verify(signature_message, signature).Should().BeTrue();
        }
Exemplo n.º 8
0
        public void RecoverySignature()
        {
            ECKey key = ECKey.FromPrivateKey(this.privatekey);

            ECDSASignature signature = key.Sign(signature_message);

            signature.Should().NotBeNull();

            key.Verify(signature_message, signature).Should().BeTrue();
            ECKey.RecoverFromSignature(signature, signature_message, false).PublicKey.SequenceEqual(key.PublicKey).Should().BeTrue();
        }
Exemplo n.º 9
0
        public byte[] GetWitnessAccountAddress()
        {
            if (this.witness_account_address == null)
            {
                byte[] privatekey = GetPrivateKey();
                if (privatekey.IsNotNullOrEmpty())
                {
                    ECKey key = ECKey.FromPrivateKey(privatekey);
                    this.witness_account_address = Wallet.PublickKeyToAddress(key.PublicKey);
                }
            }

            return(this.witness_account_address);
        }
Exemplo n.º 10
0
        public static RpcApiResult ImportWallet(string password, string privatekey)
        {
            if (password.IsNullOrEmpty() || privatekey.IsNullOrEmpty())
            {
                Console.WriteLine("Invalide password and privatekey");
                return(new RpcApiResult(false, RpcMessage.INVALID_PASSWORD, "Invalide password and privatekey"));
            }

            try
            {
                byte[] pk = privatekey.HexToBytes();
                if (pk.Length != 32)
                {
                    return(new RpcApiResult(false, RpcMessage.INVALID_PRIVATEKEY, "Invalid privatekey. Privatekey must be 32 bytes."));
                }

                ECKey  key     = ECKey.FromPrivateKey(pk);
                string address = Wallet.AddressToBase58(Wallet.PublickKeyToAddress(key.PublicKey));

                if (!KeyStoreService.GenerateKeyStore(RpcApi.FILE_PATH,
                                                      password,
                                                      pk,
                                                      address,
                                                      out _))
                {
                    Console.WriteLine();
                    return(new RpcApiResult(false, RpcMessage.INTERNAL_ERROR, "Faild to generate keystore file."));
                }
            }
            catch (System.Exception e)
            {
                throw e;
            }

            return(RpcApiResult.Success);
        }
Exemplo n.º 11
0
 public void TestSetup()
 {
     this.key = ECKey.FromPrivateKey(privatekey);
 }