Skip to content

Commit 8153a9a

Browse files
committed
add config.json
1 parent 20abd0b commit 8153a9a

File tree

5 files changed

+98
-27
lines changed

5 files changed

+98
-27
lines changed

cmd/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.gradle
22
.idea
33
build
4+
local
5+
.DS_Store

cmd/build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,20 @@ dependencies {
4848
compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
4949

5050
}
51+
52+
53+
task copyDependencies(type: Copy) {
54+
from configurations.runtime
55+
destinationDir = file('build/libs/lib')
56+
}
57+
58+
jar {
59+
manifest {
60+
attributes 'Main-Class': 'org.tds.cmd.Main'
61+
attributes 'Class-Path': '. ' + configurations.compile.collect { 'lib/' + it.getName() }.join(' ')
62+
}
63+
64+
from {
65+
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
66+
} {exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'}
67+
}

cmd/cmd-config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"version": "",
3-
"entry-point": "",
3+
"host": "",
4+
"port": "",
45
"source": "assembly/index.ts",
56
"private-key": "",
67
"asc-path": "node_modules/.bin/asc"
7-
}
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.tds.cmd;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Data;
5+
6+
@Data
7+
public class Config {
8+
private long version;
9+
private String host;
10+
private int port;
11+
private String source;
12+
13+
@JsonProperty("private-key")
14+
private String privateKey;
15+
16+
@JsonProperty("asc-path")
17+
private String ascPath;
18+
}

cmd/src/main/java/org/tds/cmd/Main.java

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22

33
import com.beust.jcommander.JCommander;
44
import 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;
514
import org.apache.http.entity.ContentType;
615
import org.apache.http.entity.StringEntity;
16+
import org.apache.http.impl.client.HttpClientBuilder;
17+
import org.apache.http.util.EntityUtils;
718
import org.apache.tika.io.IOUtils;
819
import org.tdf.common.util.HexBytes;
920
import org.tdf.crypto.CryptoHelpers;
@@ -16,33 +27,46 @@
1627
import org.tdf.sunflower.state.Address;
1728
import org.tdf.sunflower.types.CryptoContext;
1829
import 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;
2832
import java.io.IOException;
2933
import java.io.InputStream;
3034

31-
import com.fasterxml.jackson.databind.ObjectMapper;
32-
import com.fasterxml.jackson.databind.JsonNode;
33-
3435

3536
public 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

Comments
 (0)