// Returns the LedgerMR for this block. func (b *FBlock) GetLedgerMR() interfaces.IHash { b.EndOfPeriod(0) // Clean up end of minute markers, if needed. hashes := make([]interfaces.IHash, 0, len(b.Transactions)) marker := 0 for i, trans := range b.Transactions { for marker < len(b.endOfPeriod) && i != 0 && i == b.endOfPeriod[marker] { marker++ hashes = append(hashes, primitives.Sha(constants.ZERO)) } data, err := trans.MarshalBinarySig() hash := primitives.Sha(data) if err != nil { panic("Failed to get LedgerMR: " + err.Error()) } hashes = append(hashes, hash) } // Add any lagging markers for marker < len(b.endOfPeriod) { marker++ hashes = append(hashes, primitives.Sha(constants.ZERO)) } lmr := primitives.ComputeMerkleRoot(hashes) return lmr }
func newChangeServerKey() *ChangeServerKeyMsg { addserv := new(ChangeServerKeyMsg) addserv.Timestamp = primitives.NewTimestampNow() addserv.IdentityChainID = primitives.Sha([]byte("FNode0")) addserv.AdminBlockChange = 0 addserv.KeyPriority = 0 addserv.KeyType = 0 addserv.Key = primitives.Sha([]byte("A_Key")) return addserv }
// KeyMR returns the hash of the hash of the Entry Block Header concatinated // with the Merkle Root of the Entry Block Body. The Body Merkle Root is // calculated by the func (e *EBlockBody) MR() which is called by the func // (e *EBlock) BuildHeader(). func (e *EBlock) KeyMR() (interfaces.IHash, error) { // Sha(Sha(header) + BodyMR) e.BuildHeader() header, err := e.Header.MarshalBinary() if err != nil { return nil, err } h := primitives.Sha(header) return primitives.Sha(append(h.Bytes(), e.Header.GetBodyMR().Bytes()...)), nil }
func (b *FBlock) GetLedgerKeyMR() interfaces.IHash { ledgerMR := b.GetLedgerMR() data, err := b.MarshalHeader() if err != nil { panic("Failed to create FullHash: " + err.Error()) } headerHash := primitives.Sha(data) cat := append(ledgerMR.Bytes(), headerHash.Bytes()...) lkmr := primitives.Sha(cat) return lkmr }
// Calculates the Key Merkle Root for this block and returns it. func (b *FBlock) GetKeyMR() interfaces.IHash { bodyMR := b.GetBodyMR() data, err := b.MarshalHeader() if err != nil { panic("Failed to create KeyMR: " + err.Error()) } headerHash := primitives.Sha(data) cat := append(headerHash.Bytes(), bodyMR.Bytes()...) kmr := primitives.Sha(cat) return kmr }
func (b *DirectoryBlock) BuildBodyMR() (mr interfaces.IHash, err error) { hashes := make([]interfaces.IHash, len(b.GetDBEntries())) for i, entry := range b.GetDBEntries() { data, _ := entry.MarshalBinary() hashes[i] = primitives.Sha(data) } if len(hashes) == 0 { hashes = append(hashes, primitives.Sha(nil)) } merkle := primitives.BuildMerkleTreeStore(hashes) return merkle[len(merkle)-1], nil }
func (e *CommitChain) Hash() interfaces.IHash { bin, err := e.MarshalBinary() if err != nil { panic(err) } return primitives.Sha(bin) }
func GenerateGenesisBlocks(networkID uint32) (interfaces.IDirectoryBlock, interfaces.IAdminBlock, interfaces.IFBlock, interfaces.IEntryCreditBlock) { dblk := directoryBlock.NewDirectoryBlock(nil) ablk := adminBlock.NewAdminBlock(nil) fblk := factoid.GetGenesisFBlock(networkID) ecblk := entryCreditBlock.NewECBlock() if networkID != constants.MAIN_NETWORK_ID { if networkID == constants.TEST_NETWORK_ID { ablk.AddFedServer(primitives.NewZeroHash()) } else { ablk.AddFedServer(primitives.Sha([]byte("FNode0"))) } } else { ecblk.GetBody().AddEntry(entryCreditBlock.NewServerIndexNumber()) for i := 1; i < 11; i++ { minute := entryCreditBlock.NewMinuteNumber(uint8(i)) ecblk.GetBody().AddEntry(minute) } } dblk.SetABlockHash(ablk) dblk.SetECBlockHash(ecblk) dblk.SetFBlockHash(fblk) dblk.GetHeader().SetNetworkID(networkID) dblk.GetHeader().SetTimestamp(primitives.NewTimestampFromMinutes(24018960)) return dblk, ablk, fblk, ecblk }
func CreateTestDirectoryBlockHeader(prevBlock *directoryBlock.DirectoryBlock) *directoryBlock.DBlockHeader { header := new(directoryBlock.DBlockHeader) header.SetBodyMR(primitives.Sha(primitives.NewZeroHash().Bytes())) header.SetBlockCount(0) header.SetNetworkID(constants.MAIN_NETWORK_ID) if prevBlock == nil { header.SetDBHeight(0) header.SetPrevFullHash(primitives.NewZeroHash()) header.SetPrevKeyMR(primitives.NewZeroHash()) header.SetTimestamp(primitives.NewTimestampFromMinutes(1234)) } else { header.SetDBHeight(prevBlock.Header.GetDBHeight() + 1) header.SetPrevFullHash(prevBlock.GetHash()) keyMR, err := prevBlock.BuildKeyMerkleRoot() if err != nil { panic(err) } header.SetPrevKeyMR(keyMR) header.SetTimestamp(primitives.NewTimestampFromMinutes(prevBlock.Header.GetTimestamp().GetTimeMinutesUInt32() + 1)) } header.SetVersion(1) return header }
func (t Transaction) GetSigHash() interfaces.IHash { m, err := t.MarshalBinarySig() if err != nil { return nil } return primitives.Sha(m) }
func (c *CommitChain) GetSigHash() interfaces.IHash { if c.SigHash == nil { data := c.CommitMsg() c.SigHash = primitives.Sha(data) } return c.SigHash }
func Test_Factoid_Addresses(test *testing.T) { addr := NewAddress(primitives.Sha([]byte("A fake address")).Bytes()) uaddr := primitives.ConvertFctAddressToUserStr(addr) if !primitives.ValidateFUserStr(uaddr) { test.Fail() } addrBack := primitives.ConvertUserStrToAddress(uaddr) if bytes.Compare(addrBack, addr.Bytes()) != 0 { test.Fail() } buaddr := []byte(uaddr) for i, v := range buaddr { for j := uint(0); j < 8; j++ { if !primitives.ValidateFUserStr(string(buaddr)) { test.Fail() } buaddr[i] = v ^ (01 << j) if primitives.ValidateFUserStr(string(buaddr)) { test.Fail() } buaddr[i] = v } } }
func (e *RevealMatryoshkaHash) Hash() interfaces.IHash { bin, err := e.MarshalBinary() if err != nil { panic(err) } return primitives.Sha(bin) }
func (fc *FaultCore) GetHash() interfaces.IHash { data, err := fc.MarshalCore() if err != nil { return nil } return primitives.Sha(data) }
func (m *ServerFault) GetCoreHash() interfaces.IHash { data, err := m.MarshalForSignature() if err != nil { return nil } return primitives.Sha(data) }
func newAddServer() *AddServerMsg { addserv := new(AddServerMsg) addserv.Timestamp = primitives.NewTimestampNow() addserv.ServerChainID = primitives.Sha([]byte("FNode0")) addserv.ServerType = 0 return addserv }
func TestNewED25519Signature(t *testing.T) { testData := primitives.Sha([]byte("sig first half one")).Bytes() priv := testHelper.NewPrivKey(0) sig := NewED25519Signature(priv, testData) pub := testHelper.PrivateKeyToEDPub(priv) pub2 := [32]byte{} copy(pub2[:], pub) s := sig.Signature valid := ed25519.VerifyCanonical(&pub2, testData, &s) if valid == false { t.Errorf("Signature is invalid - %v", valid) } priv2 := [64]byte{} copy(priv2[:], append(priv, pub...)[:]) sig2 := ed25519.Sign(&priv2, testData) valid = ed25519.VerifyCanonical(&pub2, testData, sig2) if valid == false { t.Errorf("Test signature is invalid - %v", valid) } }
func (e *AddFederatedServer) Hash() interfaces.IHash { bin, err := e.MarshalBinary() if err != nil { panic(err) } return primitives.Sha(bin) }
func NewFullServerFault(Previous *FullServerFault, faultMessage *ServerFault, sigList []interfaces.IFullSignature, sysHeight int) *FullServerFault { sf := new(FullServerFault) sf.Timestamp = faultMessage.Timestamp sf.VMIndex = faultMessage.VMIndex sf.DBHeight = faultMessage.DBHeight sf.Height = faultMessage.Height sf.ServerID = faultMessage.ServerID sf.AuditServerID = faultMessage.AuditServerID sf.SystemHeight = uint32(sysHeight) if Previous != nil { sf.SSerialHash = Previous.GetSerialHash() } else { core, err := sf.MarshalCore() if err != nil { panic("Failed to Marshal Core of a Full Server Fault") } sf.SSerialHash = primitives.Sha(core) } numSigs := len(sigList) var allSigs []interfaces.IFullSignature for _, sig := range sigList { allSigs = append(allSigs, sig) } sl := new(SigList) sl.Length = uint32(numSigs) sl.List = allSigs sf.SignatureList = *sl return sf }
func (e *ServerIndexNumber) Hash() interfaces.IHash { bin, err := e.MarshalBinary() if err != nil { panic(err) } return primitives.Sha(bin) }
func (e *IncreaseServerCount) Hash() interfaces.IHash { bin, err := e.MarshalBinary() if err != nil { panic(err) } return primitives.Sha(bin) }
func (b *DirectoryBlock) HeaderHash() (interfaces.IHash, error) { binaryEBHeader, err := b.GetHeader().MarshalBinary() if err != nil { return nil, err } return primitives.Sha(binaryEBHeader), nil }
// This is the FullHash. func (e *ECBlock) GetFullHash() (interfaces.IHash, error) { p, err := e.MarshalBinary() if err != nil { return nil, err } return primitives.Sha(p), nil }
func (e *EndOfMinuteEntry) Hash() interfaces.IHash { bin, err := e.MarshalBinary() if err != nil { panic(err) } return primitives.Sha(bin) }
func CreateTestDirectoryBlockHeader(prevBlock *directoryBlock.DirectoryBlock) *directoryBlock.DBlockHeader { header := new(directoryBlock.DBlockHeader) header.SetBodyMR(primitives.Sha(primitives.NewZeroHash().Bytes())) header.SetBlockCount(0) header.SetNetworkID(0xffff) if prevBlock == nil { header.SetDBHeight(0) header.SetPrevLedgerKeyMR(primitives.NewZeroHash()) header.SetPrevKeyMR(primitives.NewZeroHash()) header.SetTimestamp(1234) } else { header.SetDBHeight(prevBlock.Header.GetDBHeight() + 1) header.SetPrevLedgerKeyMR(prevBlock.GetHash()) keyMR, err := prevBlock.BuildKeyMerkleRoot() if err != nil { panic(err) } header.SetPrevKeyMR(keyMR) header.SetTimestamp(prevBlock.Header.GetTimestamp() + 1) } header.SetVersion(1) return header }
func (b *DirectoryBlock) GetFullHash() interfaces.IHash { binaryDblock, err := b.MarshalBinary() if err != nil { return nil } b.DBHash = primitives.Sha(binaryDblock) return b.DBHash }
// Returns the SHA256 hash for the admin block func (b *AdminBlock) LookupHash() (interfaces.IHash, error) { var binaryAB []byte binaryAB, err := b.MarshalBinary() if err != nil { return nil, err } return primitives.Sha(binaryAB), nil }
// Build the SHA256 hash for the admin block func (b *AdminBlock) BuildPartialHash() (err error) { var binaryAB []byte binaryAB, err = b.MarshalBinary() if err != nil { return } b.partialHash = primitives.Sha(binaryAB) return }
func (e *EBlock) HeaderHash() (interfaces.IHash, error) { e.BuildHeader() header, err := e.Header.MarshalBinary() if err != nil { return nil, err } h := primitives.Sha(header) return h, nil }
func (m *DirectoryBlockSignature) GetMsgHash() interfaces.IHash { data, _ := m.MarshalForSignature() if data == nil { return nil } m.MsgHash = primitives.Sha(data) return m.MsgHash }