Commit e86d2fae authored by suyanlong's avatar suyanlong

😋 remove old cryptor and latest cryptor

parent f918c4bc
Pipeline #8153 canceled with stages
...@@ -59,8 +59,8 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error) ...@@ -59,8 +59,8 @@ func NewSidecar(repoRoot string, config *repo.Config) (internal.Launcher, error)
clients := plugins.CreateClients(config.Appchains, nil) clients := plugins.CreateClients(config.Appchains, nil)
persister := manger.NewPersister(addr.String(), store, loggers.Logger(loggers.Manger)) persister := manger.NewPersister(addr.String(), store, loggers.Logger(loggers.Manger))
appchainMgr := appchainmgr.New(persister) appchainMgr := appchainmgr.New(persister)
cryptor, err := txcrypto.NewDirectCryptor(appchainMgr, privateKey) // TODO hub client
tool.Asset(err) cryptor := txcrypto.NewCryptor(nil, appchainMgr, privateKey)
clientPort := appchain.NewPorts(clients, cryptor, logger) clientPort := appchain.NewPorts(clients, cryptor, logger)
r.Adds(clientPort) r.Adds(clientPort)
mg, err := manger.NewManager(addr.String(), pm, appchainMgr, loggers.Logger(loggers.Manger)) mg, err := manger.NewManager(addr.String(), pm, appchainMgr, loggers.Logger(loggers.Manger))
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/link33/sidecar/internal/port" "github.com/link33/sidecar/internal/port"
"github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/internal/txcrypto" "github.com/link33/sidecar/internal/txcrypto"
"github.com/link33/sidecar/model/pb" "github.com/link33/sidecar/model/pb"
"github.com/link33/sidecar/pkg/plugins" "github.com/link33/sidecar/pkg/plugins"
...@@ -150,7 +151,8 @@ func (a *appChain) applyReceiptIBTP(ibtp *pb.IBTP) error { ...@@ -150,7 +151,8 @@ func (a *appChain) applyReceiptIBTP(ibtp *pb.IBTP) error {
var err error var err error
if pd.Encrypted { if pd.Encrypted {
contentByte, err = a.cryptor.Decrypt(contentByte, ibtp.To) // TODO
contentByte, err = a.cryptor.Decrypt(contentByte, ibtp.To, repo.DirectMode)
if err != nil { if err != nil {
return fmt.Errorf("decrypt ibtp payload content: %w", err) return fmt.Errorf("decrypt ibtp payload content: %w", err)
} }
...@@ -340,7 +342,7 @@ func (a *appChain) encryption(ibtp *pb.IBTP) error { ...@@ -340,7 +342,7 @@ func (a *appChain) encryption(ibtp *pb.IBTP) error {
return nil return nil
} }
ctb, err := a.cryptor.Encrypt(pld.Content, ibtp.To) ctb, err := a.cryptor.Encrypt(pld.Content, ibtp.To, repo.DirectMode)
if err != nil { if err != nil {
return err return err
} }
......
...@@ -8,46 +8,68 @@ import ( ...@@ -8,46 +8,68 @@ import (
"github.com/meshplus/bitxhub-kit/crypto" "github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/ecdh" "github.com/meshplus/bitxhub-kit/crypto/ecdh"
"github.com/meshplus/bitxhub-kit/crypto/sym" "github.com/meshplus/bitxhub-kit/crypto/sym"
rpcx "github.com/link33/sidecar/hub/client"
"github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/model/constant"
) )
type DirectCryptor struct { type cryptor struct {
client rpcx.Client
mgr appchainmgr.AppchainMgr mgr appchainmgr.AppchainMgr
privKey crypto.PrivateKey privKey crypto.PrivateKey
keyMap map[string][]byte keyMap map[string][]byte
} }
func NewDirectCryptor(mgr appchainmgr.AppchainMgr, privKey crypto.PrivateKey) (Cryptor, error) { func NewCryptor(client rpcx.Client, mgr appchainmgr.AppchainMgr, privKey crypto.PrivateKey) Cryptor {
keyMap := make(map[string][]byte) keyMap := make(map[string][]byte)
return &DirectCryptor{
return &cryptor{
client: client,
mgr: mgr, mgr: mgr,
privKey: privKey, privKey: privKey,
keyMap: keyMap, keyMap: keyMap,
}, nil }
} }
func (d *DirectCryptor) Encrypt(content []byte, address string) ([]byte, error) { func (d *cryptor) Encrypt(content []byte, address string, mode string) ([]byte, error) {
des, err := d.getDesKey(address) des, err := d.getDesKey(address, mode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return des.Encrypt(content) return des.Encrypt(content)
} }
func (d *DirectCryptor) Decrypt(content []byte, address string) ([]byte, error) { func (d *cryptor) Decrypt(content []byte, address string, mode string) ([]byte, error) {
des, err := d.getDesKey(address) des, err := d.getDesKey(address, mode)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return des.Decrypt(content) return des.Decrypt(content)
} }
func (d *DirectCryptor) getDesKey(address string) (crypto.SymmetricKey, error) { func (d *cryptor) getDesKey(address string, mode string) (crypto.SymmetricKey, error) {
pubKey, ok := d.keyMap[address] pubKey, ok := d.keyMap[address]
if !ok { if !ok {
get, ret := d.mgr.GetPubKeyByChainID(address) var (
ret []byte
get bool
)
if mode == repo.DirectMode {
get, ret = d.mgr.GetPubKeyByChainID(address)
if !get { if !get {
return nil, fmt.Errorf("cannot find the public key") return nil, fmt.Errorf("cannot find the public key")
} }
}
if mode == repo.RelayMode {
rp, err := d.client.InvokeBVMContract(constant.AppchainMgrContractAddr.Address(), "GetPubKeyByChainID", nil, rpcx.String(address))
if err != nil {
return nil, err
}
ret = rp.Ret
}
d.keyMap[address] = ret d.keyMap[address] = ret
pubKey = ret pubKey = ret
} }
......
package txcrypto
import (
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym/ecdsa"
"github.com/meshplus/bitxhub-kit/crypto/ecdh"
"github.com/meshplus/bitxhub-kit/crypto/sym"
rpcx "github.com/link33/sidecar/hub/client"
"github.com/link33/sidecar/model/constant"
)
type RelayCryptor struct {
client rpcx.Client
privKey crypto.PrivateKey
keyMap map[string][]byte
}
func NewRelayCryptor(client rpcx.Client, privKey crypto.PrivateKey) (Cryptor, error) {
keyMap := make(map[string][]byte)
return &RelayCryptor{
client: client,
privKey: privKey,
keyMap: keyMap,
}, nil
}
func (c *RelayCryptor) Encrypt(content []byte, address string) ([]byte, error) {
des, err := c.getDesKey(address)
if err != nil {
return nil, err
}
return des.Encrypt(content)
}
func (c *RelayCryptor) Decrypt(content []byte, address string) ([]byte, error) {
des, err := c.getDesKey(address)
if err != nil {
return nil, err
}
return des.Decrypt(content)
}
func (c *RelayCryptor) getDesKey(address string) (crypto.SymmetricKey, error) {
pubKey, ok := c.keyMap[address]
if !ok {
ret, err := c.client.InvokeBVMContract(constant.AppchainMgrContractAddr.Address(), "GetPubKeyByChainID", nil, rpcx.String(address))
if err != nil {
return nil, err
}
c.keyMap[address] = ret.Ret
pubKey = ret.Ret
}
ke, err := ecdh.NewEllipticECDH(ecdsa.S256())
if err != nil {
return nil, err
}
secret, err := ke.ComputeSecret(c.privKey, pubKey)
if err != nil {
return nil, err
}
return sym.GenerateSymKey(crypto.ThirdDES, secret)
}
...@@ -2,9 +2,9 @@ package txcrypto ...@@ -2,9 +2,9 @@ package txcrypto
//go:generate mockgen -destination mock_txcrypto/mock_txcrypto.go -package mock_txcrypto -source txcrypto.go //go:generate mockgen -destination mock_txcrypto/mock_txcrypto.go -package mock_txcrypto -source txcrypto.go
type Cryptor interface { type Cryptor interface {
// encrypt can encrypt the content in IBTP // encrypt can encrypt the content in IBTP for mode: dirct、relay
Encrypt(content []byte, address string) ([]byte, error) Encrypt(content []byte, address string, mode string) ([]byte, error)
// decrypt can decrypt the content in IBTP // decrypt can decrypt the content in IBTP
Decrypt(content []byte, address string) ([]byte, error) Decrypt(content []byte, address string, mode string) ([]byte, error)
} }
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"github.com/hashicorp/go-multierror" multierror "github.com/hashicorp/go-multierror"
"github.com/juju/errors" "github.com/juju/errors"
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment