Skip to content

Commit

Permalink
Merge pull request #20 from AElfProject/dev
Browse files Browse the repository at this point in the history
Release v1.3.0
  • Loading branch information
jason-aelf authored Sep 20, 2023
2 parents 3bafe34 + c4ac0b7 commit 3d5f0fa
Show file tree
Hide file tree
Showing 17 changed files with 1,208 additions and 367 deletions.
22 changes: 11 additions & 11 deletions client/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
util "github.com/AElfProject/aelf-sdk.go/utils"
)

//GetBlockHeight Get height of the current chain.
func (a *AElfClient) GetBlockHeight() (int64, error) {
url := a.Host + BLOCKHEIGHT
heightBytes, err := util.GetRequest("GET", url, a.Version, nil)
// GetBlockHeight Get height of the current chain.
func (client *AElfClient) GetBlockHeight() (int64, error) {
url := client.Host + BLOCKHEIGHT
heightBytes, err := util.GetRequest("GET", url, client.Version, nil)
if err != nil {
return 0, errors.New("Get BlockHeight error:" + err.Error())
}
Expand All @@ -22,7 +22,7 @@ func (a *AElfClient) GetBlockHeight() (int64, error) {
}

// GetBlockByHash Get information of a block by given block hash. Optional whether to include transaction information.
func (a *AElfClient) GetBlockByHash(blockHash string, includeTransactions bool) (*dto.BlockDto, error) {
func (client *AElfClient) GetBlockByHash(blockHash string, includeTransactions bool) (*dto.BlockDto, error) {
_, err := hex.DecodeString(blockHash)
if err != nil {
return nil, errors.New("transactionID hex to []byte error:" + err.Error())
Expand All @@ -31,8 +31,8 @@ func (a *AElfClient) GetBlockByHash(blockHash string, includeTransactions bool)
"blockHash": blockHash,
"includeTransactions": includeTransactions,
}
url := a.Host + BLOCKBYHASH
blockBytes, err := util.GetRequest("GET", url, a.Version, params)
url := client.Host + BLOCKBYHASH
blockBytes, err := util.GetRequest("GET", url, client.Version, params)
if err != nil {
return nil, errors.New("Get Block ByHash error:" + err.Error())
}
Expand All @@ -41,14 +41,14 @@ func (a *AElfClient) GetBlockByHash(blockHash string, includeTransactions bool)
return block, nil
}

//GetBlockByHeight Get information of a block by specified height. Optional whether to include transaction information.
func (a *AElfClient) GetBlockByHeight(blockHeight int64, includeTransactions bool) (*dto.BlockDto, error) {
// GetBlockByHeight Get information of a block by specified height. Optional whether to include transaction information.
func (client *AElfClient) GetBlockByHeight(blockHeight int64, includeTransactions bool) (*dto.BlockDto, error) {
params := map[string]interface{}{
"blockHeight": blockHeight,
"includeTransactions": includeTransactions,
}
url := a.Host + BLOCKBYHEIGHT
blockBytes, err := util.GetRequest("GET", url, a.Version, params)
url := client.Host + BLOCKBYHEIGHT
blockBytes, err := util.GetRequest("GET", url, client.Version, params)
if err != nil {
return nil, errors.New("Get Block ByHeight error:" + err.Error())
}
Expand Down
30 changes: 15 additions & 15 deletions client/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/btcsuite/btcutil/base58"
)

//GetChainStatus Get the current status of the block chain.
func (a *AElfClient) GetChainStatus() (*dto.ChainStatusDto, error) {
url := a.Host + CHAINSTATUS
chainBytes, err := util.GetRequest("GET", url, a.Version, nil)
// GetChainStatus Get the current status of the block chain.
func (client *AElfClient) GetChainStatus() (*dto.ChainStatusDto, error) {
url := client.Host + CHAINSTATUS
chainBytes, err := util.GetRequest("GET", url, client.Version, nil)
if err != nil {
return nil, errors.New("Get ChainStatus error:" + err.Error())
}
Expand All @@ -22,20 +22,20 @@ func (a *AElfClient) GetChainStatus() (*dto.ChainStatusDto, error) {
return chain, nil
}

//GetContractFileDescriptorSet Get the definitions of proto-buff related to a contract.
func (a *AElfClient) GetContractFileDescriptorSet(address string) ([]byte, error) {
url := a.Host + FILEDESCRIPTOR
// GetContractFileDescriptorSet Get the definitions of proto-buff related to a contract.
func (client *AElfClient) GetContractFileDescriptorSet(address string) ([]byte, error) {
url := client.Host + FILEDESCRIPTOR
params := map[string]interface{}{"address": address}
data, err := util.GetRequest("GET", url, a.Version, params)
data, err := util.GetRequest("GET", url, client.Version, params)
if err != nil {
return nil, errors.New("Get ContractFile Descriptor Set error:" + err.Error())
}
return data, err
}

//GetChainID Get id of the chain.
func (a *AElfClient) GetChainID() (int, error) {
chainStatus, err := a.GetChainStatus()
// GetChainID Get id of the chain.
func (client *AElfClient) GetChainID() (int, error) {
chainStatus, err := client.GetChainStatus()
if err != nil {
return 0, errors.New("Get Chain Status error:" + err.Error())
}
Expand All @@ -53,10 +53,10 @@ func (a *AElfClient) GetChainID() (int, error) {
return util.BytesToInt(chainIDBytes), nil
}

//GetTaskQueueStatus Get the status information of the task queue.
func (a *AElfClient) GetTaskQueueStatus() ([]*dto.TaskQueueInfoDto, error) {
url := a.Host + TASKQUEUESTATUS
queues, err := util.GetRequest("GET", url, a.Version, nil)
// GetTaskQueueStatus Get the status information of the task queue.
func (client *AElfClient) GetTaskQueueStatus() ([]*dto.TaskQueueInfoDto, error) {
url := client.Host + TASKQUEUESTATUS
queues, err := util.GetRequest("GET", url, client.Version, nil)
if err != nil {
return nil, errors.New("Get Task Queue Status error:" + err.Error())
}
Expand Down
49 changes: 24 additions & 25 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/sha256"
"encoding/hex"
"errors"

"github.com/AElfProject/aelf-sdk.go/model"
pb "github.com/AElfProject/aelf-sdk.go/protobuf/generated"
util "github.com/AElfProject/aelf-sdk.go/utils"
Expand Down Expand Up @@ -51,42 +50,42 @@ const (
)

// GetAddressFromPubKey Get the account address through the public key.
func (a *AElfClient) GetAddressFromPubKey(pubkey string) string {
func (client *AElfClient) GetAddressFromPubKey(pubkey string) string {
bytes, _ := hex.DecodeString(pubkey)
return util.GetAddressByBytes(bytes)
}

// GetAddressFromPrivateKey Get the account address through the private key.
func (a *AElfClient) GetAddressFromPrivateKey(privateKey string) string {
func (client *AElfClient) GetAddressFromPrivateKey(privateKey string) string {
bytes, _ := hex.DecodeString(privateKey)
pubkeyBytes := secp256.UncompressedPubkeyFromSeckey(bytes)
return util.GetAddressByBytes(pubkeyBytes)
}

// GetFormattedAddress Convert the Address to the displayed string:symbol_base58-string_base58-string-chain-id.
func (a *AElfClient) GetFormattedAddress(address string) (string, error) {
chain, _ := a.GetChainStatus()
func (client *AElfClient) GetFormattedAddress(address string) (string, error) {
chain, _ := client.GetChainStatus()
methodName := "GetPrimaryTokenSymbol"
fromAddress := a.GetAddressFromPrivateKey(ExamplePrivateKey)
contractAddress, _ := a.GetContractAddressByName("AElf.ContractNames.Token")
transaction, _ := a.CreateTransaction(fromAddress, contractAddress, methodName, nil)
signature, _ := a.SignTransaction(ExamplePrivateKey, transaction)
fromAddress := client.GetAddressFromPrivateKey(ExamplePrivateKey)
contractAddress, _ := client.GetContractAddressByName("AElf.ContractNames.Token")
transaction, _ := client.CreateTransaction(fromAddress, contractAddress, methodName, nil)
signature, _ := client.SignTransaction(ExamplePrivateKey, transaction)
transaction.Signature = signature
transactionBytes, err := proto.Marshal(transaction)
if err != nil {
return "", errors.New("proto marshasl transaction error" + err.Error())
}
executeResult, _ := a.ExecuteTransaction(hex.EncodeToString(transactionBytes))
executeResult, _ := client.ExecuteTransaction(hex.EncodeToString(transactionBytes))
var symbol = new(wrap.StringValue)
executeBytes, err := hex.DecodeString(executeResult)
proto.Unmarshal(executeBytes, symbol)
return symbol.Value + "_" + address + "_" + chain.ChainId, nil
}

// GetContractAddressByName Get contract address by contract name.
func (a *AElfClient) GetContractAddressByName(contractName string) (string, error) {
fromAddress := a.GetAddressFromPrivateKey(ExamplePrivateKey)
toAddress, err := a.GetGenesisContractAddress()
func (client *AElfClient) GetContractAddressByName(contractName string) (string, error) {
fromAddress := client.GetAddressFromPrivateKey(ExamplePrivateKey)
toAddress, err := client.GetGenesisContractAddress()
if err != nil {
return "", errors.New("Get Genesis Contract Address error")
}
Expand All @@ -95,22 +94,22 @@ func (a *AElfClient) GetContractAddressByName(contractName string) (string, erro
hash.Value = contractNameBytes
hashBytes, _ := proto.Marshal(hash)

transaction, _ := a.CreateTransaction(fromAddress, toAddress, "GetContractAddressByName", hashBytes)
signature, _ := a.SignTransaction(ExamplePrivateKey, transaction)
transaction, _ := client.CreateTransaction(fromAddress, toAddress, "GetContractAddressByName", hashBytes)
signature, _ := client.SignTransaction(ExamplePrivateKey, transaction)
transaction.Signature = signature
transactionBytes, err := proto.Marshal(transaction)
if err != nil {
return "", errors.New("proto marshasl transaction error" + err.Error())
}
result, _ := a.ExecuteTransaction(hex.EncodeToString(transactionBytes))
result, _ := client.ExecuteTransaction(hex.EncodeToString(transactionBytes))
var address = new(pb.Address)
resultBytes, err := hex.DecodeString(result)
proto.Unmarshal(resultBytes, address)
return util.EncodeCheck(address.Value), nil
}

// SignTransaction Sign a transaction using private key.
func (a *AElfClient) SignTransaction(privateKey string, transaction *pb.Transaction) ([]byte, error) {
func (client *AElfClient) SignTransaction(privateKey string, transaction *pb.Transaction) ([]byte, error) {
transactionBytes, _ := proto.Marshal(transaction)
txDataBytes := sha256.Sum256(transactionBytes)
privateKeyBytes, _ := hex.DecodeString(privateKey)
Expand All @@ -119,8 +118,8 @@ func (a *AElfClient) SignTransaction(privateKey string, transaction *pb.Transact
}

// CreateTransaction create a transaction from the input parameters.
func (a *AElfClient) CreateTransaction(from, to, method string, params []byte) (*pb.Transaction, error) {
chainStatus, err := a.GetChainStatus()
func (client *AElfClient) CreateTransaction(from, to, method string, params []byte) (*pb.Transaction, error) {
chainStatus, err := client.GetChainStatus()
if err != nil {
return nil, errors.New("Get Chain Status error ")
}
Expand All @@ -139,8 +138,8 @@ func (a *AElfClient) CreateTransaction(from, to, method string, params []byte) (
}

// GetGenesisContractAddress Get the address of genesis contract.
func (a *AElfClient) GetGenesisContractAddress() (string, error) {
chainStatus, err := a.GetChainStatus()
func (client *AElfClient) GetGenesisContractAddress() (string, error) {
chainStatus, err := client.GetChainStatus()
if err != nil {
return "", errors.New("Get Genesis Contract Address error:" + err.Error())
}
Expand All @@ -149,20 +148,20 @@ func (a *AElfClient) GetGenesisContractAddress() (string, error) {
}

// IsConnected Verify whether this sdk successfully connects the chain.
func (a *AElfClient) IsConnected() bool {
data, err := a.GetChainStatus()
func (client *AElfClient) IsConnected() bool {
data, err := client.GetChainStatus()
if err != nil || data == nil {
return false
}
return true
}

// GenerateKeyPairInfo Generate KeyPair Info.
func (a *AElfClient) GenerateKeyPairInfo() *model.KeyPairInfo {
func (client *AElfClient) GenerateKeyPairInfo() *model.KeyPairInfo {
publicKeyBytes, privateKeyBytes := secp256.GenerateKeyPair()
publicKey := hex.EncodeToString(publicKeyBytes)
privateKey := hex.EncodeToString(privateKeyBytes)
privateKeyAddress := a.GetAddressFromPrivateKey(privateKey)
privateKeyAddress := client.GetAddressFromPrivateKey(privateKey)
var keyPair = &model.KeyPairInfo{
PrivateKey: privateKey,
PublicKey: publicKey,
Expand Down
36 changes: 18 additions & 18 deletions client/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
util "github.com/AElfProject/aelf-sdk.go/utils"
)

//GetNetworkInfo Get the node's network information.
func (a *AElfClient) GetNetworkInfo() (*dto.NetworkInfo, error) {
url := a.Host + NETWORKINFO
networkBytes, err := util.GetRequest("GET", url, a.Version, nil)
// GetNetworkInfo Get the node's network information.
func (client *AElfClient) GetNetworkInfo() (*dto.NetworkInfo, error) {
url := client.Host + NETWORKINFO
networkBytes, err := util.GetRequest("GET", url, client.Version, nil)
if err != nil {
return nil, errors.New("Get Network Info error:" + err.Error())
}
Expand All @@ -21,13 +21,13 @@ func (a *AElfClient) GetNetworkInfo() (*dto.NetworkInfo, error) {
return network, nil
}

//RemovePeer Attempt to remove a node from the connected network nodes by given the ipAddress.
func (a *AElfClient) RemovePeer(ipAddress string) (bool, error) {
url := a.Host + REMOVEPEER
combine := a.UserName + ":" + a.Password
// RemovePeer Attempt to remove a node from the connected network nodes by given the ipAddress.
func (client *AElfClient) RemovePeer(ipAddress string) (bool, error) {
url := client.Host + REMOVEPEER
combine := client.UserName + ":" + client.Password
combineToBase64 := "Basic " + base64.StdEncoding.EncodeToString([]byte(combine))
params := map[string]interface{}{"address": ipAddress}
peerBytes, err := util.GetRequestWithAuth("DELETE", url, a.Version, params, combineToBase64)
peerBytes, err := util.GetRequestWithAuth("DELETE", url, client.Version, params, combineToBase64)
if err != nil {
return false, errors.New("Remove Peer error:" + err.Error())
}
Expand All @@ -36,13 +36,13 @@ func (a *AElfClient) RemovePeer(ipAddress string) (bool, error) {
return data.(bool), nil
}

//AddPeer Attempt to add a node to the connected network nodes.Input parameter contains the ipAddress of the node.
func (a *AElfClient) AddPeer(ipAddress string) (bool, error) {
url := a.Host + ADDPEER
combine := a.UserName + ":" + a.Password
// AddPeer Attempt to add a node to the connected network nodes.Input parameter contains the ipAddress of the node.
func (client *AElfClient) AddPeer(ipAddress string) (bool, error) {
url := client.Host + ADDPEER
combine := client.UserName + ":" + client.Password
combineToBase64 := "Basic " + base64.StdEncoding.EncodeToString([]byte(combine))
params := map[string]interface{}{"Address": ipAddress}
peerBytes, err := util.PostRequestWithAuth(url, a.Version, params, combineToBase64)
peerBytes, err := util.PostRequestWithAuth(url, client.Version, params, combineToBase64)
if err != nil {
return false, errors.New("Add Peer error:" + err.Error())
}
Expand All @@ -51,11 +51,11 @@ func (a *AElfClient) AddPeer(ipAddress string) (bool, error) {
return data.(bool), nil
}

//GetPeers Gets information about the peer nodes of the current node.Optional whether to include metrics.
func (a *AElfClient) GetPeers(withMetrics bool) ([]*dto.PeerDto, error) {
url := a.Host + PEERS
// GetPeers Gets information about the peer nodes of the current node.Optional whether to include metrics.
func (client *AElfClient) GetPeers(withMetrics bool) ([]*dto.PeerDto, error) {
url := client.Host + PEERS
params := map[string]interface{}{"withMetrics": withMetrics}
peerBytes, err := util.GetRequest("GET", url, a.Version, params)
peerBytes, err := util.GetRequest("GET", url, client.Version, params)
if err != nil {
return nil, errors.New("Get Peers error:" + err.Error())
}
Expand Down
Loading

0 comments on commit 3d5f0fa

Please sign in to comment.