Coin-to-Coin Contract Execution Analysis

BytomDAO
4 min readSep 26, 2018

--

This article mainly analyses coin-to-coin contract of Bytom,its template is as following:

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
}
}

Lock contract

Step1;call create-account-receiver to generate control_program

Related code :

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

Step2:call list-pubkeys to acquire pubkey

Related codes:

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

Step3:call acquired value in Step1 and step2 to compile interface to acquire contract program

Related codes:

JSONObject param=new JSONObject();
JSONArray agrs=new JSONArray();
//The four parameter values of the contract
JSONObject assetParam=new JSONObject();
assetParam.put(“string”,”81d097312645696daea84b761d2898d950d8fba0de06c9267d8513b16663dd3a”);
agrs.put(assetParam);
JSONObject amountParam=new JSONObject();
amountParam.put(“integer”,200000000l);
agrs.put(amountParam);
JSONObject programParam=new JSONObject();
programParam.put(“string”,control_program);
agrs.put(programParam);
JSONObject publicKeyParam=new JSONObject();
publicKeyParam.put(“string”,pubkey);
agrs.put(publicKeyParam);
param.put(“agrs”,agrs);
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 } }”);
//Call compilation contract
sendHttpPost(param.toString(),”list-pubkeys”,”http://127.0.0.1:9888","");

Step4:

send program to build-transaction interface to build a transaction to data

Related codes:

param=new JSONObject();
agrs=new JSONArray();
JSONObject spendAccount=new JSONObject();
spendAccount.put(“account_id”,”0H757LPD00A02");
spendAccount.put(“amount”,9909099090000l);
spendAccount.put(“asset_id”,”161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
spendAccount.put(“type”,”spend_account”);
agrs.put(spendAccount);
JSONObject controlAccount=new JSONObject();
controlAccount.put(“control_program”,program);
controlAccount.put(“amount”,9909099090000l);
controlAccount.put(“asset_id”,”161b9767b664df907fa926a31f9e835236e57f3e9ccc5f80c12bd97723322652");
controlAccount.put(“type”,”control_program”);
agrs.put(controlAccount);
JSONObject spendAccount2=new JSONObject();
spendAccount2.put(“account_id”,”0H757LPD00A02");
spendAccount2.put(“amount”,6000000l);
spendAccount2.put(“asset_id”,”ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff”);
spendAccount2.put(“type”,”spend_account”);
agrs.put(spendAccount2);
param.put(“actions”,agrs);
param.put(“ttl”,0);
sendHttpPost(param.toString(),”build-transaction”,”http://127.0.0.1:9888","");

Step5:Enter password to call sign-transaction to sign the data which built in step 4 to acquire raw_transaction

Related codes:

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

Step 6:call submit-transactions to submit transaction

Related codes:

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

Unlock/cancel contract

decode the parameters when generating contract

call list-unspent-outputs to acquire generated contract information to acquire program

Related codes:

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

call decode-program to send generated information of contract parameters

Related codes:

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

Unlock/cancel remove the third step in the process of generating the contract and replace the parameters in the fourth step of generating the contract

Construction parameters of cancel contract:

spendAccountUnspentOutput = arguments: [{
type: ‘raw_tx_signature’,
// 生成合约第二步的pubkeylist 详情
raw_data: {
derivation_path: pubkeylist.pubkey_infos[0].derivation_path,
xpub: pubkeylist.root_xpub
}
}, {
type: ‘data’,
raw_data: {
// 参数偏移量 在一个合约里是固定的
value: ‘13000000’
}
}],
output_id: output_id,
type: ‘spend_account_unspent_output’
}
const controlAction = {
type: ‘control_program’,
amount: 100000000,
asset_id: asset_id,
control_program:control_program
}
const gasAction = {
type: ‘spend_account’,
account_id:account_id,
asset_alias: ‘BTM’,
amount: 50000000
}

construction parameters of performing contract :

const spendAccountUnspentOutput = {
arguments: [{
type: ‘data’,
raw_data: {
// 00000000 is firstclause,means perform directly
value: ‘00000000’
}
}],
output_id: output_id,
type: ‘spend_account_unspent_output’
}
// asset by contract
const issueControlAction = {
control_program: control_program,
amount: 100000000,
asset_id: asset_id,
type: ‘control_program’
}
// asset by contract
const issueSpendAction = {
account_id: account_id,
amount: 100000000,
asset_id: asset_id,
type: ‘spend_account’
}
// gas
const gasAction = {
type: ‘spend_account’,
account_id: account_id,
asset_alias: ‘BTM’,
amount: 50000000
}
// execute Contract to get the asset object
const controlAction = {
type: ‘control_program’,
amount: 100000000,
asset_id: asset_id,
control_program: compileData.control_program
}

“build”operation is the process of specified input and output

Remarks

call Byotm javautil interface based on okhttp

public static String sendHttpPost(String bodyStr,String method,String bytomApiserverUrl,String bytomApiserverToken) throws IOException {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse(“application/json”);
RequestBody body = RequestBody.create(mediaType, bodyStr);
Request request = new Request.Builder()
.url(bytomApiserverUrl+”/”+method)
.post(body)
.addHeader(“cache-control”, “no-cache”)
.addHeader(“Connection”, “close”)
.build();
if (bytomApiserverUrl==null || bytomApiserverUrl.contains(“127.0.0.1”) || bytomApiserverUrl.contains(“localhost”)){
}else {
byte[] encodedAuth = Base64.encodeBase64(bytomApiserverToken.getBytes(Charset.forName(“US-ASCII”)));
String authHeader = “Basic “ + new String(encodedAuth);
request = new Request.Builder()
.url(bytomApiserverUrl+”/”+method)
.post(body)
.addHeader(“authorization”, authHeader)
.addHeader(“cache-control”, “no-cache”)
.addHeader(“Connection”, “close”)
.build();
}
Response response = client.newCall(request).execute();
return response.body().string();
}

--

--