66import java .util .Arrays ;
77import java .util .Collections ;
88
9+ import conflux .web3j .types .*;
910import org .web3j .abi .FunctionEncoder ;
1011import org .web3j .abi .datatypes .Function ;
1112import org .web3j .abi .datatypes .Type ;
1718
1819import conflux .web3j .request .Call ;
1920import conflux .web3j .response .UsedGasAndCollateral ;
20- import conflux .web3j .types .AddressType ;
21- import conflux .web3j .types .RawTransaction ;
22- import conflux .web3j .types .SendTransactionError ;
23- import conflux .web3j .types .SendTransactionResult ;
24- import conflux .web3j .types .TransactionBuilder ;
2521
2622public class Account {
2723
2824 private Cfx cfx ;
29- private String address ;
25+ private Address address ;
3026 private BigInteger nonce ;
3127
3228 private AccountManager am ;
3329 private ECKeyPair ecKeyPair ;
3430
35- private Account (Cfx cfx , String address ) {
31+ private Account (Cfx cfx , Address address ) {
3632 this .cfx = cfx ;
3733 this .address = address ;
38- this .nonce = cfx .getNonce (address ).sendAndGet ();
34+ this .nonce = cfx .getNonce (this . address ).sendAndGet ();
3935 }
4036
41- public static Account unlock (Cfx cfx , AccountManager am , String address , String password ) throws Exception {
37+ public static Account unlock (Cfx cfx , AccountManager am , Address address , String password ) throws Exception {
4238 return unlock (cfx , am , address , password , Duration .ZERO );
4339 }
4440
45- public static Account unlock (Cfx cfx , AccountManager am , String address , String password , Duration unlockTimeout ) throws Exception {
41+ public static Account unlock (Cfx cfx , AccountManager am , Address address , String password , Duration unlockTimeout ) throws Exception {
4642 if (!am .unlock (address , password , unlockTimeout )) {
4743 throw new Exception ("account not found in keystore" );
4844 }
4945
5046 Account account = new Account (cfx , address );
5147 account .am = am ;
52-
5348 return account ;
5449 }
5550
56- public static Account create (Cfx cfx , String privateKey ) {
51+ public static Account create (Cfx cfx , String privateKey ) throws AddressException {
5752 Credentials credentials = Credentials .create (privateKey );
58-
59- Account account = new Account (cfx , AddressType .User .normalize (credentials .getAddress ()));
53+ String hexAddress = AddressType .User .normalize (credentials .getAddress ());
54+ Address address = new Address (hexAddress , cfx .getIntNetworkId ());
55+ Account account = new Account (cfx , address );
6056 account .ecKeyPair = credentials .getEcKeyPair ();
61-
6257 return account ;
6358 }
6459
@@ -67,9 +62,13 @@ public Cfx getCfx() {
6762 return cfx ;
6863 }
6964
70- public String getAddress () {
65+ public Address getAddress () {
7166 return address ;
7267 }
68+
69+ public String getHexAddress () {
70+ return this .address .getHexAddress ();
71+ }
7372
7473 public BigInteger getNonce () {
7574 return nonce ;
@@ -81,7 +80,7 @@ public void setNonce(BigInteger nonce) {
8180
8281 public String sign (RawTransaction tx ) throws Exception {
8382 return this .ecKeyPair == null
84- ? this .am .signTransaction (tx , this .address )
83+ ? this .am .signTransaction (tx , this .getAddress () )
8584 : tx .sign (this .ecKeyPair );
8685 }
8786
@@ -127,11 +126,11 @@ public SendTransactionResult send(RawTransaction tx) throws Exception {
127126 return this .send (signedTx );
128127 }
129128
130- public String transfer (String to , BigInteger value ) throws Exception {
129+ public String transfer (Address to , BigInteger value ) throws Exception {
131130 return this .transfer (new Option (), to , value );
132131 }
133132
134- public String transfer (Option option , String to , BigInteger value ) throws Exception {
133+ public String transfer (Option option , Address to , BigInteger value ) throws Exception {
135134 option .apply (this .cfx );
136135 RawTransaction tx = RawTransaction .transfer (this .nonce , to , value , option .epochHeight );
137136 option .updatePriceAndChainId (tx );
@@ -144,17 +143,17 @@ public String deploy(String bytecodes) throws Exception {
144143 }
145144
146145 public String deploy (Option option , String bytecodes ) throws Exception {
147- option .apply (this .cfx , this .address , "" , bytecodes );
146+ option .apply (this .cfx , this .getAddress (), null , bytecodes );
148147 RawTransaction tx = RawTransaction .deploy (this .nonce , option .gasLimit , option .value , option .storageLimit , option .epochHeight , bytecodes );
149148 option .updatePriceAndChainId (tx );
150149 return this .mustSend (tx );
151150 }
152151
153- public String call (String contract , String method , Type <?>... inputs ) throws Exception {
152+ public String call (Address contract , String method , Type <?>... inputs ) throws Exception {
154153 return this .call (new Option (), contract , method , inputs );
155154 }
156155
157- public String call (Option option , String contract , String method , Type <?>... inputs ) throws Exception {
156+ public String call (Option option , Address contract , String method , Type <?>... inputs ) throws Exception {
158157 String data = "" ;
159158
160159 if (!Strings .isEmpty (method )) {
@@ -165,12 +164,12 @@ public String call(Option option, String contract, String method, Type<?>... inp
165164 return this .call (option , contract , data );
166165 }
167166
168- public String call (String contract , String data ) throws Exception {
167+ public String call (Address contract , String data ) throws Exception {
169168 return this .call (new Option (), contract , data );
170169 }
171170
172- public String call (Option option , String contract , String data ) throws Exception {
173- option .apply (this .cfx , this .address , contract , data );
171+ public String call (Option option , Address contract , String data ) throws Exception {
172+ option .apply (this .cfx , this .getAddress () , contract , data );
174173 RawTransaction tx = RawTransaction .create (this .nonce , option .gasLimit , contract , option .value , option .storageLimit , option .epochHeight , data );
175174 option .updatePriceAndChainId (tx );
176175 return this .mustSend (tx );
@@ -260,7 +259,7 @@ private void apply(Cfx cfx) {
260259 }
261260 }
262261
263- private void apply (Cfx cfx , String from , String to , String data ) {
262+ private void apply (Cfx cfx , Address from , Address to , String data ) {
264263 if (this .epochHeight == null ) {
265264 this .epochHeight = cfx .getEpochNumber ().sendAndGet ();
266265 }
@@ -271,11 +270,11 @@ private void apply(Cfx cfx, String from, String to, String data) {
271270
272271 Call call = new Call ();
273272
274- if (! Strings . isEmpty ( from ) ) {
273+ if (from != null ) {
275274 call .setFrom (from );
276275 }
277276
278- if (! Strings . isEmpty ( to ) ) {
277+ if (to != null ) {
279278 call .setTo (to );
280279 }
281280
0 commit comments