trustsql

package module
v0.0.0-...-e64f1e1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2018 License: MIT Imports: 16 Imported by: 0

README

GoTrustSQL

license GoDoc

基于 Golang 语言的 TrustSQL SDK

概述

该 SDK 实现了底层密钥对生成、地址生成、签名/验签等基础功能,并对 TrustSQL 提供的三类(信息共享/身份管理) API 接口进行了封装。

特性

SDK 基础命令

  1. 生成密钥对
  2. 根据私钥生成公钥(压缩公钥)
  3. 根据公钥生成地址(压缩地址)
  4. 利用私钥对数据签名
  5. 利用公钥对数据和签名进行验签

SDK API 接口

  1. 数字资产(暂未实现)
  2. 信息共享
  3. 身份管理

示例

下列示例演示了该 SDK 的基本使用方法。

func SendToTrustSQL(content map[string]interface{}) (*tsiss.IssAppendResponse, error) {
    privateKey := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    client, _ := trustsql.NewClient(privateKey)
    client.SetIssRequestTimeout(5 * time.Second)
    //testURI := ""
    //client.SetAppendIssURI(testURI)
    accountAddr := client.GetAddrByPubkey()
    pubKey := client.GetPublicKey()
    issAppend := &tsiss.IssAppend{
        Version:  "1.0",
        SignType: "ECDSA",
        MchID:    "gbxxxxxxxxxxxxxxx",
        //MchSign:     "",
        Account:    string(accountAddr),
        CommitTime: time.Now().Format("2006-01-02 15:04:05"),
        //Content:    map[string]interface{}{"c": "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"},
        Content: content,
        InfoKey: bson.NewObjectId().Hex(),
        //InfoKey:     "1242123jjj",
        InfoVersion: "1",
        State:       "0",
        Notes:       map[string]interface{}{"notes": "comments"},
        PublicKey:   pubKey,
        Sign:        "",
        ChainID:     "ch_tencent_test",
        LedgerID:    "ld_tencent_iss",
    }
    signStr, err := client.GetIssSignStr(issAppend)
    if err != nil {
        fmt.Printf("get issSignStr error: %s\n", err)
        return nil, err
    }

    issAppend.Sign = client.SignString(signStr, true)
    appendRes, err := client.AppendIss(issAppend)
    if err != nil {
        fmt.Printf("append error: %s\n", err)
        return nil, err
    }
    //fmt.Printf("appendRes: %+v\n", appendRes)
    return appendRes, nil
}

环境依赖

参考资料

  1. Bitcoin Wiki
  2. Base58Check encoding
  3. Bitcoin Developer Reference
  4. Technical background of version 1 Bitcoin addresses
  5. Merkle Trees
  6. 数据库那么便宜,为何还要死贵的区块链来存储数据?
  7. Data Insertion in Bitcoin's Blockchain

License

GoTrustSQL is MIT licensed. See the included LICENSE file for more details.

Documentation

Index

Constants

View Source
const (
	AppendIssURI          = "https://baas.trustsql.qq.com/cgi-bin/v1.0/trustsql_iss_append_v1.cgi" // 共享信息查询
	QueryIssURI           = "https://baas.trustsql.qq.com/cgi-bin/v1.0/trustsql_iss_query_v1.cgi"  // 共享信息查询
	RegisteUserURI        = "https://baas.trustsql.qq.com/idm_v1.1/api/user_cert/register"         // 注册用户
	GetUserInfoURI        = "https://baas.trustsql.qq.com/idm_v1.1/api/user_cert/fetch"            // 获取用户信息
	RegisteAccountURI     = "https://baas.trustsql.qq.com/idm_v1.1/api/account_cert/register"      // 创建用户账户
	GetAccountsURI        = "https://baas.trustsql.qq.com/idm_v1.1/api/account_cert/fetch_list"    // 获取用户的账户地址列表
	GetPubkeyOfAccountURI = "https://baas.trustsql.qq.com/idm_v1.1/api/account_cert/fetch"         // 获取用户的账户公钥
)

trustsql URI 地址

Variables

This section is empty.

Functions

func Lint

func Lint(v interface{}, c interface{}) string

Lint 封装了请求餐具数据拼接要求 1.参数名ASCII码从小到大排序(字典序); 2.如果参数的值为空不参与签名; 3.参数名区分大小写;

Types

type Account

type Account struct {
	UserID    string `json:"user_id"     validate:"required"`
	PublicKey string `json:"public_key"  validate:"required"`
}

Account 创建用户账户参数

type AccountResponse

type AccountResponse struct {
	UserID         string `json:"user_id"`         // 用户ID
	AccountAddress string `json:"account_address"` // 创建的账户地址
	PublicKey      string `json:"public_key"`      // 用户公钥
	Created        string `json:"created"`         // 创建时间
	State          string `json:"state"`           // 状态
}

AccountResponse 创建用户账户返回值

type Accounts

type Accounts struct {
	UserID    string `json:"user_id"     validate:"required"`
	State     string `json:"state"       validate:"omitempty"`
	BeginTime string `json:"begin_time"  validate:"omitempty"`
	EndTime   string `json:"end_time"    validate:"omitempty"`
	Page      int    `json:"page"        validate:"omitempty"`
	Limit     int    `json:"limit"       validate:"omitempty"`
}

Accounts 获取用户的账户地址列表参数

type AccountsResponse

type AccountsResponse struct {
	UserID         string `json:"user_id"`         // 用户ID
	AccountAddress string `json:"account_address"` // 创建的账户地址
	PublicKey      string `json:"public_key"`      // 用户公钥
	Created        string `json:"created"`         // 创建时间
	State          string `json:"state"`           // 状态
}

AccountsResponse 获取用户的账户地址列表返回值

type Client

type Client struct {
	PrivateKey []byte
	PublicKey  []byte
	// contains filtered or unexported fields
}

Client 腾讯区块链sdk

func GenRandomPairkey

func GenRandomPairkey() *Client

GenRandomPairkey 生成随机公私钥对

func NewClient

func NewClient(privateKey string) (*Client, error)

NewClient 通过base64编码的私钥生成client

func (*Client) AppendIss

func (c *Client) AppendIss(ia *tsiss.IssAppend) (*tsiss.IssAppendResponse, error)

AppendIss 共享信息新增/追加, 第二步将已签名的signstr加入到参数ia中,再次请求接口

func (*Client) GetAccounts

func (c *Client) GetAccounts(u *Accounts, common *Common) (*AccountsResponse, error)

GetAccounts 获取用户的账户地址列表

func (*Client) GetAddrByPubkey

func (c *Client) GetAddrByPubkey() []byte

GetAddrByPubkey 计算公钥对应的地址

func (*Client) GetIssSignStr

func (c *Client) GetIssSignStr(ia *tsiss.IssAppend) (string, error)

GetIssSignStr 共享信息新增/追加, 第一步获取待签名串 注意: 留空sign字段

func (*Client) GetPrivateKey

func (c *Client) GetPrivateKey() string

GetPrivateKey 获取私钥的base64编码

func (*Client) GetPubkeyOfAccount

func (c *Client) GetPubkeyOfAccount(u *PubkeyOfAccount, common *Common) (*PubkeyOfAccountResponse, error)

GetPubkeyOfAccount 获取用户的账户公钥

func (*Client) GetPublicKey

func (c *Client) GetPublicKey() string

GetPublicKey 获取公钥的base64编码

func (*Client) GetUserInfo

func (c *Client) GetUserInfo(u *UserInfo, common *Common) (*UserInfoResponse, error)

GetUserInfo 获取用户信息参数

func (*Client) QueryIss

func (c *Client) QueryIss(iq *tsiss.IssQuery) (*tsiss.IssResponse, error)

QueryIss 共享信息查询

func (*Client) RegisteAccount

func (c *Client) RegisteAccount(u *Account, common *Common) (*AccountResponse, error)

RegisteAccount 创建用户账户

func (*Client) RegisteUser

func (c *Client) RegisteUser(u *UserRegister, common *Common) (*UserRegisterResponse, error)

RegisteUser 注册用户

func (*Client) SetAppendIssURI

func (c *Client) SetAppendIssURI(appendIssURI string)

SetAppendIssURI 设置URI

func (*Client) SetGetAccountsURI

func (c *Client) SetGetAccountsURI(getAccountsURI string)

SetGetAccountsURI 设置URI

func (*Client) SetGetPubkeyOfAccountURI

func (c *Client) SetGetPubkeyOfAccountURI(getPubkeyOfAccountURI string)

SetGetPubkeyOfAccountURI 设置URI

func (*Client) SetGetUserInfoURI

func (c *Client) SetGetUserInfoURI(getUserInfoURI string)

SetGetUserInfoURI 设置URI

func (*Client) SetIssRequestTimeout

func (c *Client) SetIssRequestTimeout(timeout time.Duration)

SetIssRequestTimeout 设置Iss的请求超时时间

func (*Client) SetQueryIssURI

func (c *Client) SetQueryIssURI(queryIssURI string)

SetQueryIssURI 设置URI

func (*Client) SetRegisteAccountURI

func (c *Client) SetRegisteAccountURI(registeAccountURI string)

SetRegisteAccountURI 设置URI

func (*Client) SetRegisteUserURI

func (c *Client) SetRegisteUserURI(registeUserURI string)

SetRegisteUserURI 设置URI

func (*Client) SignString

func (c *Client) SignString(s string, isHash bool) string

SignString 对一个字符串进行签名(通常用于生成通讯方签名)

func (*Client) VerifySignature

func (c *Client) VerifySignature(sig, data []byte) bool

VerifySignature 对签名进行验证

type Common

type Common struct {
	MchID       string `json:"mch_id"       validate:"required"`
	ProductCode string `json:"product_code" validate:"required"`
	SeqNo       string `json:"seq_no"       validate:"required"`
	Sign        string `json:"sign"         validate:"required"`
	Type        string `json:"type"         validate:"required"`
	TimeStamp   int64  `json:"time_stamp"   validate:"required"`
	ReqData     string `json:"req_data"     validate:"required"`
}

Common 公共信息

type PubkeyOfAccount

type PubkeyOfAccount struct {
	UserID         string `json:"user_id"         validate:"required"`
	AccountAddress string `json:"account_address" validate:"required"`
}

PubkeyOfAccount 获取用户的账户公钥参数

type PubkeyOfAccountResponse

type PubkeyOfAccountResponse struct {
	UserID         string `json:"user_id"`         // 用户ID
	AccountAddress string `json:"account_address"` // 创建的账户地址
	PublicKey      string `json:"public_key"`      // 用户公钥
	Created        string `json:"created"`         // 创建时间
	State          string `json:"state"`           // 状态
}

PubkeyOfAccountResponse 获取用户的账户公钥返回值

type UserInfo

type UserInfo struct {
	UserID string `json:"user_id" validate:"required"`
}

UserInfo 获取用户信息参数

type UserInfoResponse

type UserInfoResponse struct {
	UserID      string `json:"user_id"`      // 用户ID
	PublicKey   string `json:"public_key"`   // 用户公钥
	UserAddress string `json:"user_address"` // 用户密钥地址
	Created     string `json:"created"`      // 创建时间
	State       string `json:"state"`        // 状态

}

UserInfoResponse 获取用户信息返回值

type UserRegister

type UserRegister struct {
	PublicKey    string `json:"public_key"     validate:"required"`
	UserID       string `json:"user_id"        validate:"required"`
	UserFullName string `json:"user_fullName"  validate:"required"`
}

UserRegister 注册用户需要的信息

type UserRegisterResponse

type UserRegisterResponse struct {
	UserID       string `json:"user_id"`       // 用户ID
	PublicKey    string `json:"public_key"`    // 用户公钥
	UserAddress  string `json:"user_address"`  // 用户密钥地址
	UserFullName string `json:"user_fullName"` // 用户名
	Created      string `json:"created"`       // 创建时间
	State        string `json:"state"`         // 状态
}

UserRegisterResponse 注册用户返回信息

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL