Skip to content

Commit

Permalink
feat: p2p remote call
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn-Huang-Tron committed Nov 28, 2023
1 parent 2a7a1cf commit f7c3f83
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
2 changes: 2 additions & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ func TestCommands(t *testing.T) {
"/accesskey/get",
"/accesskey/list",
"/cheque/fix_cheque_cashout",
"encrypt",
"decrypt",
}

cmdSet := make(map[string]struct{})
Expand Down
59 changes: 30 additions & 29 deletions core/commands/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
cmds "github.com/bittorrent/go-btfs-cmds"
cp "github.com/bittorrent/go-btfs-common/crypto"
"github.com/bittorrent/go-btfs/core/commands/cmdenv"
"github.com/ethereum/go-ethereum/crypto"
eth "github.com/ethereum/go-ethereum/crypto"
"github.com/bittorrent/go-btfs/core/corehttp/remote"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
peer "github.com/libp2p/go-libp2p/core/peer"
)
Expand All @@ -35,9 +35,6 @@ var encryptCmd = &cmds.Command{
if err != nil {
return err
}
if err != nil {
return err
}
to, ok := r.Options[toOption].(string)
if !ok {
to = n.Identity.String()
Expand All @@ -55,7 +52,7 @@ var encryptCmd = &cmds.Command{
return errors.New("can't change from p2p public key to secp256k1 public key from peerID")
}

ethPk, err := eth.UnmarshalPubkey(pkBytes)
ethPk, err := ethCrypto.UnmarshalPubkey(pkBytes)
if err != nil {
return errors.New("can't unmarshall public key from peerID")
}
Expand Down Expand Up @@ -95,26 +92,44 @@ var decryptCmd = &cmds.Command{
if err != nil {
return err
}
_, ok := r.Options[fromOption].(string)
if ok {
// TODO: get cid from fromOption(remoteCall)
n, err := cmdenv.GetNode(e)
if err != nil {
return err
}
cid := r.Arguments[0]
btfsClient := shell.NewLocalShell()
rc, err := btfsClient.Cat(cid)
api, err := cmdenv.GetApi(e, r)
if err != nil {
return err
}
defer rc.Close()
ecdsaPrivateKey, err := crypto.HexToECDSA(conf.Identity.HexPrivKey)

var readClose io.ReadCloser
cid := r.Arguments[0]
from, ok := r.Options[fromOption].(string)
if ok {
peerID, err := peer.Decode(from)
if err != nil {
return err
}
b, err := remote.P2PCallStrings(r.Context, n, api, peerID, "/decryption", cid)
if err != nil {
return err
}
readClose = io.NopCloser(bytes.NewReader(b))
} else {
readClose, err = shell.NewLocalShell().Cat(cid)
if err != nil {
return err
}
}
ecdsaPrivateKey, err := ethCrypto.HexToECDSA(conf.Identity.HexPrivKey)
if err != nil {
return err
}
eciesPrivateKey := ecies.ImportECDSA(ecdsaPrivateKey)
endata, err := io.ReadAll(rc)
endata, err := io.ReadAll(readClose)
if err != nil {
return err
}
defer readClose.Close()
dedata, err := ECCDecrypt(endata, *eciesPrivateKey)
if err != nil {
panic(err)
Expand All @@ -133,20 +148,6 @@ var decryptCmd = &cmds.Command{
},
}

var getEncryptedCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "get encrypted file by cid",
},
Arguments: []cmds.Argument{
cmds.StringArg("cid", true, false, "cid of encrypted file"),
},

Run: func(r *cmds.Request, re cmds.ResponseEmitter, e cmds.Environment) error {
re.Emit("hello world!")
return nil
},
}

func ECCEncrypt(pt []byte, puk ecies.PublicKey) ([]byte, error) {
ct, err := ecies.Encrypt(rand.Reader, &puk, pt, nil, nil)
return ct, err
Expand Down
2 changes: 1 addition & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ var rootRemoteSubcommands = map[string]*cmds.Command{
"handshake": P2phandshakeCmd,
},
},
"encryption": getEncryptedCmd,
"decryption": GetCmd,
}

func init() {
Expand Down

0 comments on commit f7c3f83

Please sign in to comment.