From 0cee83314a8ecdaf163e5f5d4258cbb0a8fe0b2a Mon Sep 17 00:00:00 2001 From: AstaFrode Date: Tue, 4 Jul 2023 14:32:54 +0800 Subject: [PATCH] Base cmm3 (#97) * add example.go * update p2p to v0.0.36 * update example --- example/example.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- 3 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 example/example.go diff --git a/example/example.go b/example/example.go new file mode 100644 index 0000000..2339164 --- /dev/null +++ b/example/example.go @@ -0,0 +1,95 @@ +package main + +import ( + "fmt" + "time" + + cess "github.com/CESSProject/cess-go-sdk" + "github.com/CESSProject/cess-go-sdk/config" + "github.com/CESSProject/cess-go-sdk/core/utils" + "github.com/centrifuge/go-substrate-rpc-client/v4/signature" +) + +// Substrate well-known mnemonic: +// +// https://github.com/substrate-developer-hub/substrate-developer-hub.github.io/issues/613 +var MY_MNEMONIC = "bottom drive obey lake curtain smoke basket hold race lonely fit walk" + +var RPC_ADDRS = []string{ + "wss://testnet-rpc0.cess.cloud/ws/", + "wss://testnet-rpc1.cess.cloud/ws/", +} + +var Workspace = "/cess" +var Port = 4001 +var Bootstrap = []string{ + "_dnsaddr.sjc-1.bootstrap-kldr.cess.cloud", +} + +const File = "/home/test_download" +const BucketName = "myBucket" +const FileHash = "c158d7008e94d3af61033b6861aa4f35a4c2b829c7e97224fcbb54618de55945" + +func main() { + RetrieveFile() +} + +func RetrieveFile() { + sdk, err := cess.New( + config.CharacterName_Client, + cess.ConnectRpcAddrs(RPC_ADDRS), + cess.Mnemonic(MY_MNEMONIC), + cess.TransactionTimeout(time.Second*10), + cess.Workspace(Workspace), + cess.P2pPort(Port), + cess.Bootnodes(Bootstrap), + ) + if err != nil { + panic(err) + } + + fmt.Println(sdk.RetrieveFile(FileHash, File)) +} + +func StoreFile() { + sdk, err := cess.New( + config.CharacterName_Client, + cess.ConnectRpcAddrs(RPC_ADDRS), + cess.Mnemonic(MY_MNEMONIC), + cess.TransactionTimeout(time.Second*10), + cess.Workspace(Workspace), + cess.P2pPort(Port), + cess.Bootnodes(Bootstrap), + ) + if err != nil { + panic(err) + } + + keyringPair, err := signature.KeyringPairFromSecret(MY_MNEMONIC, 0) + + if !utils.CheckBucketName(BucketName) { + panic("invalid bucket name") + } + + fmt.Println(sdk.StoreFile(keyringPair.PublicKey, File, BucketName)) +} + +func CreateBucket() { + sdk, err := cess.New( + config.CharacterName_Client, + cess.ConnectRpcAddrs(RPC_ADDRS), + cess.Mnemonic(MY_MNEMONIC), + cess.TransactionTimeout(time.Second*10), + ) + if err != nil { + panic(err) + } + + keyringPair, err := signature.KeyringPairFromSecret(MY_MNEMONIC, 0) + + if !utils.CheckBucketName(BucketName) { + panic("invalid bucket name") + } + + fmt.Println(sdk.CreateBucket(keyringPair.PublicKey, BucketName)) +} diff --git a/go.mod b/go.mod index 31950dd..0ac07dd 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/CESSProject/cess-go-sdk go 1.19 require ( - github.com/CESSProject/p2p-go v0.0.35 + github.com/CESSProject/p2p-go v0.0.36 github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce github.com/cbergoon/merkletree v0.2.0 github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.13 diff --git a/go.sum b/go.sum index 2ecad85..2cb7cbc 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/CESSProject/p2p-go v0.0.35 h1:hrCfmO4DGaMrbCzA9urqITb+zrym41SEokUUXSx+jq4= -github.com/CESSProject/p2p-go v0.0.35/go.mod h1:MmuZ2UfXnEJMZMhOCC4Ec5QQUJCT7j2yg3Xk/oZ16yw= +github.com/CESSProject/p2p-go v0.0.36 h1:FyXwuE6nrfgsb32BdTgQdZeSUqLjI/KHchOTIFiLn8c= +github.com/CESSProject/p2p-go v0.0.36/go.mod h1:MmuZ2UfXnEJMZMhOCC4Ec5QQUJCT7j2yg3Xk/oZ16yw= github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRrLeDnvGIM= github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=