diff --git a/go.mod b/go.mod index 0f0253f34..01622be90 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/multiformats/go-multiaddr v0.0.4 github.com/multiformats/go-multiaddr-net v0.0.1 - github.com/tron-us/go-btfs-common v0.3.0 + github.com/tron-us/go-btfs-common v0.3.7 github.com/tron-us/go-common/v2 v2.0.5 github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c ) diff --git a/go.sum b/go.sum index 2306f4b84..f8a04cee0 100644 --- a/go.sum +++ b/go.sum @@ -158,8 +158,8 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/tron-us/go-btfs-common v0.2.11 h1:hxc7oUQHPViKzdaTZLHuS2vyYU1e6/IToZI7UY1p2Tk= github.com/tron-us/go-btfs-common v0.2.11/go.mod h1:9ND33JahGMg52sCC2/gO5DakLsd1Pg2lVe2CihW7lBE= -github.com/tron-us/go-btfs-common v0.3.0 h1:/SOwL6AetUuBJjrOgIaYRsXh0CTi12hvbMqYtAl+O18= -github.com/tron-us/go-btfs-common v0.3.0/go.mod h1:FbYoo6ZrtnJH3TKdyJTGQrUP2rbwNVATQpxplwaYQ/c= +github.com/tron-us/go-btfs-common v0.3.7 h1:fcdXv4FpfNXTvGjuuFq/bJtw//b3uHCtNI/cwUxKLjA= +github.com/tron-us/go-btfs-common v0.3.7/go.mod h1:FbYoo6ZrtnJH3TKdyJTGQrUP2rbwNVATQpxplwaYQ/c= github.com/tron-us/go-common/v2 v2.0.5 h1:LiGEmOd0FVVRisFyg3AJWDWMXlM537M3JZ+gh75sBlw= github.com/tron-us/go-common/v2 v2.0.5/go.mod h1:GiKX9noBLHotkZAU+7ET4h7N0DYWnm3OcGHOFJg1Q68= github.com/tron-us/protobuf v1.3.4 h1:oqokl6jMAfe1fb/B6t1UMllbw/KtfdJcCn8plxPkHM8= diff --git a/shell_test.go b/shell_test.go index 22380eea4..3f8592a35 100644 --- a/shell_test.go +++ b/shell_test.go @@ -507,49 +507,54 @@ func TestStorageUploadWithOffSign(t *testing.T) { //var storage Storage LOOP: for { + sleepMoment() storage, err := s.StorageUploadStatus(sessionId) is.Nil(err) switch storage.Status { case "complete": break LOOP case "error": - fmt.Printf("%#v, %#v\n", storage.Status, storage.Message) t.Fatal(fmt.Errorf("%s", storage.Message)) - case "initSignReadyEscrow", "initSignReadyGuard": - fmt.Printf("%#v\n", storage.Status) - c, err := s.StorageUploadGetContractBatch(sessionId, mhash, uts, storage.Status) + case "init": + ec, err := s.StorageUploadGetContractBatch(sessionId, uts, "escrow") + is.Nil(err) + if len(ec.Contracts) == 0 { + continue + } + gc, err := s.StorageUploadGetContractBatch(sessionId, uts, "guard") is.Nil(err) - err = s.StorageUploadSignBatch(sessionId, mhash, c, uts, storage.Status) - // Note err is set to io.EOF when the btfs daemon returns nil from the endpoint + if len(gc.Contracts) == 0 { + continue + } + err = s.StorageUploadSignBatch(sessionId, ec, uts, "escrow") + is.Nil(err) + err = s.StorageUploadSignBatch(sessionId, gc, uts, "guard") is.Nil(err) fmt.Printf("%#v\n", storage.Status) - case "balanceSignReady", "payChannelSignReady", "payRequestSignReady", "guardSignReady": - unsigned, err := s.StorageUploadGetUnsignedData(sessionId, mhash, uts, storage.Status) + case "submit", "submit:check-balance-req-singed", "pay", "pay:payin-req-signed", "guard", + "guard:file-meta-signed", "wait-upload": + unsigned, err := s.StorageUploadGetUnsignedData(sessionId, uts, storage.Status) is.Nil(err) - switch unsigned.Opcode { - case "balance": - err = s.StorageUploadSignBalance(sessionId, mhash, unsigned, uts, storage.Status) - is.Nil(err) - fmt.Printf("%#v\n", storage.Status) - case "paychannel": - err = s.StorageUploadSignPayChannel(sessionId, mhash, unsigned, uts, storage.Status, unsigned.Price) - is.Nil(err) - fmt.Printf("%#v\n", storage.Status) - case "payrequest": - err = s.StorageUploadSignPayRequest(sessionId, mhash, unsigned, uts, storage.Status) - is.Nil(err) - fmt.Printf("%#v\n", storage.Status) + switch storage.Status { + case "submit": + err = s.StorageUploadSignBalance(sessionId, unsigned, uts, storage.Status) + case "submit:check-balance-req-singed": + err = s.StorageUploadSignPayChannel(sessionId, unsigned, uts, storage.Status, unsigned.Price) + case "pay": + err = s.StorageUploadSignPayRequest(sessionId, unsigned, uts, storage.Status) case "guard": - err = s.StorageUploadSignGuardFileMeta(sessionId, mhash, unsigned, uts, storage.Status) - is.Nil(err) - fmt.Printf("%#v\n", storage.Status) + err = s.StorageUploadSignGuardFileMeta(sessionId, unsigned, uts, storage.Status) + case "guard:file-meta-signed": + err = s.StorageUploadSignGuardQuestions(sessionId, unsigned, uts, storage.Status) + case "wait-upload": + err = s.StorageUploadSignWaitupload(sessionId, unsigned, uts, storage.Status) default: - fmt.Printf("unexpected Opcode: %#v continue \n", unsigned.Opcode) } + is.Nil(err) + fmt.Printf("%#v\n", storage.Status) default: fmt.Printf("%#v continue \n", storage.Status) } - sleepMoment() } fmt.Printf("Complete\n") } diff --git a/storage.go b/storage.go index e887ecc82..74388f985 100644 --- a/storage.go +++ b/storage.go @@ -88,7 +88,7 @@ func (d UnsignedData) SignBalanceData(privateKey string) (*ledgerpb.SignedPublic if err != nil { return nil, err } - pubKeyRaw, err := privKey.GetPublic().Raw() + pubKeyRaw, err := ic.RawFull(privKey.GetPublic()) if err != nil { return nil, err } @@ -106,7 +106,7 @@ func (d UnsignedData) SignBalanceData(privateKey string) (*ledgerpb.SignedPublic return lgSignedPubKey, nil } -func (c Contracts) SignContracts(privateKey string, sessionStatus string) (*Contracts, error) { +func (c Contracts) SignContracts(privateKey string, t string) (*Contracts, error) { // Perform signing using private key privKey, err := crypto.ToPrivKey(privateKey) if err != nil { @@ -118,9 +118,8 @@ func (c Contracts) SignContracts(privateKey string, sessionStatus string) (*Cont return nil, err } var signedContract []byte - if sessionStatus == "initSignReadyEscrow" { + if t == "escrow" { escrowContract := &escrowpb.EscrowContract{} - err = proto.Unmarshal(by, escrowContract) if err != nil { return nil, err @@ -130,9 +129,8 @@ func (c Contracts) SignContracts(privateKey string, sessionStatus string) (*Cont return nil, err } - } else { + } else if t == "guard" { guardContract := &guardpb.ContractMeta{} - //var guardContract proto.Message err := proto.Unmarshal(by, guardContract) if err != nil { return nil, err @@ -141,6 +139,8 @@ func (c Contracts) SignContracts(privateKey string, sessionStatus string) (*Cont if err != nil { return nil, err } + } else { + return nil, errors.New("not support type:" + t) } // This overwrites str, err := cutils.BytesToString(signedContract, cutils.Base64) @@ -246,7 +246,7 @@ func (s *Shell) StorageUploadOffSign(hash string, uts string, options ...Storage if err != nil { return "", err } - rb := s.Request("storage/upload/offline", hash, utils.GetPeerId(), uts, offlinePeerSessionSignature) + rb := s.Request("storage/upload", hash, utils.GetPeerId(), uts, offlinePeerSessionSignature) for _, option := range options { _ = option(rb) } @@ -261,18 +261,18 @@ func (s *Shell) StorageUploadStatus(id string) (*Storage, error) { } // Storage upload get offline contract batch api. -func (s *Shell) StorageUploadGetContractBatch(sid string, hash string, uts string, sessionStatus string) (*Contracts, error) { +func (s *Shell) StorageUploadGetContractBatch(sid string, uts string, t string) (*Contracts, error) { var out Contracts offlinePeerSessionSignature, err := getSessionSignature() if err != nil { return nil, err } - rb := s.Request("storage/upload/getcontractbatch", sid, utils.GetPeerId(), uts, offlinePeerSessionSignature, sessionStatus) + rb := s.Request("storage/upload/getcontractbatch", sid, utils.GetPeerId(), uts, offlinePeerSessionSignature, t) return &out, rb.Exec(context.Background(), &out) } // Storage upload get offline unsigned data api. -func (s *Shell) StorageUploadGetUnsignedData(sid string, hash string, uts string, sessionStatus string) (*UnsignedData, error) { +func (s *Shell) StorageUploadGetUnsignedData(sid string, uts string, sessionStatus string) (*UnsignedData, error) { var out UnsignedData offlinePeerSessionSignature, err := getSessionSignature() if err != nil { @@ -283,7 +283,7 @@ func (s *Shell) StorageUploadGetUnsignedData(sid string, hash string, uts string } // Storage upload sign offline contract batch api. -func (s *Shell) StorageUploadSignBatch(sid string, hash string, unsignedBatchContracts *Contracts, uts string, sessionStatus string) error { +func (s *Shell) StorageUploadSignBatch(sid string, unsignedBatchContracts *Contracts, uts string, t string) error { var signedBatchContracts *Contracts var errSign error offlinePeerSessionSignature, err := getSessionSignature() @@ -291,7 +291,7 @@ func (s *Shell) StorageUploadSignBatch(sid string, hash string, unsignedBatchCon return err } - signedBatchContracts, errSign = unsignedBatchContracts.SignContracts(utils.GetPrivateKey(), sessionStatus) + signedBatchContracts, errSign = unsignedBatchContracts.SignContracts(utils.GetPrivateKey(), t) if errSign != nil { return err } @@ -301,8 +301,10 @@ func (s *Shell) StorageUploadSignBatch(sid string, hash string, unsignedBatchCon } rb := s.Request("storage/upload/signcontractbatch", sid, utils.GetPeerId(), uts, offlinePeerSessionSignature, - sessionStatus, string(bytesSignBatch)) - return rb.Exec(context.Background(), nil) + t, string(bytesSignBatch)) + ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) + errSign = rb.Exec(ctx, nil) + return errSign } // Storage upload sign offline data api. @@ -321,8 +323,7 @@ func (s *Shell) StorageUploadSign(id string, hash string, unsignedData *Unsigned return out, rb.Exec(context.Background(), &out) } -func (s *Shell) StorageUploadSignBalance(id string, hash string, unsignedData *UnsignedData, - uts string, sessionStatus string) error { +func (s *Shell) StorageUploadSignBalance(id string, unsignedData *UnsignedData, uts string, sessionStatus string) error { var rb *RequestBuilder offlinePeerSessionSignature, err := getSessionSignature() @@ -342,11 +343,12 @@ func (s *Shell) StorageUploadSignBalance(id string, hash string, unsignedData *U if err != nil { return err } - rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, str, sessionStatus) + rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, sessionStatus, str) return rb.Exec(context.Background(), nil) } -func (s *Shell) StorageUploadSignPayChannel(id, hash string, unsignedData *UnsignedData, uts string, sessionStatus string, totalPrice int64) error { +func (s *Shell) StorageUploadSignPayChannel(id string, unsignedData *UnsignedData, uts string, sessionStatus string, + totalPrice int64) error { var rb *RequestBuilder offlinePeerSessionSignature, err := getSessionSignature() if err != nil { @@ -399,12 +401,13 @@ func (s *Shell) StorageUploadSignPayChannel(id, hash string, unsignedData *Unsig if err != nil { return err } - rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, signedChanCommitBytesStr, sessionStatus) + rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, sessionStatus, + signedChanCommitBytesStr) return rb.Exec(context.Background(), nil) } -func (s *Shell) StorageUploadSignPayRequest(id, hash string, unsignedData *UnsignedData, - uts string, sessionStatus string) error { +func (s *Shell) StorageUploadSignPayRequest(id string, unsignedData *UnsignedData, uts string, + sessionStatus string) error { var rb *RequestBuilder offlinePeerSessionSignature, err := getSessionSignature() @@ -423,7 +426,10 @@ func (s *Shell) StorageUploadSignPayRequest(id, hash string, unsignedData *Unsig } chanState := result.Result.BuyerChannelState - privKey, _ := crypto.ToPrivKey(utils.GetPrivateKey()) + privKey, err := crypto.ToPrivKey(utils.GetPrivateKey()) + if err != nil { + return err + } sig, err := crypto.Sign(privKey, chanState.Channel) if err != nil { return err @@ -457,11 +463,11 @@ func (s *Shell) StorageUploadSignPayRequest(id, hash string, unsignedData *Unsig if err != nil { return err } - rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, str, sessionStatus) + rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, sessionStatus, str) return rb.Exec(context.Background(), nil) } -func (s *Shell) StorageUploadSignGuardFileMeta(id, hash string, unsignedData *UnsignedData, +func (s *Shell) StorageUploadSignGuardFileMeta(id string, unsignedData *UnsignedData, uts string, sessionStatus string) error { var rb *RequestBuilder @@ -479,7 +485,87 @@ func (s *Shell) StorageUploadSignGuardFileMeta(id, hash string, unsignedData *Un return err } - privKey, _ := crypto.ToPrivKey(utils.GetPrivateKey()) + privKey, err := crypto.ToPrivKey(utils.GetPrivateKey()) + if err != nil { + return err + } + signed, err := crypto.Sign(privKey, meta) + if err != nil { + return err + } + + str, err := cutils.BytesToString(signed, cutils.Base64) + if err != nil { + return err + } + rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, sessionStatus, str) + return rb.Exec(context.Background(), nil) +} + +func (s *Shell) StorageUploadSignGuardQuestions(id string, unsignedData *UnsignedData, + uts string, sessionStatus string) error { + var rb *RequestBuilder + + offlinePeerSessionSignature, err := getSessionSignature() + if err != nil { + return err + } + unsignedBytes, err := cutils.StringToBytes(unsignedData.Unsigned, cutils.Base64) + if err != nil { + return err + } + fcq := new(guardpb.FileChallengeQuestions) + err = proto.Unmarshal(unsignedBytes, fcq) + if err != nil { + return err + } + + privKey, err := crypto.ToPrivKey(utils.GetPrivateKey()) + if err != nil { + return err + } + for _, sq := range fcq.ShardQuestions { + sign, err := crypto.Sign(privKey, sq) + if err != nil { + return err + } + sq.PreparerSignature = sign + } + + signed, err := proto.Marshal(fcq) + if err != nil { + return err + } + str, err := cutils.BytesToString(signed, cutils.Base64) + if err != nil { + return err + } + rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, sessionStatus, str) + return rb.Exec(context.Background(), nil) +} + +func (s *Shell) StorageUploadSignWaitupload(id string, unsignedData *UnsignedData, + uts string, sessionStatus string) error { + var rb *RequestBuilder + + offlinePeerSessionSignature, err := getSessionSignature() + if err != nil { + return err + } + unsignedBytes, err := cutils.StringToBytes(unsignedData.Unsigned, cutils.Base64) + if err != nil { + return err + } + meta := new(guardpb.CheckFileStoreMetaRequest) + err = proto.Unmarshal(unsignedBytes, meta) + if err != nil { + return err + } + + privKey, err := crypto.ToPrivKey(utils.GetPrivateKey()) + if err != nil { + return err + } signed, err := crypto.Sign(privKey, meta) if err != nil { return err @@ -489,6 +575,6 @@ func (s *Shell) StorageUploadSignGuardFileMeta(id, hash string, unsignedData *Un if err != nil { return err } - rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, str, sessionStatus) + rb = s.Request("storage/upload/sign", id, utils.GetPeerId(), uts, offlinePeerSessionSignature, sessionStatus, str) return rb.Exec(context.Background(), nil) } diff --git a/utils/config.go b/utils/config.go index 6c0c8849e..c0ce391bc 100644 --- a/utils/config.go +++ b/utils/config.go @@ -3,14 +3,15 @@ package utils import ( "errors" "fmt" + "os" + "path/filepath" + serialize "github.com/TRON-US/go-btfs-config/serialize" + "github.com/tron-us/go-btfs-common/crypto" + "github.com/libp2p/go-libp2p-core/peer" "github.com/mitchellh/go-homedir" _ "github.com/mitchellh/go-homedir" - "github.com/tron-us/go-btfs-common/crypto" - "github.com/tron-us/go-common/v2/env" - "os" - "path/filepath" ) type ApiConfigStruct struct { @@ -32,17 +33,9 @@ const ( var ApiConfig ApiConfigStruct func init() { - //get variables via environment variable - if _, s := env.GetEnv("PRIVATE_KEY"); s != "" { - ApiConfig.PrivateKey = s - } - - if _, s := env.GetEnv("PEER_ID"); s != "" { - ApiConfig.PeerId = s - } - if _, s := env.GetEnv("PUBLIC_KEY"); s != "" { - ApiConfig.PublicKey = s - } + ApiConfig.PrivateKey = "CAISID3cjZrDs888SnmZ8SqckeuMFWsv/zp74DxHsAQv1FWM" + ApiConfig.PeerId = "16Uiu2HAm7thbsmvGPKwXRxTJjHHmp6XRHaiQHNScRwCfrcSr8aPg" + ApiConfig.PublicKey = "CAISIQK5OMJT9A/lXl+97/4Ec5CD2H+Y+hpg/SXSKtXuuOlTzw==" } // Precondition: Call This function when any of the member variables of the