func (a *SellActivity) dispatchWorkAndSaveEncryptedResult(log bitwrk.Logger, workFile cafs.File) error { // Watch transaction state and close connection to worker when transaction expires connChan := make(chan io.Closer) exitChan := make(chan bool) go a.watchdog(log, exitChan, connChan, func() bool { return a.tx.State == bitwrk.StateActive }) defer func() { exitChan <- true }() st := NewScopedTransport() connChan <- st defer st.Close() reader := workFile.Open() defer reader.Close() result, err := a.worker.DoWork(reader, NewClient(&st.Transport)) if err != nil { return err } defer result.Close() temp := a.manager.storage.Create(fmt.Sprintf("Sell #%v: encrypted result", a.GetKey())) defer temp.Dispose() // Use AES-256 to encrypt the result block, err := aes.NewCipher(a.encResultKey[:]) if err != nil { return err } // Create OFB stream with null initialization vector (ok for one-time key) var iv [aes.BlockSize]byte stream := cipher.NewOFB(block, iv[:]) writer := &cipher.StreamWriter{S: stream, W: temp} _, err = io.Copy(writer, result) if err != nil { return err } if err := temp.Close(); err != nil { return err } if err := result.Close(); err != nil { return err } a.encResultFile = temp.File() return nil }
func (a *SellActivity) dispatchWork(log bitwrk.Logger, workFile cafs.File) (io.ReadCloser, error) { // Watch transaction state and close connection to worker when transaction expires connChan := make(chan io.Closer) exitChan := make(chan bool) go a.watchdog(log, exitChan, connChan, func() bool { return a.tx.State == bitwrk.StateActive }) defer func() { exitChan <- true }() reader := workFile.Open() defer reader.Close() st := NewScopedTransport() connChan <- st defer st.Close() r, err := a.worker.DoWork(reader, NewClient(&st.Transport)) if err == nil { // Defuse connection closing mechanism st.DisownConnections() } return r, err }