github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/flip/types.go (about)

     1  package flip
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/hmac"
     6  	"crypto/rand"
     7  	"encoding/hex"
     8  	"fmt"
     9  	"math/big"
    10  	"time"
    11  
    12  	chat1 "github.com/keybase/client/go/protocol/chat1"
    13  )
    14  
    15  func (u UserDevice) Eq(v UserDevice) bool { return u.U.Eq(v.U) && u.D.Eq(v.D) }
    16  func (h Hash) Eq(i Hash) bool             { return hmac.Equal(h[:], i[:]) }
    17  func (c Commitment) String() string       { return hex.EncodeToString(c[:]) }
    18  func (s Secret) String() string           { return hex.EncodeToString(s[:]) }
    19  
    20  func (t Time) Time() time.Time {
    21  	if t == 0 {
    22  		return time.Time{}
    23  	}
    24  	return time.Unix(0, int64(t)*1000000)
    25  }
    26  
    27  func (t Time) Duration() time.Duration {
    28  	return time.Duration(t) * time.Millisecond
    29  }
    30  
    31  func ToTime(t time.Time) Time {
    32  	if t.IsZero() {
    33  		return 0
    34  	}
    35  	return Time(t.UnixNano() / 1000000)
    36  }
    37  
    38  func GenerateGameID() chat1.FlipGameID {
    39  	l := 12
    40  	ret := make([]byte, l)
    41  	n, err := rand.Read(ret)
    42  	if n != l {
    43  		panic("short random read")
    44  	}
    45  	if err != nil {
    46  		panic(fmt.Sprintf("error reading randomness: %s", err.Error()))
    47  	}
    48  	return chat1.FlipGameID(ret)
    49  }
    50  
    51  func (s Start) CommitmentWindowWithSlack(isLeader bool) time.Duration {
    52  	window := s.CommitmentCompleteWindowMsec
    53  	if isLeader {
    54  		window = s.CommitmentWindowMsec
    55  	}
    56  	return Time(window + s.SlackMsec).Duration()
    57  }
    58  
    59  func (s Start) RevealWindowWithSlack() time.Duration {
    60  	return Time(s.CommitmentWindowMsec + s.RevealWindowMsec + 2*s.SlackMsec).Duration()
    61  }
    62  
    63  func (u UserDevice) LessThan(v UserDevice) bool {
    64  	cu := bytes.Compare([]byte(u.U), []byte(v.U))
    65  	du := bytes.Compare([]byte(u.D), []byte(v.D))
    66  	return cu < 0 || (cu == 0 && du < 0)
    67  }
    68  
    69  func (h Hash) String() string {
    70  	return hex.EncodeToString(h[:])
    71  }
    72  
    73  func (m GameMessageBody) String() string {
    74  	t, err := m.T()
    75  	if err != nil {
    76  		return fmt.Sprintf("union error: %s", err.Error())
    77  	}
    78  	switch t {
    79  	case MessageType_START:
    80  		return fmt.Sprintf("START: %+v", m.Start())
    81  	case MessageType_COMMITMENT:
    82  		return fmt.Sprintf("COMMITMENT: %+v", m.Commitment())
    83  	case MessageType_COMMITMENT_COMPLETE:
    84  		return fmt.Sprintf("COMMITMENT COMPLETE: %+v", m.CommitmentComplete())
    85  	case MessageType_REVEAL:
    86  		return "REVEAL"
    87  	case MessageType_END:
    88  		return "END"
    89  	default:
    90  		return fmt.Sprintf("Unknown: %d", t)
    91  	}
    92  }
    93  
    94  func (p FlipParameters) String() string {
    95  	t, err := p.T()
    96  	if err != nil {
    97  		return fmt.Sprintf("union error: %s", err.Error())
    98  	}
    99  	switch t {
   100  	case FlipType_BOOL:
   101  		return "bool"
   102  	case FlipType_INT:
   103  		return fmt.Sprintf("int(%d)", p.Int())
   104  	case FlipType_BIG:
   105  		var n big.Int
   106  		n.SetBytes(p.Big())
   107  		return fmt.Sprintf("big(%s)", n.String())
   108  	case FlipType_SHUFFLE:
   109  		return fmt.Sprintf("shuffle(%d)", p.Shuffle())
   110  	default:
   111  		return fmt.Sprintf("Unknown: %d", t)
   112  	}
   113  }