]> Git Repo - VerusCoin.git/blob - src/zerocash/Address.cpp
1209df38c6205ac30a1ca85c88acb5b97a211856
[VerusCoin.git] / src / zerocash / Address.cpp
1 /** @file
2  *****************************************************************************
3
4  Implementation of interfaces for the classes Address and PublicAddress.
5
6  See Address.h .
7
8  *****************************************************************************
9  * @author     This file is part of libzerocash, developed by the Zerocash
10  *             project and contributors (see AUTHORS).
11  * @copyright  MIT license (see LICENSE file)
12  *****************************************************************************/
13
14 #include <cryptopp/osrng.h>
15 using CryptoPP::AutoSeededRandomPool;
16
17 #include <cryptopp/eccrypto.h>
18 using CryptoPP::ECP;
19 using CryptoPP::ECIES;
20
21 #include <cryptopp/oids.h>
22 namespace ASN1 = CryptoPP::ASN1;
23
24 #include <cryptopp/filters.h>
25 using CryptoPP::StringSink;
26 using CryptoPP::StringStore;
27
28 #include "Zerocash.h"
29 #include "Address.h"
30
31 namespace libzerocash {
32
33 PrivateAddress::PrivateAddress(const std::vector<unsigned char> a_sk, const std::string sk_enc) {
34     this->a_sk = a_sk;
35     this->sk_enc = sk_enc;
36 }
37
38 PrivateAddress::PrivateAddress() {
39
40 }
41
42 bool PrivateAddress::operator==(const PrivateAddress& rhs) const {
43         return ((this->a_sk == rhs.a_sk) && (this->sk_enc == rhs.sk_enc));
44 }
45
46 bool PrivateAddress::operator!=(const PrivateAddress& rhs) const {
47         return !(*this == rhs);
48 }
49
50 const std::string PrivateAddress::getEncryptionSecretKey() const {
51     return this->sk_enc;
52 }
53
54 const std::vector<unsigned char>& PrivateAddress::getAddressSecret() const {
55     return this->a_sk;
56 }
57
58 PublicAddress::PublicAddress(): a_pk(ZC_A_PK_SIZE) {
59     this->pk_enc = "";
60 }
61
62 PublicAddress::PublicAddress(const std::vector<unsigned char>& a_pk, std::string& pk_enc) : a_pk(a_pk), pk_enc(pk_enc) {}
63
64 PublicAddress::PublicAddress(const PrivateAddress& addr_sk): a_pk(ZC_A_PK_SIZE) {
65     std::vector<bool> a_sk_bool(ZC_A_SK_SIZE * 8);
66     convertBytesVectorToVector(addr_sk.getAddressSecret(), a_sk_bool);
67
68     std::vector<bool> zeros_256(256, 0);
69
70     std::vector<bool> a_pk_internal;
71     concatenateVectors(a_sk_bool, zeros_256, a_pk_internal);
72
73     std::vector<bool> a_pk_bool(ZC_A_PK_SIZE * 8);
74     hashVector(a_pk_internal, a_pk_bool);
75
76     convertVectorToBytesVector(a_pk_bool, this->a_pk);
77
78     ECIES<ECP>::PublicKey publicKey;
79
80     ECIES<ECP>::PrivateKey decodedPrivateKey;
81     decodedPrivateKey.Load(StringStore(addr_sk.getEncryptionSecretKey()).Ref());
82
83     decodedPrivateKey.MakePublicKey(publicKey);
84
85     std::string encodedPublicKey;
86     publicKey.Save(StringSink(encodedPublicKey).Ref());
87
88     this->pk_enc = encodedPublicKey;
89 }
90
91 const std::string PublicAddress::getEncryptionPublicKey() const {
92     return this->pk_enc;
93 }
94
95 const std::vector<unsigned char>& PublicAddress::getPublicAddressSecret() const {
96     return this->a_pk;
97 }
98
99 bool PublicAddress::operator==(const PublicAddress& rhs) const {
100         return ((this->a_pk == rhs.a_pk) && (this->pk_enc == rhs.pk_enc));
101 }
102
103 bool PublicAddress::operator!=(const PublicAddress& rhs) const {
104         return !(*this == rhs);
105 }
106
107 Address::Address(PrivateAddress& priv) : addr_pk(priv), addr_sk(priv) {
108
109 }
110
111 Address::Address() : addr_pk(), addr_sk() {
112
113 }
114
115 const PublicAddress& Address::getPublicAddress() const {
116         return this->addr_pk;
117 }
118
119 const PrivateAddress& Address::getPrivateAddress() const {
120     return this->addr_sk;
121 }
122
123 bool Address::operator==(const Address& rhs) const {
124         return ((this->addr_sk == rhs.addr_sk) && (this->addr_pk == rhs.addr_pk));
125 }
126
127 bool Address::operator!=(const Address& rhs) const {
128         return !(*this == rhs);
129 }
130
131 Address Address::CreateNewRandomAddress() {
132     std::vector<unsigned char> a_sk(ZC_A_SK_SIZE);
133
134     unsigned char a_sk_bytes[ZC_A_SK_SIZE];
135     getRandBytes(a_sk_bytes, ZC_A_SK_SIZE);
136     convertBytesToBytesVector(a_sk_bytes, a_sk);
137
138     AutoSeededRandomPool prng;
139
140     ECIES<ECP>::PrivateKey privateKey;
141     privateKey.Initialize(prng, ASN1::secp256r1());
142
143     std::string encodedPrivateKey;
144
145     privateKey.Save(StringSink(encodedPrivateKey).Ref());
146
147     PrivateAddress addr_sk(a_sk, encodedPrivateKey);
148     return Address(addr_sk);
149 }
150
151 } /* namespace libzerocash */
This page took 0.022849 seconds and 2 git commands to generate.