|
| 1 | +package org.tds.cmd; |
| 2 | + |
| 3 | +import com.beust.jcommander.Parameter; |
| 4 | +import com.beust.jcommander.Parameters; |
| 5 | +import lombok.SneakyThrows; |
| 6 | +import org.tdf.common.util.HexBytes; |
| 7 | +import org.tdf.sunflower.consensus.poa.PoAConstants; |
| 8 | +import org.tdf.sunflower.state.Address; |
| 9 | +import org.tdf.sunflower.types.CryptoContext; |
| 10 | +import org.tdf.sunflower.types.Transaction; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | + |
| 14 | +import static org.tds.cmd.Main.OBJECT_MAPPER; |
| 15 | + |
| 16 | +@Parameters(commandDescription = "compile and deploy contract to tds server") |
| 17 | +public class Deploy { |
| 18 | + @Parameter(names = {"--source", "-s"}, description = "wasm contract source file") |
| 19 | + private String source; |
| 20 | + @Parameter(names = {"--privateKey", "-k"}, description = "your tds private key") |
| 21 | + private String privateKey; |
| 22 | + @Parameter(names = {"--ascPath", "-a"}, description = "asc file path, often located in node_modules/.bin/") |
| 23 | + private String ascPath; |
| 24 | + @Parameter(names = {"--host", "-h"}, description = "tds server host") |
| 25 | + private String host; |
| 26 | + @Parameter(names = {"--port", "-p"}, description = "tds server port") |
| 27 | + private int port; |
| 28 | + |
| 29 | + @Parameter(names = {"--config", "-c"}, description = "deploy configuration file, overrided by command line options") |
| 30 | + private String config; |
| 31 | + |
| 32 | + private void setConfig(Config config) { |
| 33 | + if (this.source == null || this.source.isEmpty()) |
| 34 | + this.source = config.getSource(); |
| 35 | + |
| 36 | + if (this.privateKey == null || this.privateKey.isEmpty()) |
| 37 | + this.privateKey = config.getPrivateKey(); |
| 38 | + |
| 39 | + if (this.ascPath == null || this.ascPath.isEmpty()) |
| 40 | + this.ascPath = config.getAscPath(); |
| 41 | + |
| 42 | + if (this.host == null || this.host.isEmpty()) |
| 43 | + this.host = config.getHost(); |
| 44 | + |
| 45 | + if (this.port == 0) |
| 46 | + this.port = config.getPort(); |
| 47 | + } |
| 48 | + |
| 49 | + @SneakyThrows |
| 50 | + private void loadJsonConfig() { |
| 51 | + if (config != null && !config.isEmpty()) { |
| 52 | + File f = new File(config); |
| 53 | + if (f.exists() && !f.isDirectory()) { |
| 54 | + Config c = OBJECT_MAPPER.readValue(f, Config.class); |
| 55 | + setConfig(c); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @SneakyThrows |
| 61 | + public void run() { |
| 62 | + loadJsonConfig(); |
| 63 | + HexBytes publicKey = HexBytes.fromBytes(CryptoContext.getPkFromSk(HexBytes.decode(privateKey))); |
| 64 | + HttpUtil util = new HttpUtil(host, port); |
| 65 | + AscWrapper wrapper = new AscWrapper(ascPath); |
| 66 | + HexBytes address = Address.fromPublicKey(publicKey); |
| 67 | + Transaction tx = new Transaction( |
| 68 | + PoAConstants.TRANSACTION_VERSION, |
| 69 | + Transaction.Type.CONTRACT_DEPLOY.code, |
| 70 | + System.currentTimeMillis() / 1000, |
| 71 | + util.getLatestNonce(address) + 1, |
| 72 | + publicKey, |
| 73 | + 0, 0, |
| 74 | + HexBytes.EMPTY, |
| 75 | + HexBytes.EMPTY, |
| 76 | + HexBytes.EMPTY |
| 77 | + ); |
| 78 | + |
| 79 | + tx.setPayload(HexBytes.fromBytes(wrapper.compile(source))); |
| 80 | + byte[] sig = CryptoContext.sign(HexBytes.fromHex(privateKey).getBytes(), tx.getSignaturePlain()); |
| 81 | + tx.setSignature(HexBytes.fromBytes(sig)); |
| 82 | + System.out.println("deploy contract " + source + " address = " + tx.createContractAddress()); |
| 83 | + System.out.println("tds server response: \n" + util.sendTransaction(tx)); |
| 84 | + } |
| 85 | +} |
0 commit comments