22
33import com .beust .jcommander .JCommander ;
44import com .beust .jcommander .Parameter ;
5+ import com .fasterxml .jackson .databind .JsonNode ;
6+ import com .fasterxml .jackson .databind .ObjectMapper ;
7+ import org .apache .commons .codec .Charsets ;
8+ import org .apache .http .HttpEntity ;
9+ import org .apache .http .HttpResponse ;
10+ import org .apache .http .HttpStatus ;
11+ import org .apache .http .client .HttpClient ;
12+ import org .apache .http .client .methods .HttpGet ;
13+ import org .apache .http .client .methods .HttpPost ;
514import org .apache .http .entity .ContentType ;
615import org .apache .http .entity .StringEntity ;
16+ import org .apache .http .impl .client .HttpClientBuilder ;
17+ import org .apache .http .util .EntityUtils ;
718import org .apache .tika .io .IOUtils ;
819import org .tdf .common .util .HexBytes ;
920import org .tdf .crypto .CryptoHelpers ;
1627import org .tdf .sunflower .state .Address ;
1728import org .tdf .sunflower .types .CryptoContext ;
1829import org .tdf .sunflower .types .Transaction ;
19- import org .apache .http .client .HttpClient ;
20- import org .apache .http .client .methods .HttpGet ;
21- import org .apache .http .client .methods .HttpPost ;
22- import org .apache .http .impl .client .HttpClientBuilder ;
23- import org .apache .commons .codec .Charsets ;
24- import org .apache .http .HttpEntity ;
25- import org .apache .http .HttpResponse ;
26- import org .apache .http .HttpStatus ;
27- import org .apache .http .util .EntityUtils ;
30+
31+ import java .io .File ;
2832import java .io .IOException ;
2933import java .io .InputStream ;
3034
31- import com .fasterxml .jackson .databind .ObjectMapper ;
32- import com .fasterxml .jackson .databind .JsonNode ;
33-
3435
3536public class Main {
36- @ Parameter (names ={"--source" , "-s" })
37+ static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
38+
39+ @ Parameter (names = {"--source" , "-s" })
3740 private String source ;
38- @ Parameter (names = {"--privateKey" , "-p " })
41+ @ Parameter (names = {"--privateKey" , "-k " })
3942 private String privateKey ;
40- @ Parameter (names = {"--ascPath" , "-a" })
43+ @ Parameter (names = {"--ascPath" , "-a" })
4144 private String ascPath ;
42- @ Parameter (names = "- host" )
45+ @ Parameter (names = { "-- host", "-h" } )
4346 private String host ;
44- @ Parameter (names = "-port" )
45- private String port ;
47+ @ Parameter (names = {"--port" , "-p" })
48+ private int port ;
49+
50+ @ Parameter (names = {"--config" , "-c" })
51+ private String config ;
52+
53+
54+ public void setConfig (Config config ) {
55+ if (this .source == null || this .source .isEmpty ())
56+ this .source = config .getSource ();
57+
58+ if (this .privateKey == null || this .privateKey .isEmpty ())
59+ this .privateKey = config .getPrivateKey ();
60+
61+ if (this .ascPath == null || this .ascPath .isEmpty ())
62+ this .ascPath = config .getAscPath ();
63+
64+ if (this .host == null || this .host .isEmpty ())
65+ this .host = config .getHost ();
66+
67+ if (this .port == 0 )
68+ this .port = config .getPort ();
69+ }
4670
4771 public static void initCryptoContext () {
4872 CryptoContext .setSignatureVerifier ((pk , msg , sig ) -> new SM2PublicKey (pk ).verify (msg , sig ));
@@ -56,28 +80,37 @@ public static void initCryptoContext() {
5680 CryptoContext .setHashFunction (SM3Util ::hash );
5781 }
5882
59- public static void main (String ... args ) throws IOException {
83+ public static void main (String ... args ) throws IOException {
84+ System .out .println (PoAConstants .TRANSACTION_VERSION );
6085 initCryptoContext ();
6186 Main m = new Main ();
6287 JCommander .newBuilder ()
6388 .addObject (m )
6489 .build ()
6590 .parse (args );
91+
92+ if (m .config != null && !m .config .isEmpty ()){
93+ File f = new File (m .config );
94+ if (f .exists () && !f .isDirectory ()){
95+ Config c = OBJECT_MAPPER .readValue (f , Config .class );
96+ m .setConfig (c );
97+ }
98+ }
6699 m .run ();
67100 }
68101
69102 public void run () throws IOException {
70- ObjectMapper objectMapper = new ObjectMapper ( );
71- HexBytes publicKey = HexBytes . fromBytes ( CryptoContext . getPkFromSk ( HexBytes . decode ( privateKey ))); ;
103+ HexBytes publicKey = HexBytes . fromBytes ( CryptoContext . getPkFromSk ( HexBytes . decode ( privateKey )) );
104+ ;
72105 HexBytes address = Address .fromPublicKey (publicKey );
73106 long nonce = 0L ;
74107 String getUrl = "http://" + host + ":" + port + "/rpc/account/" + address .toHex ();
75108 HttpClient client = HttpClientBuilder .create ().build ();
76109 HttpGet get = new HttpGet (getUrl );
77110 HttpResponse getResponse = client .execute (get );
78111 if (getResponse .getStatusLine ().getStatusCode () == HttpStatus .SC_OK ) {
79- String data = EntityUtils .toString (getResponse .getEntity (),Charsets .UTF_8 );
80- JsonNode n = objectMapper .readValue (data , JsonNode .class );
112+ String data = EntityUtils .toString (getResponse .getEntity (), Charsets .UTF_8 );
113+ JsonNode n = OBJECT_MAPPER .readValue (data , JsonNode .class );
81114 nonce = n .get ("data" ).get ("nonce" ).asLong () + 1 ;
82115 }
83116 Transaction tx = new Transaction (
@@ -91,7 +124,7 @@ public void run() throws IOException {
91124 HexBytes .EMPTY ,
92125 HexBytes .EMPTY
93126 );
94- String cmd = ascPath + " " + source + " --optimize -b" ;
127+ String cmd = ascPath + " " + source + " --optimize -b" ;
95128 Process p = Runtime .getRuntime ().exec (cmd );
96129 InputStream in = p .getInputStream ();
97130 byte [] payload = IOUtils .toByteArray (in );
@@ -101,7 +134,7 @@ public void run() throws IOException {
101134 System .out .println ("deploy contract " + source + " address = " + tx .createContractAddress ());
102135 String postUrl = "http://" + host + ":" + port + "/rpc/transaction" ;
103136 HttpPost post = new HttpPost (postUrl );
104- HttpEntity entity = new StringEntity (objectMapper .writeValueAsString (tx ), ContentType .APPLICATION_JSON );
137+ HttpEntity entity = new StringEntity (OBJECT_MAPPER .writeValueAsString (tx ), ContentType .APPLICATION_JSON );
105138 post .setEntity (entity );
106139 HttpResponse postResponse = client .execute (post );
107140 if (postResponse .getStatusLine ().getStatusCode () == 200 ) {
0 commit comments