JavaScript接入Solidity区块链

2020年区块链火爆的时候研究过蚂蚁链,现在分享一下,JavaScript怎么接入蚂蚁链Solidity合约,其实很简单的,官方都有示例

1、打开蚂蚁链官网,使用支付宝登录进去

参考文档:https://antchain.antgroup.com/docs/11/107128

按要求申请证书,然后下载 ca.crtclient.keyclient.crt 三个文件

2、按文档要求准备合约链的账户,是否使用隐私链(这个一般情况下用不到),然后获取链节点 IP 和端口号。

3、下载JS示例,修改对应参数,如下:

const Chain = require("@alipay/mychain/index.node") //在 node 环境使用 TLS 协议
const fs = require("fs")
 
const accountKey = fs.readFileSync("./certs/user.pem", { encoding: "utf8" })
const accountPassword = "Zcc115,."  //需要替换为自定义的 user.pem 密码
 
const keyInfo = Chain.utils.getKeyInfo(accountKey, accountPassword)
//可打印私钥和公钥,使用 16 进制
//console.log('private key:', keyInfo.privateKey.toString('hex'))
//console.log('public key:', keyInfo.publicKey.toString('hex'))
// openssl ec -in user.key -passin pass:Zcc115,. -passout pass:Zcc115,. -aes256 -out user.pem
const passphrase = "Zcc115,." //需要替换为自定义的 client.key 密码
//配置选项
let opt = {
  host: '47.103.163.48',    //目标区块链网络节点的 IP
  port: 18130,          //端口号
  timeout: 30000,       //连接超时时间配置
  cert: fs.readFileSync("./certs/client.crt", { encoding: "utf8" }),
  ca: fs.readFileSync("./certs/ca.crt", { encoding: "utf8" }),
  key: fs.readFileSync("./certs/client.key", { encoding: "utf8" }),
  userPublicKey: keyInfo.publicKey,
  userPrivateKey: keyInfo.privateKey,
  userRecoverPublicKey: keyInfo.publicKey,
  userRecoverPrivateKey: keyInfo.privateKey,
  passphrase: passphrase
}
 
//初始化一个连接实例
const chain = Chain(opt)
//---------------------------------------------------------------------------------------------------------------------
//调用 API 查询最后的一个区块数据
chain.ctr.QueryLastBlock({}, (err, data) => {
  console.log('raw data:', data)                                     //区块结构数据
  console.log('block hash:', data.block.block_header.hash)             //区块哈希
  console.log('block number:', data.block.block_header.block_number) //区块高度
})
//---------------------------------------------------------------------------------------------------------------------
// 创建账户
// const newAccountName = 'zhouxiang' + Date.now()
// const newAccountName = 'zhouxiangxxx'
// const newKey = Chain.utils.generateECKey();
// chain.ctr.CreateAccount({
//   from: 'zhouxiang',
//   to: newAccountName,
//   data: {
//     recover_key: '0x'+ newKey.publicKey.toString('hex'),
//     auth_key: '0x'+ newKey.publicKey.toString('hex'),
//     auth_weight: 100
//   }
// }, (err, data) => {
//   // data.receipt.result === 0 表示成功
//   console.log(data)
// })
//---------------------------------------------------------------------------------------------------------------------
// 错误码参考地址
// https://antchain.antgroup.com/docs/11/101803#%E5%8C%BA%E5%9D%97%E9%93%BE%E9%94%99%E8%AF%AF%E7%A0%81
//转账
// chain.ctr.TransferBalance({
//   from: 'zhouxiang',
//   to: 'zhouxiangxxx',
//   value: 100
// }, (err, data) => {
//   console.log(data)
// })
//---------------------------------------------------------------------------------------------------------------------
// 构造合约实例

然后就可以参考文档:https://antchain.antgroup.com/docs/11/107131 进行开发

上面示例API “查询最后一个区块【QueryLastBlock】” 数据运行结果:

剩下的就是结合自己的具体需求根据API进行开发即可。

比如创建一个账户进行转账之类的。