utils

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Overview

Package utils common utils

Index

Constants

View Source
const (
	// SUCCESS ContractResult success code
	SUCCESS uint32 = 0
	// Separator chainmker hex ca
	Separator = byte(202)
)
View Source
const (
	// HibeMsgKey key
	HibeMsgKey = "hibe_msg"
	// HibeMsgIdKey key
	HibeMsgIdKey = "tx_id"
	// HibeMsgCipherTextKey key
	HibeMsgCipherTextKey = "CT"
	// HibeParamsKey key
	HibeParamsKey = "org_id"
	// HibeParamsValueKey key
	HibeParamsValueKey = "params"
)

hibe msg's Keys

View Source
const (
	// KeyBlockContractWithRWSet key
	KeyBlockContractWithRWSet = "withRWSet"
	// KeyBlockContractBlockHash key
	KeyBlockContractBlockHash = "blockHash"
	// KeyBlockContractBlockHeight key
	KeyBlockContractBlockHeight = "blockHeight"
	// KeyBlockContractTxId key
	KeyBlockContractTxId = "txId"
	// KeyBlockContractTruncateValueLen key
	KeyBlockContractTruncateValueLen = "truncateValueLen"
	// KeyBlockContractTruncateModel key
	KeyBlockContractTruncateModel = "truncateModel"

	// KeyChainConfigContractRoot key
	KeyChainConfigContractRoot = "root"
	// KeyChainConfigContractOrgId key
	KeyChainConfigContractOrgId = "org_id"
	// KeyChainConfigAddrType key
	KeyChainConfigAddrType = "addr_type"
	// KeyChainConfigContractNodeId key
	KeyChainConfigContractNodeId = "node_id"
	// KeyChainConfigContractNewNodeId key
	KeyChainConfigContractNewNodeId = "new_node_id"
	// KeyChainConfigContractNodeIds key
	KeyChainConfigContractNodeIds = "node_ids"
	// KeyChainConfigContractBlockHeight key
	KeyChainConfigContractBlockHeight = "block_height"
	// KeyChainConfigContractTrustMemberOrgId key
	KeyChainConfigContractTrustMemberOrgId = "org_id"
	// KeyChainConfigContractTrustMemberInfo key
	KeyChainConfigContractTrustMemberInfo = "member_info"
	// KeyChainConfigContractTrustMemberNodeId key
	KeyChainConfigContractTrustMemberNodeId = "node_id"
	// KeyChainConfigContractTrustMemberRole key
	KeyChainConfigContractTrustMemberRole = "role"

	// KeyTxSchedulerTimeout key
	KeyTxSchedulerTimeout = "tx_scheduler_timeout"
	// KeyTxSchedulerValidateTimeout key
	KeyTxSchedulerValidateTimeout = "tx_scheduler_validate_timeout"
	// KeyEnableOptimizeChargeGas key
	KeyEnableOptimizeChargeGas = "enable_optimize_charge_gas"

	// KeyTxTimeOut key
	KeyTxTimeOut = "tx_timeout"
	// KeyBlockTxCapacity key
	KeyBlockTxCapacity = "block_tx_capacity"
	// KeyBlockSize key
	KeyBlockSize = "block_size"
	// KeyBlockInterval key
	KeyBlockInterval = "block_interval"
	// KeyTxParamterSize key
	KeyTxParamterSize = "tx_parameter_size"
	// KeyBlockTimeOut key
	KeyBlockTimeOut = "block_timeout"

	// KeyMinSelfDelegation key
	KeyMinSelfDelegation = "min_self_delegation"
	// KeyEpochValidatorNumber key
	KeyEpochValidatorNumber = "epoch_validator_number"
	// KeyEpochBlockNumber key
	KeyEpochBlockNumber = "epoch_block_number"
	// KeyDistributionPerBlock key
	KeyDistributionPerBlock = "distribution_per_block"
	// KeySlashingPerBlock key
	KeySlashingPerBlock = "slashing_per_block"
	// KeyDistributionFromSlashing key
	KeyDistributionFromSlashing = "distribution_from_slashing"
	// KeyGasExchangeRate key
	KeyGasExchangeRate = "gas_exchange_rate"

	// KeyAddress key
	KeyAddress = "address"
	// KeyAmount key
	KeyAmount = "amount"
	// KeyOwner key
	KeyOwner = "owner"
	// KeyTo key
	KeyTo = "to"
	// KeyFrom key
	KeyFrom = "from"
	// KeyValue key
	KeyValue = "value"

	// KeyCertHashes key
	KeyCertHashes = "cert_hashes"
	// KeyCerts key
	KeyCerts = "certs"
	// KeyCertCrl key
	KeyCertCrl = "cert_crl"

	// KeyOrderId key
	KeyOrderId = "order_id"
	// KeyPrivateDir key
	KeyPrivateDir = "private_dir"
	// KeyContractName key
	KeyContractName = "contract_name"
	// KeyCodeHash key
	KeyCodeHash = "code_hash"
	// KeyResult key
	KeyResult = "result"
	// KeyCodeHeader key
	KeyCodeHeader = "code_header"
	// KeyVersion key
	KeyVersion = "version"
	// KeyIsDeploy key
	KeyIsDeploy = "is_deploy"
	// KeyRWSet key
	KeyRWSet = "rw_set"
	// KeyEvents key
	KeyEvents = "events"
	// KeyReportHash key
	KeyReportHash = "report_hash"
	// KeySign key
	KeySign = "sign"
	// KeyKey key
	KeyKey = "key"
	// KeyPrefix key
	KeyPrefix = "prefix"
	// KeyPayload key
	KeyPayload = "payload"
	// KeyOrgIds key
	KeyOrgIds = "org_ids"
	// KeySignPairs key
	KeySignPairs = "sign_pairs"
	// KeyCaCert key
	KeyCaCert = "ca_cert"
	// KeyEnclaveId key
	KeyEnclaveId = "enclave_id"
	// KeyReport key
	KeyReport = "report"
	// KeyProof key
	KeyProof = "proof"
	// KeyDeployReq key
	KeyDeployReq = "deploy_req"
	// KeyPrivateReq key
	KeyPrivateReq = "private_req"

	// KeyPubkey key
	KeyPubkey = "pubkey"
	// KeyPubkeyRole key
	KeyPubkeyRole = "role"
	// KeyPubkeyOrgId key
	KeyPubkeyOrgId = "org_id"

	// KeyGasAddressKey key
	KeyGasAddressKey = "address_key"
	// KeyGasPublicKey key
	KeyGasPublicKey = "public_key"
	// KeyGasBatchRecharge key
	KeyGasBatchRecharge = "batch_recharge"
	// KeyGasBalancePublicKey key
	KeyGasBalancePublicKey = "balance_public_key"
	// KeyGasChargePublicKey key
	KeyGasChargePublicKey = "charge_public_key"
	// KeyGasChargeGasAmount key
	KeyGasChargeGasAmount = "charge_gas_amount"
	// KeyGasFrozenPublicKey key
	KeyGasFrozenPublicKey = "frozen_public_key"
	// KeySetInvokeBaseGas key
	KeySetInvokeBaseGas = "set_invoke_base_gas"
	// KeySetInvokeGasPrice key
	KeySetInvokeGasPrice = "set_invoke_gas_price"
	// KeySetInstallBaseGas key
	KeySetInstallBaseGas = "set_install_base_gas"
	// KeySetInstallGasPrice key
	KeySetInstallGasPrice = "set_install_gas_price"

	// KeyMultiSignEnableManualRun key
	KeyMultiSignEnableManualRun = "multi_sign_enable_manual_run"

	// KeyVMType key
	KeyVMType = "vm_type"

	// ValueVMTypeDockerJava value
	ValueVMTypeDockerJava = "dockerjava"
)

nolint

View Source
const (
	// MysqlDBNamePrefix archive config: mysql db name prefix
	MysqlDBNamePrefix = "cm_archived_chain"
	// MysqlTableNamePrefix archive config: mysql table name prefix
	MysqlTableNamePrefix = "t_block_info"
	// RowsPerBlockInfoTable archive config: count of rows per block_info table
	RowsPerBlockInfoTable = 100000
)

nolint

View Source
const DecBase = 10

DecBase 10

Variables

View Source
var IV = [8]uint64{
	0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
	0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
}

IV is an initialization vector for BLAKE2b

View Source
var (
	// S256 is the secp256k1 elliptic curve
	S256 = btcec.S256()
)

Functions

func BytesToI64

func BytesToI64(b []byte) (int64, error)

BytesToI64 bytes to int64 in little endian args:

  • []byte

returns:

  • int64
  • error

func BytesToU64

func BytesToU64(b []byte) (uint64, error)

BytesToU64 bytes to uint64 in little endian args:

  • []byte

returns:

  • uint64
  • error

func CheckProposalRequestResp

func CheckProposalRequestResp(resp *common.TxResponse, needContractResult bool) error

CheckProposalRequestResp check tx response is ok or not

func CreateEthAddress

func CreateEthAddress(pub ecdsa.PublicKey) ([]byte, error)

CreateEthAddress create k1 curve eth address args:

  • ecdsa.PublicKey

returns:

  • []byte
  • error

func DecryptHibeTx

func DecryptHibeTx(localId string, hibeParams []byte, hibePrvKey []byte, tx *common.Transaction,
	keyType crypto.KeyType) ([]byte, error)

DecryptHibeTx Decrypt Hibe Tx args:

  • string
  • []byte
  • []byte
  • *common.Transaction
  • crypto.KeyType

returns:

  • []byte
  • error

func Exists

func Exists(path string) bool

Exists returns a boolean indicating whether the error is known to report that a file or directory already exists.

func F

func F(h *[8]uint64, m [16]uint64, c [2]uint64, f bool, rounds uint32)

F is a compression function for BLAKE2b. It takes as an argument the state vector `h`, message block vector `m`, offset counter `t`, final block indicator flag `f`, and number of rounds `rounds`. The state vector provided as the first parameter is modified by the function. nolint

func GenerateECDSAKey

func GenerateECDSAKey() (*ecdsa.PrivateKey, error)

GenerateECDSAKey generates a new key based on the secp256k1 elliptic curve. returns:

  • *ecdsa.PrivateKey
  • error

func GetCertificateId

func GetCertificateId(certPEM []byte, hashType string) ([]byte, error)

GetCertificateId get cert id from cert pem

func GetCertificateIdFromDER

func GetCertificateIdFromDER(certDER []byte, hashType string) ([]byte, error)

GetCertificateIdFromDER get cert id from cert der

func GetNanoByTimestampTxId

func GetNanoByTimestampTxId(timestampTxId string) (nano int64, err error)

GetNanoByTimestampTxId validate and parse 160 ... 223 22

func GetRandTxId

func GetRandTxId() string

GetRandTxId get random tx id

func GetTimestampTxId

func GetTimestampTxId() string

GetTimestampTxId by current time, see: GetTimestampTxIdByNano eg: 687dca1d9c4fdf1652fdfc072182654f53622c496aa94c05b47d34263cd99ec9

func GetTimestampTxIdByNano

func GetTimestampTxIdByNano(nano int64) string

GetTimestampTxIdByNano nanosecond

func I64ToBytes

func I64ToBytes(i int64) []byte

I64ToBytes int64 to bytes in little endian args:

  • int64

returns:

  • []byte

func IsArchived

func IsArchived(txStatusCode common.TxStatusCode) bool

IsArchived returns a boolean indicating whether is archived by txStatusCode in common.TxStatusCode

func IsArchivedString

func IsArchivedString(txStatusCode string) bool

IsArchivedString returns a boolean indicating whether is archived by txStatusCode in string

func MakeEndorser

func MakeEndorser(orgId string, hashType crypto.HashType, memberType accesscontrol.MemberType, keyPem,
	memberInfo []byte, payload *common.Payload) (*common.EndorsementEntry, error)

MakeEndorser returns *common.EndorsementEntry args:

  • string
  • crypto.HashType
  • accesscontrol.MemberType
  • []byte
  • []byte
  • *common.Payload

returns:

  • *common.EndorsementEntry
  • error

func MakeEndorserWithPath

func MakeEndorserWithPath(keyFilePath, crtFilePath string, payload *common.Payload) (*common.EndorsementEntry, error)

MakeEndorserWithPath make endorser with key/cert file path args:

  • string
  • string
  • *common.Payload

returns:

  • *common.EndorsementEntry
  • error

func MakeEndorserWithPathAndCSP

func MakeEndorserWithPathAndCSP(keyFilePath, crtFilePath string, p11Handle interface{}, kmsEnabled bool,
	payload *common.Payload) (*common.EndorsementEntry, error)

MakeEndorserWithPathAndCSP used to sign payload by kms or hsm args:

  • string
  • string
  • interface{}
  • bool
  • *common.Payload

returns:

  • *common.EndorsementEntry
  • error

func MakeEndorserWithPem

func MakeEndorserWithPem(keyPem, certPem []byte, payload *common.Payload) (*common.EndorsementEntry, error)

MakeEndorserWithPem make endorser with pem Deprecated: This function will be deleted when appropriate. Please use MakeEndorser args:

  • []byte
  • []byte
  • *common.Payload

returns:

  • *common.EndorsementEntry
  • error

func MakePkEndorserWithPath

func MakePkEndorserWithPath(keyFilePath string, hashType crypto.HashType, orgId string,
	payload *common.Payload) (*common.EndorsementEntry, error)

MakePkEndorserWithPath make public mode endorser with key file path args:

  • string
  • crypto.HashType
  • string
  • *common.Payload

returns:

  • *common.EndorsementEntry
  • error

func MakePkEndorserWithPem

func MakePkEndorserWithPem(keyPem []byte, hashType crypto.HashType, orgId string,
	payload *common.Payload) (*common.EndorsementEntry, error)

MakePkEndorserWithPem make public mode endorser with pem Deprecated: This function will be deleted when appropriate. Please use MakeEndorser args:

  • []byte
  • crypto.HashType
  • string
  • *common.Payload

returns:

  • *common.EndorsementEntry
  • error

func MarshalECDSAPrivateKey

func MarshalECDSAPrivateKey(priv *ecdsa.PrivateKey) ([]byte, error)

MarshalECDSAPrivateKey serializes the private key's D value to a []byte args:

  • *ecdsa.PrivateKey

returns:

  • []byte
  • error

func NewEndorser

func NewEndorser(orgId string, certPem []byte, sig []byte) *common.EndorsementEntry

NewEndorser returns *common.EndorsementEntry Deprecated: This function will be deleted when appropriate. Please use NewEndorserWithMemberType args:

  • string
  • []byte
  • []byte

returns:

  • *common.EndorsementEntry

func NewEndorserWithMemberType

func NewEndorserWithMemberType(orgId string, memberInfo []byte, memberType accesscontrol.MemberType,
	sig []byte) *common.EndorsementEntry

NewEndorserWithMemberType new endorser with member type args:

  • string
  • []byte
  • accesscontrol.MemberType
  • []byte

returns:

  • *common.EndorsementEntry

func NewPayload

func NewPayload(opts ...PayloadOption) *commonPb.Payload

NewPayload new payload args:

  • ...PayloadOption

returns:

  • *commonPb.Payload

func NewPkEndorser

func NewPkEndorser(orgId string, pk []byte, sig []byte) *common.EndorsementEntry

NewPkEndorser returns *common.EndorsementEntry args:

  • string
  • []byte
  • []byte

returns:

  • *common.EndorsementEntry

func ParseCert

func ParseCert(crtPEM []byte) (*bcx509.Certificate, error)

ParseCert parse cert pem to *bcx509.Certificate

func ParseECDSAPrivateKey

func ParseECDSAPrivateKey(buf []byte) (*ecdsa.PrivateKey, error)

ParseECDSAPrivateKey parse ECDSA private key args:

  • []byte

returns:

  • *ecdsa.PrivateKey
  • error

func ReadHibeParamsWithFilePath

func ReadHibeParamsWithFilePath(hibeParamsFilePath string) ([]byte, error)

ReadHibeParamsWithFilePath Returns the serialized byte array of hibeParams args:

  • string

returns:

  • []byte
  • error

func ReadHibePrvKeysWithFilePath

func ReadHibePrvKeysWithFilePath(hibePrvKeyFilePath string) ([]byte, error)

ReadHibePrvKeysWithFilePath Returns the serialized byte array of hibePrvKey args:

  • string

returns:

  • []byte
  • error

func Sign

func Sign(priv *ecdsa.PrivateKey, hash []byte) ([]byte, error)

Sign produces a compact signature of the data in hash with the given private key on the secp256k1 curve. args:

  • *ecdsa.PrivateKey
  • []byte

returns:

  • []byte
  • error

func SignPayload

func SignPayload(privateKey crypto.PrivateKey, cert *bcx509.Certificate, payload *common.Payload) ([]byte, error)

SignPayload sign payload Deprecated: This function will be deleted when appropriate. Please use SignPayloadWithHashType args:

  • crypto.PrivateKey
  • *bcx509.Certificate
  • *common.Payload

returns:

  • []byte
  • error

func SignPayloadBytes

func SignPayloadBytes(privateKey crypto.PrivateKey, cert *bcx509.Certificate, payloadBytes []byte) ([]byte, error)

SignPayloadBytes sign payload bytes Deprecated: This function will be deleted when appropriate. Please use SignPayloadBytesWithHashType args:

  • crypto.PrivateKey
  • *bcx509.Certificate
  • []byte

returns:

  • []byte
  • error

func SignPayloadBytesWithHashType

func SignPayloadBytesWithHashType(privateKey crypto.PrivateKey,
	hashType crypto.HashType, payloadBytes []byte) ([]byte, error)

SignPayloadBytesWithHashType sign payload bytes with specified hash type args:

  • crypto.PrivateKey
  • crypto.HashType
  • []byte

returns:

  • []byte
  • error

func SignPayloadWithHashType

func SignPayloadWithHashType(privateKey crypto.PrivateKey,
	hashType crypto.HashType, payload *common.Payload) ([]byte, error)

SignPayloadWithHashType sign payload with specified hash type args:

  • crypto.PrivateKey
  • crypto.HashType
  • *common.Payload

returns:

  • []byte
  • error

func SignPayloadWithPath

func SignPayloadWithPath(keyFilePath, crtFilePath string, payload *common.Payload) ([]byte, error)

SignPayloadWithPath sign payload with specified key/cert file path args:

  • string
  • string
  • *common.Payload

returns:

  • []byte
  • error

func SignPayloadWithPkPath

func SignPayloadWithPkPath(keyFilePath, hashType string, payload *common.Payload) ([]byte, error)

SignPayloadWithPkPath sign payload with specified key file path args:

  • string
  • string
  • *common.Payload

returns:

  • []byte
  • error

func U64ToBytes

func U64ToBytes(i uint64) []byte

U64ToBytes uint64 to bytes in little endian args:

  • uint64

returns:

  • []byte

Types

type BigInteger

type BigInteger struct {
	Value *big.Int
}

BigInteger wrapper for big.Int

func Div

func Div(x, y *BigInteger) *BigInteger

Div x/y

func Mod

func Mod(x, y *BigInteger) *BigInteger

Mod x%y

func Mul

func Mul(x, y *BigInteger) *BigInteger

Mul x multi y

func NewBigInteger

func NewBigInteger(value string) *BigInteger

NewBigInteger create a BigInteger

func NewZeroBigInteger

func NewZeroBigInteger() *BigInteger

NewZeroBigInteger create a BigInteger of zero

func Sub

func Sub(x, y *BigInteger) *BigInteger

Sub subtracts two big integer

func Sum

func Sum(x, y *BigInteger) *BigInteger

Sum adds two big integer

func (*BigInteger) Add

func (x *BigInteger) Add(y *BigInteger)

Add adds y to x

func (*BigInteger) Cmp

func (x *BigInteger) Cmp(y *BigInteger) int

Cmp compares x and y and returns:

-1 if x <  y
 0 if x == y
+1 if x >  y

func (*BigInteger) String

func (x *BigInteger) String() string

String gets string

func (*BigInteger) Sub

func (x *BigInteger) Sub(y *BigInteger)

Sub subs y from x

type ChainClientConfigModel

type ChainClientConfigModel struct {
	ChainClientConfig chainClientConfigModel `mapstructure:"chain_client"`
}

ChainClientConfigModel define ChainClientConfigModel

func InitConfig

func InitConfig(confPath string) (*ChainClientConfigModel, error)

InitConfig init config from config file path args:

  • confPath, sdk config file path

returns:

  • *ChainClientConfigModel
  • error

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
}

Logger define log interface

type PayloadOption

type PayloadOption func(*commonPb.Payload)

PayloadOption define payload option func

func AddParameter

func AddParameter(parameter *commonPb.KeyValuePair) PayloadOption

AddParameter add one Parameter of payload args:

  • []*commonPb.KeyValuePair

returns:

  • PayloadOption

func WithChainId

func WithChainId(chainId string) PayloadOption

WithChainId set chainId of payload args:

  • string

returns:

  • PayloadOption

func WithContractName

func WithContractName(contractName string) PayloadOption

WithContractName set ContractName of payload args:

  • string

returns:

  • PayloadOption

func WithExpirationTime

func WithExpirationTime(expirationTime int64) PayloadOption

WithExpirationTime set ExpirationTime of payload args:

  • int64

returns:

  • PayloadOption

func WithLimit

func WithLimit(limit *commonPb.Limit) PayloadOption

WithLimit set Limit of payload args:

  • *commonPb.Limit

returns:

  • PayloadOption

func WithMethod

func WithMethod(method string) PayloadOption

WithMethod set Method of payload args:

  • string

returns:

  • PayloadOption

func WithParameters

func WithParameters(parameters []*commonPb.KeyValuePair) PayloadOption

WithParameters set Parameters of payload args:

  • []*commonPb.KeyValuePair

returns:

  • PayloadOption

func WithSequence

func WithSequence(sequence uint64) PayloadOption

WithSequence set Sequence of payload args:

  • uint64

returns:

  • PayloadOption

func WithTimestamp

func WithTimestamp(timestamp int64) PayloadOption

WithTimestamp set Timestamp of payload args:

  • int64

returns:

  • PayloadOption

func WithTxId

func WithTxId(txId string) PayloadOption

WithTxId set TxId of payload args:

  • string

returns:

  • PayloadOption

func WithTxType

func WithTxType(txType commonPb.TxType) PayloadOption

WithTxType set TxType of payload args:

  • commonPb.TxType

returns:

  • PayloadOption

type TlsConfig added in v3.0.1

type TlsConfig struct {
	ServerName  string   `mapstructure:"server_name"`
	PrivKeyFile string   `mapstructure:"priv_key_file"`
	CertFile    string   `mapstructure:"cert_file"`
	TrustCaList []string `mapstructure:"trust_ca_list"`
}

TlsConfig 归档中心rpc使用的tls证书配置

Jump to

Keyboard shortcuts

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