1.6. XuperChain http 接口使用说明

XuperChain为方便用户深度使用 XuperChain 的各项功能,提供了多语言版本的SDK(JSGolangC#JavaPython),这里我们以Golang为例来介绍一下XuperChain的http接口使用方式。

注解

目前官方提供的SDK中,golang语言版本的功能最为全面,其他语言的功能弱一些,我们非常欢迎社区朋友一起建设SDK,参与贡献会获得开放网络的资源,可用于购买开放网络的服务。

1.6.1. http接口介绍

查看XuperChain的 proto文件 ,可以在service定义中获取所有支持的http接口

1.6.1.1. GetBalance

此接口用于查询指定地址中的余额

http方法:POST

请求URL:/v1/get_balance

header:Content-Type:application/json

参数说明

参数结构

AddressStatus

返回结构

AddressStatus

AddressStatus

  1. message AddressStatus {
  2. Header header = 1;
  3. string address = 2;
  4. repeated TokenDetail bcs = 3;
  5. }

TokenDetail
  1. message TokenDetail {
  2. string bcname = 1;
  3. string balance = 2;
  4. XChainErrorEnum error = 3;
  5. }

Header
  1. message Header {
  2. string logid = 1;
  3. string from_node = 2;
  4. XChainErrorEnum error = 3;
  5. }

请求示例

  • 其中的 address 字段为需要查询的地址,传入string即可

  • 其中的 bcs 字段为需要查询的链名,因为XuperChain支持平行链的功能,此字段为列表,亦可传入多个链名

  • Header中的logid是回复中也会携带的id,用来对应请求或追溯日志使用的,一般用 xupercore/lib/utils/utils.go 生成一个全局唯一id

  • Header中的from_node一般不需要填写,error字段也是返回中携带的错误内容,发请求时不需填写

  • 请求时只需传入 bcname 字段,例如 “xuper”,其余字段为返回时携带的,balance即为对应平行链上的余额

  1. var host = http://127.0.0.1:37301/v1/get_balance
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.AddressStatus{}
  7. params.Bcs = []*pb.TokenDetail{}
  8. tokenDetail := new(pb.TokenDetail)
  9. tokenDetail.Bcname = xuper
  10. params.Bcs = append(params.Bcs, tokenDetail)
  11. params.Address = TeyyPLpp9L7QAcxHangtcHTu7HUZ6iydY
  12. sendBody, err := json.Marshal(params)
  13. if err != nil {
  14. fmt.Println(err)
  15. }
  16. sendData := string(sendBody)
  17. client := &http.Client{}
  18. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  19. if err != nil {
  20. fmt.Println(err)
  21. }
  22. request.Header.Set(“Content-Type”, application/json”)
  23. response, err := client.Do(request)
  24. defer response.Body.Close()
  25. result, err := ioutil.ReadAll(response.Body)
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. fmt.Println(string(result))

1.6.1.2. GetBalanceDetail

此接口用于查询指定地址中的余额详细情况

http方法:POST

请求URL:/v1/get_balance_detail

header:Content-Type:application/json

参数说明

参数结构

AddressBalanceStatus

返回结构

AddressBalanceStatus

AddressBalanceStatus

  1. message AddressBalanceStatus {
  2. Header header = 1;
  3. string address = 2;
  4. repeated TokenFrozenDetails tfds = 3;
  5. }

请求示例

  • address字段与GetBalance一样,tfds字段则多了是否冻结的内容,tfds在请求中只需要填充bcname,返回时会有TokenFrozenDetail数组给出正常余额和冻结余额的信息
  1. var host = http://127.0.0.1:37301/v1/get_balance_detail
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.AddressBalanceStatus{}
  7. tokenFrozenDetails := new(pb.TokenFrozenDetails)
  8. tokenFrozenDetails.Bcname = xuper
  9. params.Tfds = append(params.Tfds, tokenFrozenDetails)
  10. params.Address = TeyyPLpp9L7QAcxHangtcHTu7HUZ6iydY
  11. sendBody, err := json.Marshal(params)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. sendData := string(sendBody)
  16. client := &http.Client{}
  17. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  18. if err != nil {
  19. fmt.Println(err)
  20. }
  21. request.Header.Set(“Content-Type”, application/json”)
  22. response, err := client.Do(request)
  23. defer response.Body.Close()
  24. result, err := ioutil.ReadAll(response.Body)
  25. if err != nil {
  26. fmt.Println(err)
  27. }
  28. fmt.Println(string(result))

1.6.1.3. GetFrozenBalance

此接口用于查询指定地址中的冻结余额

http方法:POST

请求URL:/v1/get_frozen_balance

header:Content-Type:application/json

参数说明

参数结构

AddressStatus

返回结构

AddressStatus

AddressStatus

  1. message AddressStatus {
  2. Header header = 1;
  3. string address = 2;
  4. repeated TokenDetail bcs = 3;
  5. }

请求示例

  • address字段与GetBalance一样,tfds字段则多了是否冻结的内容,tfds在请求中只需要填充bcname,返回时会有TokenFrozenDetail数组给出正常余额和冻结余额的信息
  1. var host = http://127.0.0.1:37301/v1/get_frozen_balance
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.AddressStatus{}
  7. params.Bcs = []*pb.TokenDetail{}
  8. tokenDetail := new(pb.TokenDetail)
  9. tokenDetail.Bcname = xuper
  10. params.Bcs = append(params.Bcs, tokenDetail)
  11. params.Address = TeyyPLpp9L7QAcxHangtcHTu7HUZ6iydY
  12. sendBody, err := json.Marshal(params)
  13. if err != nil {
  14. fmt.Println(err)
  15. }
  16. sendData := string(sendBody)
  17. client := &http.Client{}
  18. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  19. if err != nil {
  20. fmt.Println(err)
  21. }
  22. request.Header.Set(“Content-Type”, application/json”)
  23. response, err := client.Do(request)
  24. defer response.Body.Close()
  25. result, err := ioutil.ReadAll(response.Body)
  26. if err != nil {
  27. fmt.Println(err)
  28. }
  29. fmt.Println(string(result))

1.6.1.4. GetBlock

此接口用于查询指定id的区块内容

http方法:POST

请求URL:/v1/get_block

header:Content-Type:application/json

参数说明

参数结构

BlockID

返回结构

Block

BlockID

  1. message BlockID {
  2. Header header = 4;
  3. string bcname = 1;
  4. bytes blockid = 2;
  5. bool need_content = 3; //是否需要内容
  6. }

请求示例

  • blocked为要查询的区块id,注意是bytes类型,可能需要hex decode

  • need_content字段为布尔值,表明是否需要详细的区块内容(还是只查询区块是否在链和前驱后继)

  1. var host = http://127.0.0.1:37301/v1/get_block
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.BlockID{}
  7. params.Bcname = xuper
  8. params.Blockid, _ = hex.DecodeString(“9a2ba41af3621ce372352491552d75ff5d43e393dfdd98f02b9056bfd2303f97”)
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))

1.6.1.5. GetBlockByHeight

此接口用于查询指定高度的区块内容

http方法:POST

请求URL:/v1/get_block_by_height

header:Content-Type:application/json

参数说明

参数结构

BlockHeight

返回结构

Block

BlockHeight

  1. message BlockHeight {
  2. Header header = 3;
  3. string bcname = 1;
  4. int64 height = 2;
  5. }

请求示例

  • 同GetBlock类似,id换成整型的高度即可,返回内容也是类似的
  1. var host = http://127.0.0.1:37301/v1/get_block_by_height
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.BlockHeight{}
  7. params.Bcname = xuper
  8. params.Height = 88
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))

1.6.1.6. GetBlockChainStatus

此接口用于查询指定链的当前状态

http方法:POST

请求URL:/v1/get_bcstatus

header:Content-Type:application/json

参数说明

参数结构

BCStatus

返回结构

BCStatus

BCStatus

  1. message BCStatus {
  2. Header header = 1;
  3. string bcname = 2;
  4. LedgerMeta meta = 3;
  5. InternalBlock block = 4;
  6. UtxoMeta utxoMeta = 5;
  7. repeated string branchBlockid = 6;
  8. }

请求示例

  • 传入参数只需填充header,bcname即可
  1. var host = http://127.0.0.1:37301/v1/get_bcstatus
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.BCStatus{}
  7. params.Bcname = xuper
  8. sendBody, err := json.Marshal(params)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. sendData := string(sendBody)
  13. client := &http.Client{}
  14. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  15. if err != nil {
  16. fmt.Println(err)
  17. }
  18. request.Header.Set(“Content-Type”, application/json”)
  19. response, err := client.Do(request)
  20. defer response.Body.Close()
  21. result, err := ioutil.ReadAll(response.Body)
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. fmt.Println(string(result))

1.6.1.7. GetBlockChains

此接口用于查询当前节点上有哪些链

http方法:POST

请求URL:/v1/get_bcchains

header:Content-Type:application/json

参数说明

参数结构

CommonIn

返回结构

BlockChains

CommonIn

  1. message CommonIn {
  2. Header header = 1;
  3. ViewOption view_option = 2;
  4. }

请求示例

  • CommonIn结构很简单,只有header字段,返回的BlockChains也仅有一个链名的string数组
  1. var host = http://127.0.0.1:37301/v1/get_bcchains
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.CommonIn{}
  7. sendBody, err := json.Marshal(params)
  8. if err != nil {
  9. fmt.Println(err)
  10. }
  11. sendData := string(sendBody)
  12. client := &http.Client{}
  13. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  14. if err != nil {
  15. fmt.Println(err)
  16. }
  17. request.Header.Set(“Content-Type”, application/json”)
  18. response, err := client.Do(request)
  19. defer response.Body.Close()
  20. result, err := ioutil.ReadAll(response.Body)
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. fmt.Println(string(result))

1.6.1.8. GetSystemStatus

此接口用于查询当前节点的运行状态

http方法:POST

请求URL:/v1/get_sysstatus

header:Content-Type:application/json

参数说明

参数结构

CommonIn

返回结构

SystemsStatusReply

CommonIn

  1. message CommonIn {
  2. Header header = 1;
  3. ViewOption view_option = 2;
  4. }

请求示例

  • 此接口相当于先查询了GetBlockChains,在用GetBlockChainStatus查询每个链的状态
  1. var host = http://127.0.0.1:37301/v1/get_sysstatus
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.CommonIn{}
  7. sendBody, err := json.Marshal(params)
  8. if err != nil {
  9. fmt.Println(err)
  10. }
  11. sendData := string(sendBody)
  12. client := &http.Client{}
  13. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  14. if err != nil {
  15. fmt.Println(err)
  16. }
  17. request.Header.Set(“Content-Type”, application/json”)
  18. response, err := client.Do(request)
  19. defer response.Body.Close()
  20. result, err := ioutil.ReadAll(response.Body)
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. fmt.Println(string(result))

1.6.1.9. QueryACL

此接口用于查询指定合约账号的ACL内容

http方法:POST

请求URL:/v1/query_acl

header:Content-Type:application/json

参数说明

参数结构

AclStatus

返回结构

AclStatus

AclStatus

  1. message AclStatus {
  2. Header header = 1;
  3. string bcname = 2;
  4. string accountName = 3;
  5. string contractName = 4;
  6. string methodName = 5;
  7. bool confirmed = 6;
  8. Acl acl = 7;
  9. }

请求示例

  • 请求中仅需填充header,bcname,accountName即可,其余为返回内容
  1. var host = http://127.0.0.1:37301/v1/query_acl
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.AclStatus{}
  7. params.Bcname = xuper
  8. params.AccountName = XC1234567812345678@xuper
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))

1.6.1.10. QueryTx

此接口用于查询指定id的交易内容

http方法:POST

请求URL:/v1/query_tx

header:Content-Type:application/json

参数说明

参数结构

TxStatus

返回结构

TxStatus

TxStatus

  1. message TxStatus {
  2. Header header = 1;
  3. string bcname = 2;
  4. bytes txid = 3;
  5. TransactionStatus status = 4; //当前状态
  6. int64 distance = 5; //离主干末端的距离(如果在主干上)
  7. Transaction tx = 7;
  8. }

请求示例

  • 请求中仅需填充header,bcname,txid字段
  1. var host = http://127.0.0.1:37301/v1/query_tx
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.TxStatus{}
  7. params.Bcname = xuper
  8. params.Txid, _ = hex.DecodeString(“1511fc468949eaf63bc2a7c35d81d4b5fb9690ec1e1874e7645ea8cc660864d7”)
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))

1.6.1.11. SelectUTXO

此接口用于获取账号可用的utxo列表

http方法:POST

请求URL:/v1/select_utxos_v2

header:Content-Type:application/json

参数说明

参数结构

UtxoInput

返回结构

UtxoOutput

UtxoInput

  1. message UtxoInput {
  2. Header header = 1;
  3. // which bcname to select
  4. string bcname = 2;
  5. // address to select
  6. string address = 3;
  7. // publickey of the address
  8. string publickey = 4;
  9. // totalNeed refer the total need utxos to select
  10. string totalNeed = 5;
  11. // userSign of input
  12. bytes userSign = 7;
  13. // need lock
  14. bool needLock = 8;
  15. }

请求示例

  • 请求中只需填充header,bcname,address,totalNeed,needLock,其中needLock表示是否需要锁定utxo(适用于并发执行场景)

  • UtxoOutput中的返回即可在组装交易时使用,具体组装交易的过程可参考文档下方

  1. var host = http://127.0.0.1:37301/v1/select_utxos_v2
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.UtxoInput{}
  7. params.Bcname = xuper
  8. params.Address = dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN
  9. params.NeedLock = true
  10. params.TotalNeed = 50
  11. sendBody, err := json.Marshal(params)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. sendData := string(sendBody)
  16. client := &http.Client{}
  17. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  18. if err != nil {
  19. fmt.Println(err)
  20. }
  21. request.Header.Set(“Content-Type”, application/json”)
  22. response, err := client.Do(request)
  23. defer response.Body.Close()
  24. result, err := ioutil.ReadAll(response.Body)
  25. if err != nil {
  26. fmt.Println(err)
  27. }
  28. fmt.Println(string(result))

1.6.1.12. SelectUTXOBySize

此接口用于获取账号中部分utxo,填满交易后便不在继续获取

http方法:POST

请求URL:/v1/select_utxo_by_size

header:Content-Type:application/json

参数说明

参数结构

UtxoInput

返回结构

UtxoOutput

UtxoInput

  1. message UtxoInput {
  2. Header header = 1;
  3. // which bcname to select
  4. string bcname = 2;
  5. // address to select
  6. string address = 3;
  7. // publickey of the address
  8. string publickey = 4;
  9. // totalNeed refer the total need utxos to select
  10. string totalNeed = 5;
  11. // userSign of input
  12. bytes userSign = 7;
  13. // need lock
  14. bool needLock = 8;
  15. }

请求示例

  • 使用过程和SelectUTXO基本相同,仅少了totalNeed字段。适用拥有太多utxo,一次SelectUtxo内容超过交易容纳上限时使用
  1. var host = http://127.0.0.1:37301/v1/select_utxo_by_size
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.UtxoInput{}
  7. params.Bcname = xuper
  8. params.Address = dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN
  9. params.NeedLock = true
  10. sendBody, err := json.Marshal(params)
  11. if err != nil {
  12. fmt.Println(err)
  13. }
  14. sendData := string(sendBody)
  15. client := &http.Client{}
  16. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  17. if err != nil {
  18. fmt.Println(err)
  19. }
  20. request.Header.Set(“Content-Type”, application/json”)
  21. response, err := client.Do(request)
  22. defer response.Body.Close()
  23. result, err := ioutil.ReadAll(response.Body)
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. fmt.Println(string(result))

1.6.1.13. PreExec

此接口用于在节点上进行合约的预执行操作,返回预执行后的请求和回复

http方法:POST

请求URL:/v1/preexec

header:Content-Type:application/json

参数说明

参数结构

InvokeRPCRequest

返回结构

InvokeRPCResponse

InvokeRPCRequest

  1. message InvokeRPCRequest {
  2. Header header = 1;
  3. string bcname = 2;
  4. repeated InvokeRequest requests = 3;
  5. string initiator = 4;
  6. repeated string auth_require = 5;
  7. }

InvokeRequest
  1. message InvokeRequest {
  2. string module_name = 1;
  3. string contract_name = 2;
  4. string method_name = 3;
  5. map<string, bytes> args = 4;
  6. repeated ResourceLimit resource_limits = 5;
  7. string amount = 6;
  8. }

请求示例

  • 其中必填字段有module_name,contract_name,method_name,args
  1. var host = http://127.0.0.1:37301/v1/preexec
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.InvokeRPCRequest{}
  7. params.Bcname = xuper
  8. //需要先部署合约,才可以调用。
  9. var invokeRequest = &pb.InvokeRequest{}
  10. invokeRequest.ModuleName = evm
  11. invokeRequest.ContractName = Cafe20
  12. invokeRequest.MethodName = mint
  13. invokeRequest.Args = map[string][]byte{
  14. creator”:[]byte(“alice”),
  15. }
  16. params.Requests = append(params.Requests, invokeRequest)
  17. sendBody, err := json.Marshal(params)
  18. if err != nil {
  19. fmt.Println(err)
  20. }
  21. sendData := string(sendBody)
  22. client := &http.Client{}
  23. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. request.Header.Set(“Content-Type”, application/json”)
  28. response, err := client.Do(request)
  29. defer response.Body.Close()
  30. result, err := ioutil.ReadAll(response.Body)
  31. if err != nil {
  32. fmt.Println(err)
  33. }
  34. fmt.Println(string(result))

1.6.1.14. PreExecWithSelectUTXO

此接口用于在节点上进行消耗资源的合约预执行操作,内部是由一个PreExec加上一个SelectUTXO实现的,预执行并选择出需要消耗数额的utxo

http方法:POST

请求URL:/v1/preexec_select_utxo

header:Content-Type:application/json

参数说明

参数结构

PreExecWithSelectUTXORequest

返回结构

PreExecWithSelectUTXOResponse

PreExecWithSelectUTXORequest

  1. message PreExecWithSelectUTXORequest {
  2. Header header = 1;
  3. string bcname = 2;
  4. string address = 3;
  5. int64 totalAmount = 4;
  6. SignatureInfo signInfo = 6;
  7. bool needLock = 7;
  8. InvokeRPCRequest request = 5;
  9. }

请求示例

  • 把预执行的请求结构放在了SelectUTXO结构中
  1. var host = http://127.0.0.1:37301/v1/preexec_select_utxo
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.PreExecWithSelectUTXORequest{}
  7. var invokeRPCRequest = &pb.InvokeRPCRequest{}
  8. params.Bcname = xuper
  9. var invokeRequest = &pb.InvokeRequest{}
  10. invokeRequest.ModuleName = evm
  11. invokeRequest.ContractName = Cafe20
  12. invokeRequest.MethodName = mint
  13. invokeRequest.Args = map[string][]byte{
  14. creator”:[]byte(“alice”),
  15. }
  16. invokeRPCRequest.Requests = append(invokeRPCRequest.Requests, invokeRequest)
  17. params.Request = invokeRPCRequest
  18. params.Address = dpzuVdosQrF2kmzumhVeFQZa1aYcdgFpN
  19. params.TotalAmount = 193
  20. // sign start
  21. // 假设alice 是一个account, 使用 crypto 获取 alice 的私钥,然后进行签名
  22. cryptoClient := crypto.GetCryptoClient()
  23. privateKey, err := cryptoClient.GetEcdsaPrivateKeyFromJsonStr(alice.PrivateKey)
  24. if err != nil{
  25. return nil,err
  26. }
  27. sign, err := cryptoClient.SignECDSA(privateKey, digestHash)
  28. if err != nil{
  29. return nil,err
  30. }
  31. signInfo := &pb.SignInfo{
  32. Address: alice.Address,
  33. PublicKey: alice.PublicKey,
  34. Sign: sign,
  35. }
  36. params.SignInfo = signInfo
  37. sendBody, err := json.Marshal(params)
  38. if err != nil {
  39. fmt.Println(err)
  40. }
  41. sendData := string(sendBody)
  42. client := &http.Client{}
  43. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  44. if err != nil {
  45. fmt.Println(err)
  46. }
  47. request.Header.Set(“Content-Type”, application/json”)
  48. response, err := client.Do(request)
  49. defer response.Body.Close()
  50. result, err := ioutil.ReadAll(response.Body)
  51. if err != nil {
  52. fmt.Println(err)
  53. }
  54. fmt.Println(string(result))

1.6.1.15. PostTx

此接口用于提交交易,是大部分操作都需要的最终环节

http方法:POST

请求URL:/v1/post_tx

header:Content-Type:application/json

参数说明

参数结构

TxStatus

返回结构

CommonReply

TxStatus

  1. message Transaction {
  2. // txid is the id of this transaction
  3. bytes txid = 1;
  4. // the blockid the transaction belong to
  5. bytes blockid = 2;
  6. // Transaction input list
  7. repeated TxInput tx_inputs = 3;
  8. // Transaction output list
  9. repeated TxOutput tx_outputs = 4;
  10. // Transaction description or system contract
  11. bytes desc = 6;
  12. // Mining rewards
  13. bool coinbase = 7;
  14. // Random number used to avoid replay attacks
  15. string nonce = 8;
  16. // Timestamp to launch the transaction
  17. int64 timestamp = 9;
  18. // tx format version; tx格式版本号
  19. int32 version = 10;
  20. // auto generated tx
  21. bool autogen = 11;
  22. repeated TxInputExt tx_inputs_ext = 23;
  23. repeated TxOutputExt tx_outputs_ext = 24;
  24. repeated InvokeRequest contract_requests = 25;
  25. // 权限系统新增字段
  26. // 交易发起者, 可以是一个Address或者一个Account
  27. string initiator = 26;
  28. // 交易发起需要被收集签名的AddressURL集合信息,包括用于utxo转账和用于合约调用
  29. repeated string auth_require = 27;
  30. // 交易发起者对交易元数据签名,签名的内容包括auth_require字段
  31. repeated SignatureInfo initiator_signs = 28;
  32. // 收集到的签名
  33. repeated SignatureInfo auth_require_signs = 29;
  34. // 节点收到tx的时间戳,不参与签名
  35. int64 received_timestamp = 30;
  36. // 统一签名(支持多重签名/环签名等,与initiator_signs/auth_require_signs不同时使用)
  37. XuperSignature xuper_sign = 31;
  38. // 可修改区块链标记
  39. ModifyBlock modify_block = 32;
  40. // HD加解密相关信息
  41. HDInfo HD_info = 33;
  42. }

请求示例

  • Transaction属于XuperChain中比较核心的结构
  1. var host = http://127.0.0.1:37301/v1/post_tx
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. // 一般 TxStatus 都是自动拼装的,无需手动拼接,感兴趣的请自行查阅 go sdk 相关源代码
  7. var params = &pb.TxStatus{}
  8. sendBody, err := json.Marshal(params)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. sendData := string(sendBody)
  13. client := &http.Client{}
  14. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  15. if err != nil {
  16. fmt.Println(err)
  17. }
  18. request.Header.Set(“Content-Type”, application/json”)
  19. response, err := client.Do(request)
  20. defer response.Body.Close()
  21. result, err := ioutil.ReadAll(response.Body)
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. fmt.Println(string(result))

1.6.1.16. QueryUtxoRecord

此接口用于查询合约账户的utxo情况

http方法:POST

请求URL:/v1/query_utxo_record

header:Content-Type:application/json

参数说明

参数结构

UtxoRecordDetail

返回结构

UtxoRecordDetail

UtxoRecordDetail

  1. message UtxoRecordDetail {
  2. Header header = 1;
  3. string bcname = 2;
  4. string accountName = 3;
  5. UtxoRecord openUtxoRecord = 4;
  6. UtxoRecord lockedUtxoRecord = 5;
  7. UtxoRecord frozenUtxoRecord = 6;
  8. int64 displayCount = 7;
  9. }

请求示例

  1. var host = http://127.0.0.1:37301/v1/query_utxo_record
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.UtxoRecordDetail{}
  7. params.Bcname = xuper
  8. params.AccountName = XC1234567812345678@xuper
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))

1.6.1.17. QueryContractStatData

此接口用于查询链上的合约账户与合约数

http方法:POST

请求URL:/v1/query_contract_stat_data

header:Content-Type:application/json

参数说明

参数结构

ContractStatDataRequest

返回结构

ContractStatDataResponse

ContractStatDataRequest

  1. message ContractStatDataRequest {
  2. Header header = 1;
  3. string bcname = 2;
  4. }

请求示例

  1. var host = http://127.0.0.1:37301/v1/query_contract_stat_data
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.ContractStatDataRequest{}
  7. params.bcname = xuper
  8. sendBody, err := json.Marshal(params)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. sendData := string(sendBody)
  13. client := &http.Client{}
  14. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  15. if err != nil {
  16. fmt.Println(err)
  17. }
  18. request.Header.Set(“Content-Type”, application/json”)
  19. response, err := client.Do(request)
  20. defer response.Body.Close()
  21. result, err := ioutil.ReadAll(response.Body)
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. fmt.Println(string(result))

1.6.1.18. GetAccountContracts

此接口用于查询合约账号下所有的合约

http方法:POST

请求URL:/v1/get_account_contracts

header:Content-Type:application/json

参数说明

参数结构

GetAccountContractsRequest

返回结构

GetAccountContractsResponse

GetAccountContractsRequest

  1. message GetAccountContractsRequest {
  2. Header header = 1;
  3. string bcname = 2;
  4. string account = 3;
  5. }

请求示例

  1. var host = http://127.0.0.1:37301/v1/get_account_contracts
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.GetAccountContractsRequest{}
  7. params.Bcname = xuper
  8. params.Account = XC1234567812345678@xuper
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))

1.6.1.19. GetAccountByAK

此接口用于查询普通账号的合约账号

http方法:POST

请求URL:/v1/get_account_by_ak

header:Content-Type:application/json

参数说明

参数结构

AK2AccountRequest

返回结构

AK2AccountResponse

AK2AccountRequest

  1. message AK2AccountRequest {
  2. Header header = 1;
  3. string bcname = 2;
  4. string address = 3;
  5. }

请求示例

  1. var host = http://127.0.0.1:37301/v1/get_account_by_ak
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.AK2AccountRequest{}
  7. params.Bcname = xuper
  8. params.Address = gH2XKGrne4mL5y37vwChzvmLDEWkXnuB8
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))

1.6.1.20. GetAddressContracts

此接口用于查询普通账号部署的合约

http方法:POST

请求URL:/v1/get_address_contracts

header:Content-Type:application/json

参数说明

参数结构

AddressContractsRequest

返回结构

AddressContractsResponse

AddressContractsRequest

  1. message AddressContractsRequest {
  2. Header header = 1;
  3. string bcname = 2;
  4. string address = 3;
  5. bool need_content = 4;
  6. }

请求示例

  1. var host = http://127.0.0.1:37301/v1/get_address_contracts
  2. uri, err := url.Parse(host)
  3. if err != nil {
  4. fmt.Println(err)
  5. }
  6. var params = &pb.AddressContractsRequest{}
  7. params.Bcname = xuper
  8. params.Address = gH2XKGrne4mL5y37vwChzvmLDEWkXnuB8
  9. sendBody, err := json.Marshal(params)
  10. if err != nil {
  11. fmt.Println(err)
  12. }
  13. sendData := string(sendBody)
  14. client := &http.Client{}
  15. request, err := http.NewRequest(“POST”, uri.String(), strings.NewReader(sendData))
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. request.Header.Set(“Content-Type”, application/json”)
  20. response, err := client.Do(request)
  21. defer response.Body.Close()
  22. result, err := ioutil.ReadAll(response.Body)
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. fmt.Println(string(result))