diff --git a/src/main/base/org/bdware/sc/ContractGenerator.txt b/src/main/base/org/bdware/sc/ContractGenerator.txt new file mode 100644 index 0000000..1ae4d8e --- /dev/null +++ b/src/main/base/org/bdware/sc/ContractGenerator.txt @@ -0,0 +1,61 @@ +package org.bdware.sc; + +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.runtime.RuntimeConstants; +import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; +import org.bdware.sc.util.JsonUtil; + +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +public class ContractGenerator { + static VelocityEngine ve = init(); + + public static String generateAPIContract(long availableDate, String url, String method, Map header, + Map arg) { + VelocityContext ctx = new VelocityContext(); + + Template script = ve.getTemplate("org/bdware/sc/sc/datacontract.vm"); + String[][] initList = {{"availableDate", availableDate + ""}, {"baseUrl", stringify(url)}, + {"method", stringify(method)}, {"headers", convertMap(header)}, {"arg", convertMap(arg)}}; + ctx.put("initList", initList); + ctx.put("validate", "YancloudUtil.currentTimeMillis()>Global.availableDate"); + ctx.put("validateFailedMsg", "\"key expired\""); + StringWriter writer = new StringWriter(); + script.merge(ctx, writer); + return writer.toString(); + } + + private static VelocityEngine init() { + VelocityEngine ve = new VelocityEngine(); + ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); + ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); + ve.init(); + return ve; + } + + public static String generateFTPContract(long availableDate) { + VelocityContext ctx = new VelocityContext(); + Template script = ve.getTemplate("org/bdware/sc/sc/ftpcontract.vm"); + String[][] initList = {{"availableDate", availableDate + ""}}; + ctx.put("initList", initList); + ctx.put("validate", "YancloudUtil.currentTimeMillis()>Global.availableDate"); + StringWriter writer = new StringWriter(); + script.merge(ctx, writer); + return writer.toString(); + } + + private static String stringify(String url) { + return JsonUtil.toJson(url); + } + + private static String convertMap(Map arg) { + if (arg == null) { + arg = new HashMap<>(); + } + return JsonUtil.toJson(arg); + } +} diff --git a/src/main/base/org/bdware/sc/DX.java.txt b/src/main/base/org/bdware/sc/DX.java.txt new file mode 100644 index 0000000..791d586 --- /dev/null +++ b/src/main/base/org/bdware/sc/DX.java.txt @@ -0,0 +1,44 @@ +package org.bdware.sc; + +import com.android.dx.cf.direct.DirectClassFile; +import com.android.dx.cf.direct.StdAttributeFactory; +import com.android.dx.command.dexer.DxContext; +import com.android.dx.dex.DexOptions; +import com.android.dx.dex.cf.CfOptions; +import com.android.dx.dex.cf.CfTranslator; +import com.android.dx.dex.file.ClassDefItem; +import com.android.dx.dex.file.DexFile; + +import java.util.Map; + +public class DX { + public static byte[] clzsToDex(Map clzs) { + try { + DxContext context = new DxContext(); + DexOptions dexOptions = new DexOptions(); + dexOptions.minSdkVersion = 26; + CfOptions cfOptions = new CfOptions(); + DexFile outputDex = new DexFile(dexOptions); + for (String str : clzs.keySet()) { + DirectClassFile dcf = + new DirectClassFile( + clzs.get(str), + toInternalName(str) + ".class", + cfOptions.strictNameCheck); + dcf.setAttributeFactory(StdAttributeFactory.THE_ONE); + ClassDefItem item = + CfTranslator.translate( + context, dcf, clzs.get(str), cfOptions, dexOptions, outputDex); + outputDex.add(item); + } + return outputDex.toDex(null, false); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static String toInternalName(String plain) { + return plain.replaceAll("\\.", "/"); + } +} diff --git a/src/main/pythongen/org/bdware/sc/commParser/BDLedgerAdapter.txt b/src/main/pythongen/org/bdware/sc/commParser/BDLedgerAdapter.txt new file mode 100644 index 0000000..5d1f882 --- /dev/null +++ b/src/main/pythongen/org/bdware/sc/commParser/BDLedgerAdapter.txt @@ -0,0 +1,15 @@ +package org.bdware.sc.commParser; + +public class BDLedgerAdapter { + public Transaction tx; + public org.bdware.bdledger.api.grpc.pb.CommonProto.Transaction ledgerChain; + + public BDLedgerAdapter(org.bdware.bdledger.api.grpc.pb.CommonProto.Transaction chaintrans) { + super(); + this.ledgerChain = chaintrans; + this.tx = new Transaction(); + this.tx.Data = chaintrans.getData().toStringUtf8().getBytes(); + this.tx.SrcID = chaintrans.getFrom().toByteArray(); //本地库中SrcID可以是key + this.tx.Txid = chaintrans.getHash().toByteArray(); + } +} diff --git a/src/main/pythongen/org/bdware/sc/contractGen/ContractGenerator.txt b/src/main/pythongen/org/bdware/sc/contractGen/ContractGenerator.txt new file mode 100644 index 0000000..73ec291 --- /dev/null +++ b/src/main/pythongen/org/bdware/sc/contractGen/ContractGenerator.txt @@ -0,0 +1,69 @@ +package org.bdware.sc.contractGen; + +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.runtime.RuntimeConstants; +import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; +import org.bdware.sc.util.JsonUtil; + +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +public class ContractGenerator { + static VelocityEngine ve = init(); + + public static String generateAPIContract(Map arg) { + VelocityContext ctx = new VelocityContext(); + Template script = ve.getTemplate("org/bdware/sc/sc/contractGen/timeindexcontract.vm"); + ctx.put("indexData", arg.get("indexData")); + ctx.put("datalen", arg.get("datalen")); + ctx.put("sdkpath", arg.get("sdkpath")); + ctx.put("syncbyheight", arg.get("syncbyheight")); + StringWriter writer = new StringWriter(); + script.merge(ctx, writer); + return writer.toString(); + } + + public static String generateIndexContract(long availableDate, String url, String method, Map header, + Map arg) { + VelocityContext ctx = new VelocityContext(); + + Template script = ve.getTemplate("com/yancloud/sc/indexcontract.vm"); + String onblock = "var hash = com.yancloud.sc.util.HashUtil.str16ToBytes(args);\n print(hash)"; + ctx.put("onBlock", onblock); + StringWriter writer = new StringWriter(); + script.merge(ctx, writer); + return writer.toString(); + } + + private static VelocityEngine init() { + VelocityEngine ve = new VelocityEngine(); + ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); + ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); + ve.init(); + return ve; + } + + public static String generateFTPContract(long availableDate) { + VelocityContext ctx = new VelocityContext(); + Template script = ve.getTemplate("org/bdware/sc/sc/ftpcontract.vm"); + String[][] initList = {{"availableDate", availableDate + ""}}; + ctx.put("initList", initList); + ctx.put("validate", "YancloudUtil.currentTimeMillis()>Global.availableDate"); + StringWriter writer = new StringWriter(); + script.merge(ctx, writer); + return writer.toString(); + } + + private static String stringify(String url) { + return JsonUtil.toJson(url); + } + + private static String convertMap(Map arg) { + if (null == arg) + arg = new HashMap<>(); + return JsonUtil.toJson(arg); + } +} diff --git a/src/main/pythongen/org/bdware/sc/contractGen/ContractGeneratorUI.txt b/src/main/pythongen/org/bdware/sc/contractGen/ContractGeneratorUI.txt new file mode 100644 index 0000000..d60fc56 --- /dev/null +++ b/src/main/pythongen/org/bdware/sc/contractGen/ContractGeneratorUI.txt @@ -0,0 +1,131 @@ +package org.bdware.sc.contractGen; + +import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper; + +import javax.swing.*; +import javax.swing.plaf.FontUIResource; +import java.awt.*; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +public class ContractGeneratorUI { + public static void main(String[] args) { + try { + BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated; + org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + InitGlobalFont(new Font("alias", Font.PLAIN, 15)); + + JFrame frame = new JFrame("索引生成");//创建Frame窗口 + JPanel checkbox_panel; + final JCheckBox checkBox_time; + final JCheckBox checkBox_account; + checkbox_panel = new JPanel();//面板索引选择 + checkBox_time = new JCheckBox("时间索引"); + checkBox_account = new JCheckBox("账户索引"); + + checkbox_panel.add(checkBox_time); + checkbox_panel.add(checkBox_account); + + final JPanel input_sdk = new JPanel();//SDK包名; + final JLabel sdk_label = new JLabel("SDK类名"); + final JTextField sdk = new JTextField(10); + final JLabel datalen_label = new JLabel("数据长度"); + final JTextField data_length = new JTextField(10); + final JCheckBox height_sync_cbox = new JCheckBox("使用高度同步"); + input_sdk.add(sdk_label); + input_sdk.add(sdk); + input_sdk.add(datalen_label); + input_sdk.add(data_length); + input_sdk.add(height_sync_cbox); + + final JTabbedPane tab = new JTabbedPane();//选项卡 + final JPanel cards = new JPanel(); //卡片式布局的面板 + cards.add(input_sdk); + cards.add(checkbox_panel); + cards.add(tab); + frame.add(cards); + frame.setBounds(100, 200, 800, 800); + frame.setVisible(true); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + final Map framemap = new HashMap<>(); + checkBox_time.addActionListener(e -> { + System.out.println("performed"); + if (checkBox_time.isSelected()) { + if (sdk.getText().length() != 0 && data_length.getText().length() != 0) { + Map map = new HashMap<>(); + map.put("indexData", "./RocksDB"); + map.put("datalen", data_length.getText()); + map.put("sdkpath", sdk.getText()); + if (height_sync_cbox.isSelected()) { + map.put("syncbyheight", "true"); + } + String res = ContractGenerator.generateAPIContract(map); + MainFrame time_panel = new MainFrame(); + framemap.put("time", time_panel); + time_panel.textfield.setText(res); + tab.add(time_panel, "时间索引"); + try { + File file = new File("./output/timeindex.yjs"); + file.createNewFile(); + FileWriter writer = new FileWriter(file); + BufferedWriter out = new BufferedWriter(writer); + out.write(res); + out.close(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + } else { + tab.remove(framemap.get("time")); + } + }); + checkBox_account.addActionListener(e -> { + if (checkBox_account.isSelected()) { + Map map = new HashMap<>(); + map.put("indexData", "./RocksDB"); + String res = ContractGenerator.generateAPIContract(map); + MainFrame account_panel = new MainFrame(); + framemap.put("account", account_panel); + account_panel.textfield.setText(res); + tab.add(account_panel, "账户索引"); + try { + File file = new File("./output/accountindex.yjs"); + file.createNewFile(); + FileWriter writer = new FileWriter(file); + BufferedWriter out = new BufferedWriter(writer); + out.write(res); + out.close(); + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } else { + tab.remove(framemap.get("account")); + } + }); + } + + /** + * 统一设置字体,父界面设置之后,所有由父界面进入的子界面都不需要再次设置字体 + */ + private static void InitGlobalFont(Font font) { + FontUIResource fontRes = new FontUIResource(font); + for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) { + Object key = keys.nextElement(); + Object value = UIManager.get(key); + if (value instanceof FontUIResource) { + UIManager.put(key, fontRes); + } + } + } +} diff --git a/src/main/pythongen/org/bdware/sc/contractGen/MainFrame.txt b/src/main/pythongen/org/bdware/sc/contractGen/MainFrame.txt new file mode 100644 index 0000000..20bea05 --- /dev/null +++ b/src/main/pythongen/org/bdware/sc/contractGen/MainFrame.txt @@ -0,0 +1,55 @@ +package org.bdware.sc.contractGen; + +import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; +import org.fife.ui.rsyntaxtextarea.SyntaxConstants; + +import javax.swing.*; +import java.util.HashMap; +import java.util.Map; + +public class MainFrame extends JPanel { + final RSyntaxTextArea textfield; + JLabel sdk_label; + JLabel datalen_label; + + JPanel p1; + JPanel p2; + JButton generate; + JButton reset; + + public MainFrame() { + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + + p1 = new JPanel();//面板OnBlock + p2 = new JPanel();//面板OnBlock + + generate = new JButton("生成合约"); + reset = new JButton("重置内容"); + p2.add(generate); + p2.add(reset); + + this.textfield = new RSyntaxTextArea(30, 80);//height,width + textfield.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT); + textfield.setCodeFoldingEnabled(true); + + JScrollPane scroll = new JScrollPane(textfield); + scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); + + p1.add(scroll); + this.add(p1); + this.add(p2); + + reset.addActionListener(arg0 -> { + // TODO Auto-generated method stub + textfield.setText(""); + }); + generate.addActionListener(e -> { + // TODO Auto-generated method stub + // use new file to generate yjs + Map map = new HashMap<>(); + map.put("indexData", "./RocksDB"); + String res = ContractGenerator.generateAPIContract(map); + }); + } +}