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)
clients := plugins.CreateClients(config.Appchains, nil)
persister := manger.NewPersister(addr.String(), store, loggers.Logger(loggers.Manger))
appchainMgr := appchainmgr.New(persister)
cryptor, err := txcrypto.NewDirectCryptor(appchainMgr, privateKey)
tool.Asset(err)
// TODO hub client
cryptor := txcrypto.NewCryptor(nil, appchainMgr, privateKey)
clientPort := appchain.NewPorts(clients, cryptor, logger)
r.Adds(clientPort)
mg, err := manger.NewManager(addr.String(), pm, appchainMgr, loggers.Logger(loggers.Manger))
......
......@@ -12,6 +12,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/link33/sidecar/internal/port"
"github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/internal/txcrypto"
"github.com/link33/sidecar/model/pb"
"github.com/link33/sidecar/pkg/plugins"
......@@ -150,7 +151,8 @@ func (a *appChain) applyReceiptIBTP(ibtp *pb.IBTP) error {
var err error
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 {
return fmt.Errorf("decrypt ibtp payload content: %w", err)
}
......@@ -340,7 +342,7 @@ func (a *appChain) encryption(ibtp *pb.IBTP) error {
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 {
return err
}
......
......@@ -8,46 +8,68 @@ import (
"github.com/meshplus/bitxhub-kit/crypto"
"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/internal/repo"
"github.com/link33/sidecar/model/constant"
)
type DirectCryptor struct {
type cryptor struct {
client rpcx.Client
mgr appchainmgr.AppchainMgr
privKey crypto.PrivateKey
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)
return &DirectCryptor{
return &cryptor{
client: client,
mgr: mgr,
privKey: privKey,
keyMap: keyMap,
}, nil
}
}
func (d *DirectCryptor) Encrypt(content []byte, address string) ([]byte, error) {
des, err := d.getDesKey(address)
func (d *cryptor) Encrypt(content []byte, address string, mode string) ([]byte, error) {
des, err := d.getDesKey(address, mode)
if err != nil {
return nil, err
}
return des.Encrypt(content)
}
func (d *DirectCryptor) Decrypt(content []byte, address string) ([]byte, error) {
des, err := d.getDesKey(address)
func (d *cryptor) Decrypt(content []byte, address string, mode string) ([]byte, error) {
des, err := d.getDesKey(address, mode)
if err != nil {
return nil, err
}
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]
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 {
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
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
//go:generate mockgen -destination mock_txcrypto/mock_txcrypto.go -package mock_txcrypto -source txcrypto.go
type Cryptor interface {
// encrypt can encrypt the content in IBTP
Encrypt(content []byte, address string) ([]byte, error)
// encrypt can encrypt the content in IBTP for mode: dirct、relay
Encrypt(content []byte, address string, mode string) ([]byte, error)
// 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 (
"fmt"
"runtime"
"github.com/hashicorp/go-multierror"
multierror "github.com/hashicorp/go-multierror"
"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