经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 区块链 » 查看文章
币币合约执行解析(包含部分源码)
来源:cnblogs  作者:比原链Bytom  时间:2018/9/25 19:09:50  对本文有异议

作者:芈橙

比原项目仓库:

Github地址:https://github.com/Bytom/bytom

Gitee地址:https://gitee.com/BytomBlockchain/bytom

本文解析的为比原提供的币币合约 模板如下:

  1. contract TradeOffer(assetRequested: Asset,
  2. amountRequested: Amount,
  3. seller: Program,
  4. cancelKey: PublicKey) locks offered {
  5. clause trade() requires payment: amountRequested of assetRequested {
  6. lock payment with seller
  7. unlock offered
  8. }
  9. clause cancel(sellerSig: Signature) {
  10. verify checkTxSig(cancelKey, sellerSig)
  11. unlock offered
  12. }
  13. }

导读: 初次接触比原只能合约的请点击比原智能合约入门Equity 语言入门 学习,方便更好的理解该文档

锁定合约

第一步:调用create-account-receiver 生成 control_program

以下是相关代码片段:

  1. sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","create-account-receiver","http://127.0.0.1:9888","");

第二步调用list-pubkeys 获取 pubkey

以下是相关代码片段:

  1. sendHttpPost("{\"account_id\":\"0IJVD7MNG0A02\"}","list-pubkeys","http://127.0.0.1:9888","");

第三步: 将1 2步获取的值调用compile接口编译合约获得program 合约程序

以下是相关代码片段:

  1. JSONObject param=new JSONObject();
  2. JSONArray agrs=new JSONArray();
  3. //合约的四个参数值
  4. JSONObject assetParam=new JSONObject();
  5. assetParam.put("string","81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a");
  6. agrs.put(assetParam);
  7. JSONObject amountParam=new JSONObject();
  8. amountParam.put("integer",200000000l);
  9. agrs.put(amountParam);
  10. JSONObject programParam=new JSONObject();
  11. programParam.put("string",control_program);
  12. agrs.put(programParam);
  13. JSONObject publicKeyParam=new JSONObject();
  14. publicKeyParam.put("string",pubkey);
  15. agrs.put(publicKeyParam);
  16. param.put("agrs",agrs);
  17. param.put("contract","contract TradeOffer(assetRequested: Asset, amountRequested: Amount, seller: Program, cancelKey: PublicKey) locks offered { clause trade() requires payment: amountRequested of assetRequested { lock payment with seller unlock offered } clause cancel(sellerSig: Signature) { verify checkTxSig(cancelKey, sellerSig) unlock offered } }");
  18. //调用编译合约
  19. sendHttpPost(param.toString(),"list-pubkeys","http://127.0.0.1:9888","");

第四步:将program 传入build-transaction接口去build一个交易的到data


以下是相关代码片段:

  1. param=new JSONObject();
  2. agrs=new JSONArray();
  3. JSONObject spendAccount=new JSONObject();
  4. spendAccount.put("account_id","0H757LPD00A02");
  5. spendAccount.put("amount",9909099090000l);
  6. spendAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
  7. spendAccount.put("type","spend_account");
  8. agrs.put(spendAccount);
  9. JSONObject controlAccount=new JSONObject();
  10. controlAccount.put("control_program",program);
  11. controlAccount.put("amount",9909099090000l);
  12. controlAccount.put("asset_id","161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
  13. controlAccount.put("type","control_program");
  14. agrs.put(controlAccount);
  15. JSONObject spendAccount2=new JSONObject();
  16. spendAccount2.put("account_id","0H757LPD00A02");
  17. spendAccount2.put("amount",6000000l);
  18. spendAccount2.put("asset_id","ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
  19. spendAccount2.put("type","spend_account");
  20. agrs.put(spendAccount2);
  21. param.put("actions",agrs);
  22. param.put("ttl",0);
  23. sendHttpPost(param.toString(),"build-transaction","http://127.0.0.1:9888","");

第五步:输入密码调用sign-transaction签名第四步build的data 得到raw_transaction


以下是相关代码片段:

  1. param=new JSONObject();
  2. param.put("password","xxx");
  3. param.put("transaction",data);
  4. sendHttpPost(param.toString(),"sign-transaction","http://127.0.0.1:9888","");

第六步:调用submit-transactions提交交易

以下是相关代码片段:

  1. param=new JSONObject();
  2. param.put("raw_transaction",raw_transaction);
  3. sendHttpPost(param.toString(),"submit-transactions","http://127.0.0.1:9888","");

解锁/取消合约

首先需要decode出生成合约时候的参数

调用list-unspent-outputs 获取生成的合约信息获取program

以下是相关代码片段:

  1. param=new JSONObject();
  2. param.put("id",outputid);
  3. param.put("smart_contract",true);
  4. sendHttpPost(param.toString(),"list-unspent-outputs","http://127.0.0.1:9888","");

调用decode-program 传入获取生成的合约参数信息

以下是相关代码片段:

  1. param=new JSONObject();
  2. param.put("program",program);
  3. sendHttpPost(param.toString(),"decode-program","http://127.0.0.1:9888","");

需要注意的是decode出来的为值是逆序的(后续会有文章详细介绍)

解锁/取消其实就是把生成合约的步骤中的第三步去掉,替换调用生成合约第四步的参数即可

取消合约的构造参数如下:

  1. spendAccountUnspentOutput = arguments: [{
  2. type: 'raw_tx_signature',
  3. // 生成合约第二步的pubkeylist 详情
  4. raw_data: {
  5. derivation_path: pubkeylist.pubkey_infos[0].derivation_path,
  6. xpub: pubkeylist.root_xpub
  7. }
  8. }, {
  9. type: 'data',
  10. raw_data: {
  11. // 参数偏移量 在一个合约里是固定的
  12. value: '13000000'
  13. }
  14. }],
  15. output_id: output_id,
  16. type: 'spend_account_unspent_output'
  17. }
  18. const controlAction = {
  19. type: 'control_program',
  20. amount: 100000000,
  21. asset_id: asset_id,
  22. control_program:control_program
  23. }
  24. const gasAction = {
  25. type: 'spend_account',
  26. account_id:account_id,
  27. asset_alias: 'BTM',
  28. amount: 50000000
  29. }

执行合约的参数构造如下:

  1. const spendAccountUnspentOutput = {
  2. arguments: [{
  3. type: 'data',
  4. raw_data: {
  5. // 00000000 指的是第一个 clause,表示直接执行,无需跳转
  6. value: '00000000'
  7. }
  8. }],
  9. output_id: output_id,
  10. type: 'spend_account_unspent_output'
  11. }
  12. // 合约执行提供的资产
  13. const issueControlAction = {
  14. control_program: control_program,
  15. amount: 100000000,
  16. asset_id: asset_id,
  17. type: 'control_program'
  18. }
  19. // 合约执行提供的资产
  20. const issueSpendAction = {
  21. account_id: account_id,
  22. amount: 100000000,
  23. asset_id: asset_id,
  24. type: 'spend_account'
  25. }
  26. // 矿工费
  27. const gasAction = {
  28. type: 'spend_account',
  29. account_id: account_id,
  30. asset_alias: 'BTM',
  31. amount: 50000000
  32. }
  33. // 合约执行获得资产对象
  34. const controlAction = {
  35. type: 'control_program',
  36. amount: 100000000,
  37. asset_id: asset_id,
  38. control_program: compileData.control_program
  39. }

build 操作其实就是指定输入输出的过程,详情请查看 官方build文档官方api文档

备注

调用比原基于okhttp接口javautil 如下:

  1. public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
  2. OkHttpClient client = new OkHttpClient();
  3. MediaType mediaType = MediaType.parse("application/json");
  4. RequestBody body = RequestBody.create(mediaType, bodyStr);
  5. Request request = new Request.Builder()
  6. .url(bytomApiserverUrl+"/"+method)
  7. .post(body)
  8. .addHeader("cache-control", "no-cache")
  9. .addHeader("Connection", "close")
  10. .build();
  11. if (bytomApiserverUrl==null || bytomApiserverUrl.contains("127.0.0.1") || bytomApiserverUrl.contains("localhost")){
  12. }else {
  13. byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName("US-ASCII")));
  14. String authHeader = "Basic " + new String(encodedAuth);
  15. request = new Request.Builder()
  16. .url(bytomApiserverUrl+"/"+method)
  17. .post(body)
  18. .addHeader("authorization", authHeader)
  19. .addHeader("cache-control", "no-cache")
  20. .addHeader("Connection", "close")
  21. .build();
  22. }
  23. Response response = client.newCall(request).execute();
  24. return response.body().string();
  25. }
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号