4. Js SDK 接入指南

JS SDK 连接开放网络与 Go SDK 有些许不同之处,主要在配置文件,JS SDK 不需要配置文件,只需要在创建客户端时指定参数即可。

4.1. 下载私钥

在开放网络上创建账户后,会为用户生成加密的私钥,使用时需要使用密码解密后才可使用。

登录到开放网络后,通过控制台可以下载个人账户的私钥文件

../_images/xuperos-private-key-dl.png

4.2. 引入SDK

  1. npm install save @xuperchain/xuper-sdk

4.3. 加载私钥

  1. const {default:XuperSDK,Endorsement} = require(“@xuperchain/xuper-sdk”)
  2. const xsdk = XuperSDK.getInstance()
  3. // 其中最后一个参数 true 代表缓存起来,之后 sdk 发送交易默认使用本次 import 的账户。
  4. const acc = xsdk.import(“你的密码”, “你的私钥文件内容”, true);

4.4. 连接开放网络

  1. const {default:XuperSDK,Endorsement} = require(“@xuperchain/xuper-sdk”)
  2. // 连接开放网络可选择 grpc 与 http方式,本文采用http方式
  3. // grpc 地址: 39.156.69.83:37100
  4. const node = https://xuper.baidu.com/nodeapi‘; // node
  5. const chain = xuper’; // chain
  6. // 背书服务插件,连接开放网络必须加载背书服务插件
  7. // 如果node 采用grpc方式,背书服务插件的 server 应与node保持一致
  8. const params = {
  9. server: https://xuper.baidu.com/nodeapi“,
  10. fee: 400”, // 服务费
  11. endorseServiceCheckAddr: jknGxa6eyum1JrATWvSJKW3thJ9GKHA9n”,
  12. endorseServiceFeeAddr: aB2hpHnTBDxko3UoP2BpBZRujwhdcAFoT
  13. }
  14. const plugins = [
  15. Endorsement({
  16. transfer: params,
  17. makeTransaction: params
  18. })
  19. ];
  20. const xsdk = XuperSDK.getInstance({
  21. node,
  22. chain,
  23. env:{
  24. node:{
  25. disableGRPC:true // 如使用http方式连接开放网络,需要禁用grpc
  26. }
  27. },
  28. plugins
  29. });

此时你的 SDK client 便连接到了开放网络,可以进行部署、调用合约了

4.5. 合约部署

注解

  • 开放网络目前仅支持部署EVM合约与c++ wasm合约。

  • 本文测试采用EVM counter 合约作为示例,合约内容见:Counter

  1. const {default:XuperSDK,Endorsement} = require(“@xuperchain/xuper-sdk”)
  2. const node = https://xuper.baidu.com/nodeapi‘;
  3. const chain = xuper’;
  4. const params = {
  5. server: https://xuper.baidu.com/nodeapi“,
  6. fee: 400”,
  7. endorseServiceCheckAddr: jknGxa6eyum1JrATWvSJKW3thJ9GKHA9n”,
  8. endorseServiceFeeAddr: aB2hpHnTBDxko3UoP2BpBZRujwhdcAFoT
  9. }
  10. const plugins = [
  11. Endorsement({
  12. transfer: params,
  13. makeTransaction: params
  14. })
  15. ];
  16. const xsdk = XuperSDK.getInstance({
  17. node,
  18. chain,
  19. env:{
  20. node:{
  21. disableGRPC:true
  22. }
  23. },
  24. plugins
  25. });
  26. const acc = xsdk.import(“安全码”,”开放网络私钥内容”,true)
  27. const contractAccount = “开放网络工作台注册的合约账户”
  28. // evm 合约的 abi 以及 bin。这里只是示例,你可以编写自己的合约,然后编译出 abi 和 bin。
  29. const abi = “[{“inputs”:[],”payable”:false,”stateMutability”:”nonpayable”,”type”:”constructor”},{“constant”:true,”inputs”:[{“internalType”:”string”,”name”:”key”,”type”:”string”}],”name”:”get”,”outputs”:[{“internalType”:”uint256”,”name”:””,”type”:”uint256”}],”payable”:false,”stateMutability”:”view”,”type”:”function”},{“constant”:true,”inputs”:[],”name”:”getOwner”,”outputs”:[{“internalType”:”address”,”name”:””,”type”:”address”}],”payable”:false,”stateMutability”:”view”,”type”:”function”},{“constant”:false,”inputs”:[{“internalType”:”string”,”name”:”key”,”type”:”string”}],”name”:”increase”,”outputs”:[],”payable”:true,”stateMutability”:”payable”,”type”:”function”},{“constant”:false,”inputs”:[{“internalType”:”string”,”name”:”a”,”type”:”string”},{“internalType”:”string”,”name”:”b”,”type”:”string”}],”name”:”join”,”outputs”:[{“internalType”:”string”,”name”:”c”,”type”:”string”},{“internalType”:”string”,”name”:”d”,”type”:”string”}],”payable”:false,”stateMutability”:”nonpayable”,”type”:”function”}]”;
  30. const bin = 608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506106ef806100606000396000f3fe60806040526004361061003f5760003560e01c806329803b2114610044578063693ec85e14610288578063893d20e814610364578063ae896c87146103bb575b600080fd5b34801561005057600080fd5b506101a16004803603604081101561006757600080fd5b810190808035906020019064010000000081111561008457600080fd5b82018360208201111561009657600080fd5b803590602001918460018302840111640100000000831117156100b857600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561011b57600080fd5b82018360208201111561012d57600080fd5b8035906020019184600183028401116401000000008311171561014f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610476565b604051808060200180602001838103835285818151815260200191508051906020019080838360005b838110156101e55780820151818401526020810190506101ca565b50505050905090810190601f1680156102125780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561024b578082015181840152602081019050610230565b50505050905090810190601f1680156102785780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b34801561029457600080fd5b5061034e600480360360208110156102ab57600080fd5b81019080803590602001906401000000008111156102c857600080fd5b8201836020820111156102da57600080fd5b803590602001918460018302840111640100000000831117156102fc57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061049d565b6040518082815260200191505060405180910390f35b34801561037057600080fd5b50610379610510565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b610474600480360360208110156103d157600080fd5b81019080803590602001906401000000008111156103ee57600080fd5b82018360208201111561040057600080fd5b8035906020019184600183028401116401000000008311171561042257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610539565b005b606080836002908051906020019061048f929190610615565b508284915091509250929050565b60006001826040518082805190602001908083835b602083106104d557805182526020820191506020810190506020830392506104b2565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020549050919050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b600180826040518082805190602001908083835b60208310610570578051825260208201915060208101905060208303925061054d565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902054016001826040518082805190602001908083835b602083106105db57805182526020820191506020810190506020830392506105b8565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390208190555050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061065657805160ff1916838001178555610684565b82800160010185558215610684579182015b82811115610683578251825591602001919060010190610668565b5b5090506106919190610695565b5090565b6106b791905b808211156106b357600081600090555060010161069b565b5090565b9056fea265627a7a72315820cf953dc1839436e254d5a3c4c228d708600f054f50da1950ddc1960647386ef364736f6c63430005110032”;
  31. // 在部署其他合约时请使用 xsdk.deployContract()
  32. const start = async () => {
  33. try {
  34. const demo = await xsdk.deploySolidityContract(
  35. contractAccount, // 合约账户
  36. counter’, // 合约名字
  37. bin, // evm 合约 bin
  38. abi, // evm 合约 abi
  39. evm”,
  40. {
  41. creator”: contractAccount // 合约有初始化参数
  42. },
  43. acc
  44. );
  45. const res = await xsdk.postTransaction(demo.transaction);
  46. console.log(demo)
  47. }
  48. catch (e){
  49. console.log(e)
  50. }
  51. };
  52. start()

4.6. 合约调用

注解

  • EVM合约,请使用 xsdk.invokeSolidityContarct()

  • 其他合约,请使用 xsdk.invokeContract()

  1. const {default:XuperSDK,Endorsement} = require(“@xuperchain/xuper-sdk”)
  2. const node = https://xuper.baidu.com/nodeapi‘;
  3. const chain = xuper’;
  4. const params = {
  5. server: https://xuper.baidu.com/nodeapi“,
  6. fee: 400”,
  7. endorseServiceCheckAddr: jknGxa6eyum1JrATWvSJKW3thJ9GKHA9n”,
  8. endorseServiceFeeAddr: aB2hpHnTBDxko3UoP2BpBZRujwhdcAFoT
  9. }
  10. const plugins = [
  11. Endorsement({
  12. transfer: params,
  13. makeTransaction: params
  14. })
  15. ];
  16. const xsdk = XuperSDK.getInstance({
  17. node,
  18. chain,
  19. env:{
  20. node:{
  21. disableGRPC:true
  22. }
  23. },
  24. plugins
  25. });
  26. const acc = xsdk.import(“安全码”,”开放网络私钥内容”,true)
  27. const contractName = Counter
  28. const contractMethod = increase
  29. const start = async () => {
  30. try {
  31. const demo = await xsdk.invokeSolidityContarct(
  32. contractName, // 合约名字
  33. contractMethod, // 合约方法
  34. evm”,
  35. {
  36. key”: xuperos // 合约参数
  37. },
  38. 0”,
  39. acc
  40. );
  41. const res = await xsdk.postTransaction(demo.transaction);
  42. console.log(demo)
  43. }
  44. catch (e){
  45. console.log(e)
  46. }
  47. };
  48. start()

具体接口文档参考 JS SDK 使用文档