Skip to content

Commit 98f0818

Browse files
committed
update decrypter interface
1 parent 18feb5d commit 98f0818

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

pkg/loop/internal/relayer/test/relayer.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,8 @@ type fuzzerKeystore struct {
631631
acctErr bool
632632
signed []byte
633633
signErr bool
634+
decrypted []byte
635+
decryptErr bool
634636
valuesWithErr bool
635637
errStr string
636638
}
@@ -664,3 +666,21 @@ func (k fuzzerKeystore) Sign(ctx context.Context, account string, data []byte) (
664666

665667
return k.signed, nil
666668
}
669+
670+
func (k fuzzerKeystore) Decrypt(ctx context.Context, account string, encrypted []byte) ([]byte, error) {
671+
if k.decryptErr {
672+
err := errors.New(k.errStr)
673+
674+
if k.valuesWithErr {
675+
return k.decrypted, err
676+
}
677+
678+
return nil, err
679+
}
680+
681+
if len(k.decrypted) == 0 {
682+
return nil, fmt.Errorf("no decrypted data for account %s", account)
683+
}
684+
685+
return k.decrypted, nil
686+
}

pkg/types/core/keystore.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,21 @@ func (s *Ed25519Signer) Sign(r io.Reader, digest []byte, opts crypto.SignerOpts)
7676
var P2PAccountKey = "P2P_SIGNER"
7777
var WorkflowAccountKey = "WORKFLOW_DECRYPTER"
7878

79+
type Decrypter interface {
80+
Decrypt(encrypted []byte) (decrypted []byte, err error)
81+
}
82+
7983
// signerDecrypter implements Keystore for a single sign account and decrypt account.
8084
type signerDecrypter struct {
8185
signAccount *string
8286
signer crypto.Signer
8387
decryptAccount *string
84-
decrypter crypto.Decrypter
88+
decrypter Decrypter
8589
}
8690

8791
var _ Keystore = &signerDecrypter{}
8892

89-
func NewSignerDecrypter(signAccount *string, signer crypto.Signer, decryptAccount *string, decrypter crypto.Decrypter) (*signerDecrypter, error) {
93+
func NewSignerDecrypter(signAccount *string, signer crypto.Signer, decryptAccount *string, decrypter Decrypter) (*signerDecrypter, error) {
9094
return &signerDecrypter{signAccount: signAccount, signer: signer, decryptAccount: decryptAccount, decrypter: decrypter}, nil
9195
}
9296

@@ -113,7 +117,7 @@ func (c *signerDecrypter) Sign(ctx context.Context, account string, data []byte)
113117

114118
func (c *signerDecrypter) Decrypt(ctx context.Context, account string, encrypted []byte) (decrypted []byte, err error) {
115119
if c.decryptAccount != nil && *c.decryptAccount == account {
116-
return c.decrypter.Decrypt(rand.Reader, encrypted, nil)
120+
return c.decrypter.Decrypt(encrypted)
117121
}
118122
return nil, fmt.Errorf("account not found: %s", account)
119123
}

pkg/types/core/keystore_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (m *mockDecrypter) Public() crypto.PublicKey {
4545
return m.publicKey
4646
}
4747

48-
func (m *mockDecrypter) Decrypt(rand io.Reader, msg []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) {
48+
func (m *mockDecrypter) Decrypt(msg []byte) (plaintext []byte, err error) {
4949
if m.decryptError != nil {
5050
return nil, m.decryptError
5151
}
@@ -298,14 +298,14 @@ type boxDecrypter struct {
298298
publicKey *[32]byte
299299
}
300300

301-
var _ crypto.Decrypter = (*boxDecrypter)(nil)
301+
var _ core.Decrypter = (*boxDecrypter)(nil)
302302

303303
func (b *boxDecrypter) Public() crypto.PublicKey {
304304
pubKeyBytes := b.publicKey[:]
305305
return crypto.PublicKey(pubKeyBytes)
306306
}
307307

308-
func (b *boxDecrypter) Decrypt(_ io.Reader, ciphertext []byte, _ crypto.DecrypterOpts) ([]byte, error) {
308+
func (b *boxDecrypter) Decrypt(ciphertext []byte) ([]byte, error) {
309309
msg, ok := box.OpenAnonymous(nil, ciphertext, b.publicKey, b.privateKey)
310310
if !ok {
311311
return nil, fmt.Errorf("decryption failed")

0 commit comments

Comments
 (0)