Skip to content

Commit

Permalink
Ops tests
Browse files Browse the repository at this point in the history
  • Loading branch information
e-asphyx committed Dec 21, 2021
1 parent e77b387 commit 50be77d
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 182 deletions.
3 changes: 2 additions & 1 deletion cmd/signatory-tools/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ecadlabs/signatory/pkg/cryptoutils"
"github.com/ecadlabs/signatory/pkg/signatory"
"github.com/ecadlabs/signatory/pkg/tezos"
"github.com/ecadlabs/signatory/pkg/tezos/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ func NewAuthRequestCommand() *cobra.Command {
return err
}

digest := tezos.DigestFunc(data)
digest := utils.DigestFunc(data)
sig, err := cryptoutils.Sign(priv, digest[:])
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ecadlabs/signatory/pkg/server/auth"
"github.com/ecadlabs/signatory/pkg/signatory"
"github.com/ecadlabs/signatory/pkg/tezos"
"github.com/ecadlabs/signatory/pkg/tezos/utils"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -50,7 +51,7 @@ func (s *Server) authenticateSignRequest(req *signatory.SignRequest, r *http.Req
if err != nil {
return errors.Wrap(err, http.StatusBadRequest)
}
digest := tezos.DigestFunc(signed)
digest := utils.DigestFunc(signed)

sig, err := tezos.ParseSignature(v, nil)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/signatory/signatory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ecadlabs/signatory/pkg/cryptoutils"
"github.com/ecadlabs/signatory/pkg/errors"
"github.com/ecadlabs/signatory/pkg/tezos"
"github.com/ecadlabs/signatory/pkg/tezos/utils"
"github.com/ecadlabs/signatory/pkg/vault"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -250,7 +251,7 @@ func (s *Signatory) Sign(ctx context.Context, req *SignRequest) (string, error)
l.WithField("raw", hex.EncodeToString(req.Message)).Log(level, "About to sign raw bytes")

signFunc := func(ctx context.Context, message []byte, key vault.StoredKey) (cryptoutils.Signature, error) {
digest := tezos.DigestFunc(message)
digest := utils.DigestFunc(message)
if err = s.config.Watermark.IsSafeToSign(req.PublicKeyHash, digest[:], msg); err != nil {
err = errors.Wrap(err, http.StatusForbidden)
l.Error(err)
Expand Down
12 changes: 8 additions & 4 deletions pkg/tezos/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ var (
pOperationMetadataListListHash = tzPrefix{plen: 2, mlen: 32, p: [5]byte{29, 159, 182}} // LLr(53)

// 20
pED25519PublicKeyHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{6, 161, 159}} // tz1(36)
pSECP256K1PublicKeyHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{6, 161, 161}} // tz2(36)
pP256PublicKeyHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{6, 161, 164}} // tz3(36)
pContractHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{2, 90, 121}} // KT1(36)
pED25519PublicKeyHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{6, 161, 159}} // tz1(36)
pSECP256K1PublicKeyHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{6, 161, 161}} // tz2(36)
pP256PublicKeyHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{6, 161, 164}} // tz3(36)
pContractHash = tzPrefix{plen: 3, mlen: 20, p: [5]byte{2, 90, 121}} // KT1(36)
pBlindedPublicKeyHash = tzPrefix{plen: 4, mlen: 20, p: [5]byte{1, 2, 49, 223}} // btz1(37)

// 16
pCryptoboxPublicKeyHash = tzPrefix{plen: 2, mlen: 16, p: [5]byte{153, 103}} // id(30)
Expand All @@ -47,6 +48,9 @@ var (
pED25519PublicKey = tzPrefix{plen: 4, mlen: 32, p: [5]byte{13, 15, 37, 217}} // edpk(54)
pSECP256K1SecretKey = tzPrefix{plen: 4, mlen: 32, p: [5]byte{17, 162, 224, 201}} // spsk(54)
pP256SecretKey = tzPrefix{plen: 4, mlen: 32, p: [5]byte{16, 81, 238, 189}} // p2sk(54)
pValueHash = tzPrefix{plen: 3, mlen: 32, p: [5]byte{1, 106, 242}} // vh(52)
pCycleNonce = tzPrefix{plen: 3, mlen: 32, p: [5]byte{69, 220, 169}} // nce(53)
pScriptExpr = tzPrefix{plen: 4, mlen: 32, p: [5]byte{13, 44, 64, 27}} // expr(54)

// 56
pED25519EncryptedSeed = tzPrefix{plen: 5, mlen: 56, p: [5]byte{7, 90, 60, 179, 41}} // edesk(88)
Expand Down
69 changes: 38 additions & 31 deletions pkg/tezos/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tezos
import (
"fmt"
"time"

"github.com/ecadlabs/signatory/pkg/tezos/utils"
)

// UnsignedMessage is implemented by all kinds of sign request payloads
Expand Down Expand Up @@ -41,7 +43,7 @@ func (u *GenericOperationRequest) OperationKinds() []string {
func (u *GenericOperationRequest) MessageKind() string { return "generic" }

func parseGenericOperationRequest(buf *[]byte) (*GenericOperationRequest, error) {
blockHash, err := getBytes(buf, 32)
blockHash, err := utils.GetBytes(buf, 32)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -78,49 +80,49 @@ func parseShellBlockHeader(buf *[]byte) (*ShellBlockHeader, error) {
b ShellBlockHeader
err error
)
if b.Level, err = getInt32(buf); err != nil {
if b.Level, err = utils.GetInt32(buf); err != nil {
return nil, err
}
if b.Proto, err = getByte(buf); err != nil {
if b.Proto, err = utils.GetByte(buf); err != nil {
return nil, err
}
hash, err := getBytes(buf, 32)
hash, err := utils.GetBytes(buf, 32)
if err != nil {
return nil, err
}
b.Predecessor = encodeBase58(pBlockHash, hash)
ts, err := getInt64(buf)
ts, err := utils.GetInt64(buf)
if err != nil {
return nil, err
}
b.Timestamp = time.Unix(ts, 0).UTC()
if b.ValidationPass, err = getByte(buf); err != nil {
if b.ValidationPass, err = utils.GetByte(buf); err != nil {
return nil, err
}
if hash, err = getBytes(buf, 32); err != nil {
if hash, err = utils.GetBytes(buf, 32); err != nil {
return nil, err
}
b.OperationsHash = encodeBase58(pOperationListListHash, hash)
ln, err := getUint32(buf)
ln, err := utils.GetUint32(buf)
if err != nil {
return nil, err
}
fbuf, err := getBytes(buf, int(ln))
fbuf, err := utils.GetBytes(buf, int(ln))
if err != nil {
return nil, err
}
for len(fbuf) != 0 {
ln, err := getUint32(&fbuf)
ln, err := utils.GetUint32(&fbuf)
if err != nil {
return nil, err
}
elem, err := getBytes(&fbuf, int(ln))
elem, err := utils.GetBytes(&fbuf, int(ln))
if err != nil {
return nil, err
}
b.Fitness = append(b.Fitness, elem)
}
if hash, err = getBytes(buf, 32); err != nil {
if hash, err = utils.GetBytes(buf, 32); err != nil {
return nil, err
}
b.Context = encodeBase58(pContextHash, hash)
Expand All @@ -130,12 +132,11 @@ func parseShellBlockHeader(buf *[]byte) (*ShellBlockHeader, error) {
// BlockHeader represents unsigned block header
type BlockHeader struct {
ShellBlockHeader
PayloadHash []byte
PayloadHash string
PayloadRound int32
ProofOfWorkNonce []byte
SeedNonceHash []byte
SeedNonceHash string
LiquidityBakingEscapeVote bool
Signature string
}

func (b *BlockHeader) GetRound() int32 {
Expand All @@ -150,43 +151,49 @@ func parseUnsignedBlockHeader(buf *[]byte) (*BlockHeader, error) {
b := BlockHeader{
ShellBlockHeader: *sh,
}
if b.PayloadHash, err = getBytes(buf, 32); err != nil {
hash, err := utils.GetBytes(buf, 32)
if err != nil {
return nil, err
}
if b.PayloadRound, err = getInt32(buf); err != nil {
b.PayloadHash = encodeBase58(pValueHash, hash)
if b.PayloadRound, err = utils.GetInt32(buf); err != nil {
return nil, err
}
if b.ProofOfWorkNonce, err = getBytes(buf, 8); err != nil {
if b.ProofOfWorkNonce, err = utils.GetBytes(buf, 8); err != nil {
return nil, err
}
flag, err := getBool(buf)
flag, err := utils.GetBool(buf)
if err != nil {
return nil, err
}
if flag {
if b.SeedNonceHash, err = getBytes(buf, 32); err != nil {
hash, err := utils.GetBytes(buf, 32)
if err != nil {
return nil, err
}
b.SeedNonceHash = encodeBase58(pCycleNonce, hash)
}
b.LiquidityBakingEscapeVote, err = getBool(buf)
b.LiquidityBakingEscapeVote, err = utils.GetBool(buf)
if err != nil {
return nil, err
}
return &b, nil
}

/*
func parseBlockHeader(buf *[]byte) (b *BlockHeader, err error) {
b, err = parseUnsignedBlockHeader(buf)
if err != nil {
return nil, err
}
s, err := getBytes(buf, 64)
s, err := utils.GetBytes(buf, 64)
if err != nil {
return nil, err
}
b.Signature = encodeBase58(pGenericSignature, s)
return b, nil
}
*/

// TenderbakeBlockRequest represents unsigned block header
type TenderbakeBlockRequest struct {
Expand Down Expand Up @@ -234,11 +241,11 @@ func (t *TenderbakeEndorsementRequest) GetChainID() string { return t.ChainID }
func (t *TenderbakeEndorsementRequest) MessageKind() string { return "endorsement" }

func parseEmmyEndorsementRequest(buf *[]byte) (*EmmyEndorsementRequest, error) {
chainID, err := getBytes(buf, 4)
chainID, err := utils.GetBytes(buf, 4)
if err != nil {
return nil, err
}
blockHash, err := getBytes(buf, 32)
blockHash, err := utils.GetBytes(buf, 32)
if err != nil {
return nil, err
}
Expand All @@ -258,11 +265,11 @@ func parseEmmyEndorsementRequest(buf *[]byte) (*EmmyEndorsementRequest, error) {
}

func parseTenderbakeEndorsementRequest(buf *[]byte) (*TenderbakeEndorsementRequest, error) {
chainID, err := getBytes(buf, 4)
chainID, err := utils.GetBytes(buf, 4)
if err != nil {
return nil, err
}
blockHash, err := getBytes(buf, 32)
blockHash, err := utils.GetBytes(buf, 32)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -295,11 +302,11 @@ func (u *PreendorsementRequest) MessageKind() string { return "preendorsement" }
func (u *PreendorsementRequest) GetChainID() string { return u.ChainID }

func parsePreendorsementRequest(buf *[]byte) (*PreendorsementRequest, error) {
chainID, err := getBytes(buf, 4)
chainID, err := utils.GetBytes(buf, 4)
if err != nil {
return nil, err
}
blockHash, err := getBytes(buf, 32)
blockHash, err := utils.GetBytes(buf, 32)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -328,14 +335,14 @@ const (
)

func parseRequest(buf *[]byte) (u UnsignedMessage, err error) {
t, err := getByte(buf)
t, err := utils.GetByte(buf)
if err != nil {
return nil, err
}

switch t {
case magicEmmyBlock:
b, err := getBytes(buf, 4)
b, err := utils.GetBytes(buf, 4)
if err != nil {
return nil, err
}
Expand All @@ -349,7 +356,7 @@ func parseRequest(buf *[]byte) (u UnsignedMessage, err error) {
}, nil

case magicTenderbakeBlock:
b, err := getBytes(buf, 4)
b, err := utils.GetBytes(buf, 4)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 50be77d

Please sign in to comment.