Skip to content

Commit

Permalink
refactor(logic): avoid duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux authored and ccamel committed Oct 17, 2023
1 parent 567b7e8 commit a1963a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
62 changes: 28 additions & 34 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,9 @@ const (
// - ed25519_verify([127, ...], [56, 90, ..], [23, 56, ...], encoding(octet)).
func ED25519Verify(vm *engine.VM, key, data, sig, options engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise {
return engine.Delay(func(ctx context.Context) *engine.Promise {
pubKey, err := TermToBytes(key, AtomEncoding.Apply(engine.NewAtom("octet")), env)
r, err := cryptoVerify(Ed25519, key, data, sig, options, env)
if err != nil {
return engine.Error(fmt.Errorf("ed25519_verify/4: decoding public key: %w", err))
}

msg, err := TermToBytes(data, options, env)
if err != nil {
return engine.Error(fmt.Errorf("ed25519_verify/4: decoding data: %w", err))
}

signature, err := TermToBytes(sig, AtomEncoding.Apply(engine.NewAtom("octet")), env)
if err != nil {
return engine.Error(fmt.Errorf("ed25519_verify/4: decoding signature: %w", err))
}

r, err := verifySignature(Ed25519, pubKey, msg, signature)
if err != nil {
return engine.Error(fmt.Errorf("ed25519_verify/4: failed verify signature: %w", err))
return engine.Error(fmt.Errorf("ed25519_verify/4: %w", err))
}

if !r {
Expand Down Expand Up @@ -176,24 +161,10 @@ func ED25519Verify(vm *engine.VM, key, data, sig, options engine.Term, cont engi
// - ecdsa_verify([127, ...], [56, 90, ..], [23, 56, ...], [encoding(octet), type(secp256k1)]).
func ECDSAVerify(vm *engine.VM, key, data, sig, options engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise {
return engine.Delay(func(ctx context.Context) *engine.Promise {
pubKey, err := TermToBytes(key, AtomEncoding.Apply(engine.NewAtom("octet")), env)
// TODO: Get good algo in options
r, err := cryptoVerify(Secp256r1, key, data, sig, options, env)
if err != nil {
return engine.Error(fmt.Errorf("ecdsa_verify/4: decoding public key: %w", err))
}

msg, err := TermToBytes(data, options, env)
if err != nil {
return engine.Error(fmt.Errorf("ecdsa_verify/4: decoding data: %w", err))
}

signature, err := TermToBytes(sig, AtomEncoding.Apply(engine.NewAtom("octet")), env)
if err != nil {
return engine.Error(fmt.Errorf("ecdsa_verify/4: decoding signature: %w", err))
}

r, err := verifySignature(Secp256r1, pubKey, msg, signature)
if err != nil {
return engine.Error(fmt.Errorf("ecdsa_verify/4: failed verify signature: %w", err))
return engine.Error(fmt.Errorf("ecdsa_verify/4: %w", err))
}

if !r {
Expand All @@ -203,6 +174,29 @@ func ECDSAVerify(vm *engine.VM, key, data, sig, options engine.Term, cont engine
})
}

func cryptoVerify(alg Alg, key, data, sig, options engine.Term, env *engine.Env) (bool, error) {
pubKey, err := TermToBytes(key, AtomEncoding.Apply(engine.NewAtom("octet")), env)
if err != nil {
return false, fmt.Errorf("decoding public key: %w", err)
}

msg, err := TermToBytes(data, options, env)
if err != nil {
return false, fmt.Errorf("decoding data: %w", err)
}

signature, err := TermToBytes(sig, AtomEncoding.Apply(engine.NewAtom("octet")), env)
if err != nil {
return false, fmt.Errorf("decoding signature: %w", err)
}

r, err := verifySignature(alg, pubKey, msg, signature)
if err != nil {
return false, fmt.Errorf("failed verify signature: %w", err)
}
return r, nil
}

func verifySignature(alg Alg, pubKey []byte, msg, sig []byte) (r bool, err error) {
defer func() {
if recoveredErr := recover(); recoveredErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/logic/predicate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func OptionsContains(atom engine.Atom, options engine.Term, env *engine.Env) (en

// TermToBytes try to convert a term to native golang []byte.
// By default, if no encoding options is given the term is considered as hexadecimal value.
// Available encoding option is `text`, `octet` and `hex` (default value)
// Available encoding option is `text`, `octet` and `hex` (default value).
func TermToBytes(term, options engine.Term, env *engine.Env) ([]byte, error) {
encoding, err := OptionsContains(AtomEncoding, options, env)
if err != nil {
Expand Down

0 comments on commit a1963a1

Please sign in to comment.