github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/mpcService/protocol/protocol.go (about)

     1  package protocol
     2  
     3  import (
     4  	"bytes"
     5  	"time"
     6  	"strconv"
     7  	"github.com/ethereum/go-ethereum/p2p/discover"
     8  	"github.com/ethereum/go-ethereum/common"
     9  	"github.com/ethereum/go-ethereum/p2p"
    10  	"io"
    11  	"encoding/binary"
    12  	"github.com/ethereum/go-ethereum/rlp"
    13  	"math/big"
    14  )
    15  
    16  const (
    17  	MpcCreateLockAccountLeader = iota + 0
    18  	MpcCreateLockAccountPeer
    19  	MpcTXSignLeader
    20  	MpcTXSignPeer
    21  	MpcReady
    22  )
    23  const (
    24  	StatusCode = iota + 0 // used by storeman protocol
    25  	KeepaliveCode
    26  	KeepaliveOkCode
    27  	MSG_MPCError
    28  	MSG_RequestPrepare
    29  	MSG_RequestMPC // ask for a new mpc Context
    30  	MSG_MPCMessage // get a message for a Context
    31  	MSG_MPCFinish
    32  
    33  	KeepaliveCycle
    34  	NumberOfMessageCodes
    35  
    36  	MPCDegree          = 2
    37  	//MPCDegree          = 1
    38  	MPCTimeOut         = time.Second * 30
    39  	ProtocolName       = "storeman"
    40  	ProtocolVersion    = uint64(1)
    41  	ProtocolVersionStr = "1.0"
    42  )
    43  const (
    44  	MpcSeed  		 = "Seed"
    45  	MpcPrivateShare  = "PrivateShare"
    46  	MpcPrivateKey    = "PrivateKey"
    47  	MpcPublicShare   = "PublicShare"
    48  	MpcPointPart	 = "PointPart"
    49  	MpcSignASeed     = "SignASeed"
    50  	MpcSignA         = "SignA"
    51  	MpcSignA0        = "SignA0"
    52  	MpcSignRSeed     = "SignRSeed"
    53  	MpcSignR         = "SignR"
    54  	MpcSignR0        = "SignR0"
    55  	MpcSignB         = "SignB"
    56  	MpcSignBSeed     = "SignBSeed"
    57  	MpcSignC         = "SignC"
    58  	MpcSignCSeed     = "SignCSeed"
    59  	MpcSignARSeed    = "SignARSeed"
    60  	MpcSignARResult  = "SignARResult"
    61  	MpcTxSignSeed    = "TxSignSeed"
    62  	MpcTxSignResultR = "TxSignResultR"
    63  	MpcTxSignResultV = "TxSignResultV"
    64  	MpcTxSignResult  = "TxSignResult"
    65  	MpcContextResult = "ContextResult"
    66  
    67  	PublicKeyResult = "PublicKeyResult"
    68  	MpcSignAPoint   = "SignAPoint"
    69  	MpcTxHash       = "TxHash"
    70  	MpcLagrange     = "Lagrange"
    71  	MpcTransaction  = "Transaction"
    72  	MpcChainType    = "ChainType"
    73  	MpcSignType     = "SignType"
    74  	MpcChainID      = "ChainID"
    75  	MpcAddress      = "Address"
    76  	MPCAction       = "Action"
    77  	MPCPolyvalue   = "Polyvalue"
    78  	MPCSignedFrom   = "SignedFrom"
    79  	MpcStmAccType   = "StmAccType"
    80  )
    81  type PeerMessage struct {
    82  	From discover.NodeID
    83  	Message *p2p.Msg
    84  }
    85  type PeerInfo struct {
    86  	PeerID *discover.NodeID
    87  	Seed   uint64
    88  }
    89  type SlicePeers []*discover.NodeID
    90  
    91  func (s SlicePeers) Len() int {
    92  	return len(s)
    93  }
    94  func (s SlicePeers) Swap(i, j int) {
    95  	s[i], s[j] = s[j], s[i]
    96  }
    97  func (s SlicePeers) Less(i, j int) bool {
    98  	return bytes.Compare(s[i][:], s[j][:]) < 0
    99  }
   100  type MpcStepFunc interface {
   101  	GetMessageInterface
   102  	InitMessageLoop(GetMessageInterface) error
   103  	Quit(error)
   104  	InitStep() error
   105  	CreateMessage() []StepMessage
   106  	FinishStep(MpcManager) error
   107  	GetMessageChan() chan *StepMessage
   108  }
   109  type GetMessageInterface interface {
   110  	HandleMessage(*StepMessage) bool
   111  }
   112  type MpcData struct{
   113  	Key string
   114  	Data interface{}
   115  }
   116  type MpcDataTemp struct{
   117  	Key string
   118  	Data interface{}
   119  }
   120  func (md *MpcData) EncodeRLP(w io.Writer) error{
   121  	temp := MpcDataTemp{md.Key,md.Data}
   122  	switch md.Data.(type){
   123  	case uint64:
   124  		buff := make([]byte,8)
   125  		binary.BigEndian.PutUint64(buff, md.Data.(uint64))
   126  		temp.Data = buff
   127  	case uint32:
   128  		buff := make([]byte,4)
   129  		binary.BigEndian.PutUint32(buff, md.Data.(uint32))
   130  		temp.Data = buff
   131  	case uint16:
   132  		buff := make([]byte,2)
   133  		binary.BigEndian.PutUint16(buff, md.Data.(uint16))
   134  		temp.Data = buff
   135  	case uint:
   136  		buff := make([]byte,32)
   137  		binary.BigEndian.PutUint32(buff, uint32(md.Data.(uint)))
   138  		temp.Data = buff
   139  	}
   140  	return rlp.Encode(w,temp)
   141  }
   142  func (md *MpcData)DecodeRLP(s *rlp.Stream) error{
   143  	temp := &MpcDataTemp{}
   144  	if err := s.Decode(temp); err != nil {
   145  		return err
   146  	}
   147  	md.Key = temp.Key
   148  	md.Data = temp.Data
   149  	switch md.Key {
   150  	case MpcSeed:
   151  		md.Data = binary.BigEndian.Uint64(md.Data.([]byte))
   152  		//big int
   153  	case MPCPolyvalue,MpcSignA,MpcSignA0,MpcSignR,MpcSignARSeed,MpcSignB,MpcPublicShare,MpcSignC,
   154  		MpcSignASeed,MpcSignRSeed,MpcSignBSeed,MpcSignCSeed,MpcLagrange:
   155  		data := new(big.Int).SetBytes(md.Data.([]byte))
   156  		md.Data = data
   157  	case MpcPointPart:
   158  		value := md.Data.([]interface{})
   159  		var data [2]*big.Int
   160  		data[0] = big.NewInt(0)
   161  		data[0].SetBytes(value[0].([]byte))
   162  		data[1] = big.NewInt(0)
   163  		data[1].SetBytes(value[1].([]byte))
   164  		md.Data = data
   165  	}
   166  	return nil
   167  }
   168  type StepMessage struct {
   169  	Msgcode   uint64 //message code
   170  	PeerID    *discover.NodeID
   171  	Peers     []PeerInfo
   172  	Data 	  []*MpcData
   173  }
   174  
   175  type MpcMessage struct {
   176  	ContextID common.Hash
   177  	StepID    uint64
   178  	Peers     []byte
   179  	Data 	  []*MpcData
   180  	Error     string
   181  }
   182  
   183  func CheckAccountType(accType string) bool {
   184  	if accType == "WAN" || accType == "ETH" || accType == "BTC" {
   185  		return true
   186  	}
   187  
   188  	return false
   189  }
   190  
   191  func GetPreSetKeyArr(keySeed string, num int) []string {
   192  	keyArr := []string{}
   193  	for i := 0; i < num; i++ {
   194  		keyArr = append(keyArr, keySeed + "_" + strconv.Itoa(i))
   195  	}
   196  
   197  	return keyArr
   198  }