Exemplo n.º 1
0
func HandleV2RevealEntry(state interfaces.IState, params interface{}) (interface{}, *primitives.JSONError) {
	e := new(EntryRequest)
	err := MapToObject(params, e)
	if err != nil {
		return nil, NewInvalidParamsError()
	}

	entry := entryBlock.NewEntry()
	if p, err := hex.DecodeString(e.Entry); err != nil {
		return nil, NewInvalidEntryError()
	} else {
		_, err := entry.UnmarshalBinaryData(p)
		if err != nil {
			return nil, NewInvalidEntryError()
		}
	}

	msg := new(messages.RevealEntryMsg)
	msg.Entry = entry
	msg.Timestamp = state.GetTimestamp()
	state.APIQueue() <- msg

	resp := new(RevealEntryResponse)
	resp.Message = "Entry Reveal Success"
	resp.EntryHash = entry.GetHash().String()

	return resp, nil
}
Exemplo n.º 2
0
func (db *Overlay) FetchAllEntriesByChainID(chainID interfaces.IHash) ([]interfaces.IEBEntry, error) {
	list, err := db.FetchAllBlocksFromBucket(chainID.Bytes(), entryBlock.NewEntry())
	if err != nil {
		return nil, err
	}
	return toEntryList(list), nil
}
Exemplo n.º 3
0
func CreateTestAnchorEnry(dBlock *directoryBlock.DirectoryBlock) *entryBlock.Entry {
	answer := entryBlock.NewEntry()

	answer.ChainID = GetAnchorChainID()
	answer.Version = 0
	answer.ExtIDs = nil

	height := dBlock.GetHeader().GetDBHeight()

	ar := anchor.CreateAnchorRecordFromDBlock(dBlock)
	ar.Bitcoin = new(anchor.BitcoinStruct)
	ar.Bitcoin.Address = "1HLoD9E4SDFFPDiYfNYnkBLQ85Y51J3Zb1"
	ar.Bitcoin.TXID = fmt.Sprintf("%x", IntToByteSlice(int(height)))
	ar.Bitcoin.BlockHeight = int32(height)
	ar.Bitcoin.BlockHash = fmt.Sprintf("%x", IntToByteSlice(255-int(height)))
	ar.Bitcoin.Offset = int32(height % 10)

	hex, err := ar.MarshalAndSign(NewPrimitivesPrivateKey(0))
	if err != nil {
		panic(err)
	}

	answer.Content = primitives.ByteSlice{Bytes: hex}

	return answer
}
Exemplo n.º 4
0
func (m *RevealEntryMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("Error unmarshalling: %v", r)
		}
	}()
	newData = data
	if newData[0] != m.Type() {
		return nil, fmt.Errorf("%s", "Invalid Message type")
	}
	newData = newData[1:]

	t := new(primitives.Timestamp)
	newData, err = t.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}
	m.Timestamp = t

	e := entryBlock.NewEntry()
	newData, err = e.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}
	m.Entry = e

	return newData, nil
}
Exemplo n.º 5
0
func CreateTestEntry(n uint32) *entryBlock.Entry {
	answer := entryBlock.NewEntry()

	answer.ChainID = GetChainID()
	answer.Version = 1
	answer.ExtIDs = []primitives.ByteSlice{primitives.ByteSlice{Bytes: []byte(fmt.Sprintf("ExtID %v", n))}}
	answer.Content = primitives.ByteSlice{Bytes: []byte(fmt.Sprintf("Content %v", n))}

	return answer
}
Exemplo n.º 6
0
func MakeFEREntryWithHeightFromContent(passedResidentHeight uint32, passedTargetActivationHeight uint32,
	passedTargetPrice uint64, passedExpirationHeight uint32, passedPriority uint32) *FEREntryWithHeight {

	// Create and format the signing private key
	var signingPrivateKey [64]byte
	SigningPrivateKey := "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
	signingBytes, err := hex.DecodeString(SigningPrivateKey)
	if err != nil {
		fmt.Println("Signing private key isn't parsable")
		return nil
	}
	copy(signingPrivateKey[:], signingBytes[:])
	_ = ed.GetPublicKey(&signingPrivateKey) // Needed to format the public half of the key set

	anFEREntry := new(specialEntries.FEREntry)

	anFEREntry.SetExpirationHeight(passedExpirationHeight)
	anFEREntry.SetTargetActivationHeight(passedTargetActivationHeight)
	anFEREntry.SetPriority(passedPriority)
	anFEREntry.SetTargetPrice(passedTargetPrice)

	entryJson, err := json.Marshal(anFEREntry)
	if err != nil {
		fmt.Println("Bad marshal of anFEREntry")
		return nil
	}

	// Create the factom entry with the signing private key
	signingSignature := ed.Sign(&signingPrivateKey, entryJson)

	// Make a new factom entry and populate it
	anEntry := new(factom.Entry)
	anEntry.ChainID = "111111118d918a8be684e0dac725493a75862ef96d2d3f43f84b26969329bf03"
	anEntry.ExtIDs = append(anEntry.ExtIDs, signingSignature[:])
	anEntry.Content = entryJson

	// ce := common.NewEntry()
	emb, _ := anEntry.MarshalBinary()
	// ce.UnmarshalBinary(emb)

	EBEntry := entryBlock.NewEntry()
	_, err = EBEntry.UnmarshalBinaryData(emb)
	if err != nil {
		fmt.Println("Error 3:  couldn't unmarshal binary")
		return nil
	}

	ewh := new(FEREntryWithHeight)
	// Don't set the resident height in the actual FEREntry yet because the state validate loop will handle all that
	ewh.Height = passedResidentHeight
	ewh.AnFEREntry = EBEntry

	return ewh
}
Exemplo n.º 7
0
func (m *RevealEntryMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("Error unmarshalling: %v", r)
		}
	}()
	newData = data[1:]
	e := entryBlock.NewEntry()
	newData, err = e.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}
	m.Entry = e
	return newData, nil
}
Exemplo n.º 8
0
func (m *EntryBlockResponse) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("Error unmarshalling Directory Block State Missing Message: %v", r)
		}
	}()
	newData = data
	if newData[0] != m.Type() {
		return nil, fmt.Errorf("Invalid Message type")
	}
	newData = newData[1:]

	m.Peer2Peer = true // This is always a Peer2peer message

	m.Timestamp = new(primitives.Timestamp)
	newData, err = m.Timestamp.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.EBlockCount, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]

	for i := 0; i < int(m.EBlockCount); i++ {
		eBlock := entryBlock.NewEBlock()
		newData, err = eBlock.UnmarshalBinaryData(newData)
		if err != nil {
			return nil, err
		}
		m.EBlocks = append(m.EBlocks, eBlock)
	}

	m.EntryCount, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]

	for i := 0; i < int(m.EntryCount); i++ {
		entry := entryBlock.NewEntry()
		newData, err = entry.UnmarshalBinaryData(newData)
		if err != nil {
			return nil, err
		}
		m.Entries = append(m.Entries, entry)
	}

	return
}
Exemplo n.º 9
0
// FetchEntry gets an entry by hash from the database.
func (db *Overlay) FetchEntry(hash interfaces.IHash) (interfaces.IEBEntry, error) {
	chainID, err := db.FetchPrimaryIndexBySecondaryIndex(ENTRY, hash)
	if err != nil {
		return nil, err
	}
	if chainID == nil {
		return nil, nil
	}

	entry, err := db.FetchBlock(chainID.Bytes(), hash, entryBlock.NewEntry())
	if err != nil {
		return nil, err
	}
	if entry == nil {
		return nil, nil
	}

	return entry.(interfaces.IEBEntry), nil
}
Exemplo n.º 10
0
func HandleRevealEntry(ctx *web.Context) {
	type revealentry struct {
		Entry string
	}

	e := new(revealentry)
	if p, err := ioutil.ReadAll(ctx.Request.Body); err != nil {
		returnMsg(ctx, "Error Reveal Entry: "+err.Error(), false)
		return
	} else {
		if err := json.Unmarshal(p, e); err != nil {
			returnMsg(ctx, "Error Reveal Entry: "+err.Error(), false)
			return
		}
	}

	entry := entryBlock.NewEntry()
	if p, err := hex.DecodeString(e.Entry); err != nil {
		returnMsg(ctx, "Error Reveal Entry: "+err.Error(), false)
		return
	} else {
		_, err := entry.UnmarshalBinaryData(p)
		if err != nil {
			returnMsg(ctx, "Error Reveal Entry: "+err.Error(), false)
			return
		}
	}

	state := ctx.Server.Env["state"].(interfaces.IState)

	msg := new(messages.RevealEntryMsg)
	msg.Entry = entry
	msg.Timestamp = state.GetTimestamp()
	state.InMsgQueue() <- msg

	returnMsg(ctx, "Entry Reveal Success", true)
}
Exemplo n.º 11
0
//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
}
Exemplo n.º 12
0
func (m *DBStateMsg) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
	//defer func() {
	//	if r := recover(); r != nil {
	//		err = fmt.Errorf("Error unmarshalling Directory Block State Message: %v", r)
	//	}
	//}()
	newData = data
	if newData[0] != m.Type() {
		return nil, fmt.Errorf("Invalid Message type")
	}
	newData = newData[1:]

	m.Peer2Peer = true

	m.Timestamp = new(primitives.Timestamp)
	newData, err = m.Timestamp.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.DirectoryBlock = new(directoryBlock.DirectoryBlock)
	newData, err = m.DirectoryBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.AdminBlock = new(adminBlock.AdminBlock)
	newData, err = m.AdminBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.FactoidBlock = new(factoid.FBlock)
	newData, err = m.FactoidBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	m.EntryCreditBlock = entryCreditBlock.NewECBlock()
	newData, err = m.EntryCreditBlock.UnmarshalBinaryData(newData)
	if err != nil {
		return nil, err
	}

	eBlockCount, newData := binary.BigEndian.Uint32(newData[0:4]), newData[4:]

	for i := uint32(0); i < eBlockCount; i++ {
		eBlock := entryBlock.NewEBlock()
		newData, err = eBlock.UnmarshalBinaryData(newData)
		if err != nil {
			panic(err.Error())
		}
		m.EBlocks = append(m.EBlocks, eBlock)
	}

	entryCount, newData := binary.BigEndian.Uint32(newData[0:4]), newData[4:]

	for i := uint32(0); i < entryCount; i++ {
		var entrySize uint32
		entrySize, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
		entry := entryBlock.NewEntry()
		newData, err = newData[int(entrySize):], entry.UnmarshalBinary(newData[:int(entrySize)])
		if err != nil {
			panic(err.Error())
		}
		m.Entries = append(m.Entries, entry)
	}

	return
}