|
| 1 | +import * as Saga from '../util/saga' |
| 2 | +import * as TeamBuildingGen from './team-building-gen' |
| 3 | +import * as CryptoGen from './crypto-gen' |
| 4 | +import * as Types from '../constants/types/crypto' |
| 5 | +import * as Constants from '../constants/crypto' |
| 6 | +import {TypedState} from '../util/container' |
| 7 | +import commonTeamBuildingSaga, {filterForNs} from './team-building' |
| 8 | + |
| 9 | +// Get list of users from crypto TeamBuilding for encrypt operation |
| 10 | +const onSetRecipients = (state: TypedState, _: TeamBuildingGen.FinishedTeamBuildingPayload) => { |
| 11 | + const users = [...state.crypto.teamBuilding.finishedTeam] |
| 12 | + const usernames = users.map(user => user.username) |
| 13 | + return [ |
| 14 | + TeamBuildingGen.createCancelTeamBuilding({namespace: 'crypto'}), |
| 15 | + CryptoGen.createSetRecipients({operation: 'encrypt', recipients: usernames}), |
| 16 | + ] |
| 17 | +} |
| 18 | + |
| 19 | +function* teamBuildingSaga() { |
| 20 | + yield* commonTeamBuildingSaga('crypto') |
| 21 | + |
| 22 | + // This action is used to hook into the TeamBuildingGen.finishedTeamBuilding action. |
| 23 | + // We want this so that we can figure out which user(s) havbe been selected and pass that result over to store.crypto.encrypt.recipients |
| 24 | + yield* Saga.chainAction2(TeamBuildingGen.finishedTeamBuilding, filterForNs('crypto', onSetRecipients)) |
| 25 | +} |
| 26 | + |
| 27 | +const handleInput = ( |
| 28 | + state: TypedState, |
| 29 | + action: CryptoGen.SetInputPayload | CryptoGen.SetRecipientsPayload |
| 30 | +) => { |
| 31 | + const {operation} = action.payload |
| 32 | + switch (action.type) { |
| 33 | + // Handle input for any operation |
| 34 | + case CryptoGen.setInput: { |
| 35 | + const {value, type} = action.payload |
| 36 | + |
| 37 | + // Input (text or file) was cleared or deleted |
| 38 | + if (!value) { |
| 39 | + return CryptoGen.createClearInput({operation}) |
| 40 | + } |
| 41 | + |
| 42 | + if (operation === Constants.Operations.Encrypt) { |
| 43 | + return state.crypto.encrypt.meta.hasRecipients && state.crypto.encrypt.recipients?.length |
| 44 | + ? makeOperationAction(operation, value, type, state.crypto.encrypt.recipients) |
| 45 | + : null |
| 46 | + } |
| 47 | + |
| 48 | + return makeOperationAction(operation, value, type) |
| 49 | + } |
| 50 | + case CryptoGen.setRecipients: { |
| 51 | + const {recipients} = action.payload |
| 52 | + const {input, inputType} = state.crypto[operation] |
| 53 | + // User already provided input (text or file) before setting the |
| 54 | + // recipients. Get the input and pass it to the operation |
| 55 | + if (input && inputType) { |
| 56 | + return makeOperationAction(operation, input, inputType, recipients) |
| 57 | + } |
| 58 | + return |
| 59 | + } |
| 60 | + default: |
| 61 | + return |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// Dispatch action to appropriate operation |
| 66 | +const makeOperationAction = ( |
| 67 | + operation: Types.Operations, |
| 68 | + input: string, |
| 69 | + inputType: Types.InputTypes, |
| 70 | + recipients: Array<string> = [] |
| 71 | +) => { |
| 72 | + switch (operation) { |
| 73 | + case Constants.Operations.Encrypt: |
| 74 | + return CryptoGen.createSaltpackEncrypt({input, recipients, type: inputType}) |
| 75 | + case Constants.Operations.Decrypt: |
| 76 | + return CryptoGen.createSaltpackDecrypt({input, type: inputType}) |
| 77 | + case Constants.Operations.Sign: |
| 78 | + return CryptoGen.createSaltpackSign({input, type: inputType}) |
| 79 | + case Constants.Operations.Verify: |
| 80 | + return CryptoGen.createSaltpackVerify({input, type: inputType}) |
| 81 | + default: |
| 82 | + return |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/* |
| 87 | + * For the time being these functions will echo back the input as the Saltpack RPC output (plaintext -> plaintext) |
| 88 | + * Saltpack RPCs need to be updated to take input as a string and handle `inputType = 'text' | 'file'` |
| 89 | + */ |
| 90 | +const saltpackEncrypt = async (action: CryptoGen.SaltpackEncryptPayload) => { |
| 91 | + return CryptoGen.createOnOperationSuccess({ |
| 92 | + operation: Constants.Operations.Encrypt, |
| 93 | + output: action.payload.input, |
| 94 | + outputType: action.payload.type, |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +const saltpackDecrypt = async (action: CryptoGen.SaltpackDecryptPayload) => { |
| 99 | + return CryptoGen.createOnOperationSuccess({ |
| 100 | + operation: Constants.Operations.Decrypt, |
| 101 | + output: action.payload.input, |
| 102 | + outputType: action.payload.type, |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +const saltpackSign = async (action: CryptoGen.SaltpackSignPayload) => { |
| 107 | + return CryptoGen.createOnOperationSuccess({ |
| 108 | + operation: Constants.Operations.Sign, |
| 109 | + output: action.payload.input, |
| 110 | + outputType: action.payload.type, |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +const saltpackVerify = async (action: CryptoGen.SaltpackVerifyPayload) => { |
| 115 | + return CryptoGen.createOnOperationSuccess({ |
| 116 | + operation: Constants.Operations.Verify, |
| 117 | + output: action.payload.input, |
| 118 | + outputType: action.payload.type, |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +function* cryptoSaga() { |
| 123 | + yield* Saga.chainAction2([CryptoGen.setInput, CryptoGen.setRecipients], handleInput) |
| 124 | + yield* Saga.chainAction(CryptoGen.saltpackEncrypt, saltpackEncrypt) |
| 125 | + yield* Saga.chainAction(CryptoGen.saltpackDecrypt, saltpackDecrypt) |
| 126 | + yield* Saga.chainAction(CryptoGen.saltpackSign, saltpackSign) |
| 127 | + yield* Saga.chainAction(CryptoGen.saltpackVerify, saltpackVerify) |
| 128 | + yield* teamBuildingSaga() |
| 129 | +} |
| 130 | + |
| 131 | +export default cryptoSaga |
0 commit comments