Part 1. Cocos-BCX链系统互操作API -- BCXJS Javascript API,用于使用COCOS-BCX RPC API与基于COCOS-BCX的区块链集成。 Preparation: Node.js版本8.9.3或更高版本 Build: 使用npm i…
Part 1. Cocos-BCX链系统互操作API -- BCXJS Javascript API,用于使用COCOS-BCX RPC API与基于COCOS-BCX的区块链集成。 Preparation: Node.js版本8.9.3或更高版本 Build: 使用npm i…
Javascript API,用于使用COCOS-BCX RPC API与基于COCOS-BCX的区块链集成。
Node.js版本8.9.3或更高版本
使用npm install安装本地依赖项。 使用开发构建npm run dev。
构建用于发布npm run release。
构建将在./build中找到
构建用于发布npm run release-npm。
构建将在./build-npm中找到
在用 bcxjs 构建大型应用时推荐使用 NPM 安装。NPM 能很好地和诸如 webpack 或 Browserify 模块打包器配合使用
# 最新稳定版
$ npm install bcxjs-api如果您有一个转换器,例如Babel,则支持在浏览器中使用ES6模块语法导入。
import BCX from "bcxjs-api"var bcx=new BCX({
default_ws_node:”ws://XXXXXXXXX” //节点rpc地址,选填。如果没有指定此项则会自动连接ws_node_list中速度最快的节点
ws_node_list:[{url:"ws://xxxxxxx",name:"xxxxx"}]//API服务器节点列表,必填
faucet_url:"http://xxx.xxx.xxx.xxx:xxxx", //注册入口
networks:[{
core_asset:"xxx",//核心资产符号
chain_id:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"//链id
}],
auto_reconnect:false,//当RPC断开时是否自动连接,默认为true
app_keys:["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"],//合约授权,不进行合约授权,则不用配置此选项
real_sub:true//true:实时订阅,false:区块订阅,订阅通知频率为出块频率
sub_max_op:13//订阅时最大处理OP数量
});bcx.transferAsset({
to:"test2",
amount:1,
assetId:"COCOS",
memo:""
}).then(res=>{
console.log('transferAsset res',res);
})callback返回的result为Object对象,结构为{code:0,message:””}。 code=1时表示成功,无message状态描述。 code!=1时意味执行失败,message为失败状态描述。
调用示例:
bcl.getPrivateKey().then(res=>{});示例:
trx_data:{
block_num:*****,//区块高度
trx_id:"************************"//交易ID
}//示例
data:{
real_running_time: 387//运行时间
result: "4.2.288"//关联业务id
}| code | message | 说明 | 关联API |
|---|---|---|---|
| 300 | Chain sync error, please check your system clock | 链同步错误,请检查您的系统时钟 | * |
| 301 | RPC connection failed. Please check your network | 连接RPC失败,请检查你的网络 | * |
| 1 | 无 | 操作成功 | * |
| 0 | failed | 操作失败,返回错误状态描述不固定,可直接提示res.message或统一提示为操作失败 | * |
| 101 | Parameter is missing | 参数缺失 | * |
| 1011 | Parameter error | 参数错误 | * |
| 102 | The network is busy, please check your network connection | 网络繁忙,请检查你的网络连接 | * |
| 103 | Please enter the correct account name(/^a-z{4,63}/$) | 请输入正确的账户名(正则/^a-z{4,63}/$) | CreateAccountWithPassword |
| 104 | XX not found | XX 不存在 | passwordLogin等 |
| 105 | wrong password | 密码错误 | passwordLogin,unlockAccount |
| 106 | The account is already unlocked | 账户已经处于解锁状态 | unlockAccount |
| 107 | Please import the private key | 请先导入私钥 | unlockAccount |
| 108 | User name or password error (please confirm that your account is registered in account mode, and the account registered in wallet mode cannot be logged in using account mode) | 用户名或密码错误(请确认你的账户是通过账户模式注册的,钱包模式注册的账户不能使用账户模式登录) | passwordLogin |
| 109 | Please enter the correct private key | 请输入正确的私钥 | keyLogin |
| 110 | The private key has no account information | 该私钥没有对应的账户信息 | keyLogin |
| 111 | Please login first | 请先登录 | changePassword |
| 112 | Must have owner permission to change the password, please confirm that you imported the ownerPrivateKey | 必须拥有owner权限才可以进行密码修改,请确认你导入的是ownerPrivateKey | changePassword |
| 113 | Please enter the correct original/temporary password | 请输入正确的原始密码/临时密码 | changePassword |
| 114 | Account is locked or not logged in. | 帐户被锁定或未登录 | changePasswor、transferAsset、getPrivateKey等 |
| 115 | There is no asset XX on block chain | 区块链上不存在资产 XX | transferAsset等 |
| 116 | Account receivable does not exist | 收款方账户不存在 | transferAsset等 |
| 117 | The current asset precision is configured as X ,and the decimal cannot exceed X | 当前资产精度配置为 X ,小数点不能超过 X | transferAsset等 |
| 118 | Encrypt memo failed | 备注加密失败 | transferAsset等 |
| 119 | Expiry of the transaction | 交易过期 | transferAsset等 |
| 120 | Error fetching account record | 获取帐户记录错误 | queryAccountOperations |
| 121 | block and transaction information cannot be found | 查询不到相关区块及交易信息 | queryBlockTXID |
| 122 | Parameter blockOrTXID is incorrect | 参数blockOrTXID不正确 | queryBlockTXID |
| 123 | Parameter account can not be empty | 参数account不能为空 | queryAccountInfo |
| 124 | Receivables account name can not be empty | 收款方账户名不能为空 | transferAsset |
| 125 | Users do not own XX assets | 用户未拥有 XX 资产 | queryAccountBalances |
| 127 | No reward available | 没有可领取的奖励 | getVestingBalances,claimVestingBalance |
| 129 | Parameter 'memo' can not be empty | memo不能为空 | decodeMemo |
| 130 | Please enter the correct contract name(/^a-z{4,63}$/) | 请输入正确的合约名称(正则/^a-z{4,63}$/) | createContract |
| 131 | Parameter 'worldView' can not be empty | 世界观名称不能为空 | creatWorldView |
| 133 | Parameter 'toAccount' can not be empty | toAccount不能为空 | transferNHAsset |
| 135 | Please check parameter data type | 请检查参数数据类型 | creatNHAssetOrder |
| 136 | Parameter 'orderId' can not be empty | orderId不能为空 | cancelNHAssetOrder |
| 137 | Parameter 'NHAssetHashOrIds' can not be empty | NHAssetHashOrIds不能为空 | lookupNHAssets |
| 138 | Parameter 'url' can not be empty | 接入点地址不能为空 | switchAPINode |
| 139 | Node address must start with ws:// or wss:// | 节点地址必须以 ws:// 或 wss:// 开头 | addAPINode |
| 140 | API server node address already exists | API服务器节点地址已经存在 | switchAPINode |
| 141 | Please check the data in parameter NHAssets | 请检查参数NHAssets中的数据 | creatNHAsset批量创建NH |
| 142 | Please check the data type of parameter NHAssets | 请检查参数NHAssets的数据类型 | creatNHAsset批量创建NH |
| 144 | Your current batch creation / deletion / transfer number is X , and batch operations can not exceed X | 您当前批量 创建/删除/转移 NH资产数量为 X ,批量操作数量不能超过 X | creatNHAsset、deleteNHAsset、transferNHAsset |
| 145 | XX contract not found | XX 合约不存在 | callContractFunction, queryContract, queryAccountContractData |
| 146 | The account does not contain information about the contract | 账户没有该合约相关的信息 | queryAccountContractData |
| 147 | NHAsset do not exist | 非同质资产不存在 | queryNHAssets |
| 148 | Request timeout, please try to unlock the account or login the account | 请求超时,请尝试解锁账户或登录账户 | queryVotes |
| 149 | This wallet has already been imported | 此私钥已导入过钱包 | importPrivateKey |
| 150 | Key import error | 导入私钥失败 | importPrivateKey |
| 151 | File saving is not supported | 您的浏览器不支持文件保存 | backupDownload |
| 152 | Invalid backup to download conversion | 无效的备份下载转换 | backupDownload |
| 153 | Please unlock your wallet first | 请先解锁钱包 | importPrivateKey |
| 154 | Please restore your wallet first | 请先恢复你的钱包 | backupDownload、unlockAccount |
| 155 | Your browser may not support wallet file recovery | 浏览器不支持钱包文件恢复 | loadWalletFile |
| 156 | The wallet has been imported. Do not repeat import | 该钱包已经导入,请勿重复导入 | restoreWallet |
| 157 | Can't delete wallet, does not exist in index | 请求超时,请尝试解锁账户或登录账户 | deleteWallet |
| 158 | Imported Wallet core assets can not be XX , and it should be XX | 导入的钱包核心资产不能为 XX ,应为 XX | restoreWallet |
| 159 | Account exists | 账户已存在 | createAccountWithWallet |
| 160 | You are not the creator of the Asset XX . | 你不是该资产的创建者 | issueAsset |
| 161 | Orders do not exist | 订单不存在 | fillNHAssetOrder |
| 162 | The asset already exists | 资产已存在 | createAsset |
| 163 | The wallet already exists. Please try importing the private key | 钱包已经存在,请尝试导入私钥 | restoreWallet |
| 164 | worldViews do not exist | 世界观不存在 | queryWorldViews |
| 165 | There is no wallet account information on the chain | 链上没有该钱包账户信息 | restoreWallet |
| 166 | The Wallet Chain ID does not match the current chain configuration information. The chain ID of the wallet is: XX | 该钱包链id与当前链配置信息不匹配,该钱包的链id为: XXX | restoreWallet |
| 167 | The current contract version ID was not found | 当前合约版本id没有找到 X | queryContract |
| 168 | This subscription does not exist | 当前没有订阅此项 | unsubscribe |
| 169 | Method does not exist | API方法不存在 | unsubscribe |
方法:createAccountWithWallet
功能:钱包模式创建账户,钱包模式创建的账户不能用账户名密码登录。如果钱包模式已经存在账户,该操作会创建子账户,创建该子账户需要先成为终身会员账户
参数:
account:账户名注册规则,/^[a-z][a-z0-9.-]{4,63}$/,账户名长度为4-63位,由小写字母或数字构成且以字母开头
password:密码
方法:backupDownload
功能:备份钱包,调用该API会生成一个钱包文件,自动下载
参数:
callback:回调函数
方法:loadWalletFile
功能:对于web来说,file input绑定change事件,读取钱包文件
参数:
file:宿主环境若在web => file input 的 change事件触发返回的事件对象event.target,files[0]
方法:restoreWallet
功能:备份钱包,调用该API会生成一个钱包文件,自动下载。该API调用后,钱包处于锁定状态。
参数:
password:备份文件的钱包密码
方法:importPrivateKey
功能:导入私钥到钱包
参数:
privateKey:明文私钥
password:如果是已经创建钱包或恢复钱包,此时的密码为原来钱包的密码,否则是可以随意填写的临时密码
方法:deleteWallet
功能:删除钱包,使用账户模式时,最好让用户先执行删除钱包
参数:
方法:getAccounts
功能:获取钱包账户列表
参数:
callback:若有该参数,则内部会自动判断进行链初始化,否则直接返回数据,格式示例:
{
"accounts": ["tom0002"],
"currentAccount": {
"account_id": "1.2.20",
"locked": true,
"account_name": "tom0002"
}
} 方法:setCurrentAccount
功能:钱包模式切换当前使用账户
参数:
account:要切换的账户
方法:unlockAccount
功能:导入私钥或钱包模式才可以使用此方法解锁账户
参数:
password:导入私钥时设置的临时密码
方法:lockAccount
功能:锁定账户
参数:
callback:回调函数
方法:createAccountWithPassword
功能:账户注册。如果账户模式已经有账户登录,该操作会创建子账户,创建该子账户需要操作账户为终身会员账户
参数:
account:账户名注册规则,/^[a-z][a-z0-9.-]{4,63}$/,账户名长度为4-63位,由小写字母或数字构成且以字母开头
password:密码
autoLogin:boolean类型,指定是否自动登录,默认值为false
callback:回调函数
方法:passwordLogin
功能:账户登录
参数:
account:账户名
password:密码
方法:privateKeyLogin
功能:之前这个API只是过渡,严格上导入私钥只存在于钱包模式。现在为了兼容,此API还暂时留在这里,依然可用调用
参数:
privateKey:私钥
password:设置的临时密码
方法:logout
功能:该方法会清除用户相关缓存,其中包括清除加密后的密文key
参数:
callback:回调函数
方法:changePassword
功能:只有账户模式,才能修改密码;修改密码成功后,API将会自动调用退出登录。
参数:
oldPassword:旧密码
newPassword:新密码
方法:getAccountInfo
功能:当账户处于解锁状态,返回数据中将包含账户名name
参数:无
方法:createAccountWithPublicKey
功能:通过随机生成的公钥和账户名创建账户。如果账户模式已经有账户登录,该操作会创建子账户,创建该子账户需要操作账户为终身会员账户
参数:
account(string):账户名注册规则,/^[a-z][a-z0-9.-]{4,63}$/,账户名长度为4-63位,由小写字母或数字构成且以字母开头
ownerPubkey(string):账户权限公钥
activePubkey(string):资金权限公钥
方法:upgradeAccount
功能:购买终身会员账户后,可以创建子账户,此操作需消耗一定的手续费
参数:
callback:回调函数
方法:getPrivateKey
功能:获取用户active_private_key,本秘钥可用于为账户所有花费行为签名,返回的owner_private_key:可修改账户相关的各种设置,包括权限设置
参数:
callback:设置获取私钥成功后的回调函数
方法:queryAccountOperations
功能:查询用户近期操作记录
参数:
account:账户名
limit:查询记录条数
startId:开始账户记录id,如果查询范围是整个账户记录,则最开始的账户记录则是startId
endId:结束账户记录id,如果查询范围是整个账户记录,则最新的账户记录则是endId
方法:subscribeToAccountOperations
功能:订阅用户操作记录变更
参数:
account:账户名
callback:只要用户操作记录有变化,就调用此callback:回调函数
方法:queryAccountInfo
功能:账户信息中包含用户id用户名等信息
参数:
account:账户名
方法:transferAsset
功能:向目标对象发送代币
参数:
fromAccount:发款方账户名,不是发起提议,
toAccount:收款方账号名
amount:发送的代币数量
assetId:资产ID (如:X.X.X)或 代币符号(如:BTC)
memo:转账备注
isEncryption(boolean):是否加密备注
isPropose(boolean):是否发起提议
callback:设置转账后的回调函数
方法:createAsset
功能:创建token
参数:
assetId:资产符号,正则^[.A-Z]+$
precision:精度(小数位数)
maxSupply:最大资产总量
description: 资产描述,可不填
isBitAsset:是否锚定资产
callback:见统一API说明
chargeMarketFee(Object):
{
marketFeePercent:交易手续费率 (%),
maxMarketFee: 最大交易手续费
} bitassetOpts(Object):
{
feedLifetimeSec(number):喂价有效时间(分钟),
minimumFeeds(number):最少喂价次数,
forceSettlementDelaySec(number):强制清算发生前延迟时间(分钟),
forceSettlementOffsetPercent(number):强制清算价格偏离百分比,
maximumForceSettlementVolumNo open issues yet, or sync has not completed.