Skip to content

Commit

Permalink
Simplify. Fix types.
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Jan 29, 2024
1 parent 2e3e419 commit 689e7a2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
5 changes: 1 addition & 4 deletions frontend/controller/actions/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

import { PUBSUB_INSTANCE } from '@controller/instance-keys.js'
import { DAYS_MILLIS } from '@model/contracts/shared/time.js'
import sbp from '@sbp/sbp'
import { GIMessage } from '~/shared/domains/chelonia/GIMessage.js'
Expand Down Expand Up @@ -159,16 +158,14 @@ export const encryptedNotification = (

const actionReplaced = action.replace('gi.actions', 'gi.contracts')

const pubsub = sbp('okTurtles.data/get', PUBSUB_INSTANCE)
const message = sbp('chelonia/out/generateEncryptedOrUnencryptedMessage', {
return sbp('chelonia/out/encryptedOrUnencryptedPubMessage', {
contractID: params.contractID,
contractName: actionReplaced.split('/', 2).join('/'),
innerSigningKeyId,
encryptionKeyId,
signingKeyId,
data: [actionReplaced, params.data]
})
pubsub.pub(params.contractID, message.serialize())
}
return {
[action]: async function (params: GIActionParams) {
Expand Down
24 changes: 8 additions & 16 deletions shared/domains/chelonia/chelonia.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export default (sbp('sbp/selectors/register', {
return
}
try {
v(parseEncryptedOrUnencryptedMessage.call(this, {
(v: Function)(parseEncryptedOrUnencryptedMessage.call(this, {
contractID: msg.channelID,
contractName: rootState.contracts[msg.channelID].type,
serializedData: msg.data
Expand Down Expand Up @@ -1198,7 +1198,7 @@ export default (sbp('sbp/selectors/register', {
'chelonia/out/propDel': async function () {

},
'chelonia/out/generateEncryptedOrUnencryptedMessage': generateEncryptedOrUnencryptedMessage
'chelonia/out/encryptedOrUnencryptedPubMessage': outputEncryptedOrUnencryptedPubMessage
}): string[])

function contractNameFromAction (action: string): string {
Expand All @@ -1208,7 +1208,7 @@ function contractNameFromAction (action: string): string {
return contractName
}

function generateEncryptedOrUnencryptedMessage ({
function outputEncryptedOrUnencryptedPubMessage ({
contractID,
contractName,
innerSigningKeyId,
Expand All @@ -1223,7 +1223,6 @@ function generateEncryptedOrUnencryptedMessage ({
signingKeyId: string,
data: Object
}) {
const instance = this._instance
const manifestHash = this.config.contracts.manifests[contractName]
const { contract } = this.manifestToContract[manifestHash]
const state = contract.state(contractID)
Expand All @@ -1236,17 +1235,10 @@ function generateEncryptedOrUnencryptedMessage ({
? signedMessage
: encryptedOutgoingData(contractID, encryptionKeyId, signedMessage)
const message = signedOutgoingData(contractID, signingKeyId, (payload: any), this.transientSecretKeys)
const self = this
return {
get serialize () {
return () => {
if (instance !== self._instance) throw new Error('Chelonia instance has been reset')
const rootState = sbp(self.config.stateSelector)
const height = String(rootState.contracts[contractID].height)
return { ...message.serialize(height), height }
}
}
}
const rootState = sbp(this.config.stateSelector)
const height = String(rootState.contracts[contractID].height)
const serializedData = { ...message.serialize(height), height }
this.pubsub.pub(contractID, serializedData)
}

function parseEncryptedOrUnencryptedMessage ({
Expand All @@ -1267,7 +1259,7 @@ function parseEncryptedOrUnencryptedMessage ({

const numericHeight = parseInt(serializedData.height)
const rootState = sbp(this.config.stateSelector)
const currentHeight = String(rootState.contracts[contractID].height)
const currentHeight = rootState.contracts[contractID].height
if (!(numericHeight >= 0) || !(numericHeight <= currentHeight)) {
throw new Error(`[chelonia] parseEncryptedOrUnencryptedMessage: Invalid height ${serializedData.height}; it must be between 0 and ${currentHeight}`)
}
Expand Down
2 changes: 1 addition & 1 deletion shared/domains/chelonia/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ export const recreateEvent = (entry: GIMessage, state: Object, contractsState: O
return entry
}

export const getContractIDfromKeyId = (contractID: string, signingKeyId?: string, state: Object) => {
export const getContractIDfromKeyId = (contractID: string, signingKeyId?: string, state: Object): string => {
return signingKeyId && state._vm.authorizedKeys[signingKeyId].foreignKey
? new URL(state._vm.authorizedKeys[signingKeyId].foreignKey).pathname
: contractID
Expand Down

0 comments on commit 689e7a2

Please sign in to comment.