Example #1
0
// generateBroadcast generates a wire.MsgBroadcast from a Bitmessage.
func (m *Bmail) generateBroadcast(from *identity.Private, expiry time.Duration) (obj.Object, *pow.Data, error) {
	// TODO make a separate function in bmutil that does this.
	var signingKey, encKey wire.PubKey
	var tag wire.ShaHash
	sk := from.SigningKey.PubKey().SerializeUncompressed()[1:]
	ek := from.EncryptionKey.PubKey().SerializeUncompressed()[1:]
	t := from.Address.Tag()
	copy(signingKey[:], sk)
	copy(encKey[:], ek)
	copy(tag[:], t)

	powData := &pow.Data{
		NonceTrialsPerByte: from.NonceTrialsPerByte,
		ExtraBytes:         from.ExtraBytes,
	}

	data := &cipher.Bitmessage{
		FromStreamNumber:   from.Address.Stream,
		FromAddressVersion: from.Address.Version,
		SigningKey:         &signingKey,
		EncryptionKey:      &encKey,
		Pow:                powData,
		Content:            m.Content,
	}

	var broadcast *cipher.Broadcast
	var err error
	switch from.Address.Version {
	case 2:
		fallthrough
	case 3:
		broadcast, err = cipher.CreateTaglessBroadcast(time.Now().Add(expiry), data, from)
	case 4:
		broadcast, err = cipher.CreateTaggedBroadcast(time.Now().Add(expiry), data, &tag, from)
	default:
		return nil, nil, errors.New("Unknown from address version")
	}

	if err != nil {
		return nil, nil, err
	}

	return broadcast.Object(), powData, nil
}
Example #2
0
// BroadcastRead creates a Bitmessage object from an unencrypted
// wire.MsgBroadcast.
func BroadcastRead(msg *cipher.Broadcast) (*Bmail, error) {
	header := msg.Object().Header()
	data := msg.Bitmessage()

	sign, _ := data.SigningKey.ToBtcec()
	encr, _ := data.EncryptionKey.ToBtcec()
	from := identity.NewPublic(sign, encr, data.Pow, data.FromAddressVersion, data.FromStreamNumber)
	fromAddress, err := from.Address.Encode()
	if err != nil {
		return nil, err
	}

	return &Bmail{
		From:       "*****@*****.**",
		To:         bmToEmail(fromAddress),
		Expiration: header.Expiration(),
		Content:    data.Content,
	}, nil
}