Skip to content

Commit

Permalink
Merge pull request #17 from TRON-US/BTFS-1603
Browse files Browse the repository at this point in the history
BTFS-1603
  • Loading branch information
Eric Chen authored Apr 22, 2020
2 parents 5c9acda + 7f4cf16 commit 1e8a724
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 70 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
57 changes: 31 additions & 26 deletions shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
138 changes: 112 additions & 26 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -283,15 +283,15 @@ 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()
if err != nil {
return err
}

signedBatchContracts, errSign = unsignedBatchContracts.SignContracts(utils.GetPrivateKey(), sessionStatus)
signedBatchContracts, errSign = unsignedBatchContracts.SignContracts(utils.GetPrivateKey(), t)
if errSign != nil {
return err
}
Expand All @@ -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.
Expand All @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
}
Loading

0 comments on commit 1e8a724

Please sign in to comment.