diff --git a/README.md b/README.md index 6e19b4c..d1965c9 100644 --- a/README.md +++ b/README.md @@ -2,23 +2,375 @@

Go SDK For Ontology

Version 1.0.0

-## Overview +## 1、Overview This is a comprehensive Go library for the Ontology blockchain. Currently, it supports local wallet management, digital asset management, deployment and invoke for Smart Contract , and communication with Ontology Blockchain. The future will also support more rich functions and applications . -## How to use? +## 2、How to use? First of all, Create OntologySDK instance by NewOntologySdk method. -`sdk := NewOntologySdk()` +`ontSdk := NewOntologySdk()` -Then, set rpc server address. +Then, create rpc, rest or websocket client. -`sdk.Rpc.SetAddress("http://localhost:20336")` +`ontSdk.NewRpcClient().SetAddress("http://localhost:20336")` Then, call rpc server through sdk instance. -`sdk.Rpc.GetVersion()` +### 2.1 Block Chain API + +#### 2.1.1 Get current block height + +``` +ontSdk.GetCurrentBlockHeight() (uint32, error) +``` + +#### 2.1.2 Get current block hash + +``` +ontSdk.GetCurrentBlockHash() (common.Uint256, error) +``` + +#### 2.1.3 Get block by height + +``` +ontSdk.GetBlockByHeight(height uint32) (*types.Block, error) +``` + +#### 2.1.4 Get block by hash + +``` +ontSdk.GetBlockByHash(blockHash string) (*types.Block, error) +``` + +#### 2.1.5 Get transaction by transactiom hash + +``` +ontSdk.GetTransaction(txHash string) (*types.Transaction, error) +``` + +#### 2.1.6 Get block hash by block height + +``` +ontSdk.GetBlockHash(height uint32) (common.Uint256, error) +``` + +#### 2.1.7 Get block height by transaction hash + +``` +ontSdk.GetBlockHeightByTxHash(txHash string) (uint32, error) +``` + +#### 2.1.8 Get transaction hashes of block by block height + +``` +ontSdk.GetBlockTxHashesByHeight(height uint32) (*sdkcom.BlockTxHashes, error) +``` + +#### 2.1.9 Get storage value of smart contract key + +``` +ontSdk.GetStorage(contractAddress string, key []byte) ([]byte, error) +``` + +#### 2.1.10 Get smart contract by contract address + +``` +ontSdk.GetSmartContract(contractAddress string) (*sdkcom.SmartContract, error) +``` + +#### 2.1.11 Get smart contract event by transaction hash + +``` +ontSdk.GetSmartContractEvent(txHash string) (*sdkcom.SmartContactEvent, error) +``` + +#### 2.1.12 Get all of smart contract events of block by block height + +``` +ontSdk.GetSmartContractEventByBlock(height uint32) ([]*sdkcom.SmartContactEvent, error) +``` + +#### 2.1.13 Get block merkle proof by transaction hash + +``` +ontSdk.GetMerkleProof(txHash string) (*sdkcom.MerkleProof, error) +``` + +#### 2.1.14 Get transaction state of transaction pool + +``` +ontSdk.GetMemPoolTxState(txHash string) (*sdkcom.MemPoolTxState, error) +``` + +#### 2.1.15 Get transaction count in transaction pool + +``` +ontSdk.GetMemPoolTxCount() (*sdkcom.MemPoolTxCount, error) +``` + +#### 2.1.16 Get version of Ontology + +``` +ontSdk.GetVersion() (string, error) +``` + +#### 2.1.17 Get network id of Ontology + +``` +ontSdk.GetNetworkId() (uint32, error) +``` + +#### 2.1.18 Send transaction to Ontology + +``` +ontSdk.SendTransaction(mutTx *types.MutableTransaction) (common.Uint256, error) +``` + +#### 2.19 Prepare execute transaction + +``` +ontSdk.PreExecTransaction(mutTx *types.MutableTransaction) (*sdkcom.PreExecResult, error) +``` + +### 2.2 Wallet API + +#### 2.2.1 Create or Open Wallet + +``` +wa, err := OpenWallet(path string) (*Wallet, error) +``` + +If wallet file of path exist, open waller, or create new wallet. + +#### 2.2.2 Save Wallet + +``` +wa.Save() error +``` +Note that any modify of wallet need call Save() method to presist to wallet file. + +#### 2.2.3 New account + +``` +wa.NewAccount(keyType keypair.KeyType, curveCode byte, sigScheme s.SignatureScheme, passwd []byte) (*Account, error) +``` + +Ontology support three type of key: ecdsa, sm2 and ed25519, and support 224、256、384、521 bits length of key in ecdsa, but only support 256 bits length of key in sm2 and ed25519. + +Ontology support multiple signature scheme. + +For ECDSA support SHA224withECDSA、SHA256withECDSA、SHA384withECDSA、SHA512withEdDSA、SHA3-224withECDSA、SHA3-256withECDSA、SHA3-384withECDSA、SHA3-512withECDSA、RIPEMD160withECDSA; + +For SM2 support SM3withSM2, and for SHA512withEdDSA. + +#### 2.2.4 New default setting account + +``` +wa.NewDefaultSettingAccount(passwd []byte) (*Account, error) +``` + +Default setting account using ECDSA with SHA256withECDSA as signature scheme. + +#### 2.2.5 New account from wif private key + +``` +wa.NewAccountFromWIF(wif, passwd []byte) (*Account, error) +``` + +#### 2.2.5 Delete account + +``` +wa.DeleteAccount(address string) error +``` + +#### 2.2.5 Get default account + +``` +wa.GetDefaultAccount(passwd []byte) (*Account, error) +``` + +#### 2.2.6 Set default account + +``` +wa.SetDefaultAccount(address string) error +``` + +#### 2.2.7 Get account by address + +``` +wa.GetAccountByAddress(address string, passwd []byte) (*Account, error) +``` + +#### 2.2.8 Get account by lable + +``` +wa.GetAccountByLabel(label string, passwd []byte) (*Account, error) +``` + +#### 2.2.9 Get account by index + +``` +wa.GetAccountByIndex(index int, passwd []byte) (*Account, error) +``` +Note that index start from 1. + +#### 2.2.10 Get account count of wallet + +``` +wa.GetAccountCount() int +``` + +#### 2.2.11 Get default account data + +``` +wa.GetDefaultAccountData() (*AccountData, error) +``` + +#### 2.2.12 Get account data by address + +``` +wa.GetAccountDataByAddress(address string) (*AccountData, error) +``` + +#### 2.2.13 Get account data by label + +``` +wa.GetAccountDataByLabel(label string) (*AccountData, error) +``` + +#### 2.2.14 Get account data by index + +``` +wa.GetAccountDataByIndex(index int) (*AccountData, error) +``` +Note that index start from 1. + +#### 2.2.15 Set account label + +``` +wa.SetLabel(address, newLabel string) error +``` + +Note that label cannot duplicate. + +#### 2.2.16 Set signature scheme of account + +``` +wa.SetSigScheme(address string, sigScheme s.SignatureScheme) error +``` + +#### 2.2.17 Change account password + +``` +wa.ChangeAccountPassword(address string, oldPassword, newPassword []byte) error +``` + +#### 2.2.18 Import account to wallet + +``` +wa.ImportAccounts(accountDatas []*AccountData, passwds [][]byte) error +``` + +#### 2.2.19 Export account to a new wallet + +``` +wa.ExportAccounts(path string, accountDatas []*AccountData, passwds [][]byte, newScrypts ...*keypair.ScryptParam) (*Wallet, error) +``` + +### 2.3 ONT Contract API + +#### 2.3.1 Get balance + +``` +ontSdk.Native.Ont.BalanceOf(address common.Address) (uint64, error) +``` + +#### 2.3.2 Transfer + +``` +ontSdk.Native.Ont.Transfer(gasPrice, gasLimit uint64, from *Account, to common.Address, amount uint64) (common.Uint256, error) +``` + +#### 2.3.3 Multipe Transfer + +``` +ontSdk.Native.Ont.MultiTransfer(gasPrice, gasLimit uint64, states []*ont.State, signer *Account) (common.Uint256, error) +``` + +Multipe transfer means do more than one transfers ONT in one transaction. + +#### 2.3.4 Approve + +``` +ontSdk.Native.Ont.Approve(gasPrice, gasLimit uint64, from *Account, to common.Address, amount uint64) (common.Uint256, error) +``` + +#### 2.3.5 Approve Balance + +``` +ontSdk.Native.Ont.Allowance(from, to common.Address) (uint64, error) +``` + +#### 2.3.6 TransferFrom + +``` +ontSdk.Native.Ont.TransferFrom(gasPrice, gasLimit uint64, sender *Account, from, to common.Address, amount uint64) (common.Uint256, error) +``` + +### 2.4 ONG Contract API + + +#### 2.4.1 Get balance + +``` +ontSdk.Native.Ong.BalanceOf(address common.Address) (uint64, error) +``` + +#### 2.4.2 Transfer + +``` +ontSdk.Native.Ong.Transfer(gasPrice, gasLimit uint64, from *Account, to common.Address, amount uint64) (common.Uint256, error) +``` + +#### 2.4.3 Multipe Transfer + +``` +ontSdk.Native.Ong.MultiTransfer(gasPrice, gasLimit uint64, states []*ont.State, signer *Account) (common.Uint256, error) +``` + +Multipe transfer means do more than one transfers ONT in one transaction. + +#### 2.4.4 Approve + +``` +ontSdk.Native.Ong.Approve(gasPrice, gasLimit uint64, from *Account, to common.Address, amount uint64) (common.Uint256, error) +``` + +#### 2.4.5 Approve Balance + +``` +ontSdk.Native.Ong.Allowance(from, to common.Address) (uint64, error) +``` + +#### 2.4.6 TransferFrom + +``` +ontSdk.Native.Ong.TransferFrom(gasPrice, gasLimit uint64, sender *Account, from, to common.Address, amount uint64) (common.Uint256, error) +``` + +#### 2.4.7 Withdraw ONG + +``` +ontSdk.Native.Ong.WithdrawONG(gasPrice, gasLimit uint64, address *Account, amount uint64) (common.Uint256, error) +``` + +#### 2.4.8 Get unbound ONG + +``` +ontSdk.Native.Ong.UnboundONG(address common.Address) (uint64, error) +``` # Contributing diff --git a/client/client.go b/client/client.go index 674a94f..92b8216 100644 --- a/client/client.go +++ b/client/client.go @@ -200,18 +200,6 @@ func (this *ClientMgr) GetSmartContractEventByBlock(height uint32) ([]*sdkcom.Sm return utils.GetSmartContactEvents(data) } -func (this *ClientMgr) GetGenerateBlockTime() (uint32, error) { - client := this.getClient() - if client == nil { - return 0, fmt.Errorf("don't have available client of ontology") - } - data, err := client.getGenerateBlockTime(this.getNextQid()) - if err != nil { - return 0, err - } - return utils.GetUint32(data) -} - func (this *ClientMgr) GetMerkleProof(txHash string) (*sdkcom.MerkleProof, error) { client := this.getClient() if client == nil { diff --git a/client/define.go b/client/define.go index 5eab880..1679f5b 100644 --- a/client/define.go +++ b/client/define.go @@ -21,7 +21,6 @@ type OntologyClient interface { getSmartContractEvent(qid, txHash string) ([]byte, error) getSmartContractEventByBlock(qid string, blockHeight uint32) ([]byte, error) getStorage(qid, contractAddress string, key []byte) ([]byte, error) - getGenerateBlockTime(qid string) ([]byte, error) getMerkleProof(qid, txHash string) ([]byte, error) getMemPoolTxState(qid, txHash string) ([]byte, error) getMemPoolTxCount(qid string) ([]byte, error) diff --git a/client/rest.go b/client/rest.go index b55f1bc..3479564 100644 --- a/client/rest.go +++ b/client/rest.go @@ -167,11 +167,6 @@ func (this *RestClient) getBlockTxHashesByHeight(qid string, height uint32) ([]b return this.sendRestGetRequest(reqPath) } -func (this *RestClient) getGenerateBlockTime(qid string) ([]byte, error) { - reqPath := GET_GEN_BLK_TIME - return this.sendRestGetRequest(reqPath) -} - func (this *RestClient) sendRawTransaction(qid string, tx *types.Transaction, isPreExec bool) ([]byte, error) { reqPath := POST_RAW_TX var buffer bytes.Buffer diff --git a/client/rpc.go b/client/rpc.go index 2d6e04c..130ebf2 100644 --- a/client/rpc.go +++ b/client/rpc.go @@ -136,10 +136,6 @@ func (this *RpcClient) getSmartContract(qid, contractAddress string) ([]byte, er return this.sendRpcRequest(qid, RPC_GET_SMART_CONTRACT, []interface{}{contractAddress}) } -func (this *RpcClient) getGenerateBlockTime(qid string) ([]byte, error) { - return this.sendRpcRequest(qid, RPC_GET_GENERATE_BLOCK_TIME, []interface{}{}) -} - //GetMerkleProof return the merkle proof whether tx is exist in ledger. Param txHash is in hex string code func (this *RpcClient) getMerkleProof(qid, txHash string) ([]byte, error) { return this.sendRpcRequest(qid, RPC_GET_MERKLE_PROOF, []interface{}{txHash}) diff --git a/client/ws.go b/client/ws.go index 48e7489..10b3592 100644 --- a/client/ws.go +++ b/client/ws.go @@ -30,10 +30,10 @@ import ( ) type WSSubscribeStatus struct { - ContractsFilter []string - SubscribeEvent bool - SubscribeJsonBlock bool - SubscribeRawBlock bool + ContractsFilter []string + SubscribeEvent bool + SubscribeJsonBlock bool + SubscribeRawBlock bool SubscribeBlockTxHashes bool } @@ -501,10 +501,6 @@ func (this *WSClient) getSmartContractEventByBlock(qid string, blockHeight uint3 return this.sendSyncWSRequest(qid, WS_ACTION_GET_SMARTCONTRACT_BY_HEIGHT, map[string]interface{}{"Height": blockHeight}) } -func (this *WSClient) getGenerateBlockTime(qid string) ([]byte, error) { - return this.sendSyncWSRequest(qid, WS_ACTION_GET_GENERATE_BLOCK_TIME, nil) -} - func (this *WSClient) getActionCh() chan *WSAction { return this.actionCh } diff --git a/common/define.go b/common/define.go index 5e0c62b..4e66554 100644 --- a/common/define.go +++ b/common/define.go @@ -254,14 +254,14 @@ type MemPoolTxState struct { } type MemPoolTxStateItem struct { - Height uint32 - Type int - ErrCode int + Height uint32 // The height in which tx was verified + Type int // The validator flag: stateless/stateful + ErrCode int // Verified result } type MemPoolTxCount struct { - Verified uint32 - Verifing uint32 + Verified uint32 //Tx count of verified + Verifing uint32 //Tx count of verifing } type GlobalParam struct { diff --git a/neovm_contract.go b/neovm_contract.go index b2a0719..1cf3a2c 100644 --- a/neovm_contract.go +++ b/neovm_contract.go @@ -4,10 +4,10 @@ import ( "encoding/hex" "fmt" sdkcom "github.com/ontio/ontology-go-sdk/common" - httpcom "github.com/ontio/ontology/http/base/common" "github.com/ontio/ontology/common" "github.com/ontio/ontology/core/payload" "github.com/ontio/ontology/core/types" + httpcom "github.com/ontio/ontology/http/base/common" "time" ) diff --git a/ont_sdk_test.go b/ont_sdk_test.go index 3c6f244..eca9da9 100644 --- a/ont_sdk_test.go +++ b/ont_sdk_test.go @@ -10,7 +10,7 @@ import ( var ( testOntSdk *OntologySdk testWallet *Wallet - testPasswd = []byte("wangbing") + testPasswd = []byte("123456") testDefAcc *Account testGasPrice = uint64(0) testGasLimit = uint64(20000)