Skip to content

Commit

Permalink
server/eth: Implement ValidateSecret.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins authored and chappjc committed Oct 18, 2021
1 parent 08a72d1 commit 9e1e0cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
9 changes: 6 additions & 3 deletions server/asset/eth/eth.go
Expand Up @@ -7,7 +7,9 @@
package eth

import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -265,9 +267,10 @@ func (eth *Backend) Contract(coinID, _ []byte) (*asset.Contract, error) {
}, nil
}

// ValidateSecret checks that the secret satisfies the contract.
func (eth *Backend) ValidateSecret(secret, contract []byte) bool {
return false
// ValidateSecret checks that the secret satisfies the secret hash.
func (eth *Backend) ValidateSecret(secret, secretHash []byte) bool {
sh := sha256.Sum256(secret)
return bytes.Equal(sh[:], secretHash)
}

// Synced is true if the blockchain is ready for action.
Expand Down
28 changes: 28 additions & 0 deletions server/asset/eth/eth_test.go
Expand Up @@ -8,6 +8,7 @@ package eth
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
Expand Down Expand Up @@ -527,3 +528,30 @@ func TestContract(t *testing.T) {
}
}
}

func TestValidateSecret(t *testing.T) {
secret, blankHash := make([]byte, 32), make([]byte, 32)
copy(secret[:], encode.RandomBytes(32))
secretHash := sha256.Sum256(secret[:])
tests := []struct {
name string
secretHash []byte
want bool
}{{
name: "ok",
secretHash: secretHash[:],
want: true,
}, {
name: "not the right hash",
secretHash: blankHash,
}}
for _, test := range tests {
eth := &Backend{
log: tLogger,
}
got := eth.ValidateSecret(secret, test.secretHash)
if test.want != got {
t.Fatalf("expected %v but got %v for test %q", test.want, got, test.name)
}
}
}

0 comments on commit 9e1e0cb

Please sign in to comment.