区块链API

什么是区块链API

在您创建的任意一条区块链中,安链云都提供了功能各异的API,通过调取这些API,您可以在区块链中实现诸如创建账户、部署合约、实现交易等功能。

目前安链云仅提供“合约链”。

如何开启动态路由

在使用区块链API前,您需要先开启动态路由。

  1. 选择一条您已部署的区块链,点击管理,进入区块链管理界面

  2. 进入API tab

  3. 选择一个您已创建的应用,点击绑定应用 如果您还未创建应用,请先创建一个应用,可参考创建应用

  4. 绑定应用成功,您已开启动态路由,并可以调用该区块链中相应的API

如何使用区块链API

  1. 获取调用API的公私钥,公私钥由该条链绑定的应用提供,可以在应用管理中查看
  2. API调用方法请参考 API调用方法概述
  3. 测试期间仅提供“联盟链”,联盟链可提供的API请参考以下示例

init ann-api

cd $GOPATH/src/gitlab.zhonganonline.com/ann
git clone git@gitlab.zhonganonline.com:ann/ann-api.git
cd ann-api/cmd
ln -s ../ann0.8.0/vendor ./vendor
go build
./cmd

测试地址 http://10.253.24.87:8888 所有请求均增加头部 Content-Type: application/json,返回数据也是Json格式

1. 创建账号 /v1/accounts/create

请求Body

{
    'passphrase': '12******56'
}

进行json序列化

返回Body

{
    "isSuccess":true,
    "result":{
        "privkey":"7f10d92**************d6895a77908c3923e",
        "address":"0x50b1813f6b02e4751cc0416ab7bc658e81ecba93"
    }
}

privkey是账号私钥,address是账号地址,部署合约、调用合约时使用

2. 部署合约 /v1/contracts/create

请求Body

{
    'account': '0x50b1813f6b02e4751cc0416ab7bc658e81ecba93',
    'passphrase': '123456',
    'privkey': '7f10d92**********************6895a77908c3923e',
    'code': '606060405234610000575b33600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b5b6105e48061005c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063522afaec1461006a57806357344e6f146100a65780638da5cb5b146100ed578063beabacc81461013c578063f27ac4d914610197575b610000565b34610000576100a4600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101d3565b005b34610000576100d7600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506102bc565b6040518082815260200191505060405180910390f35b34610000576100fa610306565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3461000057610195600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061032c565b005b34610000576101d1600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610486565b005b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561022f57610000565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141561026957610000565b80600060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5b5050565b6000600060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490505b919050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1614806103935750600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16145b806103dc575080600060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054105b156103e657610000565b80600060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b505050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156104e257610000565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16148061055b575080600060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054105b1561056557610000565b80600060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055505b5b50505600a165627a7a7230582054c82c348ab03c11de0d95121714d87f5a9fac42be59174170975e4c584582e20029'
}

返回Body

{
    "isSuccess":true, 
    "result":"0xca6d1583000710335dcb9e417c79170f8f7c9b22"
}

result的值即为合约地址

3. 合约调用(写) /v1/contracts/call

请求Body

{
    'contract': '0xca6d1583000710335dcb9e417c79170f8f7c9b22',
    'account': '0x50b1813f6b02e4751cc0416ab7bc658e81ecba93',
    'passphrase': '123456',
    'privkey': '7f10d921e**********************9b53801d3d6895a77908c3923e',
    'method': 'increase',
    "params":[
        "0x4f33741393234b419b66621d1276031d474b3612", 100
    ],
    'abiDefinition': '[{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_credits","type":"uint256"}],"name":"increase","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"}],"name":"getCredit","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_credits","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_credits","type":"uint256"}],"name":"decrease","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]'
}

返回Body

{
    "isSuccess":true,
    "result":"0x24bc49f8e05e16a17f68cb732db13ed38255263a593b88f9a378a208bf5e6c8f"
}

4. 合约调用(读) /v1/contracts/read

请求Body

{
    'contract': '0xca6d1583000710335dcb9e417c79170f8f7c9b22',
    'account': '0x50b1813f6b02e4751cc0416ab7bc658e81ecba93',
    'passphrase': '123456',
    'privkey': '7f10d9**********************3f9e8229b53801d3d6895a77908c3923e',
    'method': 'getCredit',
    "params":[
        "0x4f33741393234b419b66621d1276031d474b3612"
    ],
    'abiDefinition': '[{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_credits","type":"uint256"}],"name":"increase","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"}],"name":"getCredit","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_credits","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_credits","type":"uint256"}],"name":"decrease","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]'
}

返回Body ```json { "isSuccess":true, "result":400