func (e *ECBlockHeader) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) if _, err = buf.Read(hash); err != nil { return } else { if fmt.Sprintf("%x", hash) != "000000000000000000000000000000000000000000000000000000000000000c" { err = fmt.Errorf("Invalid ChainID - %x", hash) return } } if _, err = buf.Read(hash); err != nil { return } else { e.BodyHash.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevHeaderHash.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevFullHash.SetBytes(hash) } if err = binary.Read(buf, binary.BigEndian, &e.DBHeight); err != nil { return } // read the Header Expansion Area hesize, tmp := primitives.DecodeVarInt(buf.DeepCopyBytes()) buf = primitives.NewBuffer(tmp) e.HeaderExpansionArea = make([]byte, hesize) if _, err = buf.Read(e.HeaderExpansionArea); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.ObjectCount); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.BodySize); err != nil { return } newData = buf.DeepCopyBytes() return }
func (e *ECBlock) unmarshalHeaderBinaryData(data []byte) (newData []byte, err error) { buf := primitives.NewBuffer(data) hash := make([]byte, 32) if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetECChainID().SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetBodyHash().SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetPrevHeaderHash().SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.Header.GetPrevFullHash().SetBytes(hash) } h := e.Header.GetDBHeight() if err = binary.Read(buf, binary.BigEndian, &h); err != nil { return } // read the Header Expansion Area hesize, tmp := primitives.DecodeVarInt(buf.DeepCopyBytes()) buf = primitives.NewBuffer(tmp) e.Header.SetHeaderExpansionArea(make([]byte, hesize)) if _, err = buf.Read(e.Header.GetHeaderExpansionArea()); err != nil { return } oc := e.Header.GetObjectCount() if err = binary.Read(buf, binary.BigEndian, &oc); err != nil { return } sz := e.Header.GetBodySize() if err = binary.Read(buf, binary.BigEndian, &sz); err != nil { return } newData = buf.DeepCopyBytes() return }
func (b *IncreaseBalance) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling IncreaseBalance: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) _, err = buf.Read(hash) if err != nil { return } b.ECPubKey = new(primitives.ByteSlice32) copy(b.ECPubKey[:], hash) _, err = buf.Read(hash) if err != nil { return } if b.TXID == nil { b.TXID = primitives.NewZeroHash() } b.TXID.SetBytes(hash) tmp := make([]byte, 0) b.Index, tmp = primitives.DecodeVarInt(buf.DeepCopyBytes()) b.NumEC, tmp = primitives.DecodeVarInt(tmp) newData = tmp return }
func (e *DirBlockInfo) UnmarshalBinaryData(data []byte) (newData []byte, err error) { dec := gob.NewDecoder(primitives.NewBuffer(data)) dbic := newDirBlockInfoCopy() err = dec.Decode(dbic) if err != nil { return nil, err } e.parseDirBlockInfoCopy(dbic) return nil, nil }
func (m *MinuteNumber) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling MinuteNumber: %v", r) } }() buf := primitives.NewBuffer(data) var c byte if c, err = buf.ReadByte(); err != nil { return } else { m.Number = c } newData = buf.DeepCopyBytes() return }
// unmarshalHeaderBinary builds the Entry Block Header from the serialized binary. func (e *EBlockHeader) UnmarshalBinaryData(data []byte) (newData []byte, err error) { buf := primitives.NewBuffer(data) hash := make([]byte, 32) newData = data if _, err = buf.Read(hash); err != nil { return } else { e.ChainID.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.BodyMR.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevKeyMR.SetBytes(hash) } if _, err = buf.Read(hash); err != nil { return } else { e.PrevFullHash.SetBytes(hash) } if err = binary.Read(buf, binary.BigEndian, &e.EBSequence); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.DBHeight); err != nil { return } if err = binary.Read(buf, binary.BigEndian, &e.EntryCount); err != nil { return } newData = buf.DeepCopyBytes() return }
// unmarshalBodyBinary builds the Entry Block Body from the serialized binary. func (e *EBlock) unmarshalBodyBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) for i := uint32(0); i < e.Header.GetEntryCount(); i++ { if _, err := buf.Read(hash); err != nil { return nil, err } h := primitives.NewZeroHash() h.SetBytes(hash) e.Body.EBEntries = append(e.Body.EBEntries, h) } newData = buf.DeepCopyBytes() return }
//Construct the entry and submit it to the server func (a *Anchor) submitEntryToAnchorChain(aRecord *AnchorRecord) error { jsonARecord, err := json.Marshal(aRecord) //anchorLog.Debug("submitEntryToAnchorChain - jsonARecord: ", string(jsonARecord)) if err != nil { return err } bufARecord := new(primitives.Buffer) bufARecord.Write(jsonARecord) aRecordSig := a.serverPrivKey.Sign(jsonARecord) //Create a new entry entry := entryBlock.NewEntry() entry.ChainID = a.anchorChainID anchorLog.Debug("anchorChainID: ", a.anchorChainID) // instead of append signature at the end of anchor record // it can be added as the first entry.ExtIDs[0] entry.ExtIDs = append(entry.ExtIDs, primitives.ByteSlice{Bytes: aRecordSig.Bytes()}) entry.Content = primitives.ByteSlice{Bytes: bufARecord.DeepCopyBytes()} //anchorLog.Debug("entry: ", spew.Sdump(entry)) buf := new(primitives.Buffer) // 1 byte version buf.Write([]byte{0}) // 6 byte milliTimestamp (truncated unix time) buf.Write(milliTime()) // 32 byte Entry Hash buf.Write(entry.GetHash().Bytes()) // 1 byte number of entry credits to pay binaryEntry, err := entry.MarshalBinary() if err != nil { return err } anchorLog.Debug("jsonARecord binary entry: ", hex.EncodeToString(binaryEntry)) if c, err := util.EntryCost(binaryEntry); err == nil { buf.WriteByte(byte(c)) } else { return err } tmp := buf.DeepCopyBytes() sig := a.serverECKey.Sign(tmp).(*primitives.Signature) buf = primitives.NewBuffer(tmp) buf.Write(a.serverECKey.Pub[:]) buf.Write(sig.Sig[:]) commit := entryCreditBlock.NewCommitEntry() err = commit.UnmarshalBinary(buf.DeepCopyBytes()) if err != nil { return err } // create a CommitEntry msg and send it to the local inmsgQ cm := messages.NewCommitEntryMsg() cm.CommitEntry = commit a.state.InMsgQueue() <- cm // create a RevealEntry msg and send it to the local inmsgQ rm := messages.NewRevealEntryMsg() rm.Entry = entry a.state.InMsgQueue() <- rm return nil }
func (e *ECBlock) unmarshalBodyBinaryData(data []byte) ([]byte, error) { buf := primitives.NewBuffer(data) var err error defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling: %v", r) } }() for i := uint64(0); i < e.Header.GetObjectCount(); i++ { var id byte id, err = buf.ReadByte() if err != nil { return nil, err } switch id { case ECIDServerIndexNumber: s := NewServerIndexNumber() if buf.Len() < ServerIndexNumberSize { err = io.EOF return nil, err } _, err = s.UnmarshalBinaryData(buf.Next(ServerIndexNumberSize)) if err != nil { return nil, err } e.Body.SetEntries(append(e.Body.GetEntries(), s)) case ECIDMinuteNumber: m := NewMinuteNumber(0) if buf.Len() < MinuteNumberSize { err = io.EOF return nil, err } _, err = m.UnmarshalBinaryData(buf.Next(MinuteNumberSize)) if err != nil { return nil, err } e.Body.SetEntries(append(e.Body.GetEntries(), m)) case ECIDChainCommit: if buf.Len() < CommitChainSize { err = io.EOF return nil, err } c := NewCommitChain() _, err = c.UnmarshalBinaryData(buf.Next(CommitChainSize)) if err != nil { return nil, err } e.Body.SetEntries(append(e.Body.GetEntries(), c)) case ECIDEntryCommit: if buf.Len() < CommitEntrySize { err = io.EOF return nil, err } c := NewCommitEntry() _, err = c.UnmarshalBinaryData(buf.Next(CommitEntrySize)) if err != nil { return nil, err } e.Body.SetEntries(append(e.Body.GetEntries(), c)) case ECIDBalanceIncrease: c := NewIncreaseBalance() tmp, err := c.UnmarshalBinaryData(buf.DeepCopyBytes()) if err != nil { return nil, err } e.Body.SetEntries(append(e.Body.GetEntries(), c)) buf = primitives.NewBuffer(tmp) default: err = fmt.Errorf("Unsupported ECID: %x\n", id) return nil, err } } return buf.DeepCopyBytes(), nil }
func (c *CommitChain) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling CommitChain: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) // 1 byte Version var b byte var p []byte if b, err = buf.ReadByte(); err != nil { return } else { c.Version = uint8(b) } if buf.Len() < 6 { err = io.EOF return } // 6 byte MilliTime if p = buf.Next(6); p == nil { err = fmt.Errorf("Could not read MilliTime") return } else { c.MilliTime = new(primitives.ByteSlice6) err = c.MilliTime.UnmarshalBinary(p) if err != nil { return } } // 32 byte ChainIDHash if _, err = buf.Read(hash); err != nil { return } c.ChainIDHash = primitives.NewHash(hash) // 32 byte Weld if _, err = buf.Read(hash); err != nil { return } c.Weld = primitives.NewHash(hash) // 32 byte Entry Hash if _, err = buf.Read(hash); err != nil { return } c.EntryHash = primitives.NewHash(hash) // 1 byte number of Entry Credits if b, err = buf.ReadByte(); err != nil { return } else { c.Credits = uint8(b) } if buf.Len() < 32 { err = io.EOF return } // 32 byte Public Key if p := buf.Next(32); p == nil { err = fmt.Errorf("Could not read ECPubKey") return } else { c.ECPubKey = new(primitives.ByteSlice32) err = c.ECPubKey.UnmarshalBinary(p) if err != nil { return } } if buf.Len() < 64 { err = io.EOF return } // 64 byte Signature if p := buf.Next(64); p == nil { err = fmt.Errorf("Could not read Sig") return } else { c.Sig = new(primitives.ByteSlice64) err = c.Sig.UnmarshalBinary(p) if err != nil { return } } err = c.ValidateSignatures() if err != nil { return } newData = buf.DeepCopyBytes() return }
func (e *Entry) UnmarshalBinaryData(data []byte) (newData []byte, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("Error unmarshalling: %v", r) } }() buf := primitives.NewBuffer(data) hash := make([]byte, 32) // 1 byte Version b, err := buf.ReadByte() if err != nil { return nil, err } else { e.Version = b } // 32 byte ChainID e.ChainID = primitives.NewZeroHash() if _, err = buf.Read(hash); err != nil { return nil, err } else if err = e.ChainID.SetBytes(hash); err != nil { return nil, err } // 2 byte size of ExtIDs var extSize uint16 if err = binary.Read(buf, binary.BigEndian, &extSize); err != nil { return nil, err } // ExtIDs for i := int16(extSize); i > 0; { var xsize int16 binary.Read(buf, binary.BigEndian, &xsize) i -= 2 if i < 0 { err = fmt.Errorf("Error parsing external IDs") return nil, err } x := make([]byte, xsize) var n int if n, err = buf.Read(x); err != nil { return nil, err } else { if c := cap(x); n != c { err = fmt.Errorf("Could not read ExtID: Read %d bytes of %d\n", n, c) return nil, err } ex := primitives.ByteSlice{} err = ex.UnmarshalBinary(x) if err != nil { return nil, err } e.ExtIDs = append(e.ExtIDs, ex) i -= int16(n) if i < 0 { err = fmt.Errorf("Error parsing external IDs") return nil, err } } } // Content err = e.Content.UnmarshalBinary(buf.DeepCopyBytes()) if err != nil { return nil, err } return }